Skip to content

Commit 76a8ccb

Browse files
Merge pull request #4464 from dotty-staging/fix-inlines
Various fixes related to inlining
2 parents 08c90e9 + 717a301 commit 76a8ccb

File tree

8 files changed

+883
-19
lines changed

8 files changed

+883
-19
lines changed

compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala

+1
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ class TreePickler(pickler: TastyPickler) {
607607
if (flags is DefaultParameterized) writeByte(DEFAULTparameterized)
608608
if (flags is Stable) writeByte(STABLE)
609609
if ((flags is ParamAccessor) && sym.isSetter) writeByte(PARAMsetter)
610+
if ((flags is Label)) writeByte(LABEL)
610611
} else {
611612
if (flags is Sealed) writeByte(SEALED)
612613
if (flags is Abstract) writeByte(ABSTRACT)

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ class TreeUnpickler(reader: TastyReader,
707707

708708
def readRhs(implicit ctx: Context) =
709709
if (noRhs(end)) EmptyTree
710-
else readLater(end, rdr => ctx => rdr.readTerm()(ctx))
710+
else readLater(end, rdr => ctx => rdr.readTerm()(ctx.retractMode(Mode.InSuperCall)))
711711

712712
def ValDef(tpt: Tree) =
713713
ta.assignType(untpd.ValDef(sym.name.asTermName, tpt, readRhs(localCtx)), sym)
@@ -1135,7 +1135,7 @@ class TreeUnpickler(reader: TastyReader,
11351135
def readLater[T <: AnyRef](end: Addr, op: TreeReader => Context => T)(implicit ctx: Context): Trees.Lazy[T] = {
11361136
val localReader = fork
11371137
goto(end)
1138-
new LazyReader(localReader, ctx.owner, op)
1138+
new LazyReader(localReader, ctx.owner, ctx.mode, op)
11391139
}
11401140

11411141
def readHole(end: Addr, isType: Boolean)(implicit ctx: Context): Tree = {
@@ -1182,10 +1182,10 @@ class TreeUnpickler(reader: TastyReader,
11821182
}
11831183
}
11841184

1185-
class LazyReader[T <: AnyRef](reader: TreeReader, owner: Symbol, op: TreeReader => Context => T) extends Trees.Lazy[T] {
1185+
class LazyReader[T <: AnyRef](reader: TreeReader, owner: Symbol, mode: Mode, op: TreeReader => Context => T) extends Trees.Lazy[T] {
11861186
def complete(implicit ctx: Context): T = {
11871187
pickling.println(i"starting to read at ${reader.reader.currentAddr} with owner $owner")
1188-
op(reader)(ctx.withPhaseNoLater(ctx.picklerPhase).withOwner(owner))
1188+
op(reader)(ctx.withPhaseNoLater(ctx.picklerPhase).withOwner(owner).withModeBits(mode))
11891189
}
11901190
}
11911191

compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
4949

5050
override protected def templateText(tree: TypeDef, impl: Template): Text = {
5151
val decl =
52-
if (!tree.mods.is(Module)) modText(tree.mods, keywordStr(if ((tree).mods is Trait) "trait" else "class"))
53-
else modText(tree.mods &~ (Final | Module), keywordStr("object"))
52+
if (!tree.mods.is(Module)) modText(tree.mods, tree.symbol, keywordStr(if ((tree).mods is Trait) "trait" else "class"))
53+
else modText(tree.mods, tree.symbol, keywordStr("object"), suppress = Final | Module)
5454
decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~ ""
5555
}
5656

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

+15-13
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
411411
case tree @ TypeDef(name, rhs) =>
412412
def typeDefText(tparamsText: => Text, rhsText: => Text) =
413413
dclTextOr(tree) {
414-
modText(tree.mods, keywordStr("type")) ~~ (varianceText(tree.mods) ~ typeText(nameIdText(tree))) ~
414+
modText(tree.mods, tree.symbol, keywordStr("type")) ~~ (varianceText(tree.mods) ~ typeText(nameIdText(tree))) ~
415415
withEnclosingDef(tree) { tparamsText ~ rhsText }
416416
}
417417
def recur(rhs: Tree, tparamsTxt: => Text): Text = rhs match {
@@ -449,7 +449,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
449449
toText(t)
450450
case tree @ ModuleDef(name, impl) =>
451451
withEnclosingDef(tree) {
452-
modText(tree.mods, keywordStr("object")) ~~ nameIdText(tree) ~ toTextTemplate(impl)
452+
modText(tree.mods, NoSymbol, keywordStr("object")) ~~ nameIdText(tree) ~ toTextTemplate(impl)
453453
}
454454
case SymbolLit(str) =>
455455
"'" + str
@@ -506,7 +506,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
506506
t ~ cxBoundToText(cxb)
507507
}
508508
case PatDef(mods, pats, tpt, rhs) =>
509-
modText(mods, keywordStr("val")) ~~ toText(pats, ", ") ~ optAscription(tpt) ~
509+
modText(mods, NoSymbol, keywordStr("val")) ~~ toText(pats, ", ") ~ optAscription(tpt) ~
510510
optText(rhs)(" = " ~ _)
511511
case ParsedTry(expr, handler, finalizer) =>
512512
changePrec(GlobalPrec) {
@@ -618,7 +618,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
618618
protected def valDefToText[T >: Untyped](tree: ValDef[T]): Text = {
619619
import untpd.{modsDeco => _, _}
620620
dclTextOr(tree) {
621-
modText(tree.mods, keywordStr(if (tree.mods is Mutable) "var" else "val")) ~~
621+
modText(tree.mods, tree.symbol, keywordStr(if (tree.mods is Mutable) "var" else "val")) ~~
622622
valDefText(nameIdText(tree)) ~ optAscription(tree.tpt) ~
623623
withEnclosingDef(tree) { optText(tree.rhs)(" = " ~ _) }
624624
}
@@ -627,7 +627,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
627627
protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
628628
import untpd.{modsDeco => _, _}
629629
dclTextOr(tree) {
630-
val prefix = modText(tree.mods, keywordStr("def")) ~~ valDefText(nameIdText(tree))
630+
val prefix = modText(tree.mods, tree.symbol, keywordStr("def")) ~~ valDefText(nameIdText(tree))
631631
withEnclosingDef(tree) {
632632
addVparamssText(prefix ~ tparamsText(tree.tparams), tree.vparamss) ~ optAscription(tree.tpt) ~
633633
optText(tree.rhs)(" = " ~ _)
@@ -642,7 +642,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
642642
val prefix: Text =
643643
if (vparamss.isEmpty || primaryConstrs.nonEmpty) tparamsTxt
644644
else {
645-
var modsText = modText(constr.mods, "")
645+
var modsText = modText(constr.mods, constr.symbol, "")
646646
if (!modsText.isEmpty) modsText = " " ~ modsText
647647
if (constr.mods.hasAnnotations && !constr.mods.hasFlags) modsText = modsText ~~ " this"
648648
withEnclosingDef(constr) { addVparamssText(tparamsTxt ~~ modsText, vparamss) }
@@ -670,7 +670,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
670670
}
671671

672672
protected def templateText(tree: TypeDef, impl: Template): Text = {
673-
val decl = modText(tree.mods, keywordStr(if ((tree).mods is Trait) "trait" else "class"))
673+
val decl = modText(tree.mods, tree.symbol, keywordStr(if ((tree).mods is Trait) "trait" else "class"))
674674
decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~
675675
(if (tree.hasType && ctx.settings.verbose.value) i"[decls = ${tree.symbol.info.decls}]" else "")
676676
}
@@ -693,16 +693,18 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
693693

694694
protected def annotText(tree: untpd.Tree): Text = "@" ~ constrText(tree) // DD
695695

696-
protected def modText(mods: untpd.Modifiers, kw: String): Text = { // DD
696+
protected def modText(mods: untpd.Modifiers, sym: Symbol, kw: String, suppress: FlagSet = EmptyFlags): Text = { // DD
697697
val suppressKw = if (enclDefIsClass) mods is ParamAndLocal else mods is Param
698698
var flagMask =
699699
if (ctx.settings.YdebugFlags.value) AnyFlags
700-
else if (suppressKw) PrintableFlags &~ Private
701-
else PrintableFlags
700+
else if (suppressKw) PrintableFlags &~ Private &~ suppress
701+
else PrintableFlags &~ suppress
702702
if (homogenizedView && mods.flags.isTypeFlags) flagMask &~= Implicit // drop implicit from classes
703-
val flags = mods.flags & flagMask
704-
val flagsText = if (flags.isEmpty) "" else keywordStr((mods.flags & flagMask).toString)
705-
val annotations = filterModTextAnnots(mods.annotations)
703+
val flags = (if (sym.exists) sym.flags else (mods.flags)) & flagMask
704+
val flagsText = if (flags.isEmpty) "" else keywordStr(flags.toString)
705+
val annotations = filterModTextAnnots(
706+
if (sym.exists) sym.annotations.filterNot(_.isInstanceOf[Annotations.BodyAnnotation]).map(_.tree)
707+
else mods.annotations)
706708
Text(annotations.map(annotText), " ") ~~ flagsText ~~ (Str(kw) provided !suppressKw)
707709
}
708710

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

+14
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._
@@ -53,6 +54,7 @@ object Inliner {
5354
* by excluding all symbols properly contained in the inlined method.
5455
*/
5556
def needsAccessor(sym: Symbol)(implicit ctx: Context) =
57+
sym.isTerm &&
5658
(sym.is(AccessFlags) || sym.privateWithin.exists) &&
5759
!sym.owner.isContainedIn(inlineMethod)
5860

@@ -537,6 +539,18 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
537539

538540
var retainedInlineables = Set[Symbol]()
539541

542+
override def ensureAccessible(tpe: Type, superAccess: Boolean, pos: Position)(implicit ctx: Context): Type = {
543+
tpe match {
544+
case tpe @ TypeRef(pre, _) if !tpe.symbol.isAccessibleFrom(pre, superAccess) =>
545+
tpe.info match {
546+
case TypeAlias(alias) => return ensureAccessible(alias, superAccess, pos)
547+
case _ =>
548+
}
549+
case _ =>
550+
}
551+
super.ensureAccessible(tpe, superAccess, pos)
552+
}
553+
540554
override def typedIdent(tree: untpd.Ident, pt: Type)(implicit ctx: Context) =
541555
tree.asInstanceOf[tpd.Tree] match {
542556
case InlineableArg(rhs) => inlining.println(i"inline arg $tree -> $rhs"); rhs

tests/pos/inlineAccesses.scala

+22
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)