Skip to content

Commit d543a89

Browse files
committed
Warn users on non canonical type references
Help users write the correct variant of the quoted type encoding. Explicit construction of a `scala.quoted.Type[_]` should be done with its constructor. Explicit construction of types in not encouraged as it should be created implicitly. Creating an `Type` explicitly is only useful for debugging purposes. Likewise, we encourage the use of the explicit inner type reference.
1 parent 972ae73 commit d543a89

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ trait QuotesAndSplices {
5757
val tree1 =
5858
if ctx.mode.is(Mode.Pattern) then
5959
typedQuotePattern(tree, pt, qctx)
60-
else if (tree.quoted.isType)
60+
else if tree.quoted.isType then
61+
report.warning(em"Consider using canonical type constructor scala.quoted.Type[${tree.quoted}] instead", tree.srcPos)
6162
typedTypeApply(untpd.TypeApply(untpd.ref(defn.QuotedTypeModule_apply.termRef), tree.quoted :: Nil), pt)(using quoteContext).select(nme.apply).appliedTo(qctx)
6263
else
6364
typedApply(untpd.Apply(untpd.ref(defn.InternalQuoted_exprQuote.termRef), tree.quoted), pt)(using pushQuoteContext(qctx)).select(nme.apply).appliedTo(qctx)
@@ -171,7 +172,9 @@ trait QuotesAndSplices {
171172
using spliceContext.retractMode(Mode.QuotedPattern).withOwner(spliceOwner(ctx)))
172173
pat.select(tpnme.spliceType)
173174
else
174-
typedSelect(untpd.Select(tree.expr, tpnme.spliceType), pt)(using spliceContext).withSpan(tree.span)
175+
val tree1 = typedSelect(untpd.Select(tree.expr, tpnme.spliceType), pt)(using spliceContext).withSpan(tree.span)
176+
report.warning(em"Consider using canonical type reference ${tree1.tpe.show} instead", tree.srcPos)
177+
tree1
175178
}
176179

177180
private def checkSpliceOutsideQuote(tree: untpd.Tree)(using Context): Unit =

tests/patmat/i9489.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import scala.quoted._
22

3-
def summonTypedType[T : Type](using QuoteContext): String = '[T] match {
3+
def summonTypedType[T : Type](using QuoteContext): String = Type[T] match {
44
case '[Boolean] => "Boolean"
55
case '[Byte] => "Byte"
66
case _ => "Other"

0 commit comments

Comments
 (0)