Skip to content

improve isdefined precision for 0 field types #58220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Compiler/src/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@ end
return Const(true)
end
end
# datatype_fieldcount is what `fieldcount` uses internally
# and returns nothing (!==0) for non-definite field counts.
elseif datatype_fieldcount(a1) === 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That comment seems to be false still, as it throws when given a NamedTuple:

julia> Base.datatype_fieldcount(NamedTuple{<:Any, Tuple}.body)
ERROR: ArgumentError: type does not have a definite number of fields
Stacktrace:
 [1] fieldcount
   @ ./runtime_internals.jl:1155 [inlined]
 [2] datatype_fieldcount(t::DataType)
   @ Base ./runtime_internals.jl:1119
 [3] top-level scope
   @ REPL[5]:1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks like a bug in datatype_fieldcount; the intent seems to be for it not to throw.

return Const(false)
end
elseif isa(a1, Union)
# Results can only be `Const` or `Bool`
Expand Down
3 changes: 2 additions & 1 deletion Compiler/test/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,8 @@ let isdefined_tfunc(@nospecialize xs...) =
end
@test isdefined_tfunc(Union{UnionIsdefinedA,UnionIsdefinedB}, Const(:x)) === Const(true)
@test isdefined_tfunc(Union{UnionIsdefinedA,UnionIsdefinedB}, Const(:y)) === Const(false)
@test isdefined_tfunc(Union{UnionIsdefinedA,Nothing}, Const(:x)) === Bool
@test isdefined_tfunc(Union{UnionIsdefinedA,Nothing}, Const(:x)) === Const(false)
@test isdefined_tfunc(Nothing, Any) === Const(false)
end

# https://github.com/aviatesk/JET.jl/issues/379
Expand Down