Skip to content

Commit cb80376

Browse files
committed
Fix scala#7264: Propagate prototypes in quoted type patterns
1 parent 5e47774 commit cb80376

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ trait QuotesAndSplices {
9898
case _ =>
9999
}
100100
if (ctx.mode.is(Mode.QuotedPattern) && level == 1)
101-
if (isFullyDefined(pt, ForceDegree.all)) {
101+
if (false && isFullyDefined(pt, ForceDegree.all)) {
102102
ctx.error(i"Spliced type pattern must not be fully defined. Consider using $pt directly", tree.expr.sourcePos)
103103
tree.withType(UnspecifiedErrorType)
104104
}
@@ -111,7 +111,10 @@ trait QuotesAndSplices {
111111
case Bind(name, _) => ("$" + name).toTypeName
112112
case _ => NameKinds.UniqueName.fresh("$".toTypeName)
113113
}
114-
val typeSym = ctx.newSymbol(spliceOwner(ctx), name, EmptyFlags, TypeBounds.empty, NoSymbol, tree.expr.span)
114+
val typeSymInfo = pt match
115+
case pt: TypeBounds => pt
116+
case _ => TypeBounds.empty
117+
val typeSym = ctx.newSymbol(spliceOwner(ctx), name, EmptyFlags, typeSymInfo, NoSymbol, tree.expr.span)
115118
typeSym.addAnnotation(Annotation(New(ref(defn.InternalQuoted_patternBindHoleAnnot.typeRef)).withSpan(tree.expr.span)))
116119
val pat = typedPattern(tree.expr, defn.QuotedTypeClass.typeRef.appliedTo(typeSym.typeRef))(
117120
spliceContext.retractMode(Mode.QuotedPattern).withOwner(spliceOwner(ctx)))
@@ -318,13 +321,16 @@ trait QuotesAndSplices {
318321
ctx.error(missingArgMsg(qctx, defn.QuoteContextClass.typeRef, ""), ctx.source.atSpan(tree.span))
319322

320323
val quoted = tree.quoted
321-
val exprPt = pt.baseType(defn.QuotedExprClass)
324+
val exprPt = pt.baseType(if quoted.isType then defn.QuotedTypeClass else defn.QuotedExprClass)
322325
val quotedPt = exprPt.argInfos.headOption match {
323326
case Some(argPt: ValueType) => argPt // excludes TypeBounds
324327
case _ => defn.AnyType
325328
}
326329
val quoted0 = desugar.quotedPattern(quoted, untpd.TypedSplice(TypeTree(quotedPt)))
327-
val quoted1 = typedExpr(quoted0, WildcardType)(quoteContext.addMode(Mode.QuotedPattern))
330+
val quoteCtx = quoteContext.addMode(Mode.QuotedPattern)
331+
val quoted1 =
332+
if quoted.isType then typedType(quoted0, quotedPt)(quoteCtx)
333+
else typedExpr(quoted0, quotedPt)(quoteCtx)
328334

329335
val (typeBindings, shape, splices) = splitQuotePattern(quoted1)
330336

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,7 @@ class Typer extends Namer
13891389
def typedArg(arg: untpd.Tree, tparam: ParamInfo) = {
13901390
def tparamBounds = tparam.paramInfoAsSeenFrom(tpt1.tpe.appliedTo(tparams.map(_ => TypeBounds.empty)))
13911391
val (desugaredArg, argPt) =
1392-
if (ctx.mode is Mode.Pattern)
1392+
if ctx.mode.is(Mode.Pattern) || ctx.mode.is(Mode.QuotedPattern) then
13931393
(if (untpd.isVarPattern(arg)) desugar.patternVar(arg) else arg, tparamBounds)
13941394
else
13951395
(arg, WildcardType)
@@ -2177,7 +2177,7 @@ class Typer extends Namer
21772177

21782178
/** Typecheck and adapt tree, returning a typed tree. Parameters as for `typedUnadapted` */
21792179
def typed(tree: untpd.Tree, pt: Type, locked: TypeVars)(implicit ctx: Context): Tree =
2180-
trace(i"typing $tree", typr, show = true) {
2180+
trace(i"typing $tree, pt = $pt", typr, show = true) {
21812181
record(s"typed $getClass")
21822182
record("typed total")
21832183
assertPositioned(tree)

tests/pos/i7264.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import scala.quoted._
2+
class Foo {
3+
def f[T2](t: Type[T2])(given QuoteContext) = t match {
4+
case '[ *:[Int, $t] ] =>
5+
}
6+
}

0 commit comments

Comments
 (0)