Skip to content

Commit 9735108

Browse files
committed
SI-9123 More coherent trees with patmat, dependent types
The pattern matcher needs to substitute references to bound variables with references to either a) synthetic temporary vals, or to b) selections. The latter occurs under -optimize to avoid to be frugal with local variable slots. For instance: ``` def test(s: Some[String]) = s match { case Some(elem) => elem.length } ``` Is translated to: ``` def test(s: Some[String]): Int = { case <synthetic> val x1: Some[String] = s; case4(){ if (x1.ne(null)) matchEnd3(x1.x.length()) else case5() }; case5(){ matchEnd3(throw new MatchError(x1)) }; matchEnd3(x: Int){ x } } ``` However, for a long time this translation failed to consider references to the binder in types. scala#4122 tried to address this by either using standard substitution facilities where available (references to temp vals), and by expanding the patmat's home grown substitution to handle the more complex case of referencing a selection. However, this left the tree in an incoherent state; while it patched up the `.tpe` field of `Tree`s, it failed to modify the info of `Symbol`-s. This led to a crash in the later uncurry phase under `-Ydelambdafy:method`. This commit modifies the info of such symbols to get rid of stray refeferences to the pattern binder symbols.
1 parent 7b5998a commit 9735108

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

src/compiler/scala/tools/nsc/transform/patmat/PatternMatching.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ trait Interface extends ast.TreeDSL {
239239
case Ident(_) => subst(from, to)
240240
case _ => super.transform(tree)
241241
}
242+
tree1 match {
243+
case _: DefTree =>
244+
tree1.symbol.modifyInfo(_.substituteTypes(from, toTypes))
245+
case _ =>
246+
}
242247
tree1.modifyType(_.substituteTypes(from, toTypes))
243248
}
244249
}

test/files/pos/t9123.flags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-optimize -Ydelambdafy:method

test/files/pos/t9123.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
trait Setting {
2+
type T
3+
def value: T
4+
}
5+
6+
object Test {
7+
def test(x: Some[Setting]) = x match {
8+
case Some(dep) => Some(dep.value) map (_ => true)
9+
}
10+
}

0 commit comments

Comments
 (0)