Skip to content

Commit 30bfbd5

Browse files
authored
Merge pull request #2127 from dotty-staging/fix/variable-pattern-access
Fix desugaring of variable pattern leaking into API
2 parents 1fe7304 + 6135363 commit 30bfbd5

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ object desugar {
557557
* val/var/lazy val p = e ==> val/var/lazy val x_1 = (e: @unchecked) match (case p => (x_1))
558558
*
559559
* in case there are zero or more than one variables in pattern
560-
* val/var/lazy p = e ==> private synthetic [lazy] val t$ = (e: @unchecked) match (case p => (x_1, ..., x_N))
560+
* val/var/lazy p = e ==> private[this] synthetic [lazy] val t$ = (e: @unchecked) match (case p => (x_1, ..., x_N))
561561
* val/var/def x_1 = t$._1
562562
* ...
563563
* val/var/def x_N = t$._N
@@ -586,7 +586,8 @@ object desugar {
586586
derivedValDef(original, named, tpt, matchExpr, mods)
587587
case _ =>
588588
val tmpName = ctx.freshName().toTermName
589-
val patMods = mods & (AccessFlags | Lazy) | Synthetic
589+
val patMods =
590+
mods & Lazy | Synthetic | (if (ctx.owner.isClass) PrivateLocal else EmptyFlags)
590591
val firstDef =
591592
ValDef(tmpName, TypeTree(), matchExpr)
592593
.withPos(pat.pos.union(rhs.pos)).withMods(patMods)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Fields of A:
2+
private final int A.a$$local
3+
private final int A.b$$local
4+
private final scala.Tuple2 A.$1$
5+
# Methods of A:
6+
public int A.a()
7+
public int A.b()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class A {
2+
val (a, b) = (1, 2)
3+
}
4+
object Test {
5+
def printFields(cls: Class[_]) =
6+
println(cls.getDeclaredFields.map(_.toString).sorted.deep.mkString("\n"))
7+
def printMethods(cls: Class[_]) =
8+
println(cls.getDeclaredMethods.map(_.toString).sorted.deep.mkString("\n"))
9+
10+
def main(args: Array[String]): Unit = {
11+
println("# Fields of A:")
12+
printFields(classOf[A])
13+
println("# Methods of A:")
14+
printMethods(classOf[A])
15+
}
16+
}

0 commit comments

Comments
 (0)