Skip to content

Commit 6fd82d7

Browse files
authored
lattice: fix correctness bug in tmerge limiting (#50939)
In 162ee48, the added code causes us to violate the tmerge_fast_path requirements on the lattice. This was causing the fall-though from the earlier tmerge_fast_path to not return correct answers to inference anymore. Adding back another tmerge_fast_path, on the types, allows us to recover correctness without regressing accuracy to before #47992. Also added a test case for an example in which tmerge_fast_path does not return a correctly limited answer, since it does not model UnionAll complexity growth.
1 parent 74ce6cf commit 6fd82d7

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

base/compiler/typelimits.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ end
676676
return tmerge(wl, typea, typeb)
677677
end
678678

679-
@nospecializeinfer function tmerge(::JLTypeLattice, @nospecialize(typea::Type), @nospecialize(typeb::Type))
679+
@nospecializeinfer function tmerge(lattice::JLTypeLattice, @nospecialize(typea::Type), @nospecialize(typeb::Type))
680680
# it's always ok to form a Union of two concrete types
681681
act = isconcretetype(typea)
682682
bct = isconcretetype(typeb)
@@ -687,8 +687,8 @@ end
687687
if (act || isType(typea)) && (bct || isType(typeb))
688688
return Union{typea, typeb}
689689
end
690-
typea <: typeb && return typeb
691-
typeb <: typea && return typea
690+
u = tmerge_fast_path(lattice, typea, typeb)
691+
u === nothing || return u
692692
return tmerge_types_slow(typea, typeb)
693693
end
694694

test/compiler/inference.jl

+13
Original file line numberDiff line numberDiff line change
@@ -4584,6 +4584,19 @@ end
45844584
g = Base.ImmutableDict(g, 1=>2)
45854585
end
45864586
end |> only === Union{}
4587+
4588+
a = Val{Union{}}
4589+
a = Core.Compiler.tmerge(Union{a, Val{a}}, a)
4590+
@test a == Union{Val{Union{}}, Val{Val{Union{}}}}
4591+
a = Core.Compiler.tmerge(Union{a, Val{a}}, a)
4592+
@test a == Union{Val{Union{}}, Val{Val{Union{}}}, Val{Union{Val{Union{}}, Val{Val{Union{}}}}}}
4593+
a = Core.Compiler.tmerge(Union{a, Val{a}}, a)
4594+
@test a == Val
4595+
4596+
a = Val{Union{}}
4597+
a = Core.Compiler.tmerge(Core.Compiler.JLTypeLattice(), Val{<:a}, a)
4598+
@test_broken a != Val{<:Val{Union{}}}
4599+
@test_broken a == Val{<:Val} || a == Val
45874600
end
45884601

45894602
# Test that a function-wise `@max_methods` works as expected

0 commit comments

Comments
 (0)