Skip to content

Commit

Permalink
Helpful implicit not found message for Quotes
Browse files Browse the repository at this point in the history
Fixes #16888
  • Loading branch information
nicolasstucki committed Feb 28, 2023
1 parent 7de74bd commit 8210235
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
19 changes: 19 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,25 @@ 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("""explain=Maybe this methods is missing a `(using Quotes)` parameter.
Maybe that splice `$ { ... }` is missing?
Given instances of `Quotes` are generated from an enclosing splice `$ { ... }` (or `scala.staging.run` call).
A splice can be thought as a method with the following signature.
def $[T](body: Quotes ?=> Expr[T]): T
""")
trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

// Extension methods for `Expr[T]`
Expand Down
14 changes: 14 additions & 0 deletions tests/neg-custom-args/explain/i16888.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- [E172] Type Error: tests/neg-custom-args/explain/i16888.scala:1:38 --------------------------------------------------
1 |def test = summon[scala.quoted.Quotes] // error
| ^
| No given instance of type quoted.Quotes was found for parameter x of method summon in object Predef
|---------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Maybe this methods is missing a `(using Quotes)` parameter.
|
| Maybe that splice `$ { ... }` is missing?
| Given instances of `Quotes` are generated from an enclosing splice `$ { ... }` (or `scala.staging.run` call).
| A splice can be thought as a method with the following signature.
| def $[T](body: Quotes ?=> Expr[T]): T
---------------------------------------------------------------------------------------------------------------------
1 change: 1 addition & 0 deletions tests/neg-custom-args/explain/i16888.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def test = summon[scala.quoted.Quotes] // error
2 changes: 2 additions & 0 deletions tests/neg-macros/i6436.check
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
5 | case '{ StringContext(${Varargs(parts)}*) } => // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| No given instance of type scala.quoted.Quotes was found
|
| longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: tests/neg-macros/i6436.scala:6:34 -----------------------------------------------------------
6 | val ps: Seq[Expr[String]] = parts // error
| ^^^^^
Expand Down

0 comments on commit 8210235

Please sign in to comment.