Skip to content

Commit 1e4bdac

Browse files
committed
Fix #7743: Widen by-name parameter termrefs in pattern matching translation
1 parent 9aa7991 commit 1e4bdac

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,20 @@ class PatternMatcher extends MiniPhase {
3232
override def transformMatch(tree: Match)(implicit ctx: Context): Tree =
3333
if (tree.isInstanceOf[InlineMatch]) tree
3434
else {
35-
val translated = new Translator(tree.tpe, this).translateMatch(tree)
35+
// Widen termrefs with underlying `=> T` types. Otherwise ElimByName will produce
36+
// inconsistent types. See i7743.scala.
37+
// Question: Does this need to be done more systematically, not just for pattern matches?
38+
val matchType = tree.tpe.widenSingleton match
39+
case ExprType(rt) => rt
40+
case rt => tree.tpe
41+
val translated = new Translator(matchType, this).translateMatch(tree)
3642

3743
// check exhaustivity and unreachability
3844
val engine = new patmat.SpaceEngine
3945
engine.checkExhaustivity(tree)
4046
engine.checkRedundancy(tree)
4147

42-
translated.ensureConforms(tree.tpe)
48+
translated.ensureConforms(matchType)
4349
}
4450
}
4551

tests/pos/i7743.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def foo(x: => String) = 1 match {
2+
case _ => x
3+
}

0 commit comments

Comments
 (0)