Skip to content

Commit 1297b51

Browse files
authored
#34518 - replace isimmutable with ismutable (#34652)
1 parent 8b6a8c2 commit 1297b51

File tree

10 files changed

+40
-16
lines changed

10 files changed

+40
-16
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ New library functions
5050
provide one-argument method that returns a closure. The old methods of `merge` and
5151
`merge!` are still available for backward compatibility ([#34296]).
5252
* The new `isdisjoint` function indicates whether two collections are disjoint ([#34427]).
53+
* Add function `ismutable` and deprecate `isimmutable` to check whether something is mutable.([#34652])
5354

5455
New library features
5556
--------------------

base/compiler/abstractinterpretation.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function const_prop_profitable(@nospecialize(arg))
167167
isconstType(b) && return true
168168
const_prop_profitable(b) && return true
169169
end
170-
elseif !isa(arg, Const) || (isa(arg.val, Symbol) || isa(arg.val, Type) || (!isa(arg.val, String) && isimmutable(arg.val)))
170+
elseif !isa(arg, Const) || (isa(arg.val, Symbol) || isa(arg.val, Type) || (!isa(arg.val, String) && !ismutable(arg.val)))
171171
# don't consider mutable values or Strings useful constants
172172
return true
173173
end

base/compiler/ssair/passes.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ function lift_leaves(compact::IncrementalCompact, @nospecialize(stmt),
380380
elseif isa(leaf, Union{Argument, Expr})
381381
return nothing
382382
end
383-
isimmutable(leaf) || return nothing
383+
!ismutable(leaf) || return nothing
384384
isdefined(leaf, field) || return nothing
385385
val = getfield(leaf, field)
386386
is_inlineable_constant(val) || return nothing

base/compiler/tfuncs.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ function isdefined_tfunc(@nospecialize(args...))
288288
return Const(true)
289289
elseif isa(arg1, Const)
290290
arg1v = (arg1::Const).val
291-
if isimmutable(arg1v) || isdefined(arg1v, idx) || (isa(arg1v, DataType) && is_dt_const_field(idx))
291+
if !ismutable(arg1v) || isdefined(arg1v, idx) || (isa(arg1v, DataType) && is_dt_const_field(idx))
292292
return Const(isdefined(arg1v, idx))
293293
end
294294
end
@@ -722,7 +722,7 @@ function getfield_tfunc(@nospecialize(s00), @nospecialize(name))
722722
if !(isa(nv,Symbol) || isa(nv,Int))
723723
return Bottom
724724
end
725-
if (isa(sv, SimpleVector) || isimmutable(sv)) && isdefined(sv, nv)
725+
if (isa(sv, SimpleVector) || !ismutable(sv)) && isdefined(sv, nv)
726726
return AbstractEvalConstant(getfield(sv, nv))
727727
end
728728
end

base/deprecated.jl

+22
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,27 @@ end
193193

194194
# BEGIN 1.5 deprecations
195195

196+
"""
197+
isimmutable(v) -> Bool
198+
!!! warning
199+
Consider using `!ismutable(v)` instead, as `isimmutable(v)` will be replaced by `!ismutable(v)` in a future release. (Since Julia 1.5)
200+
Return `true` iff value `v` is immutable. See [Mutable Composite Types](@ref)
201+
for a discussion of immutability. Note that this function works on values, so if you give it
202+
a type, it will tell you that a value of `DataType` is mutable.
203+
204+
# Examples
205+
```jldoctest
206+
julia> isimmutable(1)
207+
true
208+
209+
julia> isimmutable([1,2])
210+
false
211+
```
212+
"""
213+
isimmutable(@nospecialize(x)) = !ismutable(x)
214+
export isimmutable
215+
216+
196217
macro get!(h, key0, default)
197218
f, l = __source__.file, __source__.line
198219
depwarn("`@get!(dict, key, default)` at $f:$l is deprecated, use `get!(()->default, dict, key)` instead.", Symbol("@get!"))
@@ -201,4 +222,5 @@ macro get!(h, key0, default)
201222
end
202223
end
203224

225+
204226
# END 1.5 deprecations

base/exports.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ export
641641
identity,
642642
isbits,
643643
isequal,
644-
isimmutable,
644+
ismutable,
645645
isless,
646646
ifelse,
647647
objectid,

base/gcutils.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ end
2727
```
2828
"""
2929
function finalizer(@nospecialize(f), @nospecialize(o))
30-
if isimmutable(o)
30+
if !ismutable(o)
3131
error("objects of type ", typeof(o), " cannot be finalized")
3232
end
3333
ccall(:jl_gc_add_finalizer_th, Cvoid, (Ptr{Cvoid}, Any, Any),
@@ -37,7 +37,7 @@ end
3737

3838
function finalizer(f::Ptr{Cvoid}, o::T) where T
3939
@_inline_meta
40-
if isimmutable(o)
40+
if !ismutable(o)
4141
error("objects of type ", typeof(o), " cannot be finalized")
4242
end
4343
ccall(:jl_gc_add_ptr_finalizer, Cvoid, (Ptr{Cvoid}, Any, Ptr{Cvoid}),

base/reflection.jl

+7-7
Original file line numberDiff line numberDiff line change
@@ -402,22 +402,22 @@ function datatype_fielddesc_type(dt::DataType)
402402
end
403403

404404
"""
405-
isimmutable(v) -> Bool
405+
ismutable(v) -> Bool
406406
407-
Return `true` iff value `v` is immutable. See [Mutable Composite Types](@ref)
407+
Return `true` iff value `v` is mutable. See [Mutable Composite Types](@ref)
408408
for a discussion of immutability. Note that this function works on values, so if you give it
409409
a type, it will tell you that a value of `DataType` is mutable.
410410
411411
# Examples
412412
```jldoctest
413-
julia> isimmutable(1)
414-
true
415-
416-
julia> isimmutable([1,2])
413+
julia> ismutable(1)
417414
false
415+
416+
julia> ismutable([1,2])
417+
true
418418
```
419419
"""
420-
isimmutable(@nospecialize(x)) = (@_pure_meta; !typeof(x).mutable)
420+
ismutable(@nospecialize(x)) = (@_pure_meta; typeof(x).mutable)
421421

422422
"""
423423
isstructtype(T) -> Bool

doc/src/base/base.md

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ Base.isdispatchtuple
158158
### Declared structure
159159

160160
```@docs
161+
Base.ismutable
161162
Base.isimmutable
162163
Base.isabstracttype
163164
Base.isprimitivetype

test/reflection.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ not_const = 1
104104
@test isconst(@__MODULE__, :not_const) == false
105105
@test isconst(@__MODULE__, :is_not_defined) == false
106106

107-
@test isimmutable(1) == true
108-
@test isimmutable([]) == false
107+
@test ismutable(1) == false
108+
@test ismutable([]) == true
109109

110110
## find bindings tests
111111
@test ccall(:jl_get_module_of_binding, Any, (Any, Any), Base, :sin)==Base

0 commit comments

Comments
 (0)