Skip to content

Commit

Permalink
Make sure fieldcount constant-folds for Tuple{...} (#54239)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Keno authored Apr 25, 2024
1 parent 729a8c9 commit 3d6da2c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1005,12 +1005,15 @@ function datatype_fieldcount(t::DataType)
return fieldcount(types)
end
return nothing
elseif isabstracttype(t) || (t.name === Tuple.name && isvatuple(t))
elseif isabstracttype(t)
return nothing
end
if isdefined(t, :types)
if t.name === Tuple.name
isvatuple(t) && return nothing
return length(t.types)
end
# Equivalent to length(t.types), but `t.types` is lazy and we do not want
# to be forced to compute it.
return length(t.name.names)
end

Expand Down
3 changes: 3 additions & 0 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5725,3 +5725,6 @@ let interp = CachedConditionalInterp();
result.linfo.def.name === :func_cached_conditional
end == 1
end

# fieldcount on `Tuple` should constant fold, even though `.fields` not const
@test fully_eliminated(Base.fieldcount, Tuple{Type{Tuple{Nothing, Int, Int}}})

0 comments on commit 3d6da2c

Please sign in to comment.