Skip to content

Commit 6323e43

Browse files
committed
Cleanup: Use issingletontype consistently
Rather than reaching for `isdefined(x, :instance)`. They are currently equivalent, but I was playing with an extension that would have made them not be. I don't currently have plans to finish that particular PR, but this cleanup seems good regardless to specify exactly what is meant rather than reaching for the implementation.
1 parent ef6974d commit 6323e43

File tree

10 files changed

+16
-16
lines changed

10 files changed

+16
-16
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,7 @@ function abstract_call_builtin(interp::AbstractInterpreter, f::Builtin, (; fargs
16401640
bty = widenconditional(bty)
16411641
elseif rt === Const(true)
16421642
bty = Union{}
1643-
elseif bty isa Type && isdefined(typeof(aty.val), :instance) # can only widen a if it is a singleton
1643+
elseif bty isa Type && issingletontype(typeof(aty.val)) # can only widen a if it is a singleton
16441644
bty = typesubtract(bty, typeof(aty.val), InferenceParams(interp).MAX_UNION_SPLITTING)
16451645
else
16461646
bty = widenconditional(bty)
@@ -1653,7 +1653,7 @@ function abstract_call_builtin(interp::AbstractInterpreter, f::Builtin, (; fargs
16531653
aty = widenconditional(aty)
16541654
elseif rt === Const(true)
16551655
aty = Union{}
1656-
elseif aty isa Type && isdefined(typeof(bty.val), :instance) # same for b
1656+
elseif aty isa Type && issingletontype(typeof(bty.val)) # same for b
16571657
aty = typesubtract(aty, typeof(bty.val), InferenceParams(interp).MAX_UNION_SPLITTING)
16581658
else
16591659
aty = widenconditional(aty)

base/compiler/inferenceresult.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function most_general_argtypes(method::Union{Method, Nothing}, @nospecialize(spe
150150
end
151151
for i in 1:length(vargtype_elements)
152152
atyp = vargtype_elements[i]
153-
if isa(atyp, DataType) && isdefined(atyp, :instance)
153+
if issingletontype(atyp)
154154
# replace singleton types with their equivalent Const object
155155
vargtype_elements[i] = Const(atyp.instance)
156156
elseif isconstType(atyp)
@@ -179,7 +179,7 @@ function most_general_argtypes(method::Union{Method, Nothing}, @nospecialize(spe
179179
tail_index -= 1
180180
end
181181
atyp = unwraptv(atyp)
182-
if isa(atyp, DataType) && isdefined(atyp, :instance)
182+
if issingletontype(atyp)
183183
# replace singleton types with their equivalent Const object
184184
atyp = Const(atyp.instance)
185185
elseif isconstType(atyp)

base/compiler/tfuncs.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ function egal_tfunc(::ConstsLattice, @nospecialize(x), @nospecialize(y))
251251
return Const(x.val === y.val)
252252
elseif !hasintersect(widenconst(x), widenconst(y))
253253
return Const(false)
254-
elseif (isa(x, Const) && y === typeof(x.val) && isdefined(y, :instance)) ||
255-
(isa(y, Const) && x === typeof(y.val) && isdefined(x, :instance))
254+
elseif (isa(x, Const) && y === typeof(x.val) && issingletontype(x)) ||
255+
(isa(y, Const) && x === typeof(y.val) && issingletontype(y))
256256
return Const(true)
257257
end
258258
return Bool
@@ -1739,7 +1739,7 @@ function tuple_tfunc(@specialize(lattice::AbstractLattice), argtypes::Vector{Any
17391739
end
17401740
typ = Tuple{params...}
17411741
# replace a singleton type with its equivalent Const object
1742-
isdefined(typ, :instance) && return Const(typ.instance)
1742+
issingletontype(typ) && return Const(typ.instance)
17431743
return anyinfo ? PartialStruct(typ, argtypes) : typ
17441744
end
17451745
tuple_tfunc(argtypes::Vector{Any}) = tuple_tfunc(fallback_lattice, argtypes)

base/compiler/typelattice.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ function ⊑(lattice::ConstsLattice, @nospecialize(a), @nospecialize(b))
320320
# most conservative option.
321321
return isa(b, Type) && isa(a.val, b)
322322
elseif isa(b, Const)
323-
if isa(a, DataType) && isdefined(a, :instance)
323+
if issingletontype(a)
324324
return a.instance === b.val
325325
end
326326
return false

base/compiler/typeutils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function has_nontrivial_const_info(lattice::ConstsLattice, @nospecialize t)
5555
isa(t, PartialTypeVar) && return true
5656
if isa(t, Const)
5757
val = t.val
58-
return !isdefined(typeof(val), :instance) && !(isa(val, Type) && hasuniquerep(val))
58+
return !issingletontype(typeof(val)) && !(isa(val, Type) && hasuniquerep(val))
5959
end
6060
return has_nontrivial_const_info(widenlattice(lattice), t)
6161
end

base/compiler/utilities.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,15 @@ function singleton_type(@nospecialize(ft))
295295
return ft.val
296296
elseif isconstType(ft)
297297
return ft.parameters[1]
298-
elseif ft isa DataType && isdefined(ft, :instance)
298+
elseif issingletontype(ft)
299299
return ft.instance
300300
end
301301
return nothing
302302
end
303303

304304
function maybe_singleton_const(@nospecialize(t))
305305
if isa(t, DataType)
306-
if isdefined(t, :instance)
306+
if issingletontype(t)
307307
return Const(t.instance)
308308
elseif isconstType(t)
309309
return Const(t.parameters[1])

base/summarysize.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ end
7777
(ss::SummarySize)(@nospecialize obj) = _summarysize(ss, obj)
7878
# define the general case separately to make sure it is not specialized for every type
7979
@noinline function _summarysize(ss::SummarySize, @nospecialize obj)
80-
isdefined(typeof(obj), :instance) && return 0
80+
issingletontype(typeof(obj)) && return 0
8181
# NOTE: this attempts to discover multiple copies of the same immutable value,
8282
# and so is somewhat approximate.
8383
key = ccall(:jl_value_ptr, Ptr{Cvoid}, (Any,), obj)

stdlib/Serialization/src/Serialization.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ function deserialize_typename(s::AbstractSerializer, number)
13231323
tn.max_methods = maxm
13241324
if has_instance
13251325
ty = ty::DataType
1326-
if !isdefined(ty, :instance)
1326+
if !Base.issingletontype(ty)
13271327
singleton = ccall(:jl_new_struct, Any, (Any, Any...), ty)
13281328
# use setfield! directly to avoid `fieldtype` lowering expecting to see a Singleton object already on ty
13291329
ccall(:jl_set_nth_field, Cvoid, (Any, Csize_t, Any), ty, Base.fieldindex(DataType, :instance)-1, singleton)

test/compiler/contextual.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ module MiniCassette
7070
end
7171

7272
function overdub_generator(self, c, f, args)
73-
if !isdefined(f, :instance)
73+
if !Base.issingletontype(f)
7474
return :(return f(args...))
7575
end
7676

test/core.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4870,8 +4870,8 @@ end
48704870
let a = Val{Val{TypeVar(:_, Int)}},
48714871
b = Val{Val{x} where x<:Int}
48724872

4873-
@test !isdefined(a, :instance)
4874-
@test isdefined(b, :instance)
4873+
@test !Base.issingletontype(a)
4874+
@test Base.issingletontype(b)
48754875
@test Base.isconcretetype(b)
48764876
end
48774877

0 commit comments

Comments
 (0)