Reapply "gf: record complete method interferences and fix ambiguity reporting" (#62396, #63469)
`Method.interferences` was an incomplete record of the pairwise
intersecting-but-not-morespecific relation: `get_intersect_visitor` stopped the
insertion scan early once the new method was a strict subtype of an existing
method that dominates its dispatch. The implicit justification was monotonicity
of `morespecific` under subtyping, which is false -- `morespecific`
short-circuits on subtyping, but a supertype's structural specificity relations
do not transfer to a subtype and can even reverse. A dominated-looking supertype
could therefore hide intersections that were never recorded, making dispatch
order-dependent and silently wrong:
f(::T, ::Vararg{T}) where T<:Integer = "A"
f(::Integer, ::Vararg{Union{Int,String}}) = "C"
f(::Integer, ::Vararg{String}) = "B"
f(1) # silently returned "B"; these three form a specificity cycle
# and the call is ambiguous, as it is in the A,B,C order
Record every intersecting method instead. With the relation complete,
`morespecific(target, start)` is exactly `target in interferences(start) &&
start not in interferences(target)`, so the transitive interference-graph walk
is replaced by that two-membership test in both the runtime and the Compiler --
the walk was itself a liability, returning false positives on disjoint pairs and
inverted answers on the omitted ones, including on the `verify_call`
invalidation fast path.
On that foundation, make the match sort's ambiguity reporting exact. A match may
be dropped from the sort only when strictly-morespecific covers take over both
of its roles: what it was beating, and what it was *blocking* -- selection
requires beating every applicable match, so a match that can never win can still
be the only reason another does not. Both obligations become one pointwise rule:
a cover only blocks where it is applicable, so when the blocked match beats some
cover, the union of the covers it does not beat must contain the region where
the dropped match was blocking it. Ambiguity certification is likewise no longer
single-cover: an apparent ambiguity is resolved when the intersection is a
subtype of the union of the more-specific methods' signatures, with methods
excluded whose specificity cycles back through the pair, which also fixes the
spurious `Test.detect_ambiguities` reports on `Union`-typed arguments. The
minmax match now flows through that same cover protocol rather than three
drifted hand-rolled kernels, fully-covering matches the sort never visits get
the same transfer check before being skipped, and an ambiguous call with no
beat-everything winner is no longer reported as having no applicable method.
The Method-level `METHOD_SIG_LATEST_ONLY` bit is exactly the fact that the
interference set is empty, so it is read from the set directly and
`METHOD_SIG_PRECOMPILE_MANY`, which existed only to suppress it at load, is
removed. The opposite pole -- a method strictly morespecific than nothing it
intersects -- cannot be read from the method's own set and gets a new
`METHOD_SIG_NO_LOSERS` bit, maintained for free by the activation scan and
carried through incremental images rather than re-derived, since that scan
cannot see same-image methods. Both facts gate fast paths: an empty set marks
the unique dispatch winner, and `NO_LOSERS` skips the dominance-transfer scan.
`Core.MethodMatch.fully_covers` is a plain `Bool` again (the out-of-range
`SENTINEL` marker is gone, and with it the tri-valued enum), the match sort is
exposed as `jl_sort_method_matches` so `Base.isambiguous` reuses the runtime
algorithm instead of reimplementing it, and interference-set snapshots held
across safepoints in the sort are rooted or reloaded.
Removing the early-out means insertion records dominated intersectors it used to
skip, so a fully pairwise-intersecting clique of methods under a dominating
umbrella now costs O(n^2) in total (~2.4/4.6/9.5 ms per method at n=500/1000/
2000); tables without such cliques are unaffected, as disjoint entries are still
pruned by the typemap.
Note that this may cause extra compilation in packages that have
ambiguities they were failing to handle, since we didn't previously
handle those correctly (https://github.com/JuliaLang/julia/pull/62297
and https://github.com/JuliaLang/julia/pull/62322 for additional info).
Adding methods to handle `Type{Union{}}` (as demonstrated in
https://github.com/JuliaLang/julia/pull/49349) will fix those packages
(once https://github.com/JuliaLang/julia/pull/63346 also fixes the bugs
introduced by the TypeEgal work).
Fixes #62262
Assisted-by: Claude Code (Opus 4.8, Opus 5, Fable 5, others)