From 8faa424f6827f25b2aeb4e1240abdcc8cad299df Mon Sep 17 00:00:00 2001 From: Shan Sikdar Date: Mon, 3 Feb 2020 22:13:37 -0500 Subject: [PATCH 1/8] rename isimmutable to ismutable --- base/compiler/abstractinterpretation.jl | 2 +- base/compiler/ssair/passes.jl | 2 +- base/compiler/tfuncs.jl | 4 ++-- base/exports.jl | 1 + base/gcutils.jl | 4 ++-- base/reflection.jl | 23 ++++++++++++++++++++++- doc/src/base/base.md | 1 + test/reflection.jl | 4 ++-- 8 files changed, 32 insertions(+), 9 deletions(-) diff --git a/base/compiler/abstractinterpretation.jl b/base/compiler/abstractinterpretation.jl index 23384c02a190e..a48fd81b8fb3e 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 c61527053fe51..2e6a55db16e25 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 d1b2034b7d4ba..e22aafed63fdf 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/exports.jl b/base/exports.jl index ff541ca5404c0..7124b1fd2b6c7 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -642,6 +642,7 @@ export isbits, isequal, isimmutable, + ismutable, isless, ifelse, objectid, diff --git a/base/gcutils.jl b/base/gcutils.jl index f5e6660d66095..c64d06bf34758 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 e865490ae8cab..ed5502f7ad3da 100644 --- a/base/reflection.jl +++ b/base/reflection.jl @@ -417,7 +417,28 @@ julia> isimmutable([1,2]) false ``` """ -isimmutable(@nospecialize(x)) = (@_pure_meta; !typeof(x).mutable) +function isimmutable(@nospecialize(x)) + depwarn(" isimmutable is deprecated use ismutable instead ", :isimmutable) + return (@_pure_meta; !typeof(x).mutable) +end + +""" + ismutable(v) -> Bool + +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> ismutable(1) +false + +julia> ismutable([1,2]) +true +``` +""" +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 9dc9f651be80d..c7a1e40503b32 100644 --- a/doc/src/base/base.md +++ b/doc/src/base/base.md @@ -159,6 +159,7 @@ Base.isdispatchtuple ```@docs Base.isimmutable +Base.ismutable Base.isabstracttype Base.isprimitivetype Base.issingletontype diff --git a/test/reflection.jl b/test/reflection.jl index f567eb40d55f5..2250630155b9f 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 From 6b76c59bdcc938309797c24cf079a2e299c8658b Mon Sep 17 00:00:00 2001 From: Shan Sikdar Date: Tue, 4 Feb 2020 09:40:24 -0500 Subject: [PATCH 2/8] remove depwarn and move isimmutable to the bottom of deprecated.jl --- base/deprecated.jl | 21 +++++++++++++++++++++ base/reflection.jl | 21 --------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index 58c4e3f61db62..31b42a5f5a365 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -190,3 +190,24 @@ MPFR.BigFloat(x::Real, prec::Int, rounding::RoundingMode) = BigFloat(x, rounding end # END 1.3 deprecations + +# BEGIN 1.5 deprecations +""" + isimmutable(v) -> Bool + +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)) = (@_pure_meta; !typeof(x).mutable) + +# END 1.5 deprecations diff --git a/base/reflection.jl b/base/reflection.jl index ed5502f7ad3da..813c1cdd08c4d 100644 --- a/base/reflection.jl +++ b/base/reflection.jl @@ -401,27 +401,6 @@ function datatype_fielddesc_type(dt::DataType) return (flags >> 1) & 3 end -""" - isimmutable(v) -> Bool - -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 -``` -""" -function isimmutable(@nospecialize(x)) - depwarn(" isimmutable is deprecated use ismutable instead ", :isimmutable) - return (@_pure_meta; !typeof(x).mutable) -end - """ ismutable(v) -> Bool From a77d03af3a7c8090edada5863e6ccaaee47d9003 Mon Sep 17 00:00:00 2001 From: Shan Sikdar Date: Tue, 4 Feb 2020 18:52:29 -0500 Subject: [PATCH 3/8] add warning message to isimmutable documentation --- base/deprecated.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index 31b42a5f5a365..58cab646ff8b8 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -194,7 +194,8 @@ 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. 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. From ca4d10423f708709aac9fc5ae8e103f26aaf03a6 Mon Sep 17 00:00:00 2001 From: Shan Sikdar Date: Tue, 4 Feb 2020 18:57:21 -0500 Subject: [PATCH 4/8] add NEWS item --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 9280a30ea42ac..a949d08fc1d4d 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 -------------------- From a252f5e64f9a9f07551238567b7d76eafc23d634 Mon Sep 17 00:00:00 2001 From: Shan Sikdar Date: Wed, 5 Feb 2020 10:00:57 -0500 Subject: [PATCH 5/8] add Julia version to deprecation warning in docstring --- base/deprecated.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index 58cab646ff8b8..c17ab3a2d7683 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -195,7 +195,7 @@ end """ isimmutable(v) -> Bool !!! warning - Consider using `!ismutable(v)` instead, as `isimmutable(v)` will be replaced by `!ismutable(v)` in a future release. + 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. From eed8b677ae7e29a4157434ec2b9469012dc47f5c Mon Sep 17 00:00:00 2001 From: Shan Sikdar Date: Wed, 5 Feb 2020 10:11:16 -0500 Subject: [PATCH 6/8] change isimmutable to be defined in terms of ismutable --- base/deprecated.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index c17ab3a2d7683..48105e831f6eb 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -209,6 +209,6 @@ julia> isimmutable([1,2]) false ``` """ -isimmutable(@nospecialize(x)) = (@_pure_meta; !typeof(x).mutable) +isimmutable(@nospecialize(x)) = !ismutable(x) # END 1.5 deprecations From 28f7f9f80e340dca3f1d1e9c1e47aa73fe26a390 Mon Sep 17 00:00:00 2001 From: Shan Sikdar Date: Wed, 5 Feb 2020 10:35:31 -0500 Subject: [PATCH 7/8] move isimmutable export to deprecated.jl --- base/deprecated.jl | 1 + base/exports.jl | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index 48105e831f6eb..5f1efc85cf4ce 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -210,5 +210,6 @@ false ``` """ isimmutable(@nospecialize(x)) = !ismutable(x) +export isimmutable # END 1.5 deprecations diff --git a/base/exports.jl b/base/exports.jl index 7124b1fd2b6c7..6362bc2316598 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -641,7 +641,6 @@ export identity, isbits, isequal, - isimmutable, ismutable, isless, ifelse, From aba863fd98d9778f6db64ada4cac2469fe726bbb Mon Sep 17 00:00:00 2001 From: Shan Sikdar Date: Wed, 5 Feb 2020 10:36:18 -0500 Subject: [PATCH 8/8] change order of ismutable isimmutable in docs --- doc/src/base/base.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/src/base/base.md b/doc/src/base/base.md index c7a1e40503b32..fab114d5749c2 100644 --- a/doc/src/base/base.md +++ b/doc/src/base/base.md @@ -158,8 +158,8 @@ Base.isdispatchtuple ### Declared structure ```@docs -Base.isimmutable Base.ismutable +Base.isimmutable Base.isabstracttype Base.isprimitivetype Base.issingletontype