Skip to content

Commit 8210235

Browse files
committed
Helpful implicit not found message for Quotes
Fixes #16888
1 parent 7de74bd commit 8210235

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

library/src/scala/quoted/Quotes.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package scala.quoted
22

33
import scala.annotation.experimental
4+
import scala.annotation.implicitNotFound
45
import scala.reflect.TypeTest
56

67
/** Current Quotes in scope
@@ -21,7 +22,25 @@ transparent inline def quotes(using q: Quotes): q.type = q
2122
*
2223
* It contains the low-level Typed AST API metaprogramming API.
2324
* This API does not have the static type guarantees that `Expr` and `Type` provide.
25+
* `Quotes` are generated from an enclosing `${ ... }` or `scala.staging.run`. For example:
26+
* ```scala sc:nocompile
27+
* import scala.quoted._
28+
* inline def myMacro: Expr[T] =
29+
* ${ /* (quotes: Quotes) ?=> */ myExpr }
30+
* def myExpr(using Quotes): Expr[T] =
31+
* '{ f(${ /* (quotes: Quotes) ?=> */ myOtherExpr }) }
32+
* }
33+
* def myOtherExpr(using Quotes): Expr[U] = '{ ... }
34+
* ```
2435
*/
36+
37+
@implicitNotFound("""explain=Maybe this methods is missing a `(using Quotes)` parameter.
38+
39+
Maybe that splice `$ { ... }` is missing?
40+
Given instances of `Quotes` are generated from an enclosing splice `$ { ... }` (or `scala.staging.run` call).
41+
A splice can be thought as a method with the following signature.
42+
def $[T](body: Quotes ?=> Expr[T]): T
43+
""")
2544
trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
2645

2746
// Extension methods for `Expr[T]`
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- [E172] Type Error: tests/neg-custom-args/explain/i16888.scala:1:38 --------------------------------------------------
2+
1 |def test = summon[scala.quoted.Quotes] // error
3+
| ^
4+
| No given instance of type quoted.Quotes was found for parameter x of method summon in object Predef
5+
|---------------------------------------------------------------------------------------------------------------------
6+
| Explanation (enabled by `-explain`)
7+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8+
| Maybe this methods is missing a `(using Quotes)` parameter.
9+
|
10+
| Maybe that splice `$ { ... }` is missing?
11+
| Given instances of `Quotes` are generated from an enclosing splice `$ { ... }` (or `scala.staging.run` call).
12+
| A splice can be thought as a method with the following signature.
13+
| def $[T](body: Quotes ?=> Expr[T]): T
14+
---------------------------------------------------------------------------------------------------------------------
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def test = summon[scala.quoted.Quotes] // error

tests/neg-macros/i6436.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
5 | case '{ StringContext(${Varargs(parts)}*) } => // error
33
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44
| No given instance of type scala.quoted.Quotes was found
5+
|
6+
| longer explanation available when compiling with `-explain`
57
-- [E006] Not Found Error: tests/neg-macros/i6436.scala:6:34 -----------------------------------------------------------
68
6 | val ps: Seq[Expr[String]] = parts // error
79
| ^^^^^

0 commit comments

Comments
 (0)