Skip to content

Helpful implicit not found message for quotes.Quotes #16914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scala.quoted

import scala.annotation.experimental
import scala.annotation.implicitNotFound
import scala.reflect.TypeTest

/** Current Quotes in scope
Expand All @@ -21,7 +22,23 @@ transparent inline def quotes(using q: Quotes): q.type = q
*
* It contains the low-level Typed AST API metaprogramming API.
* This API does not have the static type guarantees that `Expr` and `Type` provide.
*
* `Quotes` are generated from an enclosing `${ ... }` or `scala.staging.run`. For example:
* ```scala sc:nocompile
* import scala.quoted._
* inline def myMacro: Expr[T] =
* ${ /* (quotes: Quotes) ?=> */ myExpr }
* def myExpr(using Quotes): Expr[T] =
* '{ f(${ /* (quotes: Quotes) ?=> */ myOtherExpr }) }
* }
* def myOtherExpr(using Quotes): Expr[U] = '{ ... }
* ```
*/
@implicitNotFound("""No given instance of type scala.quoted.Quotes was found.

Quotes are generated from an enclosing splice `$ { ... }` or `scala.staging.run` call.
Maybe that splice `$ { ... }` or `scala.staging.run` is missing?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to use $ { because I could not find a way to write ${ within the message of implicitNotFound. It assumes that ${ is the start of a type interpolation. Escape \,$ characters or escape Unicode do not work because the string is interpreted raw.

""")
trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

// Extension methods for `Expr[T]`
Expand Down
7 changes: 7 additions & 0 deletions tests/neg-macros/i16888.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- [E172] Type Error: tests/neg-macros/i16888.scala:1:38 ---------------------------------------------------------------
1 |def test = summon[scala.quoted.Quotes] // error
| ^
| No given instance of type scala.quoted.Quotes was found.
|
| Quotes are generated from an enclosing splice `$ { ... }` or `scala.staging.run` call.
| Maybe that splice `$ { ... }` or `scala.staging.run` is missing?
1 change: 1 addition & 0 deletions tests/neg-macros/i16888.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def test = summon[scala.quoted.Quotes] // error
5 changes: 4 additions & 1 deletion tests/neg-macros/i6436.check
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
-- [E172] Type Error: tests/neg-macros/i6436.scala:5:9 -----------------------------------------------------------------
5 | case '{ StringContext(${Varargs(parts)}*) } => // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| No given instance of type scala.quoted.Quotes was found
| No given instance of type scala.quoted.Quotes was found.
|
| Quotes are generated from an enclosing splice `$ { ... }` or `scala.staging.run` call.
| Maybe that splice `$ { ... }` or `scala.staging.run` is missing?
-- [E006] Not Found Error: tests/neg-macros/i6436.scala:6:34 -----------------------------------------------------------
6 | val ps: Seq[Expr[String]] = parts // error
| ^^^^^
Expand Down