Skip to content

Commit 760deff

Browse files
Merge pull request #7900 from dotty-staging/fix-#7898
Fix #7898: Handle annotation without symbols
2 parents 795db6c + 28231bf commit 760deff

File tree

12 files changed

+71
-36
lines changed

12 files changed

+71
-36
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
15901590
type Symbol = core.Symbols.Symbol
15911591

15921592
def Symbol_owner(self: Symbol)(given Context): Symbol = self.owner
1593+
def Symbol_maybeOwner(self: Symbol)(given Context): Symbol = self.maybeOwner
15931594

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

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,9 +1164,12 @@ trait CompilerInterface {
11641164
*/
11651165
type Symbol <: AnyRef
11661166

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

1170+
/** 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. */
1171+
def Symbol_maybeOwner(self: Symbol)(given ctx: Context): Symbol
1172+
11701173
/** Flags of this symbol */
11711174
def Symbol_flags(self: Symbol)(given ctx: Context): Flags
11721175

library/src/scala/tasty/reflect/SourceCodePrinter.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
12281228

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

14681468
object ScalaPackage {
14691469
def unapply(tpe: TypeOrBounds)(given ctx: Context): Boolean = tpe match {
1470-
case tpe: TermRef => tpe.termSymbol == defn.ScalaPackage
1470+
case tpe: Type => tpe.termSymbol == defn.ScalaPackage
14711471
case _ => false
14721472
}
14731473
}

library/src/scala/tasty/reflect/SymbolOps.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ trait SymbolOps extends Core { selfSymbolOps: FlagsOps =>
1616

1717
given symbolOps: extension (self: Symbol) {
1818

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

22+
/** 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. */
23+
def maybeOwner(given ctx: Context): Symbol = internal.Symbol_maybeOwner(self)
24+
2225
/** Flags of this symbol */
2326
def flags(given ctx: Context): Flags = internal.Symbol_flags(self)
2427

tests/run-macros/flops-rewrite.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ scala.Nil
44
scala.Nil.map[scala.Nothing](((x: scala.Nothing) => x)).++[scala.Nothing](scala.Nil.map[scala.Nothing](((x: scala.Nothing) => x)))
55
scala.Nil
66

7-
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)
8-
scala.List.apply[scala.Int]((3: scala.<repeated>[scala.Int]))
7+
scala.Nil.map[scala.Nothing](((x: scala.Nothing) => x)).++[scala.Int](scala.List.apply[scala.Int](3)).++[scala.Int](scala.Nil)
8+
scala.List.apply[scala.Int](3)
99

tests/run-macros/i7898.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
scala.List.apply[scala.PartialFunction[scala.Int, scala.Predef.String]](((x$1: scala.Int) => (x$1: @scala.unchecked) match {
2+
case 1 =>
3+
"x"
4+
}))

tests/run-macros/i7898/Macro_1.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import quoted._
2+
object Main {
3+
4+
def myMacroImpl(body: Expr[_])(given qctx: QuoteContext): Expr[_] = {
5+
import qctx.tasty.{_, given}
6+
val bodyTerm = body.underlyingArgument.unseal
7+
val showed = bodyTerm.show
8+
'{
9+
println(${Expr(showed)})
10+
${bodyTerm.seal}
11+
}
12+
}
13+
14+
inline def myMacro(body: => Any) <: Any = ${
15+
myMacroImpl('body)
16+
}
17+
}

tests/run-macros/i7898/Test_2.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Test {
2+
def main(args: Array[String]) = {
3+
val _ = Main.myMacro(List[PartialFunction[Int, String]] {
4+
case 1 => "x"
5+
})
6+
}
7+
}

tests/run-macros/quote-matcher-runtime.check

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -189,20 +189,20 @@ Pattern: fs()
189189
Result: Some(List())
190190

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

195-
Scrutinee: fs((1, 2, 3: scala.<repeated>[scala.Int]))
196-
Pattern: fs((1, 2, 3: scala.<repeated>[scala.Int]))
195+
Scrutinee: fs(1, 2, 3)
196+
Pattern: fs(1, 2, 3)
197197
Result: Some(List())
198198

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

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

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

243-
Scrutinee: scala.StringContext.apply(("abc", "xyz": scala.<repeated>[scala.Predef.String]))
244-
Pattern: scala.StringContext.apply(("abc", "xyz": scala.<repeated>[scala.Predef.String]))
243+
Scrutinee: scala.StringContext.apply("abc", "xyz")
244+
Pattern: scala.StringContext.apply("abc", "xyz")
245245
Result: Some(List())
246246

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

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

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

717-
Scrutinee: scala.List.apply[scala.Int]((1, 2, 3: scala.<repeated>[scala.Int])).foreach[scala.Unit](((x: scala.Int) => scala.Predef.println(x)))
717+
Scrutinee: scala.List.apply[scala.Int](1, 2, 3).foreach[scala.Unit](((x: scala.Int) => scala.Predef.println(x)))
718718
Pattern: {
719719
@scala.internal.Quoted.patternBindHole type T
720720
scala.internal.Quoted.patternHole[scala.List[scala.Int]].foreach[T](scala.internal.Quoted.patternHole[scala.Function1[scala.Int, T]])
721721
}
722-
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)))))
722+
Result: Some(List(Type(scala.Unit), Expr(scala.List.apply[scala.Int](1, 2, 3)), Expr(((x: scala.Int) => scala.Predef.println(x)))))
723723

