Skip to content

Forbid StringConstant(null) #23064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2529,7 +2529,12 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end StringConstantTypeTest

object StringConstant extends StringConstantModule:
def apply(x: String): StringConstant = dotc.core.Constants.Constant(x)
def apply(x: String): StringConstant =
require(x != null, "value of StringConstant cannot be `null`")
// A `null` constant must be represented as a `NullConstant`, c.f. a
// constant with `tag == NullTag`, which is not a `StringConstant`.
// See issue 23008.
dotc.core.Constants.Constant(x)
def unapply(constant: StringConstant): Some[String] = Some(constant.stringValue)
end StringConstant

Expand Down
5 changes: 4 additions & 1 deletion library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3640,7 +3640,10 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** Methods of the module object `val StringConstant` */
trait StringConstantModule { this: StringConstant.type =>
/** Create a constant String value */
/** Create a constant String value
*
* @throw `IllegalArgumentException` if the argument is `null`
*/
def apply(x: String): StringConstant
/** Match String value constant and extract its value */
def unapply(constant: StringConstant): Some[String]
Expand Down
21 changes: 21 additions & 0 deletions tests/neg-macros/i23008.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

-- Error: tests/neg-macros/i23008/Test_2.scala:1:24 --------------------------------------------------------------------
1 |@main def Test = Macros.buildString // error
| ^^^^^^^^^^^^^^^^^^
| Exception occurred while executing macro expansion.
| java.lang.IllegalArgumentException: requirement failed: value of StringConstant cannot be `null`
| at scala.Predef$.require(Predef.scala:337)
| at scala.quoted.runtime.impl.QuotesImpl$reflect$StringConstant$.apply(QuotesImpl.scala:2533)
| at scala.quoted.runtime.impl.QuotesImpl$reflect$StringConstant$.apply(QuotesImpl.scala:2532)
| at scala.quoted.ToExpr$StringToExpr.apply(ToExpr.scala:80)
| at scala.quoted.ToExpr$StringToExpr.apply(ToExpr.scala:78)
| at scala.quoted.Expr$.apply(Expr.scala:70)
| at Macros$.buildStringCode(Macro_1.scala:9)
|
|---------------------------------------------------------------------------------------------------------------------
|Inline stack trace
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|This location contains code that was inlined from Macro_1.scala:4
4 | inline def buildString = ${buildStringCode}
| ^^^^^^^^^^^^^^^^^^
---------------------------------------------------------------------------------------------------------------------
11 changes: 11 additions & 0 deletions tests/neg-macros/i23008/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.quoted.*

object Macros {
inline def buildString = ${buildStringCode}

def buildStringCode(using Quotes): Expr[String] = {
import quotes.reflect.*
val str: String = null
Expr(str)
}
}
1 change: 1 addition & 0 deletions tests/neg-macros/i23008/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@main def Test = Macros.buildString // error
Loading