File tree Expand file tree Collapse file tree 4 files changed +29
-3
lines changed
compiler/src/dotty/tools/dotc/typer Expand file tree Collapse file tree 4 files changed +29
-3
lines changed Original file line number Diff line number Diff line change @@ -1341,7 +1341,7 @@ trait Applications extends Compatibility {
1341
1341
// Constraining only fails if the pattern cannot possibly match,
1342
1342
// but useless pattern checks detect more such cases, so we simply rely on them instead.
1343
1343
withMode(Mode .GadtConstraintInference )(TypeComparer .constrainPatternType(unapplyArgType, selType))
1344
- val patternBound = maximizeType(unapplyArgType, tree .span)
1344
+ val patternBound = maximizeType(unapplyArgType, unapplyFn .span.endPos )
1345
1345
if (patternBound.nonEmpty) unapplyFn = addBinders(unapplyFn, patternBound)
1346
1346
unapp.println(i " case 2 $unapplyArgType ${ctx.typerState.constraint}" )
1347
1347
unapplyArgType
Original file line number Diff line number Diff line change @@ -405,8 +405,8 @@ object Inferencing {
405
405
val patternBindings = new mutable.ListBuffer [(Symbol , TypeParamRef )]
406
406
vs foreachBinding { (tvar, v) =>
407
407
if ! tvar.isInstantiated then
408
- if (v == 1 ) tvar.instantiate(fromBelow = false )
409
- else if (v == - 1 ) tvar.instantiate(fromBelow = true )
408
+ if (v == 1 && tvar.hasUpperBound ) tvar.instantiate(fromBelow = false )
409
+ else if (v == - 1 && tvar.hasLowerBound ) tvar.instantiate(fromBelow = true )
410
410
else {
411
411
val bounds = TypeComparer .fullBounds(tvar.origin)
412
412
if bounds.hi <:< bounds.lo || bounds.hi.classSymbol.is(Final ) then
Original file line number Diff line number Diff line change
1
+ sealed trait Tree [+ A ]
2
+ final case class Leaf [+ B ](v : B ) extends Tree [B ]
3
+ final case class Node [+ C ](xs : List [C ]) extends Tree [List [C ]]
4
+
5
+ object Test :
6
+ def meth [X ](tree : Tree [X ]): X = tree match
7
+ case Leaf (v) => v
8
+ case Node (x) => List (" boom" ) // error
9
+
10
+ def main (args : Array [String ]): Unit =
11
+ val tree = Node (List (42 ))
12
+ val res = meth(tree)
13
+ assert(res.head == 42 ) // was: ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer
Original file line number Diff line number Diff line change
1
+ sealed trait Tree [+ A ]
2
+ final case class Leaf [+ B ](v : B ) extends Tree [B ]
3
+ final case class Node [+ C ](xs : List [C ]) extends Tree [List [C ]]
4
+
5
+ object Test :
6
+ def meth [X ](tree : Tree [X ]): X = tree match
7
+ case Leaf (v) => v
8
+ case Node (x) => x // ok
9
+
10
+ def main (args : Array [String ]): Unit =
11
+ val tree = Node (List (42 ))
12
+ val res = meth(tree)
13
+ assert(res.head == 42 ) // ok
You can’t perform that action at this time.
0 commit comments