Skip to content

Commit

Permalink
Fix HOAS pattern example and error message (#19655)
Browse files Browse the repository at this point in the history
Fixes #19342
  • Loading branch information
nicolasstucki authored Feb 9, 2024
2 parents 2c0b021 + d332cb0 commit 9bf429d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
val spliceTypeText = (keywordStr("[") ~ toTextGlobal(tree.typeOpt) ~ keywordStr("]")).provided(printDebug && tree.typeOpt.exists)
keywordStr("$") ~ spliceTypeText ~ {
if args.isEmpty then keywordStr("{") ~ inPattern(toText(pattern)) ~ keywordStr("}")
else toText(pattern.symbol.name) ~ "(" ~ toTextGlobal(args, ", ") ~ ")"
else toText(pattern) ~ "(" ~ toTextGlobal(args, ", ") ~ ")"
}
case Hole(isTerm, idx, args, content) =>
val (prefix, postfix) = if isTerm then ("{{{", "}}}") else ("[[[", "]]]")
Expand Down
6 changes: 3 additions & 3 deletions docs/_docs/reference/metaprogramming/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,10 @@ The lambda arguments will replace the variables that might have been extruded.

```scala
'{ ((x: Int) => x + 1).apply(2) } match
case '{ ((y: Int) => $f(y)).apply($z: Int) } =>
case '{ ((y: Int) => $f(y): Int).apply($z: Int) } =>
// f may contain references to `x` (replaced by `$y`)
// f = (y: Expr[Int]) => '{ $y + 1 }
f(z) // generates '{ 2 + 1 }
// f = '{ (y: Int) => $y + 1 }
Expr.betaReduce('{ $f($z)}) // generates '{ 2 + 1 }
```


Expand Down
6 changes: 6 additions & 0 deletions tests/neg-macros/i19342.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Error: tests/neg-macros/i19342.scala:5:26 ---------------------------------------------------------------------------
5 | case '{ ((y: Int) => $f(y)).apply($z: Int) } => () // error
| ^
| Type must be fully defined.
| Consider annotating the splice using a type ascription:
| ($f(y): XYZ).
7 changes: 7 additions & 0 deletions tests/neg-macros/i19342.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.quoted.*

def scrutinizeHoas(expr: Expr[Int])(using Quotes): Unit =
expr match {
case '{ ((y: Int) => $f(y)).apply($z: Int) } => () // error
case _ => ()
}

0 comments on commit 9bf429d

Please sign in to comment.