Skip to content

Type error with HOAS quote pattern with contextual method capture #17905

Closed
@zeptometer

Description

@zeptometer

Compiler version

3.2.2

Case 1: given outside quote

Minimized code

// Macro_1.scala
import scala.quoted.*

inline def testCtxParam(inline body: Any) = ${ testCtxParamImpl('body) }
def testCtxParamImpl(body: Expr[Any])(using Quotes): Expr[String] =
  body match
    case '{ def g(using s: String) = "placeholder"; $a(g): String } =>
      '{ $a((s: String) => s"(inside ${s})") }
    case _ => Expr("not matched")
// Test_2.scala
@main def Test: Unit =
  given String = "given"
  println(testCtxParam { def f(using t: String) = "placeholder"; f + " outside" })

Output

scalac Macro_1.scala Test_2.scala gives a type error

-- Error: macro_1.scala:6:56 ---------------------------------------------------
6 |    case '{ def g(using s: String) = "placeholder"; $a(g): String } =>
  |                                                        ^
  |    No given instance of type String was found for parameter s of method g
1 error found

Expectation

Compile succeeds and scala Test outputs (inside given) outside

Case 2: given inside quote

Minimized code

// Macro_1.scala
import scala.quoted.*

inline def testCtxParam(inline body: Any) = ${ testCtxParamImpl('body) }
def testCtxParamImpl(body: Expr[Any])(using Quotes): Expr[String] =
  body match
    case '{ given i:String="given"; def g(using s: String) = "placeholder"; $a(g, i): String } =>
      '{ $a((s: String) => s"(inside ${s})", "given2") }
    case _ => Expr("not matched")
// Test_2.scala
@main def Test: Unit =
  given String = "given"
  println(testCtxParam { given String="given"; def f(using t: String) = "placeholder"; f + " outside" })

Output

scalac Macro_1.scala Test_2.scala gives a type error

-- [E007] Type Mismatch Error: macro_1.scala:7:12 ------------------------------
7 |      '{ $a((s: String) => s"(inside ${s})", "given2") }
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |            Found:    String => String
  |            Required: String
  |
  | longer explanation available when compiling with `-explain`
1 error found

Expectation

Compile succeeds and scala Test outputs (inside given2) outside

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions