Skip to content

Commit 49e5d78

Browse files
Merge pull request #12235 from dotty-staging/fix-#12196
Support inline val references in top level splices
2 parents 17b62df + d101327 commit 49e5d78

File tree

6 files changed

+37
-1
lines changed

6 files changed

+37
-1
lines changed

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ object Splicer {
161161
case SeqLiteral(elems, _) =>
162162
elems.foreach(checkIfValidArgument)
163163

164-
case tree: Ident if summon[Env].contains(tree.symbol) =>
164+
case tree: Ident if summon[Env].contains(tree.symbol) || tree.symbol.is(Inline, butNot = Method) =>
165165
// OK
166166

167167
case _ =>
@@ -172,6 +172,7 @@ object Splicer {
172172
|Parameters may only be:
173173
| * Quoted parameters or fields
174174
| * Literal values of primitive types
175+
| * References to `inline val`s
175176
|""".stripMargin, tree.srcPos)
176177
}
177178

@@ -242,6 +243,11 @@ object Splicer {
242243
case Literal(Constant(value)) =>
243244
interpretLiteral(value)
244245

246+
case tree: Ident if tree.symbol.is(Inline, butNot = Method) =>
247+
tree.tpe.widenTermRefExpr match
248+
case ConstantType(c) => c.value.asInstanceOf[Object]
249+
case _ => throw new StopInterpretation(em"${tree.symbol} could not be inlined", tree.srcPos)
250+
245251
// TODO disallow interpreted method calls as arguments
246252
case Call(fn, args) =>
247253
if (fn.symbol.isConstructor && fn.symbol.owner.owner.is(Package))

tests/neg-macros/i7839.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
| Parameters may only be:
77
| * Quoted parameters or fields
88
| * Literal values of primitive types
9+
| * References to `inline val`s

tests/neg/i12196.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import scala.quoted.*
2+
3+
def qqq(s: String)(using Quotes): Expr[Unit] = '{()}
4+
5+
inline val InlineVal = "i"
6+
inline def InlineDef = "i"
7+
8+
inline def withInlineVal = ${ qqq(InlineVal) }
9+
inline def withInlineDef = ${ qqq(InlineDef) } // error
10+
inline def withString = ${ qqq("i") }

tests/neg/i12196b.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import scala.quoted.*
2+
3+
def qqq(s: String)(using Quotes): Expr[Unit] = '{()}
4+
5+
abstract class Foo:
6+
inline val InlineVal: String
7+
8+
val foo: Foo = ???
9+
10+
inline def withInlineVal = ${ qqq(foo.InlineVal) } // error
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
import scala.quoted.*
3+
4+
inline val InlineStringVal = "abc"
5+
6+
inline def withInlineVal = ${ qqq(InlineStringVal) }
7+
8+
def qqq(s: String)(using Quotes): Expr[String] = Expr(s)

tests/pos-macros/i12196/Test_2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def test = withInlineVal

0 commit comments

Comments
 (0)