Skip to content

Commit e7aee4f

Browse files
mboveltgodzik
authored andcommitted
Do not handle null in StringToExpr
[Cherry-picked 2e3c803]
1 parent 29d76b9 commit e7aee4f

File tree

8 files changed

+29
-14
lines changed

8 files changed

+29
-14
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -2420,7 +2420,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
24202420

24212421
object StringConstant extends StringConstantModule:
24222422
def apply(x: String): StringConstant =
2423-
require(x != null, "StringConstant cannot be null")
2423+
require(x != null, "value of StringConstant cannot be `null`")
24242424
// A `null` constant must be represented as a `NullConstant`, c.f. a
24252425
// constant with `tag == NullTag`, which is not a `StringConstant`.
24262426
// See issue 23008.

library/src/scala/quoted/Quotes.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -3556,7 +3556,10 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
35563556

35573557
/** Methods of the module object `val StringConstant` */
35583558
trait StringConstantModule { this: StringConstant.type =>
3559-
/** Create a constant String value */
3559+
/** Create a constant String value
3560+
*
3561+
* @throw `IllegalArgumentException` if the argument is `null`
3562+
*/
35603563
def apply(x: String): StringConstant
35613564
/** Match String value constant and extract its value */
35623565
def unapply(constant: StringConstant): Some[String]

library/src/scala/quoted/ToExpr.scala

+1-7
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,7 @@ object ToExpr {
7777
given StringToExpr[T <: String]: ToExpr[T] with {
7878
def apply(x: T)(using Quotes) =
7979
import quotes.reflect.*
80-
val literal =
81-
if (x: Any) == null then
82-
// Can happen if called from code without `-Yexplicit-nulls`.
83-
Literal(NullConstant())
84-
else
85-
Literal(StringConstant(x))
86-
literal.asExpr.asInstanceOf[Expr[T]]
80+
Literal(StringConstant(x)).asExpr.asInstanceOf[Expr[T]]
8781
}
8882

8983
/** Default implementation of `ToExpr[Class[T]]` */

tests/neg-macros/i23008.check

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
-- Error: tests/neg-macros/i23008/Test_2.scala:1:24 --------------------------------------------------------------------
3+
1 |@main def Test = Macros.buildString // error
4+
| ^^^^^^^^^^^^^^^^^^
5+
| Exception occurred while executing macro expansion.
6+
| java.lang.IllegalArgumentException: requirement failed: value of StringConstant cannot be `null`
7+
| at scala.Predef$.require(Predef.scala:337)
8+
| at scala.quoted.runtime.impl.QuotesImpl$reflect$StringConstant$.apply(QuotesImpl.scala:2533)
9+
| at scala.quoted.runtime.impl.QuotesImpl$reflect$StringConstant$.apply(QuotesImpl.scala:2532)
10+
| at scala.quoted.ToExpr$StringToExpr.apply(ToExpr.scala:80)
11+
| at scala.quoted.ToExpr$StringToExpr.apply(ToExpr.scala:78)
12+
| at scala.quoted.Expr$.apply(Expr.scala:70)
13+
| at Macros$.buildStringCode(Macro_1.scala:9)
14+
|
15+
|---------------------------------------------------------------------------------------------------------------------
16+
|Inline stack trace
17+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
18+
|This location contains code that was inlined from Macro_1.scala:4
19+
4 | inline def buildString = ${buildStringCode}
20+
| ^^^^^^^^^^^^^^^^^^
21+
---------------------------------------------------------------------------------------------------------------------

tests/run-macros/i23008/Macros_1.scala renamed to tests/neg-macros/i23008/Macro_1.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ object Macros {
66
def buildStringCode(using Quotes): Expr[String] = {
77
import quotes.reflect.*
88
val str: String = null
9-
val exprString = Expr(str)
10-
Expr(exprString.show)
9+
Expr(str)
1110
}
1211
}

tests/neg-macros/i23008/Test_2.scala

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@main def Test = Macros.buildString // error

tests/run-macros/i23008.check

-1
This file was deleted.

tests/run-macros/i23008/Test_2.scala

-2
This file was deleted.

0 commit comments

Comments
 (0)