Skip to content

Commit ac4519a

Browse files
Kenopull[bot]
authored andcommitted
Cleanup: Use issingletontype consistently (JuliaLang#47618)
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 8ef3c2a commit ac4519a

File tree

10 files changed

+15
-15
lines changed

10 files changed

+15
-15
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1671,7 +1671,7 @@ end
16711671
@inline function egal_condition(c::Const, @nospecialize(xt), max_union_splitting::Int)
16721672
thentype = c
16731673
elsetype = widenslotwrapper(xt)
1674-
if elsetype isa Type && isdefined(typeof(c.val), :instance) # can only widen a if it is a singleton
1674+
if elsetype isa Type && issingletontype(typeof(c.val)) # can only widen a if it is a singleton
16751675
elsetype = typesubtract(elsetype, typeof(c.val), max_union_splitting)
16761676
end
16771677
return ConditionalTypes(thentype, elsetype)

base/compiler/inferenceresult.jl

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

base/compiler/tfuncs.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ function egal_tfunc(::ConstsLattice, @nospecialize(x), @nospecialize(y))
253253
return Const(x.val === y.val)
254254
elseif !hasintersect(widenconst(x), widenconst(y))
255255
return Const(false)
256-
elseif (isa(x, Const) && y === typeof(x.val) && isdefined(y, :instance)) ||
257-
(isa(y, Const) && x === typeof(y.val) && isdefined(x, :instance))
256+
elseif (isa(x, Const) && y === typeof(x.val) && issingletontype(x)) ||
257+
(isa(y, Const) && x === typeof(y.val) && issingletontype(y))
258258
return Const(true)
259259
end
260260
return Bool
@@ -1753,7 +1753,7 @@ function tuple_tfunc(@specialize(lattice::AbstractLattice), argtypes::Vector{Any
17531753
end
17541754
typ = Tuple{params...}
17551755
# replace a singleton type with its equivalent Const object
1756-
isdefined(typ, :instance) && return Const(typ.instance)
1756+
issingletontype(typ) && return Const(typ.instance)
17571757
return anyinfo ? PartialStruct(typ, argtypes) : typ
17581758
end
17591759
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
@@ -493,7 +493,7 @@ function ⊑(lattice::ConstsLattice, @nospecialize(a), @nospecialize(b))
493493
# most conservative option.
494494
return isa(b, Type) && isa(a.val, b)
495495
elseif isa(b, Const)
496-
if isa(a, DataType) && isdefined(a, :instance)
496+
if issingletontype(a)
497497
return a.instance === b.val
498498
end
499499
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
@@ -296,15 +296,15 @@ function singleton_type(@nospecialize(ft))
296296
return ft.val
297297
elseif isconstType(ft)
298298
return ft.parameters[1]
299-
elseif ft isa DataType && isdefined(ft, :instance)
299+
elseif issingletontype(ft)
300300
return ft.instance
301301
end
302302
return nothing
303303
end
304304

305305
function maybe_singleton_const(@nospecialize(t))
306306
if isa(t, DataType)
307-
if isdefined(t, :instance)
307+
if issingletontype(t)
308308
return Const(t.instance)
309309
elseif isconstType(t)
310310
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)