Skip to content

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

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

Closed
TomasMikula opened this issue Mar 8, 2024 · 2 comments · Fixed by #19906
Closed

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

TomasMikula opened this issue Mar 8, 2024 · 2 comments · Fixed by #19906
Labels
area:metaprogramming:reflection Issues related to the quotes reflection API itype:enhancement
Milestone

Comments

@TomasMikula
Copy link
Contributor

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.
@TomasMikula TomasMikula added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 8, 2024
@TomasMikula TomasMikula changed the title MatchError: NoPrefix() when trying to print code of the NoPrefix() type MatchError: NoPrefix() when printing code of the NoPrefix() type Mar 8, 2024
@nicolasstucki
Copy link
Contributor

I would go against printing "<no-prefix>" because that would hide bugs for the user implementation and our library implementation.

A more useful error message seems to be the solution.

@nicolasstucki nicolasstucki added itype:enhancement area:metaprogramming:reflection Issues related to the quotes reflection API and removed itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 8, 2024
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Mar 8, 2024
@dwijnand
Copy link
Member

dwijnand commented Mar 9, 2024

I dunno, making a method take a Type and return a string, then typecheck and blow up at runtime because it's the "wrong kind of Type" also sounds like a good way to hide bugs. If we, hypothetically, end up with some library or user code that decomposes a type and constructs a string incorrectly, such as passing a NoPrefix prefix, then they'll realise if they ever care to look at the result, I would think.

@Kordyjan Kordyjan added this to the 3.4.2 milestone Mar 28, 2024
@Kordyjan Kordyjan modified the milestones: 3.4.2, 3.5.0 May 10, 2024
WojciechMazur pushed a commit that referenced this issue Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:reflection Issues related to the quotes reflection API itype:enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants