File tree Expand file tree Collapse file tree 7 files changed +34
-28
lines changed
compiler/src/dotty/tools/dotc Expand file tree Collapse file tree 7 files changed +34
-28
lines changed Original file line number Diff line number Diff line change @@ -131,6 +131,7 @@ public enum ErrorMessageID {
131131 MatchCaseOnlyNullWarningID ,
132132 ImportRenamedTwiceID ,
133133 TypeTestAlwaysSucceedsID ,
134+ SpliceOutsideQuotesID ,
134135 ;
135136
136137 public int errorNumber () {
Original file line number Diff line number Diff line change @@ -2105,4 +2105,12 @@ object messages {
21052105 }
21062106 val explanation = " "
21072107 }
2108+
2109+ case class SpliceOutsideQuotes () extends Message (SpliceOutsideQuotesID ) {
2110+ val kind = " Syntax"
2111+ val msg = " splice outside quotes"
2112+ val explanation =
2113+ """ A splice may only appear inside quotes '{ ... },
2114+ |or else it must be the whole right hand side of a transparent method.""" .stripMargin
2115+ }
21082116}
Original file line number Diff line number Diff line change @@ -156,23 +156,10 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
156156 }
157157 }
158158
159- /** 1. If we are in a transparent method but not in a nested quote, mark the transparent method
160- * as a macro.
161- *
162- * 2. If selection is a quote or splice node, record that fact in the current compilation unit.
159+ /** If selection is a quote or splice node, record that fact in the current compilation unit.
163160 */
164- private def handleMeta (sym : Symbol )(implicit ctx : Context ): Unit = {
165-
166- def markAsMacro (c : Context ): Unit =
167- if (c.owner eq c.outer.owner) markAsMacro(c.outer)
168- else if (c.owner.isTransparentMethod) c.owner.setFlag(Macro | Erased )
169- else if (! c.outer.owner.is(Package )) markAsMacro(c.outer)
170-
171- if (sym.isSplice || sym.isQuote) {
172- markAsMacro(ctx)
173- ctx.compilationUnit.containsQuotesOrSplices = true
174- }
175- }
161+ private def handleMeta (sym : Symbol )(implicit ctx : Context ): Unit =
162+ if (sym.isSplice || sym.isQuote) ctx.compilationUnit.containsQuotesOrSplices = true
176163
177164 private object dropInlines extends TreeMap {
178165 override def transform (tree : Tree )(implicit ctx : Context ): Tree = tree match {
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ import SymUtils._
1212import NameKinds ._
1313import dotty .tools .dotc .ast .tpd .Tree
1414import typer .Implicits .SearchFailureType
15-
15+ import reporting . diagnostic . messages . _
1616import scala .collection .mutable
1717import dotty .tools .dotc .core .StdNames ._
1818import dotty .tools .dotc .core .quoted ._
@@ -230,7 +230,7 @@ class ReifyQuotes extends MacroTransformWithImplicits {
230230
231231 /** Issue a "splice outside quote" error unless we are in the body of a transparent method */
232232 def spliceOutsideQuotes (pos : Position )(implicit ctx : Context ): Unit =
233- ctx.error(i " splice outside quotes " , pos)
233+ ctx.error(SpliceOutsideQuotes () , pos)
234234
235235 /** Try to heal phase-inconsistent reference to type `T` using a local type definition.
236236 * @return None if successful
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ import ProtoTypes.selectionProto
2020import SymDenotations .SymDenotation
2121import Annotations ._
2222import transform .{ExplicitOuter , AccessProxies }
23+ import transform .SymUtils ._
2324import Inferencing .fullyDefinedType
2425import config .Printers .inlining
2526import ErrorReporting .errorTree
@@ -250,6 +251,15 @@ object PrepareTransparent {
250251 inlined.updateAnnotation(LazyBodyAnnotation { _ =>
251252 implicit val ctx = inlineCtx
252253 val rawBody = treeExpr(ctx)
254+
255+ def markAsMacro (body : Tree ): Unit = body match {
256+ case _ : Select if body.symbol.isSplice => inlined.setFlag(Erased | Macro )
257+ case Block (Nil , expr) => markAsMacro(expr)
258+ case Block (expr :: Nil , Literal (Constant (()))) => markAsMacro(expr)
259+ case _ =>
260+ }
261+ markAsMacro(rawBody)
262+
253263 val typedBody =
254264 if (ctx.reporter.hasErrors) rawBody
255265 else ctx.compilationUnit.inlineAccessors.makeInlineable(rawBody)
Original file line number Diff line number Diff line change @@ -2,23 +2,23 @@ import scala.quoted._
22
33object Test {
44
5- transparent def foo1 : Int = { // error
5+ transparent def foo1 : Int = {
66 println()
7- ~ impl(1 .toExpr)
7+ ~ impl(1 .toExpr) // error: splice outside quotes
88 }
99
10- transparent def foo2 : Int = { // error
11- ~ impl(1 .toExpr)
12- ~ impl(2 .toExpr)
10+ transparent def foo2 : Int = {
11+ ~ impl(1 .toExpr) // error: splice outside quotes
12+ ~ impl(2 .toExpr) // error: splice outside quotes
1313 }
1414
15- transparent def foo3 : Int = { // error
15+ transparent def foo3 : Int = {
1616 val a = 1
17- ~ impl('(a))
17+ ~ impl('(a)) // error: splice outside quotes
1818 }
1919
20- transparent def foo4 : Int = { // error
21- ~ impl('(1))
20+ transparent def foo4 : Int = {
21+ ~ impl('(1)) // error: splice outside quotes
2222 1
2323 }
2424
Original file line number Diff line number Diff line change @@ -3,5 +3,5 @@ import scala.quoted._
33object Foo {
44 transparent def foo2 (): Unit = ~ foo2Impl()
55 def foo2Impl (): Expr [Unit ] = '()
6- erased transparent def foo (): Unit = foo2()
6+ transparent def foo (): Unit = foo2()
77}
You can’t perform that action at this time.
0 commit comments