Skip to content

Commit cc83b2a

Browse files
Improve checks in phases
Instead of checking if we are inside of an inline method, some code snippets need to check if we are inside of an inline trait as well
1 parent 6eaa573 commit cc83b2a

File tree

6 files changed

+15
-8
lines changed

6 files changed

+15
-8
lines changed

compiler/src/dotty/tools/dotc/inlines/Inlines.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ object Inlines:
5454
def inInlineMethod(using Context): Boolean =
5555
ctx.owner.ownersIterator.exists(_.isInlineMethod)
5656

57+
def inInlineContext(using Context): Boolean =
58+
ctx.owner.ownersIterator.exists(sym => sym.isInlineMethod || sym.isInlineTrait)
59+
5760
/** Can a call to method `meth` be inlined? */
5861
def isInlineable(meth: Symbol)(using Context): Boolean =
5962
meth.isInlineMethod && meth.hasAnnotation(defn.BodyAnnot) && !inInlineMethod
@@ -82,7 +85,7 @@ object Inlines:
8285
case _ =>
8386
isInlineable(tree.symbol) && !tree.tpe.widenTermRefExpr.isInstanceOf[MethodOrPoly] && isInlineableInCtx
8487

85-
private def symbolFromParent(parent: Tree)(using Context): Symbol =
88+
private[dotc] def symbolFromParent(parent: Tree)(using Context): Symbol =
8689
if parent.symbol.isConstructor then parent.symbol.owner else parent.symbol
8790

8891
private def inlineTraitAncestors(cls: TypeDef)(using Context): List[Tree] = cls match {

compiler/src/dotty/tools/dotc/transform/Inlining.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Inlining extends MacroTransform, SymTransformer {
4747
new TreeTraverser {
4848
def traverse(tree: Tree)(using Context): Unit =
4949
tree match
50-
case tree: RefTree if !Inlines.inInlineMethod && StagingLevel.level == 0 =>
50+
case tree: RefTree if !Inlines.inInlineContext && StagingLevel.level == 0 =>
5151
assert(!tree.symbol.isInlineMethod, tree.show)
5252
case _ =>
5353
traverseChildren(tree)

compiler/src/dotty/tools/dotc/transform/PickleQuotes.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ class PickleQuotes extends MacroTransform {
8484
override def checkPostCondition(tree: Tree)(using Context): Unit =
8585
tree match
8686
case tree: Quote =>
87-
assert(Inlines.inInlineMethod)
87+
assert(Inlines.inInlineContext)
8888
case tree: Splice =>
89-
assert(Inlines.inInlineMethod)
90-
case _ : TypeDef if !Inlines.inInlineMethod =>
89+
assert(Inlines.inInlineContext)
90+
case _ : TypeDef if !Inlines.inInlineContext =>
9191
assert(!tree.symbol.hasAnnotation(defn.QuotedRuntime_SplicedTypeAnnot),
9292
s"${tree.symbol} should have been removed by PickledQuotes because it has a @quoteTypeTag")
9393
case _ =>

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
389389
processValOrDefDef(superAcc.wrapDefDef(tree1)(super.transform(tree1).asInstanceOf[DefDef]))
390390
case tree: TypeDef =>
391391
if tree.symbol.isInlineTrait then
392-
ctx.compilationUnit.needsInlining = true
392+
ctx.compilationUnit.needsInlining = true // Transform inner classes to traits
393393
registerIfHasMacroAnnotations(tree)
394394
val sym = tree.symbol
395395
if (sym.isClass)
@@ -401,6 +401,8 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
401401
tree.rhs match
402402
case impl: Template =>
403403
for parent <- impl.parents do
404+
if Inlines.symbolFromParent(parent).isInlineTrait then
405+
ctx.compilationUnit.needsInlining = true
404406
Checking.checkTraitInheritance(parent.tpe.classSymbol, sym.asClass, parent.srcPos)
405407
// Constructor parameters are in scope when typing a parent.
406408
// While they can safely appear in a parent tree, to preserve

compiler/src/dotty/tools/dotc/transform/Splicing.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ class Splicing extends MacroTransform:
8989
case tree: Quote =>
9090
val body1 = QuoteTransformer().transform(tree.body)(using quoteContext)
9191
cpy.Quote(tree)(body = body1)
92-
case tree: DefDef if tree.symbol.is(Inline) =>
93-
// Quotes in inlined methods are only pickled after they are inlined.
92+
case _: DefDef | _: TypeDef if tree.symbol.is(Inline) =>
93+
// Quotes in inlined methods and traits are only pickled after they are inlined.
9494
tree
9595
case _ =>
9696
super.transform(tree)

tests/pos/inline-trait-1-simple-trait.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ inline trait A:
1010
def f[T](x: T): Int = 2
1111

1212
private def g = 1
13+
protected[this] def p = 123
14+
private[this] def pp = 123456
1315

1416
def xx: X = "foo".asInstanceOf[X]
1517
end A

0 commit comments

Comments
 (0)