Skip to content

Fix #7898: Handle annotation without symbols #7900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
type Symbol = core.Symbols.Symbol

def Symbol_owner(self: Symbol)(given Context): Symbol = self.owner
def Symbol_maybeOwner(self: Symbol)(given Context): Symbol = self.maybeOwner

def Symbol_flags(self: Symbol)(given Context): Flags = self.flags

Expand Down
5 changes: 4 additions & 1 deletion library/src/scala/tasty/reflect/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1164,9 +1164,12 @@ trait CompilerInterface {
*/
type Symbol <: AnyRef

/** Owner of this symbol. The owner is the symbol in which this symbol is defined. */
/** Owner of this symbol. The owner is the symbol in which this symbol is defined. Throws if this symbol does not have an owner. */
def Symbol_owner(self: Symbol)(given ctx: Context): Symbol

/** Owner of this symbol. The owner is the symbol in which this symbol is defined. Returns `NoSymbol` if this symbol does not have an owner. */
def Symbol_maybeOwner(self: Symbol)(given ctx: Context): Symbol

/** Flags of this symbol */
def Symbol_flags(self: Symbol)(given ctx: Context): Flags

Expand Down
4 changes: 2 additions & 2 deletions library/src/scala/tasty/reflect/SourceCodePrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig

def printAnnotation(annot: Term)(given elideThis: Option[Symbol]): Buffer = {
val Annotation(ref, args) = annot
if (annot.symbol.owner.fullName == "scala.internal.quoted.showName") this
if (annot.symbol.maybeOwner.fullName == "scala.internal.quoted.showName") this
else {
this += "@"
printTypeTree(ref)
Expand Down Expand Up @@ -1467,7 +1467,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig

object ScalaPackage {
def unapply(tpe: TypeOrBounds)(given ctx: Context): Boolean = tpe match {
case tpe: TermRef => tpe.termSymbol == defn.ScalaPackage
case tpe: Type => tpe.termSymbol == defn.ScalaPackage
case _ => false
}
}
Expand Down
5 changes: 4 additions & 1 deletion library/src/scala/tasty/reflect/SymbolOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ trait SymbolOps extends Core { selfSymbolOps: FlagsOps =>

given symbolOps: extension (self: Symbol) {

/** Owner of this symbol. The owner is the symbol in which this symbol is defined */
/** Owner of this symbol. The owner is the symbol in which this symbol is defined. Throws if this symbol does not have an owner. */
def owner(given ctx: Context): Symbol = internal.Symbol_owner(self)

/** Owner of this symbol. The owner is the symbol in which this symbol is defined. Returns `NoSymbol` if this symbol does not have an owner. */
def maybeOwner(given ctx: Context): Symbol = internal.Symbol_maybeOwner(self)

/** Flags of this symbol */
def flags(given ctx: Context): Flags = internal.Symbol_flags(self)

Expand Down
4 changes: 2 additions & 2 deletions tests/run-macros/flops-rewrite.check
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ scala.Nil
scala.Nil.map[scala.Nothing](((x: scala.Nothing) => x)).++[scala.Nothing](scala.Nil.map[scala.Nothing](((x: scala.Nothing) => x)))
scala.Nil

scala.Nil.map[scala.Nothing](((x: scala.Nothing) => x)).++[scala.Int](scala.List.apply[scala.Int]((3: scala.<repeated>[scala.Int]))).++[scala.Int](scala.Nil)
scala.List.apply[scala.Int]((3: scala.<repeated>[scala.Int]))
scala.Nil.map[scala.Nothing](((x: scala.Nothing) => x)).++[scala.Int](scala.List.apply[scala.Int](3)).++[scala.Int](scala.Nil)
scala.List.apply[scala.Int](3)

4 changes: 4 additions & 0 deletions tests/run-macros/i7898.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
scala.List.apply[scala.PartialFunction[scala.Int, scala.Predef.String]](((x$1: scala.Int) => (x$1: @scala.unchecked) match {
case 1 =>
"x"
}))
17 changes: 17 additions & 0 deletions tests/run-macros/i7898/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import quoted._
object Main {

def myMacroImpl(body: Expr[_])(given qctx: QuoteContext): Expr[_] = {
import qctx.tasty.{_, given}
val bodyTerm = body.underlyingArgument.unseal
val showed = bodyTerm.show
'{
println(${Expr(showed)})
${bodyTerm.seal}
}
}

inline def myMacro(body: => Any) <: Any = ${
myMacroImpl('body)
}
}
7 changes: 7 additions & 0 deletions tests/run-macros/i7898/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object Test {
def main(args: Array[String]) = {
val _ = Main.myMacro(List[PartialFunction[Int, String]] {
case 1 => "x"
})
}
}
46 changes: 23 additions & 23 deletions tests/run-macros/quote-matcher-runtime.check
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,20 @@ Pattern: fs()
Result: Some(List())

Scrutinee: fs()
Pattern: fs((scala.internal.Quoted.patternHole[scala.Seq[scala.Int]]: scala.<repeated>[scala.Int]))
Result: Some(List(Expr((: scala.<repeated>[scala.Int]))))
Pattern: fs(scala.internal.Quoted.patternHole[scala.Seq[scala.Int]]: _*)
Result: Some(List(Expr()))

Scrutinee: fs((1, 2, 3: scala.<repeated>[scala.Int]))
Pattern: fs((1, 2, 3: scala.<repeated>[scala.Int]))
Scrutinee: fs(1, 2, 3)
Pattern: fs(1, 2, 3)
Result: Some(List())

Scrutinee: fs((1, 2, 3: scala.<repeated>[scala.Int]))
Pattern: fs((scala.internal.Quoted.patternHole[scala.Int], scala.internal.Quoted.patternHole[scala.Int], 3: scala.<repeated>[scala.Int]))
Scrutinee: fs(1, 2, 3)
Pattern: fs(scala.internal.Quoted.patternHole[scala.Int], scala.internal.Quoted.patternHole[scala.Int], 3)
Result: Some(List(Expr(1), Expr(2)))

Scrutinee: fs((1, 2, 3: scala.<repeated>[scala.Int]))
Pattern: fs((scala.internal.Quoted.patternHole[scala.Seq[scala.Int]]: scala.<repeated>[scala.Int]))
Result: Some(List(Expr((1, 2, 3: scala.<repeated>[scala.Int]))))
Scrutinee: fs(1, 2, 3)
Pattern: fs(scala.internal.Quoted.patternHole[scala.Seq[scala.Int]]: _*)
Result: Some(List(Expr(1, 2, 3)))

Scrutinee: f2(1, 2)
Pattern: f2(1, 2)
Expand Down Expand Up @@ -240,17 +240,17 @@ Scrutinee: ((x: scala.Int) => "abc")
Pattern: ((x: scala.Int @scala.internal.Quoted.patternBindHole) => scala.internal.Quoted.patternHole[scala.Predef.String])
Result: Some(List(Sym(x), Expr("abc")))

Scrutinee: scala.StringContext.apply(("abc", "xyz": scala.<repeated>[scala.Predef.String]))
Pattern: scala.StringContext.apply(("abc", "xyz": scala.<repeated>[scala.Predef.String]))
Scrutinee: scala.StringContext.apply("abc", "xyz")
Pattern: scala.StringContext.apply("abc", "xyz")
Result: Some(List())

Scrutinee: scala.StringContext.apply(("abc", "xyz": scala.<repeated>[scala.Predef.String]))
Pattern: scala.StringContext.apply((scala.internal.Quoted.patternHole[java.lang.String], scala.internal.Quoted.patternHole[java.lang.String]: scala.<repeated>[scala.Predef.String]))
Scrutinee: scala.StringContext.apply("abc", "xyz")
Pattern: scala.StringContext.apply(scala.internal.Quoted.patternHole[java.lang.String], scala.internal.Quoted.patternHole[java.lang.String])
Result: Some(List(Expr("abc"), Expr("xyz")))

Scrutinee: scala.StringContext.apply(("abc", "xyz": scala.<repeated>[scala.Predef.String]))
Pattern: scala.StringContext.apply((scala.internal.Quoted.patternHole[scala.Seq[scala.Predef.String]]: scala.<repeated>[scala.Predef.String]))
Result: Some(List(Expr(("abc", "xyz": scala.<repeated>[scala.Predef.String]))))
Scrutinee: scala.StringContext.apply("abc", "xyz")
Pattern: scala.StringContext.apply(scala.internal.Quoted.patternHole[scala.Seq[scala.Predef.String]]: _*)
Result: Some(List(Expr("abc", "xyz")))

Scrutinee: {
val a: scala.Int = 45
Expand Down Expand Up @@ -714,21 +714,21 @@ Pattern: try scala.internal.Quoted.patternHole[scala.Int] finally {
}
Result: Some(List(Expr(1), Expr(2)))

Scrutinee: scala.List.apply[scala.Int]((1, 2, 3: scala.<repeated>[scala.Int])).foreach[scala.Unit](((x: scala.Int) => scala.Predef.println(x)))
Scrutinee: scala.List.apply[scala.Int](1, 2, 3).foreach[scala.Unit](((x: scala.Int) => scala.Predef.println(x)))
Pattern: {
@scala.internal.Quoted.patternBindHole type T
scala.internal.Quoted.patternHole[scala.List[scala.Int]].foreach[T](scala.internal.Quoted.patternHole[scala.Function1[scala.Int, T]])
}
Result: Some(List(Type(scala.Unit), Expr(scala.List.apply[scala.Int]((1, 2, 3: scala.<repeated>[scala.Int]))), Expr(((x: scala.Int) => scala.Predef.println(x)))))
Result: Some(List(Type(scala.Unit), Expr(scala.List.apply[scala.Int](1, 2, 3)), Expr(((x: scala.Int) => scala.Predef.println(x)))))

Scrutinee: scala.List.apply[scala.Int]((1, 2, 3: scala.<repeated>[scala.Int])).foreach[scala.Unit](((x: scala.Int) => scala.Predef.println(x)))
Scrutinee: scala.List.apply[scala.Int](1, 2, 3).foreach[scala.Unit](((x: scala.Int) => scala.Predef.println(x)))
Pattern: {
@scala.internal.Quoted.patternBindHole type T = scala.Unit
scala.internal.Quoted.patternHole[scala.List[scala.Int]].foreach[T](scala.internal.Quoted.patternHole[scala.Function1[scala.Int, T]])
}
Result: Some(List(Type(scala.Unit), Expr(scala.List.apply[scala.Int]((1, 2, 3: scala.<repeated>[scala.Int]))), Expr(((x: scala.Int) => scala.Predef.println(x)))))
Result: Some(List(Type(scala.Unit), Expr(scala.List.apply[scala.Int](1, 2, 3)), Expr(((x: scala.Int) => scala.Predef.println(x)))))

Scrutinee: scala.List.apply[scala.Int]((1, 2, 3: scala.<repeated>[scala.Int])).foreach[scala.Unit](((x: scala.Int) => scala.Predef.println(x)))
Scrutinee: scala.List.apply[scala.Int](1, 2, 3).foreach[scala.Unit](((x: scala.Int) => scala.Predef.println(x)))
Pattern: {
@scala.internal.Quoted.patternBindHole type T <: scala.Predef.String
scala.internal.Quoted.patternHole[scala.List[scala.Int]].foreach[T](scala.internal.Quoted.patternHole[scala.Function1[scala.Int, T]])
Expand Down Expand Up @@ -787,15 +787,15 @@ Pattern: {
}
Result: None

Scrutinee: scala.List.apply[scala.Int]((1, 2, 3: scala.<repeated>[scala.Int])).map[scala.Double](((x: scala.Int) => x.toDouble./(2))).map[java.lang.String](((y: scala.Double) => y.toString()))
Scrutinee: scala.List.apply[scala.Int](1, 2, 3).map[scala.Double](((x: scala.Int) => x.toDouble./(2))).map[java.lang.String](((y: scala.Double) => y.toString()))
Pattern: {
@scala.internal.Quoted.patternBindHole type T
@scala.internal.Quoted.patternBindHole type U
@scala.internal.Quoted.patternBindHole type V

(scala.internal.Quoted.patternHole[scala.List[T]].map[U](scala.internal.Quoted.patternHole[scala.Function1[T, U]]).map[V](scala.internal.Quoted.patternHole[scala.Function1[U, V]]): scala.collection.immutable.List[scala.Any])
}
Result: Some(List(Type(scala.Int), Type(scala.Double), Type(java.lang.String), Expr(scala.List.apply[scala.Int]((1, 2, 3: scala.<repeated>[scala.Int]))), Expr(((x: scala.Int) => x.toDouble./(2))), Expr(((y: scala.Double) => y.toString()))))
Result: Some(List(Type(scala.Int), Type(scala.Double), Type(java.lang.String), Expr(scala.List.apply[scala.Int](1, 2, 3)), Expr(((x: scala.Int) => x.toDouble./(2))), Expr(((y: scala.Double) => y.toString()))))

Scrutinee: ((x: scala.Int) => x)
Pattern: {
Expand Down
8 changes: 4 additions & 4 deletions tests/run-macros/quote-matching-optimize-1.check
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ Original: ls.filter(((x: scala.Int) => x.<(3))).foreach[scala.Unit](((x: scala.I
Optimized: ls.foreach[scala.Unit](((x: scala.Int) => if (x.<(3)) scala.Predef.println(x) else ()))
Result: ()

Original: scala.List.apply[scala.Int]((1, 2, 3: scala.<repeated>[scala.Int])).map[scala.Int](((a: scala.Int) => a.*(2))).map[java.lang.String](((b: scala.Int) => b.toString()))
Optimized: scala.List.apply[scala.Int]((1, 2, 3: scala.<repeated>[scala.Int])).map[java.lang.String](((x: scala.Int) => {
Original: scala.List.apply[scala.Int](1, 2, 3).map[scala.Int](((a: scala.Int) => a.*(2))).map[java.lang.String](((b: scala.Int) => b.toString()))
Optimized: scala.List.apply[scala.Int](1, 2, 3).map[java.lang.String](((x: scala.Int) => {
val x$1: scala.Int = x.*(2)
x$1.toString()
}))
Result: List(2, 4, 6)

Original: scala.List.apply[scala.Int]((55, 67, 87: scala.<repeated>[scala.Int])).map[scala.Char](((a: scala.Int) => a.toChar)).map[java.lang.String](((b: scala.Char) => b.toString()))
Optimized: scala.List.apply[scala.Int]((55, 67, 87: scala.<repeated>[scala.Int])).map[java.lang.String](((x: scala.Int) => {
Original: scala.List.apply[scala.Int](55, 67, 87).map[scala.Char](((a: scala.Int) => a.toChar)).map[java.lang.String](((b: scala.Char) => b.toString()))
Optimized: scala.List.apply[scala.Int](55, 67, 87).map[java.lang.String](((x: scala.Int) => {
val x$2: scala.Char = x.toChar
x$2.toString()
}))
Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/quoted-matching-docs-2.check
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
6
12.+(Test.a)
17
4.+(Macro_1$package.sum((Test.seq: scala.<repeated>[scala.Int])))
4.+(Macro_1$package.sum(Test.seq: _*))
13
4 changes: 2 additions & 2 deletions tests/run-macros/tasty-macro-positions.check
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ quoted_2.scala:[217..222]
quoted_2.scala:[232..245]
("abc": scala.Predef.String)
quoted_2.scala:[255..269]
_root_.scala.StringContext.apply(("abc", "": scala.<repeated>[scala.Predef.String])).s(("def": scala.<repeated>[scala.Any]))
_root_.scala.StringContext.apply("abc", "").s("def")
quoted_2.scala:[281..282]
a
quoted_2.scala:[293..294]
Expand All @@ -25,7 +25,7 @@ quoted_2.scala:[329..334]
quoted_2.scala:[345..358]
("abc": scala.Predef.String)
quoted_2.scala:[369..383]
_root_.scala.StringContext.apply(("abc", "": scala.<repeated>[scala.Predef.String])).s(("def": scala.<repeated>[scala.Any]))
_root_.scala.StringContext.apply("abc", "").s("def")
quoted_2.scala:[426..427]
T
quoted_2.scala:[438..444]
Expand Down