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

HOAS quote pattern fails to match with def caprure #17105

Closed
nicolasstucki opened this issue Mar 15, 2023 · 3 comments · Fixed by #17567
Closed

HOAS quote pattern fails to match with def caprure #17105

nicolasstucki opened this issue Mar 15, 2023 · 3 comments · Fixed by #17567
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug

Comments

@nicolasstucki
Copy link
Contributor

nicolasstucki commented Mar 15, 2023

Compiler version

3.2

Minimized code

// Macro_1.scala
import scala.quoted.*

inline def test: String =  ${ testExpr }
def testExpr(using Quotes): Expr[String] =
  '{ def f(x: Int) = 1; f(3) } match
    case '{ def g(y: Int) = 1; $a(g): Int } =>  Expr(a.show)
    case _ => Expr("not matched")
// Test_2.scala
def app = test

Output

scalac -Xprint:inlining Macro_1.scala Test_2.scala

package <empty> {
  final lazy module val Test_2$package: Test_2$package = new Test_2$package()
  @SourceFile("t/Test_2.scala") final module class Test_2$package() extends
    Object() { this: Test_2$package.type =>
    private def writeReplace(): AnyRef =
      new scala.runtime.ModuleSerializationProxy(classOf[Test_2$package.type])
    def app: String = "not matched":String
  }
}

Expectation

The quote pattern should match and the result should be something like

    def app: String = "(g: Int => Int) => g(3)":String
@nicolasstucki nicolasstucki added itype:bug area:metaprogramming:quotes Issues related to quotes and splices labels Mar 15, 2023
@nicolasstucki
Copy link
Contributor Author

Note that '{ def f(x: Int) = 1; identity(3) } does match.

@nicolasstucki
Copy link
Contributor Author

This bug is probably in https://github.com/lampepfl/dotty/blob/main/compiler/src/scala/quoted/runtime/impl/QuoteMatcher.scala.

It might be related to the environment Env and ClosedPatternTerm.freePatternVars.

@nicolasstucki
Copy link
Contributor Author

Or the way we recover the symbol of the arguments. The reference to g in $a(g) might be eta-expanded and we might be accidentally taking the symbol of this new lambda.

https://github.com/lampepfl/dotty/blob/main/compiler/src/scala/quoted/runtime/impl/QuoteMatcher.scala#L210

zeptometer added a commit to zeptometer/dotty that referenced this issue Jun 19, 2023
zeptometer added a commit to zeptometer/dotty that referenced this issue Jun 20, 2023
zeptometer added a commit to zeptometer/dotty that referenced this issue Jun 20, 2023
nicolasstucki added a commit that referenced this issue Jun 21, 2023
This PR will fix #17105 by extracting symbols from eta-expanded
identifiers.

This fix enables the use of patterns such as
```scala
case '{ def f(...): T = ...; $g(f): U } => 
```
where `g` will match any expression that may contain references to `f`.
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.

1 participant