-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Improve precision of tmeet
for vararg PartialStruct
#39437
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -586,39 +586,40 @@ end | |
# compute typeintersect over the extended inference lattice | ||
# where v is in the extended lattice, and t is a Type | ||
function tmeet(@nospecialize(v), @nospecialize(t)) | ||
if isa(v, Const) | ||
if !has_free_typevars(t) && !isa(v.val, t) | ||
return Bottom | ||
end | ||
if isa(v, Type) | ||
return typeintersect(v, t) | ||
end | ||
has_free_typevars(t) && return v | ||
widev = widenconst(v) | ||
if widev <: t | ||
return v | ||
elseif isa(v, PartialStruct) | ||
has_free_typevars(t) && return v | ||
widev = widenconst(v) | ||
if widev <: t | ||
return v | ||
end | ||
ti = typeintersect(widev, t) | ||
if ti === Bottom | ||
return Bottom | ||
end | ||
end | ||
ti = typeintersect(widev, t) | ||
if ti === Bottom | ||
return Bottom | ||
end | ||
if isa(v, PartialStruct) | ||
@assert widev <: Tuple | ||
new_fields = Vector{Any}(undef, length(v.fields)) | ||
for i = 1:length(new_fields) | ||
if isa(v.fields[i], Core.TypeofVararg) | ||
new_fields[i] = v.fields[i] | ||
else | ||
new_fields[i] = tmeet(v.fields[i], widenconst(getfield_tfunc(t, Const(i)))) | ||
if new_fields[i] === Bottom | ||
return Bottom | ||
end | ||
if isa(ti, DataType) && ti.name === Tuple.name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't we just assert that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a somewhat indirect way and with some assumptions on the well-behavedness of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But, huh, is the assertion actually valid? I think it implies that if the |
||
num_fields = length(ti.parameters) | ||
isva = isvatuple(ti) | ||
else | ||
nfields_ti = nfields_tfunc(ti) | ||
isva = !isa(nfields_ti, Const) | ||
num_fields = isva ? length(v.fields) : (nfields_ti::Const).val | ||
end | ||
Comment on lines
+603
to
+610
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not too happy about this. What I really want to know is: What is the minimum number of fields of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC, this means that if we started with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me illustrate with an example: Let v == PartialStruct(Tuple{Int64, Vararg{Number}}, Any[Core.Const(1), Vararg{Number}] and t == Union{Tuple{Int,Int,Int},Tuple{Int,Float64,Float64,Float64}} then we get Core.PartialStruct(Tuple{Int64, Vararg{Union{Float64, Int64}}}, Any[Core.Const(1), Vararg{Union{Float64, Int64}}]) but it could even be Core.PartialStruct(Tuple{Int64, Union{Float64, Int64}, Union{Float64, Int64}, Vararg{Float64}}, Any[Core.Const(1), Union{Float64, Int64}, Union{Float64, Int64}, Vararg{Union{Float64, Int64}}]) (But maybe that's a bit too greedy and we leave it as is.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think it is worthwhile to extend these for |
||
new_fields = Vector{Any}(undef, num_fields) | ||
for i = 1:num_fields | ||
new_fields[i] = tmeet(unwrapva(v.fields[min(i, end)]), widenconst(getfield_tfunc(ti, Const(i)))) | ||
if new_fields[i] === Bottom | ||
return Bottom | ||
end | ||
end | ||
return tuple_tfunc(new_fields) | ||
elseif isa(v, Conditional) | ||
if !(Bool <: t) | ||
return Bottom | ||
if isva && isvarargtype(v.fields[end]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
new_fields[end] = Vararg{new_fields[end]} | ||
end | ||
return v | ||
return tuple_tfunc(new_fields) | ||
end | ||
return typeintersect(widenconst(v), t) | ||
# v is a Const or Conditional and its type is compatible with t | ||
return v | ||
end |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is basically an NFC, but it looks a bit funny this way: Is it really safe to rely on the type intersection if
isa(v, Type)
even ifhas_free_typevars(t)
? And if so, why is it not safe ifv
is something else (then usingwidev = widenconst(v)
of course)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to stop them from appearing, but that's a project for later. Mostly likely it would actually over-estimate the intersection (which is generally what we do here too), so we only try to avoid them most of the time.