Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do no widen constant type on selector type in inline matches #12902

Merged
merged 1 commit into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1465,8 +1465,10 @@ class Typer extends Namer
case _ =>
if tree.isInline then checkInInlineContext("inline match", tree.srcPos)
val sel1 = typedExpr(tree.selector)
val selType = fullyDefinedType(sel1.tpe, "pattern selector", tree.span).widen

val rawSelectorTpe = fullyDefinedType(sel1.tpe, "pattern selector", tree.span)
val selType = rawSelectorTpe match
case c: ConstantType if tree.isInline => c
nicolasstucki marked this conversation as resolved.
Show resolved Hide resolved
case otherTpe => otherTpe.widen
/** Extractor for match types hidden behind an AppliedType/MatchAlias */
object MatchTypeInDisguise {
def unapply(tp: AppliedType): Option[MatchType] = tp match {
Expand Down
28 changes: 28 additions & 0 deletions tests/pos/i12715/full.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package repro

import compiletime.{constValue, erasedValue}

sealed trait ValidateExprInt

class And[A <: ValidateExprInt, B <: ValidateExprInt] extends ValidateExprInt
class GreaterThan[T <: Int] extends ValidateExprInt

object Repro:
inline def validate[E <: ValidateExprInt](v: Int): String =
val failMsg = validateV[E](v)
if failMsg == "neverPass" then "neverPass"
else "something else"

transparent inline def validateV[E <: ValidateExprInt](v: Int): String =
inline erasedValue[E] match
case _: GreaterThan[t] =>
"GreaterThan"
case _: And[a, b] =>
inline validateV[a](v) match
case "" =>
validateV[b](v)
case other =>
other

@main def test(): Unit =
println(validate[And[GreaterThan[10], GreaterThan[12]]](5))
8 changes: 8 additions & 0 deletions tests/pos/i12715/minimized.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
transparent inline def f: String =
inline 10 match
case _ =>
inline "foo" match
case x : String => x
nicolasstucki marked this conversation as resolved.
Show resolved Hide resolved

def test =
inline val failMsg = f