Skip to content

Commit 2ba6289

Browse files
committed
Tweak: Tweak rechecking of returns
1 parent 3964599 commit 2ba6289

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

compiler/src/dotty/tools/dotc/transform/Recheck.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,16 @@ abstract class Recheck extends Phase, SymTransformer:
300300

301301
val rawType = recheck(tree.expr)
302302
val ownType = avoidMap(rawType)
303-
checkConforms(ownType, tree.from.symbol.returnProto, tree)
303+
// The pattern matching translation, which runs before this phase
304+
// sometimes instantiates return types with singleton type alternatives
305+
// but the returned expression is widened. We compensate by widening the expected
306+
// type as well.
307+
def widened(tp: Type): Type = tp match
308+
case tp: SingletonType => tp.widen
309+
case tp: AndOrType => tp.derivedAndOrType(widened(tp.tp1), widened(tp.tp2))
310+
case tp @ AnnotatedType(tp1, ann) => tp.derivedAnnotatedType(widened(tp1), ann)
311+
case _ => tp
312+
checkConforms(ownType, widened(tree.from.symbol.returnProto), tree)
304313
defn.NothingType
305314
end recheckReturn
306315

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class T
2+
class A extends T
3+
class B extends T
4+
5+
def test(tp: T) =
6+
val mapping: Map[A, String] = ???
7+
8+
tp match
9+
case a: A => mapping(a) match
10+
case s: String => B()
11+
case null => a

0 commit comments

Comments
 (0)