Fixes #16018: Existential widening for wildcard arguments#26152
Open
He-Pin wants to merge 1 commit into
Open
Conversation
Contributor
Author
|
This fix will facilitate cross-compilation between Scala 2.13 and Scala 3, especially for Java users. |
He-Pin
commented
May 23, 2026
He-Pin
commented
May 23, 2026
odersky
requested changes
May 28, 2026
Contributor
odersky
left a comment
There was a problem hiding this comment.
Please don't inundate reviewers with tons of AI-slop generated stuff. You are imposing with this on my valuable time. Please, next time pre-digest the material and present only the relevant points in a concise fashion.
He-Pin
added a commit
to He-Pin/scala3
that referenced
this pull request
May 29, 2026
Motivation: PR scala#26152 review asked for a narrower recursive-bound guard in TypeComparer.compareCaptured: removing the early broad guard that bypassed capture conversion, replacing the lazy val cache with a def, and checking only the bound the branch actually consults. Modification: - compareCaptured: drop the early `if hasRecursiveParamBounds(tparam)` guard so capture conversion runs unconditionally as before. - Replace `lazy val recursiveParamBounds` / `def hasRecursiveParamBounds` with a single per-call `def hasRecursiveBound(bound, tparam)` walked over the relevant half only (`tparam.info.bounds.hi` for v > 0, `.lo` for v < 0). - tests/neg/i16018d.scala: rewrite the F-bounded case to nest under a covariant container so capture conversion does not apply and the existential-widening branch is the one that runs, exercising the per-bound guard directly. Result: Net -22 lines in TypeComparer. The guard is per-bound and called only when needed. Capture conversion runs unconditionally and produces the sound path-dependent typed tree for stable LHS (which Ycheck:all accepts) — the previous broad guard was over-restrictive. References: scala#26152
`TypeComparer.compareCaptured` consulted only the declared parameter bound, which collapses to `Any`/`Nothing` for unconstrained parameters, so `F[? <: hi] <: F[hi]` (covariant) and `G[? >: lo] <: G[lo]` (contravariant) were rejected. Consult the wildcard's own bound, intersected (resp. unioned) with the declared bound for soundness. Recursive declared bounds stay on the conservative path to avoid forcing `paramBounds(tparam)` to loop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #16018.
TypeComparer.compareCapturedconsulted only the declared parameter bound (paramBounds(tparam).hi/.lo), which collapses toAny/Nothingfor unconstrained parameters, soF[? <: hi] <: F[hi](covariant) andG[? >: lo] <: G[lo](contravariant) were rejected. The fix consults the wildcard's own bound, intersected (resp. unioned) with the declared bound for soundness. Recursive (F-bounded) declared bounds stay on the conservative path to avoid forcingparamBounds(tparam)to loop.Tests:
tests/pos/i16018.scala,tests/pos/i16018b.scala,tests/neg/i16018.scala.How much have you relied on LLM-based tools in this contribution?
Extensively, for the fix and test.
How was the solution tested?
Covered by tests from the issue and some more tests