From faa5ae6152b5515fb9980d93b30bd780649ee7fe Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Sat, 30 Jan 2016 15:55:43 +1000 Subject: SI-9349 Fix use of patmat binder as prefix for new x.Inner When substituting in references to the synthetic values representing pattern binders, we were replacing: Select(Ident(o).setType(o.type), TypeName("Inner")) with: Select(Ident(x2).setType(typeOf[Outer]), TypeName("Inner")) The post transform in uncurry would then run: else if (tree.isType) TypeTree(tree.tpe) setPos tree.pos Which would loses track of the outer term `o` and crashes the compiler in ExplicitOuter. This commit generates stable references to the binders. I made this change in the substitutions for all `TreeMakers`, however only one of seems like it triggers a crash in the test variations I tried. Here's how the trees for the first pattern in the test case change after this patch: ``` @@ -1,30 +1,30 @@ [[syntax trees at end of patmat]] // test.scala package {.type} { object Test extends scala.AnyRef { def (): Test.type = { Test.super{Test.type}.{()Object}(){Object}; (){Unit} }{Unit}; def main(args: Array[String]): Unit = { val o1: Outer = Outer.apply{(i: Int)Outer}(5{Int(5)}){Outer}; { case val x1: Outer = o1{Outer}; case5(){ if (x1.ne{(x$1: AnyRef)Boolean}(null{Null(null)}){Boolean}) matchEnd4{(x: Unit)Unit}({ - val i: Outer#Inner = new x1.Inner{Outer#Inner}{()Outer#Inner}(){Outer#Inner}; + val i: x1.Inner = new x1.Inner{x1.Inner}{()x1.Inner}(){x1.Inner}; (){Unit} }{Unit}){Unit} else case6{()Unit}(){Unit}{Unit} }{Unit}; case6(){ matchEnd4{(x: Unit)Unit}(throw new MatchError{MatchError}{(obj: Any)MatchError}(x1{Outer}){MatchError}{Nothing}){Unit} }{Unit}; matchEnd4(x: Unit){ x{Unit} }{Unit} }{Unit} }{Unit} } ``` --- .../nsc/transform/patmat/MatchTreeMaking.scala | 6 +++--- test/files/run/t9349/data.scala | 1 + test/files/run/t9349/test.scala | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 test/files/run/t9349/data.scala create mode 100644 test/files/run/t9349/test.scala diff --git a/src/compiler/scala/tools/nsc/transform/patmat/MatchTreeMaking.scala b/src/compiler/scala/tools/nsc/transform/patmat/MatchTreeMaking.scala index 18a63a6e53..c6e7f8fcda 100644 --- a/src/compiler/scala/tools/nsc/transform/patmat/MatchTreeMaking.scala +++ b/src/compiler/scala/tools/nsc/transform/patmat/MatchTreeMaking.scala @@ -101,7 +101,7 @@ trait MatchTreeMaking extends MatchCodeGen with Debugging { case class SubstOnlyTreeMaker(prevBinder: Symbol, nextBinder: Symbol) extends TreeMaker { val pos = NoPosition - val localSubstitution = Substitution(prevBinder, CODE.REF(nextBinder)) + val localSubstitution = Substitution(prevBinder, gen.mkAttributedStableRef(nextBinder)) def chainBefore(next: Tree)(casegen: Casegen): Tree = substitution(next) override def toString = "S"+ localSubstitution } @@ -118,7 +118,7 @@ trait MatchTreeMaking extends MatchCodeGen with Debugging { val res: Tree lazy val nextBinder = freshSym(pos, nextBinderTp) - lazy val localSubstitution = Substitution(List(prevBinder), List(CODE.REF(nextBinder))) + lazy val localSubstitution = Substitution(List(prevBinder), List(gen.mkAttributedStableRef(nextBinder))) def chainBefore(next: Tree)(casegen: Casegen): Tree = atPos(pos)(casegen.flatMapCond(cond, res, nextBinder, substitution(next))) @@ -485,7 +485,7 @@ trait MatchTreeMaking extends MatchCodeGen with Debugging { // NOTE: generate `patTree == patBinder`, since the extractor must be in control of the equals method (also, patBinder may be null) // equals need not be well-behaved, so don't intersect with pattern's (stabilized) type (unlike MaybeBoundTyped's accumType, where it's required) val cond = codegen._equals(patTree, prevBinder) - val res = CODE.REF(prevBinder) + val res = gen.mkAttributedStableRef(prevBinder) override def toString = "ET"+((prevBinder.name, patTree)) } diff --git a/test/files/run/t9349/data.scala b/test/files/run/t9349/data.scala new file mode 100644 index 0000000000..f88a6cfaeb --- /dev/null +++ b/test/files/run/t9349/data.scala @@ -0,0 +1 @@ +case class Outer(i: Int) { class Inner } diff --git a/test/files/run/t9349/test.scala b/test/files/run/t9349/test.scala new file mode 100644 index 0000000000..ebce4e77dd --- /dev/null +++ b/test/files/run/t9349/test.scala @@ -0,0 +1,21 @@ +object Test { + def main(args: Array[String]): Unit = { + val o1 = Outer(5) + o1 match { + case o @ Outer(_) => + val i = new o.Inner + } + o1 match { + case o : Outer => + val i = new o.Inner + + } + object Extractor { + def unapply(a: Any): Option[Outer] = Some(o1) + } + null match { + case Extractor(o2) => + val i = new o2.Inner + } + } +} -- cgit v1.2.3