From 1e4bdac3ebb5a575d3c6d954c9fd7f4bbe7c5c3f Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 16 Dec 2019 10:55:49 +0100 Subject: [PATCH] Fix #7743: Widen by-name parameter termrefs in pattern matching translation --- .../dotty/tools/dotc/transform/PatternMatcher.scala | 10 ++++++++-- tests/pos/i7743.scala | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 tests/pos/i7743.scala 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