Skip to content

Commit

Permalink
#34518 - replace isimmutable with ismutable (#34652)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssikdar1 authored and KristofferC committed Apr 11, 2020
1 parent 3bae9a4 commit 4f02bc3
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 16 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ New library functions
provide one-argument method that returns a closure. The old methods of `merge` and
`merge!` are still available for backward compatibility ([#34296]).
* The new `isdisjoint` function indicates whether two collections are disjoint ([#34427]).
* Add function `ismutable` and deprecate `isimmutable` to check whether something is mutable.([#34652])

New library features
--------------------
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function const_prop_profitable(@nospecialize(arg))
isconstType(b) && return true
const_prop_profitable(b) && return true
end
elseif !isa(arg, Const) || (isa(arg.val, Symbol) || isa(arg.val, Type) || (!isa(arg.val, String) && isimmutable(arg.val)))
elseif !isa(arg, Const) || (isa(arg.val, Symbol) || isa(arg.val, Type) || (!isa(arg.val, String) && !ismutable(arg.val)))
# don't consider mutable values or Strings useful constants
return true
end
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/ssair/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ function lift_leaves(compact::IncrementalCompact, @nospecialize(stmt),
elseif isa(leaf, Union{Argument, Expr})
return nothing
end
isimmutable(leaf) || return nothing
!ismutable(leaf) || return nothing
isdefined(leaf, field) || return nothing
val = getfield(leaf, field)
is_inlineable_constant(val) || return nothing
Expand Down
4 changes: 2 additions & 2 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ function isdefined_tfunc(@nospecialize(args...))
return Const(true)
elseif isa(arg1, Const)
arg1v = (arg1::Const).val
if isimmutable(arg1v) || isdefined(arg1v, idx) || (isa(arg1v, DataType) && is_dt_const_field(idx))
if !ismutable(arg1v) || isdefined(arg1v, idx) || (isa(arg1v, DataType) && is_dt_const_field(idx))
return Const(isdefined(arg1v, idx))
end
end
Expand Down Expand Up @@ -722,7 +722,7 @@ function getfield_tfunc(@nospecialize(s00), @nospecialize(name))
if !(isa(nv,Symbol) || isa(nv,Int))
return Bottom
end
if (isa(sv, SimpleVector) || isimmutable(sv)) && isdefined(sv, nv)
if (isa(sv, SimpleVector) || !ismutable(sv)) && isdefined(sv, nv)
return AbstractEvalConstant(getfield(sv, nv))
end
end
Expand Down
22 changes: 22 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,27 @@ end

# BEGIN 1.5 deprecations

"""
isimmutable(v) -> Bool
!!! warning
Consider using `!ismutable(v)` instead, as `isimmutable(v)` will be replaced by `!ismutable(v)` in a future release. (Since Julia 1.5)
Return `true` iff value `v` is immutable. See [Mutable Composite Types](@ref)
for a discussion of immutability. Note that this function works on values, so if you give it
a type, it will tell you that a value of `DataType` is mutable.
# Examples
```jldoctest
julia> isimmutable(1)
true
julia> isimmutable([1,2])
false
```
"""
isimmutable(@nospecialize(x)) = !ismutable(x)
export isimmutable


macro get!(h, key0, default)
f, l = __source__.file, __source__.line
depwarn("`@get!(dict, key, default)` at $f:$l is deprecated, use `get!(()->default, dict, key)` instead.", Symbol("@get!"))
Expand All @@ -201,4 +222,5 @@ macro get!(h, key0, default)
end
end


# END 1.5 deprecations
2 changes: 1 addition & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ export
identity,
isbits,
isequal,
isimmutable,
ismutable,
isless,
ifelse,
objectid,
Expand Down
4 changes: 2 additions & 2 deletions base/gcutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ end
```
"""
function finalizer(@nospecialize(f), @nospecialize(o))
if isimmutable(o)
if !ismutable(o)
error("objects of type ", typeof(o), " cannot be finalized")
end
ccall(:jl_gc_add_finalizer_th, Cvoid, (Ptr{Cvoid}, Any, Any),
Expand All @@ -37,7 +37,7 @@ end

function finalizer(f::Ptr{Cvoid}, o::T) where T
@_inline_meta
if isimmutable(o)
if !ismutable(o)
error("objects of type ", typeof(o), " cannot be finalized")
end
ccall(:jl_gc_add_ptr_finalizer, Cvoid, (Ptr{Cvoid}, Any, Ptr{Cvoid}),
Expand Down
14 changes: 7 additions & 7 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -402,22 +402,22 @@ function datatype_fielddesc_type(dt::DataType)
end

"""
isimmutable(v) -> Bool
ismutable(v) -> Bool
Return `true` iff value `v` is immutable. See [Mutable Composite Types](@ref)
Return `true` iff value `v` is mutable. See [Mutable Composite Types](@ref)
for a discussion of immutability. Note that this function works on values, so if you give it
a type, it will tell you that a value of `DataType` is mutable.
# Examples
```jldoctest
julia> isimmutable(1)
true
julia> isimmutable([1,2])
julia> ismutable(1)
false
julia> ismutable([1,2])
true
```
"""
isimmutable(@nospecialize(x)) = (@_pure_meta; !typeof(x).mutable)
ismutable(@nospecialize(x)) = (@_pure_meta; typeof(x).mutable)

"""
isstructtype(T) -> Bool
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ Base.isdispatchtuple
### Declared structure

```@docs
Base.ismutable
Base.isimmutable
Base.isabstracttype
Base.isprimitivetype
Expand Down
4 changes: 2 additions & 2 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ not_const = 1
@test isconst(@__MODULE__, :not_const) == false
@test isconst(@__MODULE__, :is_not_defined) == false

@test isimmutable(1) == true
@test isimmutable([]) == false
@test ismutable(1) == false
@test ismutable([]) == true

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

0 comments on commit 4f02bc3

Please sign in to comment.