724-
Scrutinee: scala.List.apply[scala.Int]((1, 2, 3: scala.<repeated>[scala.Int])).foreach[scala.Unit](((x: scala.Int) => scala.Predef.println(x)))
724+
Scrutinee: scala.List.apply[scala.Int](1, 2, 3).foreach[scala.Unit](((x: scala.Int) => scala.Predef.println(x)))
725725
Pattern: {
726726
@scala.internal.Quoted.patternBindHole type T = scala.Unit
727727
scala.internal.Quoted.patternHole[scala.List[scala.Int]].foreach[T](scala.internal.Quoted.patternHole[scala.Function1[scala.Int, T]])
728728
}
729-
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)))))
729+
Result: Some(List(Type(scala.Unit), Expr(scala.List.apply[scala.Int](1, 2, 3)), Expr(((x: scala.Int) => scala.Predef.println(x)))))
730730

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

790-
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()))
790+
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()))
791791
Pattern: {
792792
@scala.internal.Quoted.patternBindHole type T
793793
@scala.internal.Quoted.patternBindHole type U
794794
@scala.internal.Quoted.patternBindHole type V
795795

796796
(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])
797797
}
798-
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()))))
798+
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()))))
799799

800800
Scrutinee: ((x: scala.Int) => x)
801801
Pattern: {

tests/run-macros/quote-matching-optimize-1.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ Original: ls.filter(((x: scala.Int) => x.<(3))).foreach[scala.Unit](((x: scala.I
1616
Optimized: ls.foreach[scala.Unit](((x: scala.Int) => if (x.<(3)) scala.Predef.println(x) else ()))
1717
Result: ()
1818

19-
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()))
20-
Optimized: scala.List.apply[scala.Int]((1, 2, 3: scala.<repeated>[scala.Int])).map[java.lang.String](((x: scala.Int) => {
19+
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()))
20+
Optimized: scala.List.apply[scala.Int](1, 2, 3).map[java.lang.String](((x: scala.Int) => {
2121
val x$1: scala.Int = x.*(2)
2222
x$1.toString()
2323
}))
2424
Result: List(2, 4, 6)
2525

26-
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()))
27-
Optimized: scala.List.apply[scala.Int]((55, 67, 87: scala.<repeated>[scala.Int])).map[java.lang.String](((x: scala.Int) => {
26+
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()))
27+
Optimized: scala.List.apply[scala.Int](55, 67, 87).map[java.lang.String](((x: scala.Int) => {
2828
val x$2: scala.Char = x.toChar
2929
x$2.toString()
3030
}))

tests/run-macros/quoted-matching-docs-2.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
6
33
12.+(Test.a)
44
17
5-
4.+(Macro_1$package.sum((Test.seq: scala.<repeated>[scala.Int])))
5+
4.+(Macro_1$package.sum(Test.seq: _*))
66
13

tests/run-macros/tasty-macro-positions.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ quoted_2.scala:[217..222]
1111
quoted_2.scala:[232..245]
1212
("abc": scala.Predef.String)
1313
quoted_2.scala:[255..269]
14-
_root_.scala.StringContext.apply(("abc", "": scala.<repeated>[scala.Predef.String])).s(("def": scala.<repeated>[scala.Any]))
14+
_root_.scala.StringContext.apply("abc", "").s("def")
1515
quoted_2.scala:[281..282]
1616
a
1717
quoted_2.scala:[293..294]
@@ -25,7 +25,7 @@ quoted_2.scala:[329..334]
2525
quoted_2.scala:[345..358]
2626
("abc": scala.Predef.String)
2727
quoted_2.scala:[369..383]
28-
_root_.scala.StringContext.apply(("abc", "": scala.<repeated>[scala.Predef.String])).s(("def": scala.<repeated>[scala.Any]))
28+
_root_.scala.StringContext.apply("abc", "").s("def")
2929
quoted_2.scala:[426..427]
3030
T
3131
quoted_2.scala:[438..444]

0 commit comments

Comments
 (0)