Skip to content

Commit

Permalink
Rename annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Nov 28, 2022
1 parent b4105f2 commit 0cc6b48
Show file tree
Hide file tree
Showing 33 changed files with 96 additions and 96 deletions.
10 changes: 5 additions & 5 deletions compiler/src/dotty/tools/dotc/CompilationUnit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CompilationUnit protected (val source: SourceFile) {
*/
var needsInlining: Boolean = false

var hasMacroAnnotations: Boolean = false
var hasTastyAnnotations: Boolean = false

/** Set to `true` if inliner added anonymous mirrors that need to be completed */
var needsMirrorSupport: Boolean = false
Expand Down Expand Up @@ -121,7 +121,7 @@ object CompilationUnit {
force.traverse(unit1.tpdTree)
unit1.needsStaging = force.containsQuote
unit1.needsInlining = force.containsInline
unit1.hasMacroAnnotations = force.containsMacroAnnotation
unit1.hasTastyAnnotations = force.containsTastyAnnotation
}
unit1
}
Expand Down Expand Up @@ -150,7 +150,7 @@ object CompilationUnit {
var containsQuote = false
var containsInline = false
var containsCaptureChecking = false
var containsMacroAnnotation = false
var containsTastyAnnotation = false
def traverse(tree: Tree)(using Context): Unit = {
if (tree.symbol.isQuote)
containsQuote = true
Expand All @@ -165,8 +165,8 @@ object CompilationUnit {
case _ =>
case _ =>
for annot <- tree.symbol.annotations do
if annot.tree.symbol.denot != NoDenotation && annot.tree.symbol.owner.derivesFrom(defn.MacroAnnotationClass) then
ctx.compilationUnit.hasMacroAnnotations = true
if annot.tree.symbol.denot != NoDenotation && annot.tree.symbol.owner.derivesFrom(defn.TastyAnnotationClass) then
ctx.compilationUnit.hasTastyAnnotations = true
traverseChildren(tree)
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ class Definitions {
@tu lazy val QuotedTypeModule: Symbol = QuotedTypeClass.companionModule
@tu lazy val QuotedTypeModule_of: Symbol = QuotedTypeModule.requiredMethod("of")

@tu lazy val MacroAnnotationClass: ClassSymbol = requiredClass("scala.annotation.MacroAnnotation")
@tu lazy val TastyAnnotationClass: ClassSymbol = requiredClass("scala.annotation.TastyAnnotation")

@tu lazy val CanEqualClass: ClassSymbol = getClassIfDefined("scala.Eql").orElse(requiredClass("scala.CanEqual")).asClass
def CanEqual_canEqualAny(using Context): TermSymbol =
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/Inlining.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Inlining extends MacroTransform with IdentityDenotTransformer {
override def changesMembers: Boolean = true

override def run(using Context): Unit =
if ctx.compilationUnit.needsInlining || ctx.compilationUnit.hasMacroAnnotations then
if ctx.compilationUnit.needsInlining || ctx.compilationUnit.hasTastyAnnotations then
try super.run
catch case _: CompilationUnit.SuspendException => ()

Expand Down Expand Up @@ -69,9 +69,9 @@ class Inlining extends MacroTransform with IdentityDenotTransformer {
else if
!tree.symbol.isPrimaryConstructor
&& StagingContext.level == 0
&& MacroAnnotations.hasMacro(tree.symbol)
&& TastyAnnotations.hasMacro(tree.symbol)
then
val trees = new MacroAnnotations(Inlining.this).transform(tree)
val trees = new TastyAnnotations(Inlining.this).transform(tree)
flatTree(trees.map(super.transform))
else super.transform(tree)
case _: Typed | _: Block =>
Expand Down
28 changes: 14 additions & 14 deletions compiler/src/dotty/tools/dotc/transform/MacroAnnotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import scala.quoted.runtime.impl.{QuotesImpl, SpliceScope}

import scala.quoted.Quotes

class MacroAnnotations(thisPhase: DenotTransformer):
class TastyAnnotations(thisPhase: DenotTransformer):
import tpd.*
import MacroAnnotations.*
import TastyAnnotations.*

/** Expands every macro annotation that is on this tree.
* Returns a list with transformed definition and any added definitions.
Expand Down Expand Up @@ -50,7 +50,7 @@ class MacroAnnotations(thisPhase: DenotTransformer):

// Apply all macro annotation to `tree` and collect new definitions in order
val transformedTree: DefTree = tree.symbol.annotations.foldLeft(tree) { (tree, annot) =>
if isMacroAnnotation(annot) then
if isTastyAnnotation(annot) then
debug.println(i"Expanding macro annotation: ${annot}")

// Interpret call to `new myAnnot(..).transform(using <Quotes>)(<tree>)`
Expand Down Expand Up @@ -81,16 +81,16 @@ class MacroAnnotations(thisPhase: DenotTransformer):

/** Interpret the code `new annot(..).transform(using <Quotes(ctx)>)(<tree>)` */
private def callMacro(interpreter: Interpreter, tree: MemberDef, annot: Annotation)(using Context): List[MemberDef] =
// TODO: Remove when scala.annaotaion.MacroAnnotation is no longer experimental
// TODO: Remove when scala.annaotaion.TastyAnnotation is no longer experimental
import scala.reflect.Selectable.reflectiveSelectable
type MacroAnnotation = {
type TastyAnnotation = {
def transform(using Quotes)(tree: Object/*Erased type of quotes.refelct.Definition*/): List[MemberDef /*quotes.refelct.Definition known to be MemberDef in QuotesImpl*/]
}

// Interpret macro annotation instantiation `new myAnnot(..)`
val annotInstance = interpreter.interpret[MacroAnnotation](annot.tree).get
// TODO: Remove when scala.annaotaion.MacroAnnotation is no longer experimental
assert(annotInstance.getClass.getClassLoader.loadClass("scala.annotation.MacroAnnotation").isInstance(annotInstance))
val annotInstance = interpreter.interpret[TastyAnnotation](annot.tree).get
// TODO: Remove when scala.annaotaion.TastyAnnotation is no longer experimental
assert(annotInstance.getClass.getClassLoader.loadClass("scala.annotation.TastyAnnotation").isInstance(annotInstance))

val quotes = QuotesImpl()(using SpliceScope.contextWithNewSpliceScope(tree.symbol.sourcePos)(using MacroExpansion.context(tree)).withOwner(tree.symbol))
annotInstance.transform(using quotes)(tree.asInstanceOf[quotes.reflect.Definition])
Expand All @@ -107,13 +107,13 @@ class MacroAnnotations(thisPhase: DenotTransformer):
else
sym.enteredAfter(thisPhase)

object MacroAnnotations:
object TastyAnnotations:

/** Is this an annotation that implements `scala.annation.MacroAnnotation` */
def isMacroAnnotation(annot: Annotation)(using Context): Boolean =
/** Is this an annotation that implements `scala.annation.TastyAnnotation` */
def isTastyAnnotation(annot: Annotation)(using Context): Boolean =
val sym = annot.tree.symbol
sym.denot != NoDenotation && sym.owner.derivesFrom(defn.MacroAnnotationClass)
sym.denot != NoDenotation && sym.owner.derivesFrom(defn.TastyAnnotationClass)

/** Is this symbol annotated with an annotation that implements `scala.annation.MacroAnnotation` */
/** Is this symbol annotated with an annotation that implements `scala.annation.TastyAnnotation` */
def hasMacro(sym: Symbol)(using Context): Boolean =
sym.getAnnotation(defn.MacroAnnotationClass).isDefined
sym.getAnnotation(defn.TastyAnnotationClass).isDefined
14 changes: 7 additions & 7 deletions compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
if (sym.isClass)
VarianceChecker.check(tree)
annotateExperimental(sym)
checkMacroAnnotation(sym)
checkTastyAnnotation(sym)
tree.rhs match
case impl: Template =>
for parent <- impl.parents do
Expand Down Expand Up @@ -488,13 +488,13 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
if (sym.isEffectivelyErased) dropInlines.transform(rhs) else rhs

private def checkForMacrosAnnotations(tree: Tree)(using Context) =
if !ctx.compilationUnit.hasMacroAnnotations then
ctx.compilationUnit.hasMacroAnnotations |=
tree.symbol.annotations.exists(MacroAnnotations.isMacroAnnotation)
if !ctx.compilationUnit.hasTastyAnnotations then
ctx.compilationUnit.hasTastyAnnotations |=
tree.symbol.annotations.exists(TastyAnnotations.isTastyAnnotation)

private def checkMacroAnnotation(sym: Symbol)(using Context) =
if sym.derivesFrom(defn.MacroAnnotationClass) && !sym.isStatic then
report.error("Implementation restriction: classes that extend MacroAnnotation must not be inner/local classes", sym.srcPos)
private def checkTastyAnnotation(sym: Symbol)(using Context) =
if sym.derivesFrom(defn.TastyAnnotationClass) && !sym.isStatic then
report.error("Implementation restriction: classes that extend TastyAnnotation must not be inner/local classes", sym.srcPos)

private def checkErasedDef(tree: ValOrDefDef)(using Context): Unit =
if tree.symbol.is(Erased, butNot = Macro) then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class YCheckPositions extends Phase {

private def isMacro(call: Tree)(using Context) =
call.symbol.is(Macro) ||
(call.symbol.isClass && call.tpe.derivesFrom(defn.MacroAnnotationClass)) ||
(call.symbol.isClass && call.tpe.derivesFrom(defn.TastyAnnotationClass)) ||
// The call of a macro after typer is encoded as a Select while other inlines are Ident
// TODO remove this distinction once Inline nodes of expanded macros can be trusted (also in Inliner.inlineCallTrace)
(!(ctx.phase <= postTyperPhase) && call.isInstanceOf[Select])
Expand Down
4 changes: 2 additions & 2 deletions library/src/scala/annotation/MacroAnnotation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scala.quoted._

/** Base trait for macro annotation that will transform a definition */
@experimental
trait MacroAnnotation extends StaticAnnotation:
trait TastyAnnotation extends StaticAnnotation:

/** Transform the `tree` definition and add other definitions
*
Expand All @@ -27,7 +27,7 @@ trait MacroAnnotation extends StaticAnnotation:
* import scala.quoted.*
* import scala.collection.mutable
*
* class memoize extends MacroAnnotation:
* class memoize extends TastyAnnotation:
* def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
* import quotes.reflect._
* tree match
Expand Down
6 changes: 0 additions & 6 deletions tests/neg-macros/annot-MacroAnnotation-direct.check

This file was deleted.

4 changes: 0 additions & 4 deletions tests/neg-macros/annot-MacroAnnotation-direct.scala

This file was deleted.

6 changes: 6 additions & 0 deletions tests/neg-macros/annot-TastyAnnotation-direct.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- [E042] Type Error: tests/neg-macros/annot-TastyAnnotation-direct.scala:3:0 ------------------------------------------
3 |@TastyAnnotation // error
|^^^^^^^^^^^^^^^^
|TastyAnnotation is a trait; it cannot be instantiated
|
| longer explanation available when compiling with `-explain`
4 changes: 4 additions & 0 deletions tests/neg-macros/annot-TastyAnnotation-direct.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import scala.annotation.TastyAnnotation

@TastyAnnotation // error
def test = ()
4 changes: 2 additions & 2 deletions tests/neg-macros/annot-accessIndirect/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import scala.annotation.{experimental, MacroAnnotation}
import scala.annotation.{experimental, TastyAnnotation}
import scala.quoted._

@experimental
class hello extends MacroAnnotation {
class hello extends TastyAnnotation {
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
import quotes.reflect._
val helloSymbol = Symbol.newUniqueVal(tree.symbol.owner, "hello", TypeRepr.of[String], Flags.EmptyFlags, Symbol.noSymbol)
Expand Down
4 changes: 2 additions & 2 deletions tests/neg-macros/annot-accessIndirect/Macro_2.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import scala.annotation.{experimental, MacroAnnotation}
import scala.annotation.{experimental, TastyAnnotation}
import scala.quoted._

@experimental
class foo extends MacroAnnotation {
class foo extends TastyAnnotation {
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
import quotes.reflect._
val s = '{@hello def foo1(x: Int): Int = x + 1;()}.asTerm
Expand Down
4 changes: 2 additions & 2 deletions tests/neg-macros/annot-empty-result/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import scala.annotation.{experimental, MacroAnnotation}
import scala.annotation.{experimental, TastyAnnotation}
import scala.quoted._

@experimental
class nilAnnot extends MacroAnnotation {
class nilAnnot extends TastyAnnotation {
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
Nil
}
4 changes: 2 additions & 2 deletions tests/neg-macros/annot-error-annot/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import scala.annotation.{experimental, MacroAnnotation}
import scala.annotation.{experimental, TastyAnnotation}
import scala.quoted._

@experimental
class error extends MacroAnnotation {
class error extends TastyAnnotation {
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
quotes.reflect.report.error("MACRO ERROR", tree.pos)
List(tree)
Expand Down
22 changes: 11 additions & 11 deletions tests/neg-macros/annot-nested.scala
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import scala.annotation.{experimental, MacroAnnotation}
import scala.annotation.{experimental, TastyAnnotation}
import scala.quoted._

class Foo:
@experimental
class void extends MacroAnnotation: // error
class void extends TastyAnnotation: // error
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] = List(tree)

object Bar:
@experimental
class void extends MacroAnnotation: // error
class void extends TastyAnnotation: // error
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] = List(tree)

class Foo2:
@experimental
trait void extends MacroAnnotation // error
trait void extends TastyAnnotation // error

object Bar:
@experimental
trait void extends MacroAnnotation // error
trait void extends TastyAnnotation // error

def test: Unit =
@experimental
class void extends MacroAnnotation: // error
class void extends TastyAnnotation: // error
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] = List(tree)

trait void2 extends MacroAnnotation // error
trait void2 extends TastyAnnotation // error

new MacroAnnotation {} // error
new TastyAnnotation {} // error

()

val test2: Unit =
@experimental
class void extends MacroAnnotation: // error
class void extends TastyAnnotation: // error
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] = List(tree)

trait void2 extends MacroAnnotation // error
trait void2 extends TastyAnnotation // error

new MacroAnnotation {} // error
new TastyAnnotation {} // error

()
4 changes: 2 additions & 2 deletions tests/neg-macros/annot-on-class/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import scala.annotation.{experimental, MacroAnnotation}
import scala.annotation.{experimental, TastyAnnotation}
import scala.quoted._

@experimental
class voidAnnot extends MacroAnnotation {
class voidAnnot extends TastyAnnotation {
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
List(tree)
}
4 changes: 2 additions & 2 deletions tests/neg-macros/annot-on-object/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import scala.annotation.{experimental, MacroAnnotation}
import scala.annotation.{experimental, TastyAnnotation}
import scala.quoted._

@experimental
class voidAnnot extends MacroAnnotation {
class voidAnnot extends TastyAnnotation {
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
List(tree)
}
4 changes: 2 additions & 2 deletions tests/neg-macros/annot-on-type/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import scala.annotation.{experimental, MacroAnnotation}
import scala.annotation.{experimental, TastyAnnotation}
import scala.quoted._

@experimental
class voidAnnot extends MacroAnnotation {
class voidAnnot extends TastyAnnotation {
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
List(tree)
}
4 changes: 2 additions & 2 deletions tests/neg-macros/annot-result-owner/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import scala.annotation.{experimental, MacroAnnotation}
import scala.annotation.{experimental, TastyAnnotation}
import scala.quoted._

@experimental
class insertVal extends MacroAnnotation:
class insertVal extends TastyAnnotation:
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
import quotes.reflect._
// Use of wrong owner
Expand Down
6 changes: 3 additions & 3 deletions tests/pos-macros/annot-in-object/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import scala.annotation.{experimental, MacroAnnotation}
import scala.annotation.{experimental, TastyAnnotation}
import scala.quoted._

object Foo:
@experimental
class void extends MacroAnnotation:
class void extends TastyAnnotation:
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] = List(tree)

object Bar:
@experimental
class void extends MacroAnnotation:
class void extends TastyAnnotation:
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] = List(tree)
4 changes: 2 additions & 2 deletions tests/pos-macros/annot-suspend/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import scala.annotation.{experimental, MacroAnnotation}
import scala.annotation.{experimental, TastyAnnotation}
import scala.quoted._

@experimental
class void extends MacroAnnotation:
class void extends TastyAnnotation:
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
List(tree)
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ val experimentalDefinitionInLibrary = Set(
"scala.deriving.Mirror$.fromProductTyped", // This API is a bit convoluted. We may need some more feedback before we can stabilize it.

//// New feature: Macro annotations
"scala.annotation.MacroAnnotation",
"scala.annotation.TastyAnnotation",

//// New APIs: Quotes
// Can be stabilized in 3.3.0 (unsure) or later
Expand Down
4 changes: 2 additions & 2 deletions tests/run-macros/annot-annot-order/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import scala.annotation.{experimental, MacroAnnotation}
import scala.annotation.{experimental, TastyAnnotation}
import scala.quoted._

@experimental
class print(msg: String) extends MacroAnnotation:
class print(msg: String) extends TastyAnnotation:
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
import quotes.reflect._
tree match
Expand Down
Loading

0 comments on commit 0cc6b48

Please sign in to comment.