Skip to content

Commit

Permalink
tfunc: Constant fold isdefined for (dt::DataType).fields
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 `fieldcount` for `Tuple`s, causing significant inference
quality degradation. Adjust `fieldcount` to avoid this.
  • Loading branch information
Keno committed Apr 24, 2024
1 parent c38e7cd commit e555a66
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 @@ -1003,12 +1003,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 e555a66

Please sign in to comment.