Skip to content

Commit 3f1cc4d

Browse files
committed
Helpful implicit not found message for quotes.Quotes
Fixes #16888
1 parent f313d16 commit 3f1cc4d

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

library/src/scala/quoted/Quotes.scala

Lines changed: 17 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,23 @@ 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+
*
26+
* `Quotes` are generated from an enclosing `${ ... }` or `scala.staging.run`. For example:
27+
* ```scala
28+
* import scala.quoted._
29+
* inline def myMacro: Expr[T] =
30+
* ${ /* (quotes: Quotes) ?=> */ myExpr }
31+
* def myExpr(using Quotes): Expr[T] =
32+
* '{ f(${ /* (quotes: Quotes) ?=> */ myOtherExpr }) }
33+
* }
34+
* def myOtherExpr(using Quotes): Expr[U] = '{ ... }
35+
* ```
2436
*/
37+
@implicitNotFound("""No given instance of type scala.quoted.Quotes was found.
38+
39+
Quotes are generated from an enclosing splice `$ { ... }` or `scala.staging.run` call.
40+
Maybe that splice `$ { ... }` or `scala.staging.run` is missing?
41+
""")
2542
trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
2643

2744
// Extension methods for `Expr[T]`

tests/neg-macros/i16888.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- [E172] Type Error: tests/neg-macros/i16888.scala:1:38 ---------------------------------------------------------------
2+
1 |def test = summon[scala.quoted.Quotes] // error
3+
| ^
4+
| No given instance of type scala.quoted.Quotes was found.
5+
|
6+
| Quotes are generated from an enclosing splice `$ { ... }` or `scala.staging.run` call.
7+
| Maybe that splice `$ { ... }` or `scala.staging.run` is missing?

tests/neg-macros/i16888.scala

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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
-- [E172] Type Error: tests/neg-macros/i6436.scala:5:9 -----------------------------------------------------------------
22
5 | case '{ StringContext(${Varargs(parts)}*) } => // error
33
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4-
| No given instance of type scala.quoted.Quotes was found
4+
| No given instance of type scala.quoted.Quotes was found.
5+
|
6+
| Quotes are generated from an enclosing splice `$ { ... }` or `scala.staging.run` call.
7+
| Maybe that splice `$ { ... }` or `scala.staging.run` is missing?
58
-- [E006] Not Found Error: tests/neg-macros/i6436.scala:6:34 -----------------------------------------------------------
69
6 | val ps: Seq[Expr[String]] = parts // error
710
| ^^^^^

0 commit comments

Comments
 (0)