Skip to content

Commit 3b0afb3

Browse files
oderskynicolasstucki
authored andcommitted
Fix #7264: Propagate prototypes in quoted type patterns
1 parent af6823e commit 3b0afb3

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
ctx.error("expected a name binding", expr.sourcePos)
112112
"$error".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)))
@@ -317,13 +320,16 @@ trait QuotesAndSplices {
317320
ctx.error(missingArgMsg(qctx, defn.QuoteContextClass.typeRef, ""), ctx.source.atSpan(tree.span))
318321

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

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

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)