Skip to content

Commit fa62c04

Browse files
committed
Merge pull request scala#3331 from retronym/ticket/8120
SI-8120 Avoid tree sharing when typechecking patmat anon functions
2 parents ea4a765 + 5b9966d commit fa62c04

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4240,7 +4240,11 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
42404240
}
42414241
val ids = for (p <- params) yield Ident(p.name)
42424242
val selector1 = atPos(tree.pos.focusStart) { if (arity == 1) ids.head else gen.mkTuple(ids) }
4243-
val body = treeCopy.Match(tree, selector1, cases)
4243+
// SI-8120 If we don't duplicate the cases, the original Match node will share trees with ones that
4244+
// receive symbols owned by this function. However if, after a silent mode session, we discard
4245+
// this Function and try a different approach (e.g. applying a view to the reciever) we end up
4246+
// with orphaned symbols which blows up far down the pipeline (or can be detected with -Ycheck:typer).
4247+
val body = treeCopy.Match(tree, selector1, (cases map duplicateAndKeepPositions).asInstanceOf[List[CaseDef]])
42444248
typed1(atPos(tree.pos) { Function(params, body) }, mode, pt)
42454249
}
42464250
} else

test/files/pos/t8120.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object A {
2+
class C {
3+
def m(a: Nothing): Int = 0
4+
}
5+
implicit class RichAny(a: Any) {
6+
def m(a: Any): Int = 0
7+
}
8+
(new C).m({ case (x, y) => x } : Any => Any)
9+
}

0 commit comments

Comments
 (0)