|
| 1 | +// Directional matrix for the fix to `TypeComparer.compareCaptured` |
| 2 | +// (#16018 / Existential widening for wildcard arguments). |
| 3 | +// |
| 4 | +// The fix replaces `paramBounds(tparam).hi` (the *declared* parameter |
| 5 | +// bound) with `arg1.hi & paramBounds(tparam).hi` (the wildcard's own |
| 6 | +// upper bound, intersected with the declared one to preserve soundness). |
| 7 | +// Dually for contravariance, with `arg1.lo | paramBounds(tparam).lo`. |
| 8 | +// |
| 9 | +// Three implementations are distinguishable on this matrix: |
| 10 | +// |
| 11 | +// OLD : isSubType( paramBounds(tparam).hi, arg2 ) |
| 12 | +// NAIVE : isSubType( arg1.hi, arg2 ) |
| 13 | +// THE FIX : isSubType( arg1.hi & paramBounds(tparam).hi, arg2 ) |
| 14 | +// |
| 15 | +// Sections 1–3 below distinguish OLD from THE FIX (these are the cases |
| 16 | +// the original ticket cares about). Sections 4–5 distinguish a hypothetical |
| 17 | +// NAIVE refactor (drop the intersection) from THE FIX — they pin the |
| 18 | +// soundness guard so that future code archaeology cannot quietly delete it. |
| 19 | + |
| 20 | +object Test: |
| 21 | + |
| 22 | + class Co[+T] |
| 23 | + class Contra[-T] |
| 24 | + |
| 25 | + // ---- 1. Covariant + upper-bounded wildcard widens to its hi -------- |
| 26 | + // |
| 27 | + // For `Co[+T]`, `paramBounds(tparam) = [Nothing, Any]`. OLD asked |
| 28 | + // `Any <: M` (false). THE FIX asks `M & Any <: M` (true). |
| 29 | + |
| 30 | + def co_upper_direct[M](xs: Co[? <: M]): Co[M] = xs |
| 31 | + def co_upper_in_co[M](xs: Co[Co[? <: M]]): Co[Co[M]] = xs |
| 32 | + def co_upper_in_seq[M](xs: scala.collection.immutable.Seq[Co[? <: M]]) |
| 33 | + : scala.collection.immutable.Seq[Co[M]] = xs |
| 34 | + def co_upper_via_tparam[A, M <: A](xs: Co[? <: M]): Co[M] = xs |
| 35 | + |
| 36 | + // ---- 2. Contravariant + lower-bounded wildcard widens to its lo ---- |
| 37 | + // |
| 38 | + // Dual to (1). For `Contra[-T]`, `paramBounds(tparam) = [Nothing, Any]`, |
| 39 | + // so `paramBounds(tparam).lo = Nothing`. OLD asked `M <: Nothing` |
| 40 | + // (false). THE FIX asks `M <: Nothing | M = M` (true). |
| 41 | + |
| 42 | + def contra_lower_direct[M](xs: Contra[? >: M]): Contra[M] = xs |
| 43 | + def contra_lower_in_co[M](xs: Co[Contra[? >: M]]): Co[Contra[M]] = xs |
| 44 | + |
| 45 | + // ---- 3. The headline regression -------------------------------------- |
| 46 | + // |
| 47 | + // The wildcard hi is reached through a covariant container; this is |
| 48 | + // the shape that drove the akka report and the pure-Scala minimization |
| 49 | + // in tests/pos/i16018.scala. |
| 50 | + |
| 51 | + trait Holder |
| 52 | + def headline(xs: Co[? <: Holder]): Co[Holder] = xs |
| 53 | + |
| 54 | + // ---- 4. Soundness guard, covariant (intersection in action) ---------- |
| 55 | + // |
| 56 | + // For `CoBounded[+T <: Holder]`, `paramBounds(tparam).hi = Holder`. |
| 57 | + // We feed a wildcard `? <: Any` whose hi (`Any`) is *wider* than the |
| 58 | + // declared bound. The three candidates diverge: |
| 59 | + // |
| 60 | + // OLD : isSubType( Holder, Holder ) = true (would pass) |
| 61 | + // NAIVE : isSubType( Any, Holder ) = false (would fail) |
| 62 | + // THE FIX : isSubType( Any & Holder = Holder, Holder ) = true |
| 63 | + // |
| 64 | + // The case must pass: a value of `CoBounded[? <: Any]` is, by the |
| 65 | + // parameter's own constraint, a value of `CoBounded[X]` for some |
| 66 | + // `X <: Holder`, hence (by covariance) of `CoBounded[Holder]`. |
| 67 | + // This is what guards `& paramBounds(tparam).hi` from being deleted. |
| 68 | + |
| 69 | + class CoBounded[+T <: Holder] |
| 70 | + def soundness_guard_co(xs: CoBounded[? <: Any]): CoBounded[Holder] = xs |
| 71 | + |
| 72 | + // ---- 5. Soundness guard, contravariant (union in action) ------------- |
| 73 | + // |
| 74 | + // Dual to (4). For `ContraBounded[-T >: Holder]`, |
| 75 | + // `paramBounds(tparam).lo = Holder`. We feed a wildcard `? >: Nothing` |
| 76 | + // whose lo (`Nothing`) is *lower* than the declared bound. The three |
| 77 | + // candidates diverge: |
| 78 | + // |
| 79 | + // OLD : isSubType( Holder, Holder ) = true (would pass) |
| 80 | + // NAIVE : isSubType( Holder, Nothing ) = false (would fail) |
| 81 | + // THE FIX : isSubType( Holder, Nothing | Holder = Holder ) = true |
| 82 | + // |
| 83 | + // This is what guards `| paramBounds(tparam).lo` from being deleted. |
| 84 | + |
| 85 | + class ContraBounded[-T >: Holder] |
| 86 | + def soundness_guard_contra(xs: ContraBounded[? >: Nothing]): ContraBounded[Holder] = xs |
0 commit comments