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

Package private method cannot be matched using quotes API #15676

Open
WojciechMazur opened this issue Jul 14, 2022 · 4 comments
Open

Package private method cannot be matched using quotes API #15676

WojciechMazur opened this issue Jul 14, 2022 · 4 comments
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug

Comments

@WojciechMazur
Copy link
Contributor

WojciechMazur commented Jul 14, 2022

Community build regression for https://github.com/ciaraobrien/dottytags (maintainer @CiaraOBrien)
Based on Open Community Build #1858
Part of #15647 regressions tracker

Compiler version

Works with 3.1.0
Fails in 3.1.1
Fails in 3.2.1-RC1-bin-20220712-7e20b81-NIGHTLYon` instead).

Minimized code

package dottytags

import scala.quoted.*

class TagClass private[dottytags] (val name: String)
private[dottytags] object TagClass:
  def apply(name: String): TagClass = new TagClass(name)

inline def tag(inline name: String): TagClass = ${ tagClassMacro('name) }
private def tagClassMacro(nameExpr: Expr[String])(using Quotes): Expr[TagClass] = {
  val tgExpr = Expr(TagClass(nameExpr.value.get))
  TagClassFromExpr.unapply(tgExpr).foreach(println) // To trigger failure
  tgExpr
}

private given TagClassFromExpr: FromExpr[TagClass] with
  def unapply(x: Expr[TagClass])(using Quotes): Option[TagClass] =
    x match
      case '{ TagClass(${ Expr(name: String) }) } =>
        println(s"in apply, name=$name)")
        Some(TagClass(name))
      case _ =>
        import quotes.reflect.report
        report.error(s"Failed to extract value from ${x.show}")
        None

private given TagClassToExpr: ToExpr[TagClass] with
  def apply(x: TagClass)(using Quotes): Expr[TagClass] = '{ TagClass(${ Expr(x.name) }) }
// usage.scala
import dottytags.*
@main def Test() = println(tag("html"))

Output

-- Error: test.scala:4:13 -------------------------------------------------------------------------------------------------------
4 |  println(tag("html"))
  |          ^^^^^^^^^^^
  |          Failed to extract value from dottytags.macro$package.inline$TagClass.apply("html")
1 error found

Expectation

Compiles with the same output as in 3.1.0:

in apply, name=html)
dottytags.TagClass@5b3a8bea
@WojciechMazur WojciechMazur added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 14, 2022
@mbovel mbovel 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 Jul 15, 2022
@mbovel
Copy link
Member

mbovel commented Jul 15, 2022

How can I reproduce?

I tried sbt "scalac test.scala" on main, scala-cli compile -S 3.2.1-RC1-bin-20220712-7e20b81-NIGHTLY test.scala and scala-cli compile -S 3.1.1 test.scala but all compile successfully. Do I need to set additional options?

@WojciechMazur
Copy link
Contributor Author

@mbovel Sorry, I forget to include the second file that actually uses the macro. I've updated the issue reproducer

@KacperFKorban
Copy link
Member

I managed to bisect it and it looks like the first bad commit is: 0b072d6

I checked and it seems that adding makeInlineable here fixes the issue and doesn't break the compilation for the snippet from #13546. And I don't know why this change was introduced, since the PR only added one test case.

@anatoliykmetyuk anatoliykmetyuk added this to the 3.2.0 backports milestone Aug 8, 2022
@nicolasstucki
Copy link
Contributor

Minimized

package foo
import scala.quoted.*

private[foo] object Foo:
  def apply(): Int = ???

inline def test(): Unit = ${ testExpr() }

private def testExpr()(using Quotes): Expr[Unit] = {
  '{ Foo() } match
    case '{ Foo() } =>
  '{}
}
// usage.scala
def Test() = foo.test()

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

Successfully merging a pull request may close this issue.

6 participants