Skip to content

Commit f09a826

Browse files
committed
check literal pattern conformance
1 parent b6db698 commit f09a826

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
12161216
else tree1
12171217
}
12181218

1219+
def typedPatternLiteral(tree: untpd.Literal, pt: Type)(using Context): Tree = {
1220+
val tree1 = typedLiteral(tree)
1221+
1222+
if (!(tree1.tpe <:< pt))
1223+
report.error(TypeMismatch(tree1.tpe, pt, Some(tree1)), tree.srcPos)
1224+
tree1
1225+
else tree1
1226+
}
1227+
12191228
def typedNew(tree: untpd.New, pt: Type)(using Context): Tree =
12201229
tree.tpt match {
12211230
case templ: untpd.Template =>
@@ -3675,7 +3684,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
36753684
if (ctx.mode is Mode.Pattern) typedUnApply(tree, pt) else typedApply(tree, pt)
36763685
case tree: untpd.This => typedThis(tree)
36773686
case tree: untpd.Number => typedNumber(tree, pt)
3678-
case tree: untpd.Literal => typedLiteral(tree)
3687+
case tree: untpd.Literal =>
3688+
if (ctx.mode is Mode.Pattern) typedPatternLiteral(tree, pt) else typedLiteral(tree)
36793689
case tree: untpd.New => typedNew(tree, pt)
36803690
case tree: untpd.Typed => typedTyped(tree, pt)
36813691
case tree: untpd.NamedArg => typedNamedArg(tree, pt)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test:
2+
def bad: Unit =
3+
(BigInt(1): BigInt) match
4+
case 1 => () // error: .*BigInt
5+
case _ => ()

0 commit comments

Comments
 (0)