Skip to content

Commit 19b9a27

Browse files
committed
Fix: handle accesses to private aliases from inlined code
Dealias them and try again.
1 parent 18c807e commit 19b9a27

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import ErrorReporting.errorTree
2626
import collection.mutable
2727
import transform.TypeUtils._
2828
import reporting.trace
29+
import util.Positions.Position
2930

3031
object Inliner {
3132
import tpd._
@@ -91,26 +92,26 @@ object Inliner {
9192
def addAccessor(tree: Tree, refPart: Tree, targs: List[Tree], argss: List[List[Tree]],
9293
accessedType: Type, rhs: (Tree, List[Type], List[List[Tree]]) => Tree)(implicit ctx: Context): Tree = {
9394
val qual = qualifier(refPart)
94-
95+
9596
def refIsLocal = qual match {
9697
case qual: This => qual.symbol == refPart.symbol.owner
9798
case _ => false
9899
}
99-
100+
100101
def addAcc(accessorDef: MemberDef) = {
101102
accessors += accessorDef
102103
inlining.println(i"added inline accessor: $accessorDef")
103104
}
104-
105+
105106
if (refPart.symbol.isStatic || refIsLocal) {
106107
// Easy case: Reference to a static symbol or a symbol referenced via `this.`
107108
val accessorType = accessedType.ensureMethodic
108109
val accessor = accessorSymbol(tree, accessorType).asTerm
109-
110+
110111
addAcc(
111112
polyDefDef(accessor, tps => argss =>
112113
rhs(refPart, tps, argss).withPos(tree.pos.focus)))
113-
114+
114115
ref(accessor).appliedToTypeTrees(targs).appliedToArgss(argss).withPos(tree.pos)
115116
}
116117
else {
@@ -150,7 +151,7 @@ object Inliner {
150151
polyDefDef(accessor, tps => argss =>
151152
rhs(argss.head.head.select(refPart.symbol), tps.drop(localRefs.length), argss.tail)
152153
.withPos(tree.pos.focus)))
153-
154+
154155
ref(accessor)
155156
.appliedToTypeTrees(localRefs.map(TypeTree(_)) ++ targs)
156157
.appliedToArgss((qual :: Nil) :: argss)
@@ -547,6 +548,18 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
547548

548549
var retainedInlineables = Set[Symbol]()
549550

551+
override def ensureAccessible(tpe: Type, superAccess: Boolean, pos: Position)(implicit ctx: Context): Type = {
552+
tpe match {
553+
case tpe @ TypeRef(pre, _) if !tpe.symbol.isAccessibleFrom(pre, superAccess) =>
554+
tpe.info match {
555+
case TypeAlias(alias) => return ensureAccessible(alias, superAccess, pos)
556+
case _ =>
557+
}
558+
case _ =>
559+
}
560+
super.ensureAccessible(tpe, superAccess, pos)
561+
}
562+
550563
override def typedIdent(tree: untpd.Ident, pt: Type)(implicit ctx: Context) =
551564
tree.asInstanceOf[tpd.Tree] match {
552565
case InlineableArg(rhs) => inlining.println(i"inline arg $tree -> $rhs"); rhs

tests/pos/inlineAccesses.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
trait T {
2+
object O
3+
}
4+
5+
class C {
6+
private type T = C
7+
private var x = 0
8+
9+
inline def f = {
10+
x += 1
11+
x = x + 1
12+
x
13+
}
14+
inline def dup = new T
15+
}
16+
17+
object Test {
18+
19+
val c = new C
20+
c.f
21+
c.dup
22+
}

0 commit comments

Comments
 (0)