Skip to content

MatchError: NoPrefix() when printing code of the NoPrefix() type #19905

Closed
@TomasMikula

Description

@TomasMikula

Compiler version

3.3.3
3.4.0
3.4.1-RC1
3.4.2-RC1-bin-20240308-18645ee-NIGHTLY

Minimized code

Macro definitions

import scala.quoted.*

inline def noPrefixShortCode: String =
  ${ noPrefixShortCodeImpl }

inline def noPrefixCode: String =
  ${ noPrefixCodeImpl }

inline def noPrefixStructure: String =
  ${ noPrefixStructure }

def noPrefix(using Quotes): quotes.reflect.TypeRepr =
  import quotes.reflect.*
  TypeRepr.of[Unit] match
    case TypeRef(ThisType(TypeRef(prefix, _)), _) => prefix

def noPrefixShortCodeImpl(using Quotes): Expr[String] =
  import quotes.reflect.*
  Expr(Printer.TypeReprShortCode.show(noPrefix))

def noPrefixCodeImpl(using Quotes): Expr[String] =
  import quotes.reflect.*
  Expr(Printer.TypeReprCode.show(noPrefix))

def noPrefixStructure(using Quotes): Expr[String] =
  import quotes.reflect.*
  Expr(Printer.TypeReprStructure.show(noPrefix))

Macro use

@main
def main: Unit =
  println(noPrefixShortCode) // MatchError: NoPrefix() during macro expansion
  println(noPrefixCode)      // MatchError: NoPrefix() during macro expansion
  println(noPrefixStructure) // OK

Output

[info] compiling 2 Scala sources to /Users/tomas/tmp/type-printer-match-error-NoPrefix/target/scala-3.4.2-RC1-bin-20240308-18645ee-NIGHTLY/classes ...
[error] -- Error: /Users/tomas/tmp/type-printer-match-error-NoPrefix/use.scala:3:10 ----
[error] 3 |  println(noPrefixShortCode) // MatchError: NoPrefix() during macro expansion
[error]   |          ^^^^^^^^^^^^^^^^^
[error]   |Exception occurred while executing macro expansion.
[error]   |scala.MatchError: NoPrefix() (of class java.lang.String)
[error]   |	at scala.quoted.runtime.impl.printers.SourceCode$SourceCodePrinter.printType(SourceCode.scala:1251)
[error]   |	at scala.quoted.runtime.impl.printers.SourceCode$.showType(SourceCode.scala:13)
[error]   |	at scala.quoted.runtime.impl.QuotesImpl$$anon$16.show(QuotesImpl.scala:3184)
[error]   |	at scala.quoted.runtime.impl.QuotesImpl$$anon$16.show(QuotesImpl.scala:3183)
[error]   |	at defns$package$.noPrefixShortCodeImpl(defns.scala:19)
[error]   |
[error]   |-----------------------------------------------------------------------------
[error]   |Inline stack trace
[error]   |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]   |This location contains code that was inlined from defns.scala:4
[error] 4 |  ${ noPrefixShortCodeImpl }
[error]   |  ^^^^^^^^^^^^^^^^^^^^^^^^^^
[error]    -----------------------------------------------------------------------------
[error] -- Error: /Users/tomas/tmp/type-printer-match-error-NoPrefix/use.scala:4:10 ----
[error] 4 |  println(noPrefixCode)      // MatchError: NoPrefix() during macro expansion
[error]   |          ^^^^^^^^^^^^
[error]   |Exception occurred while executing macro expansion.
[error]   |scala.MatchError: NoPrefix() (of class java.lang.String)
[error]   |	at scala.quoted.runtime.impl.printers.SourceCode$SourceCodePrinter.printType(SourceCode.scala:1251)
[error]   |	at scala.quoted.runtime.impl.printers.SourceCode$.showType(SourceCode.scala:13)
[error]   |	at scala.quoted.runtime.impl.QuotesImpl$$anon$15.show(QuotesImpl.scala:3180)
[error]   |	at scala.quoted.runtime.impl.QuotesImpl$$anon$15.show(QuotesImpl.scala:3179)
[error]   |	at defns$package$.noPrefixCodeImpl(defns.scala:23)
[error]   |
[error]   |-----------------------------------------------------------------------------
[error]   |Inline stack trace
[error]   |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]   |This location contains code that was inlined from defns.scala:7
[error] 7 |  ${ noPrefixCodeImpl }
[error]   |  ^^^^^^^^^^^^^^^^^^^^^
[error]    -----------------------------------------------------------------------------
[error] two errors found
[error] (Compile / compileIncremental) Compilation failed

Expectation

Printing NoPrefix should either

  • return some stringy representation (like "<no-prefix>"); or
  • abort compilation with a useful error message, including the position in user code where the user is trying to print code of NoPrefix.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions