Skip to content
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

Improve precision of nfields_tfunc on Union #37497

Merged
merged 1 commit into from
Sep 13, 2020
Merged

Conversation

Keno
Copy link
Member

@Keno Keno commented Sep 9, 2020

nfields_tfunc(Tuple{Int, Union{Int, Float64}}) gave Const(2),
but nfields_tfunc(Union{Tuple{Int, Float64}, Tuple{Int, Int}}) gave
Int. In general, it's not great for the tfuncs to give different
answers when given == types, because other parts of the system
will happily substitute such types without changes to the user code,
which can change the inference result and lead to instability.

@@ -381,6 +381,9 @@ function nfields_tfunc(@nospecialize(x))
return Const(isdefined(x, :types) ? length(x.types) : length(x.name.names))
end
end
if isa(x, Union)
return tmerge(nfields_tfunc(x.a), nfields_tfunc(x.b))
Copy link
Member

Choose a reason for hiding this comment

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

Could optimize a bit for larger unions by returning immediately if nfields_tfunc(x.a)==Int and only recursing down nfields_tfunc(x.b)otherwise (or vice versa). Not sure if worth it.

`nfields_tfunc(Tuple{Int, Union{Int, Float64}})` gave `Const(2)`,
but `nfields_tfunc(Union{Tuple{Int, Float64}, Tuple{Int, Int}})` gave
`Int`. In general, it's not great for the tfuncs to give different
answers when given `==` types, because other parts of the system
will happily substitute such types without changes to the user code,
which can change the inference result and lead to instability.
@Keno Keno merged commit 938fdaa into master Sep 13, 2020
@Keno Keno deleted the kf/nfieldsprecision branch September 13, 2020 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants