diff --git a/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala b/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala index 6930539ec990..d799be0adc49 100644 --- a/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -32,14 +32,20 @@ class PatternMatcher extends MiniPhase { override def transformMatch(tree: Match)(implicit ctx: Context): Tree = if (tree.isInstanceOf[InlineMatch]) tree else { - val translated = new Translator(tree.tpe, this).translateMatch(tree) + // Widen termrefs with underlying `=> T` types. Otherwise ElimByName will produce + // inconsistent types. See i7743.scala. + // Question: Does this need to be done more systematically, not just for pattern matches? + val matchType = tree.tpe.widenSingleton match + case ExprType(rt) => rt + case rt => tree.tpe + val translated = new Translator(matchType, this).translateMatch(tree) // check exhaustivity and unreachability val engine = new patmat.SpaceEngine engine.checkExhaustivity(tree) engine.checkRedundancy(tree) - translated.ensureConforms(tree.tpe) + translated.ensureConforms(matchType) } } diff --git a/tests/pos/i7743.scala b/tests/pos/i7743.scala new file mode 100644 index 000000000000..088f892aa206 --- /dev/null +++ b/tests/pos/i7743.scala @@ -0,0 +1,3 @@ +def foo(x: => String) = 1 match { + case _ => x +} \ No newline at end of file