diff --git a/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala b/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala index d2bcabb378b0..d2b8e383f197 100644 --- a/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -308,12 +308,12 @@ object PatternMatcher { if (scrutinee.info.isNotNull || nonNull(scrutinee)) unappPlan else TestPlan(NonNullTest, scrutinee, tree.pos, unappPlan, onFailure) case Bind(name, body) => - val body1 = patternPlan(scrutinee, body, onSuccess, onFailure) - if (name == nme.WILDCARD) body1 + if (name == nme.WILDCARD) patternPlan(scrutinee, body, onSuccess, onFailure) else { + // The type of `name` may refer to val in `body`, therefore should come after `body` val bound = tree.symbol.asTerm initializer(bound) = ref(scrutinee) - LetPlan(bound, body1) + patternPlan(scrutinee, body, LetPlan(bound, onSuccess), onFailure) } case Alternative(alts) => labelAbstract(onSuccess) { ons => diff --git a/tests/run/3179.scala b/tests/run/3179.scala new file mode 100644 index 000000000000..79c06c8fb1cc --- /dev/null +++ b/tests/run/3179.scala @@ -0,0 +1,8 @@ +object Test { + def main(args: Array[String]): Unit = { + ("": Any) match { + case a @ Test => 1 + case _ => 2 + } + } +}