Skip to content

Commit

Permalink
Backport "Fix HOAS pattern example and error message" to LTS (#20891)
Browse files Browse the repository at this point in the history
Backports #19655 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Jul 1, 2024
2 parents 95b8e13 + f638f7d commit 15e2ecd
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 @@ -743,7 +743,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 15e2ecd

Please sign in to comment.