Skip to content

Macro example code not compiling: "Recovering precise types using patterns"  #10308

Closed
@hmf

Description

@hmf

Minimized example

The example code for "Recovering precise types using patterns" does not compile. Here is the code:

  extension (inline sc: StringContext) inline def showMe(inline args: Any*): String = ${ showMeExpr('sc, 'args) }

  private def showMeExpr(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using QuoteContext): Expr[String] = {
    argsExpr match {
      case Varargs(argExprs) =>
        val argShowedExprs = argExprs.map {
          case '{ $arg: t } =>
          Expr.summon[Show[t]] match {
            case Some(showExpr) => '{ $showExpr.show($arg) }
            case None => Reporting.error(s"could not find implicit for ${showTp.show}", arg); '{???}
          }
        }
        val newArgsExpr = Varargs(argShowedExprs)
        '{ $sc.s($newArgsExpr: _*) }
      case _ =>
        // `new StringContext(...).showMeExpr(args: _*)` not an explicit `showMeExpr"..."`
        Reporting.error(s"Args must be explicit", argsExpr)
        '{???}
    }
  }

  trait Show[-T] {
    def show(x: T): String
  }

Output

[error] -- [E006] Not Found Error: RecordMacro.scala:400:24 
[error] 400 |          case '{ $arg: t } =>
[error]     |                        ^
[error]     |                        Not found: type t
[error] -- [E006] Not Found Error: RecordMacro.scala:401:27 
[error] 401 |          Expr.summon[Show[t]] match {
[error]     |                           ^
[error]     |                           Not found: type t
[error] -- [E006] Not Found Error: RecordMacro.scala:403:25 
[error] 403 |            case None => Reporting.error(s"could not find implicit for ${showTp.show}", arg); '{???}
[error]     |                         ^^^^^^^^^
[error]     |                         Not found: Reporting
[error] -- [E006] Not Found Error: RecordMacro.scala:410:8 
[error] 410 |        Reporting.error(s"Args must be explicit", argsExpr)
[error]     |        ^^^^^^^^^
[error]     |        Not found: Reporting
[error] four errors found
1 targets failed
dcase2020.compile Compilation failed

Expectation

Expected it to compile. Tested with 3.0.0-M1. With the changes below I am left with the error related to the t type variable.

  extension (inline sc: StringContext) inline def showMe(inline args: Any*): String = ${ showMeExpr('sc, 'args) }

  private def showMeExpr(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using QuoteContext): Expr[String] = {
    argsExpr match {
      case Varargs(argExprs) =>
        val argShowedExprs = argExprs.map {
          case '{ $arg: t } =>
          Expr.summon[Show[t]] match {
            case Some(showExpr) => '{ $showExpr.show($arg) }
            case None => report.error(s"could not find implicit for ${arg.show}", arg); '{???}
          }
        }
        val newArgsExpr = Varargs(argShowedExprs)
        '{ $sc.s($newArgsExpr: _*) }
      case _ =>
        // `new StringContext(...).showMeExpr(args: _*)` not an explicit `showMeExpr"..."`
        report.error(s"Args must be explicit", argsExpr)
        '{???}
    }
  }

  trait Show[-T] {
    def show(x: T): String
  }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions