Skip to content

Commit aced916

Browse files
committed
Fix comment in isAsSpecificValue type and add test
Fix comment in isAsSpecificValue to match the code, drop a probably spurious case from the code which referred to the old encoding of applied types in terms of refinements, and add a test that exercises some of the behavior.
1 parent bbcd512 commit aced916

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,15 +1142,15 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
11421142
*
11431143
* flip(T) <: flip(U)
11441144
*
1145-
* where `flip` changes top-level contravariant type aliases to covariant ones.
1146-
* Intuitively `<:s` means subtyping `<:`, except that all top-level arguments
1145+
* where `flip` changes covariant occurrences of contravariant type parameters to
1146+
* covariant ones. Intuitively `<:s` means subtyping `<:`, except that all arguments
11471147
* to contravariant parameters are compared as if they were covariant. E.g. given class
11481148
*
11491149
* class Cmp[-X]
11501150
*
1151-
* `Cmp[T] <:s Cmp[U]` if `T <: U`. On the other hand, nested occurrences
1152-
* of parameters are not affected.
1153-
* So `T <: U` would imply `List[Cmp[U]] <:s List[Cmp[T]]`, as usual.
1151+
* `Cmp[T] <:s Cmp[U]` if `T <: U`. On the other hand, non-variant occurrences
1152+
* of parameters are not affected. So `T <: U` would imply `Set[Cmp[U]] <:s Set[Cmp[T]]`,
1153+
* as usual, because `Set` is non-variant.
11541154
*
11551155
* This relation might seem strange, but it models closely what happens for methods.
11561156
* Indeed, if we integrate the existing rules for methods into `<:s` we have now that
@@ -1167,7 +1167,6 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
11671167
else {
11681168
val flip = new TypeMap {
11691169
def apply(t: Type) = t match {
1170-
case t: TypeBounds => t
11711170
case t @ AppliedType(tycon, args) =>
11721171
def mapArg(arg: Type, tparam: TypeParamInfo) =
11731172
if (variance > 0 && tparam.paramVariance < 0) defn.FunctionOf(arg :: Nil, defn.UnitType)

tests/run/contrarivant.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object Test extends App {
2+
3+
implicit def f: String => String = x => "f"
4+
5+
implicit def g: Object => String = x => "g"
6+
7+
def h(implicit x: String => String) = x("")
8+
9+
implicit def fs: List[String => String] = List(f)
10+
11+
implicit def gs: List[Object => String] = List(g)
12+
13+
def hs(implicit xs: List[String => String]) = xs.head("")
14+
15+
assert(h == "f")
16+
assert(hs == "f")
17+
}

0 commit comments

Comments
 (0)