Skip to content

Commit 3d6da2c

Browse files
authored
Make sure fieldcount constant-folds for Tuple{...} (#54239)
In PR #53750, we stopped constant folding `isdefined` for non-`const` fields assuming that they may revert to `undef`. Unfortunately, this broke const-prop for `fieldcount` for `Tuple`s, causing significant inference quality degradation. Adjust `fieldcount` to avoid this.
1 parent 729a8c9 commit 3d6da2c

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

base/reflection.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,12 +1005,15 @@ function datatype_fieldcount(t::DataType)
10051005
return fieldcount(types)
10061006
end
10071007
return nothing
1008-
elseif isabstracttype(t) || (t.name === Tuple.name && isvatuple(t))
1008+
elseif isabstracttype(t)
10091009
return nothing
10101010
end
1011-
if isdefined(t, :types)
1011+
if t.name === Tuple.name
1012+
isvatuple(t) && return nothing
10121013
return length(t.types)
10131014
end
1015+
# Equivalent to length(t.types), but `t.types` is lazy and we do not want
1016+
# to be forced to compute it.
10141017
return length(t.name.names)
10151018
end
10161019

test/compiler/inference.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5725,3 +5725,6 @@ let interp = CachedConditionalInterp();
57255725
result.linfo.def.name === :func_cached_conditional
57265726
end == 1
57275727
end
5728+
5729+
# fieldcount on `Tuple` should constant fold, even though `.fields` not const
5730+
@test fully_eliminated(Base.fieldcount, Tuple{Type{Tuple{Nothing, Int, Int}}})

0 commit comments

Comments
 (0)