File tree 7 files changed +34
-28
lines changed
compiler/src/dotty/tools/dotc
7 files changed +34
-28
lines changed Original file line number Diff line number Diff line change @@ -131,6 +131,7 @@ public enum ErrorMessageID {
131
131
MatchCaseOnlyNullWarningID ,
132
132
ImportRenamedTwiceID ,
133
133
TypeTestAlwaysSucceedsID ,
134
+ SpliceOutsideQuotesID ,
134
135
;
135
136
136
137
public int errorNumber () {
Original file line number Diff line number Diff line change @@ -2105,4 +2105,12 @@ object messages {
2105
2105
}
2106
2106
val explanation = " "
2107
2107
}
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
+ }
2108
2116
}
Original file line number Diff line number Diff line change @@ -156,23 +156,10 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
156
156
}
157
157
}
158
158
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.
163
160
*/
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
176
163
177
164
private object dropInlines extends TreeMap {
178
165
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._
12
12
import NameKinds ._
13
13
import dotty .tools .dotc .ast .tpd .Tree
14
14
import typer .Implicits .SearchFailureType
15
-
15
+ import reporting . diagnostic . messages . _
16
16
import scala .collection .mutable
17
17
import dotty .tools .dotc .core .StdNames ._
18
18
import dotty .tools .dotc .core .quoted ._
@@ -230,7 +230,7 @@ class ReifyQuotes extends MacroTransformWithImplicits {
230
230
231
231
/** Issue a "splice outside quote" error unless we are in the body of a transparent method */
232
232
def spliceOutsideQuotes (pos : Position )(implicit ctx : Context ): Unit =
233
- ctx.error(i " splice outside quotes " , pos)
233
+ ctx.error(SpliceOutsideQuotes () , pos)
234
234
235
235
/** Try to heal phase-inconsistent reference to type `T` using a local type definition.
236
236
* @return None if successful
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ import ProtoTypes.selectionProto
20
20
import SymDenotations .SymDenotation
21
21
import Annotations ._
22
22
import transform .{ExplicitOuter , AccessProxies }
23
+ import transform .SymUtils ._
23
24
import Inferencing .fullyDefinedType
24
25
import config .Printers .inlining
25
26
import ErrorReporting .errorTree
@@ -250,6 +251,15 @@ object PrepareTransparent {
250
251
inlined.updateAnnotation(LazyBodyAnnotation { _ =>
251
252
implicit val ctx = inlineCtx
252
253
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
+
253
263
val typedBody =
254
264
if (ctx.reporter.hasErrors) rawBody
255
265
else ctx.compilationUnit.inlineAccessors.makeInlineable(rawBody)
Original file line number Diff line number Diff line change @@ -2,23 +2,23 @@ import scala.quoted._
2
2
3
3
object Test {
4
4
5
- transparent def foo1 : Int = { // error
5
+ transparent def foo1 : Int = {
6
6
println()
7
- ~ impl(1 .toExpr)
7
+ ~ impl(1 .toExpr) // error: splice outside quotes
8
8
}
9
9
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
13
13
}
14
14
15
- transparent def foo3 : Int = { // error
15
+ transparent def foo3 : Int = {
16
16
val a = 1
17
- ~ impl('(a))
17
+ ~ impl('(a)) // error: splice outside quotes
18
18
}
19
19
20
- transparent def foo4 : Int = { // error
21
- ~ impl('(1))
20
+ transparent def foo4 : Int = {
21
+ ~ impl('(1)) // error: splice outside quotes
22
22
1
23
23
}
24
24
Original file line number Diff line number Diff line change @@ -3,5 +3,5 @@ import scala.quoted._
3
3
object Foo {
4
4
transparent def foo2 (): Unit = ~ foo2Impl()
5
5
def foo2Impl (): Expr [Unit ] = '()
6
- erased transparent def foo (): Unit = foo2()
6
+ transparent def foo (): Unit = foo2()
7
7
}
You can’t perform that action at this time.
0 commit comments