Skip to content

Commit 8716a38

Browse files
committed
Address some review comments
1 parent 52d0786 commit 8716a38

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,7 +2045,7 @@ object Types {
20452045
else {
20462046
if (isType) {
20472047
val res =
2048-
if (currentSymbol.is(ClassTypeParam)) argForParam(prefix, currentSymbol.paramVariance)
2048+
if (currentSymbol.is(ClassTypeParam)) argForParam(prefix, symbol.paramVariance)
20492049
else prefix.lookupRefined(name)
20502050
if (res.exists) return res
20512051
if (Config.splitProjections)
@@ -4471,17 +4471,19 @@ object Types {
44714471
* If the expansion is a wildcard parameter reference, convert its
44724472
* underlying bounds to a range, otherwise return the expansion.
44734473
*/
4474-
def expandParam(tp: NamedType, pre: Type, variance: Int): Type =
4475-
tp.argForParam(pre, variance) match {
4474+
def expandParam(tp: NamedType, pre: Type): Type = {
4475+
def expandBounds(tp: TypeBounds) =
4476+
range(atVariance(-variance)(reapply(tp.lo)), reapply(tp.hi))
4477+
tp.argForParam(pre, tp.symbol.paramVariance) match {
44764478
case arg @ TypeRef(pre, _) if pre.isArgPrefixOf(arg.symbol) =>
44774479
arg.info match {
4478-
case TypeBounds(lo, hi) => range(atVariance(-variance)(reapply(lo)), reapply(hi))
4479-
case arg => reapply(arg)
4480+
case argInfo: TypeBounds => expandBounds(argInfo)
4481+
case argInfo => reapply(arg)
44804482
}
4481-
case TypeBounds(lo, hi) =>
4482-
range(lo, hi)
4483+
case arg: TypeBounds => expandBounds(arg)
44834484
case arg => reapply(arg)
44844485
}
4486+
}
44854487

44864488
/** Derived selection.
44874489
* @pre the (upper bound of) prefix `pre` has a member named `tp.name`.
@@ -4491,7 +4493,7 @@ object Types {
44914493
else pre match {
44924494
case Range(preLo, preHi) =>
44934495
val forwarded =
4494-
if (tp.symbol.is(ClassTypeParam)) expandParam(tp, preHi, tp.symbol.paramVariance)
4496+
if (tp.symbol.is(ClassTypeParam)) expandParam(tp, preHi)
44954497
else tryWiden(tp, preHi)
44964498
forwarded.orElse(
44974499
range(super.derivedSelect(tp, preLo), super.derivedSelect(tp, preHi)))

tests/neg/i5202.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,27 @@ object Test {
33

44
f.apply(5) // error - found: Int expected: Int & String
55
f("c") // error - found: String expected: Int & String
6+
}
7+
8+
class Foo[A] {
9+
def foo(a: A): Unit = {}
10+
}
11+
class Co[+A] {
12+
def foo(a: A): Unit = {} // error: contravariant occurs in covariant position
13+
def bar: A = ???
14+
}
15+
class Contra[-A] {
16+
def foo(a: A): Unit = {}
17+
}
18+
19+
object Test2 {
20+
def main(args: Array[String]): Unit = {
21+
val x: Foo[Int] | Foo[String] = new Foo[Int]
22+
x.foo("") // error, found: String, required: Int & String
23+
val y: Contra[Int] | Contra[String] = new Contra[Int]
24+
y.foo("") // error, found: String, required: Int & String
25+
val z: Co[Int] | Co[String] = new Co[Int]
26+
z.foo("") // OK
27+
val s: String = z.bar // error: found Int | String, required: String
28+
}
629
}

0 commit comments

Comments
 (0)