Skip to content

Commit c53cc91

Browse files
Merge pull request #12902 from tgodzik/fix-12715
Do no widen constant type on selector type in inline matches
2 parents 1578fa7 + 4bb42ac commit c53cc91

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,8 +1478,10 @@ class Typer extends Namer
14781478
case _ =>
14791479
if tree.isInline then checkInInlineContext("inline match", tree.srcPos)
14801480
val sel1 = typedExpr(tree.selector)
1481-
val selType = fullyDefinedType(sel1.tpe, "pattern selector", tree.span).widen
1482-
1481+
val rawSelectorTpe = fullyDefinedType(sel1.tpe, "pattern selector", tree.span)
1482+
val selType = rawSelectorTpe match
1483+
case c: ConstantType if tree.isInline => c
1484+
case otherTpe => otherTpe.widen
14831485
/** Extractor for match types hidden behind an AppliedType/MatchAlias */
14841486
object MatchTypeInDisguise {
14851487
def unapply(tp: AppliedType): Option[MatchType] = tp match {

tests/pos/i12715/full.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package repro
2+
3+
import compiletime.{constValue, erasedValue}
4+
5+
sealed trait ValidateExprInt
6+
7+
class And[A <: ValidateExprInt, B <: ValidateExprInt] extends ValidateExprInt
8+
class GreaterThan[T <: Int] extends ValidateExprInt
9+
10+
object Repro:
11+
inline def validate[E <: ValidateExprInt](v: Int): String =
12+
val failMsg = validateV[E](v)
13+
if failMsg == "neverPass" then "neverPass"
14+
else "something else"
15+
16+
transparent inline def validateV[E <: ValidateExprInt](v: Int): String =
17+
inline erasedValue[E] match
18+
case _: GreaterThan[t] =>
19+
"GreaterThan"
20+
case _: And[a, b] =>
21+
inline validateV[a](v) match
22+
case "" =>
23+
validateV[b](v)
24+
case other =>
25+
other
26+
27+
@main def test(): Unit =
28+
println(validate[And[GreaterThan[10], GreaterThan[12]]](5))

tests/pos/i12715/minimized.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
transparent inline def f: String =
2+
inline 10 match
3+
case _ =>
4+
inline "foo" match
5+
case x : String => x
6+
7+
def test =
8+
inline val failMsg = f

0 commit comments

Comments
 (0)