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.

(cherry picked from commit 3d6da2c)
  • Loading branch information
Keno authored and KristofferC committed May 6, 2024
1 parent 6ca5297 commit bb57f82
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 @@ -1000,12 +1000,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 @@ -5651,3 +5651,6 @@ let t = ntuple(i -> i % 8 == 1 ? Int64 : Float64, 4000)
@test only(Base.return_types(Base.promote_typeof, t)) == Type{Float64}
@test only(Base.return_types(vcat, t)) == Vector{Float64}
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 bb57f82

Please sign in to comment.