Skip to content

Commit b783d0b

Browse files
committed
Only allow bodyType on typed quotes
1 parent d59048c commit b783d0b

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,21 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
10581058
if tree.symbol.isTypeSplice then Some(tree.qualifier) else None
10591059
}
10601060

1061+
extension (tree: tpd.Quote)
1062+
/** Type of the quoted expression as seen from outside the quote */
1063+
def bodyType(using Context): Type =
1064+
val quoteType = tree.tpe // `Quotes ?=> Expr[T]` or `Quotes ?=> Type[T]`
1065+
val exprType = quoteType.argInfos.last // `Expr[T]` or `Type[T]`
1066+
exprType.argInfos.head // T
1067+
end extension
1068+
1069+
extension (tree: tpd.QuotePattern)
1070+
/** Type of the quoted pattern */
1071+
def bodyType(using Context): Type =
1072+
val quoteType = tree.tpe // `Expr[T]` or `Type[T]`
1073+
quoteType.argInfos.head // T
1074+
end extension
1075+
10611076
/** Extractor for not-null assertions.
10621077
* A not-null assertion for reference `x` has the form `x.$asInstanceOf$[x.type & T]`.
10631078
*/

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -704,12 +704,6 @@ object Trees {
704704
/** Is this a type quote `'[tpe]' */
705705
def isTypeQuote = body.isType
706706

707-
/** Type of the quoted expression as seen from outside the quote */
708-
def bodyType(using Context): Type =
709-
val quoteType = typeOpt // `Quotes ?=> Expr[T]` or `Quotes ?=> Type[T]`
710-
val exprType = quoteType.argInfos.last // `Expr[T]` or `Type[T]`
711-
exprType.argInfos.head // T
712-
713707
/** Set the type of the body of the quote */
714708
def withBodyType(tpe: Type)(using Context): Quote[Type] =
715709
val exprType = // `Expr[T]` or `Type[T]`
@@ -752,11 +746,6 @@ object Trees {
752746
case class QuotePattern[+T <: Untyped] private[ast] (bindings: List[Tree[T]], body: Tree[T], quotes: Tree[T])(implicit @constructorOnly src: SourceFile)
753747
extends PatternTree[T] {
754748
type ThisTree[+T <: Untyped] = QuotePattern[T]
755-
756-
/** Type of the quoted pattern */
757-
def bodyType(using Context): Type =
758-
val quoteType = typeOpt // `Expr[T]` or `Type[T]`
759-
quoteType.argInfos.head // T
760749
}
761750

762751
/** A tree representing a pattern splice `${ pattern }`, `$ident` or `$ident(args*)` in a quote pattern.

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
734734
keywordStr("macro ") ~ toTextGlobal(call)
735735
case tree @ Quote(body, tags) =>
736736
val tagsText = (keywordStr("<") ~ toTextGlobal(tags, ", ") ~ keywordStr(">")).provided(tree.tags.nonEmpty)
737-
val exprTypeText = (keywordStr("[") ~ toTextGlobal(tree.bodyType) ~ keywordStr("]")).provided(printDebug && tree.typeOpt.exists)
737+
val exprTypeText = (keywordStr("[") ~ toTextGlobal(tpd.bodyType(tree.asInstanceOf[tpd.Quote])) ~ keywordStr("]")).provided(printDebug && tree.typeOpt.exists)
738738
val open = if (body.isTerm) keywordStr("{") else keywordStr("[")
739739
val close = if (body.isTerm) keywordStr("}") else keywordStr("]")
740740
keywordStr("'") ~ tagsText ~ exprTypeText ~ open ~ toTextGlobal(body) ~ close

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class ReTyper(nestingLevel: Int = 0) extends Typer(nestingLevel) with ReChecking
9797

9898
override def typedQuote(tree: untpd.Quote, pt: Type)(using Context): Tree =
9999
assertTyped(tree)
100-
val body1 = typed(tree.body, tree.bodyType)(using quoteContext)
100+
val body1 = typed(tree.body, promote(tree).bodyType)(using quoteContext)
101101
for tag <- tree.tags do assertTyped(tag)
102102
untpd.cpy.Quote(tree)(body1, tree.tags).withType(tree.typeOpt)
103103

@@ -115,7 +115,7 @@ class ReTyper(nestingLevel: Int = 0) extends Typer(nestingLevel) with ReChecking
115115
assertTyped(tree)
116116
val bindings1 = tree.bindings.map(typed(_))
117117
val bodyCtx = quoteContext.retractMode(Mode.Pattern).addMode(Mode.QuotedPattern)
118-
val body1 = typed(tree.body, tree.bodyType)(using bodyCtx)
118+
val body1 = typed(tree.body, promote(tree).bodyType)(using bodyCtx)
119119
val quotes1 = typed(tree.quotes, defn.QuotesClass.typeRef)
120120
untpd.cpy.QuotePattern(tree)(bindings1, body1, quotes1).withType(tree.typeOpt)
121121

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ object QuotesImpl {
3838
}
3939

4040
class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler, QuoteMatching:
41+
import tpd.*
4142

4243
private val xCheckMacro: Boolean = ctx.settings.XcheckMacros.value
4344

0 commit comments

Comments
 (0)