Skip to content

Commit fffa86d

Browse files
committed
Fix Typed abstraction
The typed expression can contain non-Term trees such as Wildcard and Alternatives. The sulution is to type the expression of Typed as a Tree and not a Term. This comes with some complications at the user site where we now need to use `case Typed(expr: Term, _) =>`.
1 parent e5c3258 commit fffa86d

File tree

19 files changed

+50
-35
lines changed

19 files changed

+50
-35
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ object Matcher {
178178

179179
/* Term hole */
180180
// Match a scala.internal.Quoted.patternHole typed as a repeated argument and return the scrutinee tree
181-
case (scrutinee @ Typed(s, tpt1), Typed(TypeApply(patternHole, tpt :: Nil), tpt2))
181+
case (scrutinee @ Typed(s: Term, tpt1), Typed(TypeApply(patternHole, tpt :: Nil), tpt2))
182182
if patternHole.symbol == patternHoleSymbol &&
183183
s.tpe <:< tpt.tpe &&
184184
tpt2.tpe.derivesFrom(defn.RepeatedParamClass) =>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
188188
def unapply(x: Tree): Option[Statement & x.type] = x match
189189
case _: tpd.PatternTree => None
190190
case _ =>
191-
if x.isTerm then TermTypeTest.unapply(x)
192-
else DefinitionTypeTest.unapply(x)
191+
TermTypeTest.unapply(x).orElse(DefinitionTypeTest.unapply(x))
193192
end StatementTypeTest
194193

195194
type Definition = tpd.MemberDef

compiler/src/scala/quoted/runtime/impl/printers/Extractors.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ object Extractors {
172172
case Unapply(fun, implicits, patterns) =>
173173
this += "Unapply(" += fun += ", " ++= implicits += ", " ++= patterns += ")"
174174
case Alternatives(patterns) =>
175-
this += "Alternative(" ++= patterns += ")"
175+
this += "Alternatives(" ++= patterns += ")"
176176
}
177177

178178
def visitConstant(x: Constant): this.type = x match {

compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ object SourceCode {
428428
inSquare(this += id)
429429
this
430430

431-
case Typed(term, tpt) =>
431+
case Typed(term: Term, tpt) =>
432432
tpt.tpe match {
433433
case Types.Repeated(_) =>
434434
printTree(term)

library/src/scala/quoted/ExprMap.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ trait ExprMap:
5656
tree
5757
case New(tpt) =>
5858
New.copy(tree)(transformTypeTree(tpt)(owner))
59-
case Typed(expr, tpt) =>
59+
case Typed(expr: Term, tpt) =>
6060
val tp = tpt.tpe match
6161
case AppliedType(TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "<repeated>"), List(tp0: TypeRepr)) =>
6262
TypeRepr.of[Seq].appliedTo(tp0)

library/src/scala/quoted/FromExpr.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ object FromExpr {
8686
def rec(tree: Term): Option[T] = tree match {
8787
case Block(stats, e) => if stats.isEmpty then rec(e) else None
8888
case Inlined(_, bindings, e) => if bindings.isEmpty then rec(e) else None
89-
case Typed(e, _) => rec(e)
89+
case Typed(e: Term, _) => rec(e)
9090
case _ =>
9191
tree.tpe.widenTermRefByName match
9292
case ConstantType(c) => Some(c.value.asInstanceOf[T])

library/src/scala/quoted/Quotes.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,13 +1101,13 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
11011101
/** Methods of the module object `val Typed` */
11021102
trait TypedModule { this: Typed.type =>
11031103

1104-
/** Create a type ascription `<x: Term>: <tpt: TypeTree>` */
1105-
def apply(expr: Term, tpt: TypeTree): Typed
1104+
/** Create a type ascription `<x: Tree>: <tpt: TypeTree>` */
1105+
def apply(expr: Tree, tpt: TypeTree): Typed
11061106

1107-
def copy(original: Tree)(expr: Term, tpt: TypeTree): Typed
1107+
def copy(original: Tree)(expr: Tree, tpt: TypeTree): Typed
11081108

1109-
/** Matches `<expr: Term>: <tpt: TypeTree>` */
1110-
def unapply(x: Typed): (Term, TypeTree)
1109+
/** Matches `<expr: Tree>: <tpt: TypeTree>` */
1110+
def unapply(x: Typed): (Tree, TypeTree)
11111111
}
11121112

11131113
/** Makes extension methods on `Typed` available without any imports */
@@ -1116,7 +1116,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
11161116
/** Extension methods of `Typed` */
11171117
trait TypedMethods:
11181118
extension (self: Typed)
1119-
def expr: Term
1119+
def expr: Tree
11201120
def tpt: TypeTree
11211121
end extension
11221122
end TypedMethods
@@ -4377,7 +4377,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
43774377
case New(tpt) =>
43784378
New.copy(tree)(transformTypeTree(tpt)(owner))
43794379
case Typed(expr, tpt) =>
4380-
Typed.copy(tree)(transformTerm(expr)(owner), transformTypeTree(tpt)(owner))
4380+
Typed.copy(tree)(transformTree(expr)(owner), transformTypeTree(tpt)(owner))
43814381
case tree: NamedArg =>
43824382
NamedArg.copy(tree)(tree.name, transformTerm(tree.value)(owner))
43834383
case Assign(lhs, rhs) =>

library/src/scala/quoted/Varargs.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object Varargs {
4242
import quotes.reflect._
4343
def rec(tree: Term): Option[Seq[Expr[T]]] = tree match {
4444
case Repeated(elems, _) => Some(elems.map(x => x.asExpr.asInstanceOf[Expr[T]]))
45-
case Typed(e, _) => rec(e)
45+
case Typed(e: Term, _) => rec(e)
4646
case Block(Nil, e) => rec(e)
4747
case Inlined(_, Nil, e) => rec(e)
4848
case _ => None

scaladoc/src/dotty/tools/scaladoc/tasty/BasicSupport.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ trait BasicSupport:
1616
val dri = annotTerm.tpe.typeSymbol.dri
1717
def inner(t: Term): List[Annotation.AnnotationParameter] = t match {
1818
case i: Ident => List(Annotation.LinkParameter(None, i.tpe.typeSymbol.dri, i.name))
19-
case Typed(term, tpeTree) => inner(term)
19+
case Typed(term: Term, tpeTree) => inner(term)
2020
case SeqLiteral(args, tpeTree) => args.map(_.asInstanceOf[Term]).flatMap(inner)
2121
case Literal(constant) => List(Annotation.PrimitiveParameter(None, constant.show))
2222
case NamedArg(name, Literal(constant)) => List(Annotation.PrimitiveParameter(Some(name), constant.show))

tests/neg-macros/i11483/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ object X {
5151
)
5252
case Apply(f,List()) =>
5353
Apply(TypeApply(Select.unique(m,"pure"),List(Inferred(t.tpe.widen))),List(t))
54-
case Typed(x,tp) => Typed(processTree(x,m), Inferred(TypeRepr.of[F].appliedTo(tp.tpe)) )
54+
case Typed(x: Term,tp) => Typed(processTree(x,m), Inferred(TypeRepr.of[F].appliedTo(tp.tpe)) )
5555
case _ => throw new RuntimeException(s"tree not recoginized: $t")
5656
r
5757

0 commit comments

Comments
 (0)