Skip to content

Commit 58d5dca

Browse files
Merge pull request #6326 from dotty-staging/fix-#6325
Fix #6325: Propagate error type to internal splice
2 parents f09550d + c5251ea commit 58d5dca

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,9 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
11811181
/** An extractor for typed splices */
11821182
object Splice {
11831183
def apply(tree: Tree)(implicit ctx: Context): Tree = {
1184-
val argType = tree.tpe.baseType(defn.QuotedExprClass).argTypesHi.head
1184+
val argType =
1185+
if (tree.tpe.widen.isError) tree.tpe.widen
1186+
else tree.tpe.baseType(defn.QuotedExprClass).argTypesHi.head
11851187
ref(defn.InternalQuoted_exprSplice).appliedToType(argType).appliedTo(tree)
11861188
}
11871189
def unapply(tree: Tree)(implicit ctx: Context): Option[Tree] = tree match {

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,12 +2011,16 @@ class Typer extends Namer
20112011
typed(innerExpr, pt)
20122012
case expr =>
20132013
if (ctx.mode.is(Mode.QuotedPattern) && level == 1) {
2014-
fullyDefinedType(pt, "quoted pattern selector", tree.span)
2015-
def spliceOwner(ctx: Context): Symbol =
2016-
if (ctx.mode.is(Mode.QuotedPattern)) spliceOwner(ctx.outer) else ctx.owner
2017-
val pat = typedPattern(expr, defn.QuotedExprType.appliedTo(pt))(
2018-
spliceContext.retractMode(Mode.QuotedPattern).withOwner(spliceOwner(ctx)))
2019-
Splice(pat)
2014+
if (isFullyDefined(pt, ForceDegree.all)) {
2015+
def spliceOwner(ctx: Context): Symbol =
2016+
if (ctx.mode.is(Mode.QuotedPattern)) spliceOwner(ctx.outer) else ctx.owner
2017+
val pat = typedPattern(expr, defn.QuotedExprType.appliedTo(pt))(
2018+
spliceContext.retractMode(Mode.QuotedPattern).withOwner(spliceOwner(ctx)))
2019+
Splice(pat)
2020+
} else {
2021+
ctx.error(i"Type must be fully defined.\nConsider annotating the splice using a type ascription:\n ($tree: XYZ).", expr.sourcePos)
2022+
tree.withType(UnspecifiedErrorType)
2023+
}
20202024
}
20212025
else
20222026
typedApply(untpd.Apply(untpd.ref(defn.InternalQuoted_exprSpliceR), tree.expr), pt)(spliceContext).withSpan(tree.span)

tests/neg/i6324.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Test {
2+
def res(x: quoted.Expr[Int]) given tasty.Reflection: quoted.Expr[Int] = x match {
3+
case '{ 1 + $b } => // error: Type must be fully defined. Consider annotating the splice using a type ascription: (${b}: XYZ).
4+
b // error: Not found: b
5+
}
6+
}

tests/neg/i6325.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//import scala.quoted.matching.Bind
2+
object Test {
3+
def res(x: quoted.Expr[Int]) given tasty.Reflection: quoted.Expr[Int] = x match {
4+
case '{ 1 + (${Bind(b)}: Int) } => ??? // error: Not found: Bind
5+
case _ => ???
6+
}
7+
}

0 commit comments

Comments
 (0)