Skip to content

Enable some recursive gadt to work with inline match #10390

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

Merged
merged 1 commit into from
Nov 30, 2020
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
19 changes: 17 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,21 @@ object Inliner {
def codeOf(arg: Tree, pos: SrcPos)(using Context): Tree =
Literal(Constant(arg.show)).withSpan(pos.span)
}

extension (tp: Type) {

/** same as widenTermRefExpr, but preserves modules and singleton enum values */
private final def widenInlineScrutinee(using Context): Type = tp.stripTypeVar match {
case tp: TermRef =>
val sym = tp.termSymbol
if sym.isAllOf(EnumCase, butNot=JavaDefined) || sym.is(Module) then tp
else if !tp.isOverloaded then tp.underlying.widenExpr.widenInlineScrutinee
else tp
case _ => tp
}

}

}

/** Produces an inlined version of `call` via its `inlined` method.
Expand Down Expand Up @@ -1003,7 +1018,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
* scrutinee as RHS and type that corresponds to RHS.
*/
def newTermBinding(sym: TermSymbol, rhs: Tree): Unit = {
val copied = sym.copy(info = rhs.tpe.widenTermRefExpr, coord = sym.coord, flags = sym.flags &~ Case).asTerm
val copied = sym.copy(info = rhs.tpe.widenInlineScrutinee, coord = sym.coord, flags = sym.flags &~ Case).asTerm
caseBindingMap += ((sym, ValDef(copied, constToLiteral(rhs)).withSpan(sym.span)))
}

Expand Down Expand Up @@ -1121,7 +1136,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
def reduceSubPatterns(pats: List[Tree], selectors: List[Tree]): Boolean = (pats, selectors) match {
case (Nil, Nil) => true
case (pat :: pats1, selector :: selectors1) =>
val elem = newSym(InlineBinderName.fresh(), Synthetic, selector.tpe.widenTermRefExpr).asTerm
val elem = newSym(InlineBinderName.fresh(), Synthetic, selector.tpe.widenInlineScrutinee).asTerm
val rhs = constToLiteral(selector)
elem.defTree = rhs
caseBindingMap += ((NoSymbol, ValDef(elem, rhs).withSpan(elem.span)))
Expand Down
37 changes: 37 additions & 0 deletions tests/run/enum-nat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Nat._
import compiletime._

enum Nat:
case Zero
case Succ[N <: Nat.Refract](n: N)

object Nat:
type Refract = Zero.type | Succ[_]

inline def toIntTypeLevel[N <: Nat]: Int = inline erasedValue[N] match
case _: Zero.type => 0
case _: Succ[n] => toIntTypeLevel[n] + 1

inline def toInt[N <: Nat.Refract](inline nat: N): Int = inline nat match
case nat: Zero.type => 0
case nat: Succ[n] => toInt(nat.n) + 1

inline def toIntUnapply[N <: Nat.Refract](inline nat: N): Int = inline nat match
case Zero => 0
case Succ(n) => toIntUnapply(n) + 1

inline def toIntTypeTailRec[N <: Nat, Acc <: Int]: Int = inline erasedValue[N] match
case _: Zero.type => constValue[Acc]
case _: Succ[n] => toIntTypeTailRec[n, S[Acc]]

inline def toIntErased[N <: Nat.Refract](inline nat: N): Int = toIntTypeTailRec[N, 0]

@main def Test: Unit =
println("erased value:")
assert(toIntTypeLevel[Succ[Succ[Succ[Zero.type]]]] == 3)
println("type test:")
assert(toInt(Succ(Succ(Succ(Zero)))) == 3)
println("unapply:")
assert(toIntUnapply(Succ(Succ(Succ(Zero)))) == 3)
println("infer erased:")
assert(toIntErased(Succ(Succ(Succ(Zero)))) == 3)