Skip to content

Commit b593958

Browse files
committed
Better explanation for adaptHkVariances
1 parent dba4b94 commit b593958

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/dotty/tools/dotc/core/TypeApplications.scala

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -353,29 +353,35 @@ class TypeApplications(val self: Type) extends AnyVal {
353353
}
354354

355355
/** If argument A and type parameter P are higher-kinded, adapt the variances
356-
* of A to those of P, ensuring that the variances of the type lambda A
357-
* agree with the variances of corresponding higherkinded type parameters of P. Example:
356+
* of A to those of P, ensuring that the variances of the type lambda A
357+
* agree with the variances of corresponding higher-kinded type parameters of P. Example:
358358
*
359-
* class Companion[+CC[X]]
360-
* Companion[List]
359+
* class GenericCompanion[+CC[X]]
360+
* GenericCompanion[List]
361361
*
362-
* with adaptArgs, this will expand to
362+
* with adaptHkVariances, the argument `List` will expand to
363363
*
364-
* Companion[[X] => List[X]]
364+
* [X] => List[X]
365365
*
366366
* instead of
367367
*
368-
* Companion[[+X] => List[X]]
368+
* [+X] => List[X]
369369
*
370370
* even though `List` is covariant. This adaptation is necessary to ignore conflicting
371-
* variances in overriding members that have types of hk-type parameters such as `Companion[GenTraversable]`
372-
* or `Companion[ListBuffer]`. Without the adaptation we would end up with
371+
* variances in overriding members that have types of hk-type parameters such as
372+
* `GenericCompanion[GenTraversable]` or `GenericCompanion[ListBuffer]`.
373+
* When checking overriding, we need to validate the subtype relationship
373374
*
374-
* Companion[[+X] => GenTraversable[X]]
375-
* Companion[[X] => List[X]]
375+
* GenericCompanion[[X] -> ListBuffer[X]] <: GenericCompanion[[+X] -> GenTraversable[X]]
376376
*
377-
* and the second is not a subtype of the first. So if we have overridding memebrs of the two
378-
* types we get an error.
377+
* Without adaptation, this would be false, and hence an overriding error would
378+
* result. But with adaptation, the rhs argument will be adapted to
379+
*
380+
* [X] -> GenTraversable[X]
381+
*
382+
* which makes the subtype test succeed. The crucial point here is that, since
383+
* GenericCompanion only expects a non-variant CC, the fact that GenTraversable
384+
* is covariant is irrelevant, so can be ignored.
379385
*/
380386
def adaptHkVariances(bound: Type)(implicit ctx: Context): Type = {
381387
val boundLambda = bound.LambdaTrait

0 commit comments

Comments
 (0)