@@ -450,8 +450,7 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
450
450
451
451
// The tree map to apply to the inlined tree. This maps references to this-types
452
452
// and parameters to references of their arguments or their proxies.
453
- def treeMap (tree : Tree ) = {
454
- tree match {
453
+ def treeMap (tree : Tree ) = tree match {
455
454
case _ : This =>
456
455
tree.tpe match {
457
456
case thistpe : ThisType =>
@@ -468,7 +467,7 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
468
467
case None => tree
469
468
}
470
469
case _ => tree
471
- }}
470
+ }
472
471
473
472
val inlineCtx = inlineContext(call)
474
473
// The complete translation maps references to `this` and parameters to
@@ -519,9 +518,10 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
519
518
* 5. Make sure that the tree's typing is idempotent (so that future -Ycheck passes succeed)
520
519
*/
521
520
private object InlineTyper extends ReTyper {
522
-
523
521
var retainedClosures = Set [Symbol ]()
524
522
523
+ private val bindings = bindingsBuf.map(_.symbol)
524
+
525
525
override def typedIdent (tree : untpd.Ident , pt : Type )(implicit ctx : Context ) = {
526
526
val tree1 = super .typedIdent(tree, pt)
527
527
tree1 match {
@@ -559,5 +559,18 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
559
559
case _ =>
560
560
super .typedApply(tree, pt)
561
561
}
562
+
563
+ /** If expected type contans a reference rooted in a this-proxy,
564
+ * the tree's type might still be a subtype of the same reference, but
565
+ * rooted in the original `this`. In this case we insert a cast to ensure
566
+ * the inlined code is type correct.
567
+ */
568
+ override def adaptInterpolated (tree : Tree , pt : Type )(implicit ctx : Context ) = {
569
+ def refersToProxy (tr : NamedType ) = tr.isTerm && bindings.contains(tr.symbol)
570
+ val casted =
571
+ if (tree.isType || pt.namedPartsWith(refersToProxy).isEmpty) tree
572
+ else tree.ensureConforms(pt)
573
+ super .adaptInterpolated(casted, pt)
574
+ }
562
575
}
563
576
}
0 commit comments