Skip to content

Commit

Permalink
remove an unreachable branch
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Oct 22, 2023
1 parent 3d34738 commit c79a631
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
12 changes: 6 additions & 6 deletions core/src/main/scala/org/bykn/bosatsu/rankn/Infer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -866,14 +866,14 @@ object Infer {
extendEnv(name, tpe) {
checkSigma(rhs, tpe).parProduct(typeCheckRho(body, expect))
}
case _ =>
case notAnnotated =>
newMetaType(Kind.Type) // the kind of a let value is a Type
.flatMap { rhsTpe =>
extendEnv(name, rhsTpe) {
for {
// the type variable needs to be unified with varT
// note, varT could be a sigma type, it is not a Tau or Rho
typedRhs <- inferSigmaMeta(rhs, Some((name, rhsTpe, region(rhs))))
typedRhs <- inferSigmaMeta(notAnnotated, Some((name, rhsTpe, region(notAnnotated))))
varT = typedRhs.getType
// we need to overwrite the metavariable now with the full type
typedBody <- extendEnv(name, varT)(typeCheckRho(body, expect))
Expand Down Expand Up @@ -1284,6 +1284,7 @@ object Infer {
def inferSigma[A: HasRegion](e: Expr[A]): Infer[TypedExpr[A]] =
inferSigmaMeta(e, None)

// invariant: if meta.isDefined then e is not Expr.Annotated
def inferSigmaMeta[A: HasRegion](e: Expr[A], meta: Option[(Identifier, Type.TyMeta, Region)]): Infer[TypedExpr[A]] = {
def unifySelf(tpe: Type.Rho): Infer[Map[Name, Type]] =
meta match {
Expand All @@ -1304,8 +1305,6 @@ object Infer {
case Some((_, tpe, rtpe)) =>
def maybeUnified(e: Expr[A]): Infer[Unit] =
e match {
case Expr.Annotation(e1, t, _) =>
unifyType(tpe, t, rtpe, region(e)) *> maybeUnified(e1)
case Expr.Lambda(args, res, _) =>
unifyFn(args.length, tpe, rtpe, region(e) - region(res)).void
case _ =>
Expand Down Expand Up @@ -1378,9 +1377,9 @@ object Infer {
expr match {
case Expr.Annotated(tpe) =>
extendEnv(name, tpe)(checkSigma(expr, tpe))
case _ =>
case notAnnotated =>
newMetaType(Kind.Type).flatMap { tpe =>
extendEnv(name, tpe)(typeCheckMeta(expr, Some((name, tpe, region(expr)))))
extendEnv(name, tpe)(typeCheckMeta(notAnnotated, Some((name, tpe, region(notAnnotated)))))
}
}

Expand All @@ -1400,6 +1399,7 @@ object Infer {
}
}

// Invariant: if optMeta.isDefined then t is not Expr.Annotated
private def typeCheckMeta[A: HasRegion](t: Expr[A], optMeta: Option[(Identifier, Type.TyMeta, Region)]): Infer[TypedExpr[A]] = {
def run(t: Expr[A]) = inferSigmaMeta(t, optMeta).flatMap(zonkTypedExpr _)

Expand Down
18 changes: 18 additions & 0 deletions core/src/test/scala/org/bykn/bosatsu/rankn/RankNInferTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1138,4 +1138,22 @@ foo = (
""", "Foo")
}

test("widening inside a match") {
parseProgram("""#
enum B: True, False
def not(b):
match b:
case True: False
case False: True
def branch(x):
match x:
case True: (x -> x): forall a. a -> a
case False: i -> not(i)
res = branch(True)(True)
""", "B")
}
}

0 comments on commit c79a631

Please sign in to comment.