|
| 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 — OLD rejects, THE FIX accepts). |
| 17 | +// Sections 4–5 distinguish a hypothetical NAIVE refactor (drop the |
| 18 | +// intersection / union with `paramBounds(tparam)`) from THE FIX — they |
| 19 | +// pin the *declared-bound guard*, which is a completeness property |
| 20 | +// (NAIVE produces *false negatives* on `CoBounded[? <: Any] <:< CoBounded[Holder]` |
| 21 | +// while THE FIX accepts), not a soundness property. |
| 22 | + |
| 23 | +object Test: |
| 24 | + |
| 25 | + class Co[+T] |
| 26 | + class Contra[-T] |
| 27 | + |
| 28 | + // ---- 1. Covariant + upper-bounded wildcard widens to its hi -------- |
| 29 | + // |
| 30 | + // For `Co[+T]`, `paramBounds(tparam) = [Nothing, Any]`. OLD asked |
| 31 | + // `Any <: M` (false). THE FIX asks `M & Any <: M` (true). |
| 32 | + |
| 33 | + def co_upper_direct[M](xs: Co[? <: M]): Co[M] = xs |
| 34 | + def co_upper_in_co[M](xs: Co[Co[? <: M]]): Co[Co[M]] = xs |
| 35 | + def co_upper_in_seq[M](xs: scala.collection.immutable.Seq[Co[? <: M]]) |
| 36 | + : scala.collection.immutable.Seq[Co[M]] = xs |
| 37 | + def co_upper_via_tparam[A, M <: A](xs: Co[? <: M]): Co[M] = xs |
| 38 | + |
| 39 | + // ---- 2. Contravariant + lower-bounded wildcard widens to its lo ---- |
| 40 | + // |
| 41 | + // Dual to (1). For `Contra[-T]`, `paramBounds(tparam) = [Nothing, Any]`, |
| 42 | + // so `paramBounds(tparam).lo = Nothing`. OLD asked `M <: Nothing` |
| 43 | + // (false). THE FIX asks `M <: Nothing | M = M` (true). |
| 44 | + |
| 45 | + def contra_lower_direct[M](xs: Contra[? >: M]): Contra[M] = xs |
| 46 | + def contra_lower_in_co[M](xs: Co[Contra[? >: M]]): Co[Contra[M]] = xs |
| 47 | + |
| 48 | + // ---- 3. The headline regression -------------------------------------- |
| 49 | + // |
| 50 | + // The wildcard hi is reached through a covariant container; this is |
| 51 | + // the shape that drove the akka report and the pure-Scala minimization |
| 52 | + // in tests/pos/i16018.scala. |
| 53 | + |
| 54 | + trait Holder |
| 55 | + def headline(xs: Co[? <: Holder]): Co[Holder] = xs |
| 56 | + |
| 57 | + // ---- 4. Declared-bound guard, covariant (intersection in action) ----- |
| 58 | + // |
| 59 | + // For `CoBounded[+T <: Holder]`, `paramBounds(tparam).hi = Holder`. We |
| 60 | + // feed a wildcard `? <: Any` whose hi (`Any`) is *wider* than the |
| 61 | + // declared bound. The three candidates diverge: |
| 62 | + // |
| 63 | + // OLD : isSubType( Holder, Holder ) = true (would pass) |
| 64 | + // NAIVE : isSubType( Any, Holder ) = false (would fail) |
| 65 | + // THE FIX : isSubType( Any & Holder = Holder, Holder ) = true |
| 66 | + // |
| 67 | + // The case must pass: a value of `CoBounded[? <: Any]` is, by the |
| 68 | + // parameter's own constraint, a value of `CoBounded[X]` for some |
| 69 | + // `X <: Holder`, hence (by covariance) of `CoBounded[Holder]`. |
| 70 | + // The intersection `& paramBounds(tparam).hi` is what saves THE FIX |
| 71 | + // from inheriting NAIVE's *false negative* here. (Note: vs OLD this is |
| 72 | + // a completeness improvement, since OLD also accepts this case; vs |
| 73 | + // NAIVE this distinguishes THE FIX as the only one that does.) |
| 74 | + |
| 75 | + class CoBounded[+T <: Holder] |
| 76 | + def declared_bound_guard_co(xs: CoBounded[? <: Any]): CoBounded[Holder] = xs |
| 77 | + |
| 78 | + // ---- 5. Declared-bound guard, contravariant (union in action) -------- |
| 79 | + // |
| 80 | + // Dual to (4). For `ContraBounded[-T >: Holder]`, |
| 81 | + // `paramBounds(tparam).lo = Holder`. We feed a wildcard `? >: Nothing` |
| 82 | + // whose lo (`Nothing`) is *lower* than the declared bound. The three |
| 83 | + // candidates diverge: |
| 84 | + // |
| 85 | + // OLD : isSubType( Holder, Holder ) = true (would pass) |
| 86 | + // NAIVE : isSubType( Holder, Nothing ) = false (would fail) |
| 87 | + // THE FIX : isSubType( Holder, Nothing | Holder = Holder ) = true |
| 88 | + // |
| 89 | + // The union `| paramBounds(tparam).lo` is what saves THE FIX from |
| 90 | + // NAIVE's *false negative*. Same completeness caveat as (4). |
| 91 | + |
| 92 | + class ContraBounded[-T >: Holder] |
| 93 | + def declared_bound_guard_contra(xs: ContraBounded[? >: Nothing]): ContraBounded[Holder] = xs |
0 commit comments