Skip to content

Expr(null:String).show in macro throws NullPointerException #23008

Closed
@Alexey-NM

Description

@Alexey-NM

Compiler version

3.6.4

Minimized code

Macro:

import scala.quoted.*

object ExprStringBugMacro {

  def buildStringCode(using Quotes): Expr[String] = {
    import quotes.reflect.*
    val str: String = null
    val exprString = Expr(str)
    //It throws NullPointerException
    println("Macro string: " + exprString.show)
    exprString
  }
}

Test:

import munit.FunSuite

class ExprStringBugTest extends munit.FunSuite {

  test("NullPoinreException") {
    buildString
  }

  implicit inline def buildString[T]: Unit = ${
    ExprStringBugMacro.buildStringCode
  }
}import scala.quoted.*

object ExprStringBugMacro {

  def buildStringCode(using Quotes): Expr[String] = {
    import quotes.reflect.*
    //Test value
    val str: String = null
    val exprString = Expr(str)
    println("Macro string: " + exprString.show)
    exprString
  }
}

Output

Image

Expectation

Absence of the exception

Description

stringValue

def stringValue: String = value.toString

just does not expect null.

It requres to implement a custom string converter to generate exp from java types.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions