Skip to content

Commit bb57f82

Browse files
KenoKristofferC
authored andcommitted
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. (cherry picked from commit 3d6da2c)
1 parent 6ca5297 commit bb57f82

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
@@ -1000,12 +1000,15 @@ function datatype_fieldcount(t::DataType)
10001000
return fieldcount(types)
10011001
end
10021002
return nothing
1003-
elseif isabstracttype(t) || (t.name === Tuple.name && isvatuple(t))
1003+
elseif isabstracttype(t)
10041004
return nothing
10051005
end
1006-
if isdefined(t, :types)
1006+
if t.name === Tuple.name
1007+
isvatuple(t) && return nothing
10071008
return length(t.types)
10081009
end
1010+
# Equivalent to length(t.types), but `t.types` is lazy and we do not want
1011+
# to be forced to compute it.
10091012
return length(t.name.names)
10101013
end
10111014

test/compiler/inference.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5651,3 +5651,6 @@ let t = ntuple(i -> i % 8 == 1 ? Int64 : Float64, 4000)
56515651
@test only(Base.return_types(Base.promote_typeof, t)) == Type{Float64}
56525652
@test only(Base.return_types(vcat, t)) == Vector{Float64}
56535653
end
5654+
5655+
# fieldcount on `Tuple` should constant fold, even though `.fields` not const
5656+
@test fully_eliminated(Base.fieldcount, Tuple{Type{Tuple{Nothing, Int, Int}}})

0 commit comments

Comments
 (0)