From 4f02bc37e5d13b1d288a1b0072c8ab22170be701 Mon Sep 17 00:00:00 2001 From: ssikdar1 Date: Fri, 7 Feb 2020 13:46:07 -0500 Subject: [PATCH] #34518 - replace isimmutable with ismutable (#34652) --- NEWS.md | 1 + base/compiler/abstractinterpretation.jl | 2 +- base/compiler/ssair/passes.jl | 2 +- base/compiler/tfuncs.jl | 4 ++-- base/deprecated.jl | 22 ++++++++++++++++++++++ base/exports.jl | 2 +- base/gcutils.jl | 4 ++-- base/reflection.jl | 14 +++++++------- doc/src/base/base.md | 1 + test/reflection.jl | 4 ++-- 10 files changed, 40 insertions(+), 16 deletions(-) diff --git a/NEWS.md b/NEWS.md index 2f2b711668ba4c..b6cd935bab524a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 -------------------- diff --git a/base/compiler/abstractinterpretation.jl b/base/compiler/abstractinterpretation.jl index 23384c02a190e5..a48fd81b8fb3e8 100644 --- a/base/compiler/abstractinterpretation.jl +++ b/base/compiler/abstractinterpretation.jl @@ -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 diff --git a/base/compiler/ssair/passes.jl b/base/compiler/ssair/passes.jl index c61527053fe515..2e6a55db16e25a 100644 --- a/base/compiler/ssair/passes.jl +++ b/base/compiler/ssair/passes.jl @@ -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 diff --git a/base/compiler/tfuncs.jl b/base/compiler/tfuncs.jl index d1b2034b7d4ba6..e22aafed63fdfc 100644 --- a/base/compiler/tfuncs.jl +++ b/base/compiler/tfuncs.jl @@ -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 @@ -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 diff --git a/base/deprecated.jl b/base/deprecated.jl index fcd64e42061a45..c01000a66768b4 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -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!")) @@ -201,4 +222,5 @@ macro get!(h, key0, default) end end + # END 1.5 deprecations diff --git a/base/exports.jl b/base/exports.jl index ff541ca5404c07..6362bc23165987 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -641,7 +641,7 @@ export identity, isbits, isequal, - isimmutable, + ismutable, isless, ifelse, objectid, diff --git a/base/gcutils.jl b/base/gcutils.jl index f5e6660d660955..c64d06bf347586 100644 --- a/base/gcutils.jl +++ b/base/gcutils.jl @@ -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), @@ -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}), diff --git a/base/reflection.jl b/base/reflection.jl index e865490ae8cab8..813c1cdd08c4d4 100644 --- a/base/reflection.jl +++ b/base/reflection.jl @@ -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 diff --git a/doc/src/base/base.md b/doc/src/base/base.md index 9dc9f651be80dc..fab114d5749c2c 100644 --- a/doc/src/base/base.md +++ b/doc/src/base/base.md @@ -158,6 +158,7 @@ Base.isdispatchtuple ### Declared structure ```@docs +Base.ismutable Base.isimmutable Base.isabstracttype Base.isprimitivetype diff --git a/test/reflection.jl b/test/reflection.jl index f567eb40d55f5e..2250630155b9fe 100644 --- a/test/reflection.jl +++ b/test/reflection.jl @@ -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