Skip to content

Commit 2e3c803

Browse files
committed
Do not handle null in StringToExpr
1 parent e0fbfde commit 2e3c803

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
@@ -2530,7 +2530,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
25302530

25312531
object StringConstant extends StringConstantModule:
25322532
def apply(x: String): StringConstant =
2533-
require(x != null, "StringConstant cannot be null")
2533+
require(x != null, "value of StringConstant cannot be `null`")
25342534
// A `null` constant must be represented as a `NullConstant`, c.f. a
25352535
// constant with `tag == NullTag`, which is not a `StringConstant`.
25362536
// See issue 23008.

library/src/scala/quoted/Quotes.scala

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

36413641
/** Methods of the module object `val StringConstant` */
36423642
trait StringConstantModule { this: StringConstant.type =>
3643-
/** Create a constant String value */
3643+
/** Create a constant String value
3644+
*
3645+
* @throw `IllegalArgumentException` if the argument is `null`
3646+
*/
36443647
def apply(x: String): StringConstant
36453648
/** Match String value constant and extract its value */
36463649
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)