Skip to content

Commit

Permalink
Do not remove inline method implementations until PruneErasedDefs
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed May 4, 2023
1 parent 8a055d6 commit 5c9c61c
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 19 deletions.
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/staging/CrossStageSafety.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ class CrossStageSafety extends TreeMapWithStages {
if level != 0 then cpy.Apply(tree)(cpy.TypeApply(tree.fun)(fun, transformedBody :: Nil), quotes :: Nil)
else tpd.Quote(transformedBody).select(nme.apply).appliedTo(quotes).withSpan(tree.span)

case _: DefDef if tree.symbol.isInlineMethod =>
tree

case _ if !inQuoteOrSpliceScope =>
checkAnnotations(tree) // Check quotes in annotations
super.transform(tree)
Expand Down Expand Up @@ -117,8 +120,6 @@ class CrossStageSafety extends TreeMapWithStages {
// propagate healed types
tree1.withType(tree1.tpt.tpe.appliedTo(tree1.args.map(_.tpe)))
case tree1 => tree1
case tree: DefDef if tree.symbol.is(Inline) && level > 0 =>
EmptyTree // Remove inline defs in quoted code. Already fully inlined.
case tree: ValOrDefDef =>
checkAnnotations(tree)
healInfo(tree, tree.tpt.srcPos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import NameKinds.OuterSelectName
import StdNames._
import TypeUtils.isErasedValueType
import config.Feature
import inlines.Inlines.inInlineMethod

object FirstTransform {
val name: String = "firstTransform"
Expand Down
4 changes: 1 addition & 3 deletions compiler/src/dotty/tools/dotc/transform/PickleQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ class PickleQuotes extends MacroTransform {
val pickled = PickleQuotes.pickle(quote1, quotes, contents)
transform(pickled) // pickle quotes that are in the contents
case tree: DefDef if !tree.rhs.isEmpty && tree.symbol.isInlineMethod =>
// Shrink size of the tree. The methods have already been inlined.
// TODO move to FirstTransform to trigger even without quotes
cpy.DefDef(tree)(rhs = defaultValue(tree.rhs.tpe))
tree
case _ =>
super.transform(tree)
}
Expand Down
30 changes: 16 additions & 14 deletions compiler/src/dotty/tools/dotc/transform/Staging.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.Types._
import dotty.tools.dotc.inlines.Inlines
import dotty.tools.dotc.util.SrcPos
import dotty.tools.dotc.transform.SymUtils._
import dotty.tools.dotc.staging.StagingLevel.*
Expand Down Expand Up @@ -57,21 +58,22 @@ class Staging extends MacroTransform {
case _ =>
}

tree match {
case tree: RefTree =>
assert(level != 0 || tree.symbol != defn.QuotedTypeModule_of,
"scala.quoted.Type.of at level 0 should have been replaced with Quote AST in staging phase")
case _ =>
}
if !Inlines.inInlineMethod then
tree match {
case tree: RefTree =>
assert(level != 0 || tree.symbol != defn.QuotedTypeModule_of,
"scala.quoted.Type.of at level 0 should have been replaced with Quote AST in staging phase")
case _ =>
}

tree.tpe match {
case tpe @ TypeRef(prefix, _) if tpe.typeSymbol.isTypeSplice =>
// Type splices must have a know term ref, usually to an implicit argument
// This is mostly intended to catch `quoted.Type[T]#splice` types which should just be `T`
assert(prefix.isInstanceOf[TermRef] || prefix.isInstanceOf[ThisType], prefix)
case _ =>
// OK
}
tree.tpe match {
case tpe @ TypeRef(prefix, _) if tpe.typeSymbol.isTypeSplice =>
// Type splices must have a know term ref, usually to an implicit argument
// This is mostly intended to catch `quoted.Type[T]#splice` types which should just be `T`
assert(prefix.isInstanceOf[TermRef] || prefix.isInstanceOf[ThisType], prefix)
case _ =>
// OK
}
}

override def run(using Context): Unit =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class BootstrappedOnlyCompilationTests {
aggregateTests(
compileFilesInDir("tests/neg-macros", defaultOptions.and("-Xcheck-macros")),
compileFile("tests/pos-macros/i9570.scala", defaultOptions.and("-Xfatal-warnings")),
compileFile("tests/pos-macros/macro-deprecation.scala", defaultOptions.and("-Xfatal-warnings", "-deprecation")),
compileFile("tests/pos-macros/macro-experimental.scala", defaultOptions.and("-Yno-experimental")),
).checkExpectedErrors()
}

Expand Down
4 changes: 4 additions & 0 deletions tests/pos-macros/macro-deprecation.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import scala.quoted.*

inline def f = ${ impl } // error
@deprecated def impl(using Quotes) = '{1}
5 changes: 5 additions & 0 deletions tests/pos-macros/macro-experimental.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import scala.quoted.*
import scala.annotation.experimental

inline def f = ${ impl } // error
@experimental def impl(using Quotes) = '{1}

0 comments on commit 5c9c61c

Please sign in to comment.