Skip to content
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

ClassCastException in Quotes' extractor #21616

Open
prolativ opened this issue Sep 19, 2024 · 0 comments
Open

ClassCastException in Quotes' extractor #21616

prolativ opened this issue Sep 19, 2024 · 0 comments
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug

Comments

@prolativ
Copy link
Contributor

Compiler version

3.6.0-RC1-bin-20240918-be10596-NIGHTLY and anything before

Minimized code

Macro.scala:

import scala.quoted.*

object Macro:
  inline def foo(inline str: String): Unit = ${ fooImpl('str) }

  def fooImpl(str: Expr[String])(using Quotes): Expr[Unit] =
    import quotes.reflect.*

    str match
      case Literal(_) => {}
      case _ => {}

    '{}

Main.scala:

@main def run(): Unit =
  Macro.foo("abcd")

Output

Compilation fails with:

[warn] ./Macro.scala:10:12
[warn] the type test for x$2.reflect.Literal cannot be checked at runtime because it refers to an abstract type member or type parameter
[warn]       case Literal(_) => {}
[warn]            ^
[error] ./Main.scala:2:3
[error] Exception occurred while executing macro expansion.
[error] java.lang.ClassCastException: class scala.quoted.runtime.impl.ExprImpl cannot be cast to class dotty.tools.dotc.ast.Trees$Literal (scala.quoted.runtime.impl.ExprImpl and dotty.tools.dotc.ast.Trees$Literal are in unnamed module of loader java.net.URLClassLoader @7f83ad80)
[error]         at scala.quoted.runtime.impl.QuotesImpl$reflect$Literal$.unapply(QuotesImpl.scala:572)
[error]         at Macro$.fooImpl(Macro.scala:10)
[error] 
[error]   Macro.foo("abcd")

Expectation

This code hides a user's error that might not be that easy to spot (str match instead of str.asTerm match), nevertheless this behaviour seems unexpected to me. IMO if case Literal(_) cannot be matched, there should be no exception and the fallback case _ branch of the match should be used and the entire compilation should succeed.

@prolativ prolativ added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 19, 2024
@Gedochao Gedochao added area:metaprogramming:quotes Issues related to quotes and splices and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug
Projects
None yet
Development

No branches or pull requests

2 participants