Skip to content

Commit

Permalink
Extract fun types instead of coercing
Browse files Browse the repository at this point in the history
Summary:
`Narrow.extractFunTypes` is mostly identical to `Narrow.asFunType` except it filters out non compatible types, while the latter performs an early return with `None` in that case.

However, `Narrow.asFunType` is always used with `.get` and after a check that the type is actually compatible, thus defeating the point of the optional result.

This diff migrates everything to `extractFunTypes`, the semantics are unchanged.

Reviewed By: ilya-klyuchnikov

Differential Revision: D62570404

fbshipit-source-id: 9ee92cb7bfe736df56032f41fcc89ef05bae629e
  • Loading branch information
VLanvin authored and facebook-github-bot committed Sep 12, 2024
1 parent 5ca1650 commit 114a361
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ final class Check(pipelineContext: PipelineContext) {
} else {
ty
}
val funTys = narrow.asFunType(funTy, args.size).get
val funTys = narrow.extractFunTypes(funTy, args.size)
val (argTys, env2) = elab.elabExprs(args, env1)
if (funTys.nonEmpty) {
funTys.foreach { ft =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ final class Elab(pipelineContext: PipelineContext) {
} else {
ty
}
val funTys = narrow.asFunType(funTy, args.size).get
val funTys = narrow.extractFunTypes(funTy, args.size)
if (funTys.isEmpty) {
val (_, env2) = elabExprs(args, env1)
(NoneType, env2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class ElabApplyCustom(pipelineContext: PipelineContext) {
diagnosticsInfo.add(ExpectedSubtype(funArg.pos, funArg, expected = expFunTy, got = funArgTy))
List(DynamicType)
} else
narrow.asFunType(funArgTy, 1).get.map(_.resTy)
narrow.extractFunTypes(funArgTy, 1).map(_.resTy)
}
def funResultsToItemTy(tys: Iterable[Type], defaultTy: Type, pos: Pos): Type =
tys.foldLeft(NoneType: Type)((memo, ty) => subtype.join(memo, funResultToItemTy(ty, defaultTy, pos)))
Expand Down Expand Up @@ -285,7 +285,7 @@ class ElabApplyCustom(pipelineContext: PipelineContext) {
case _ =>
val expFunTy = FunType(Nil, List(keyTy, valTy, accTy), AnyType)
val funArgCoercedTy = coerce(funArg, funArgTy, expFunTy)
val vTys = narrow.asFunType(funArgCoercedTy, 3).get.map(_.resTy)
val vTys = narrow.extractFunTypes(funArgCoercedTy, 3).map(_.resTy).toList
accTy1 :: vTys
}
def validateAccumulatorTy(accTy: Type): Unit = funArg match {
Expand Down Expand Up @@ -385,7 +385,7 @@ class ElabApplyCustom(pipelineContext: PipelineContext) {
subtype.join(vTys)
case _ =>
val funArgCoercedTy = coerce(funArg, funArgTy, expFunTy)
val vTys = narrow.asFunType(funArgCoercedTy, 2).get.map(_.resTy)
val vTys = narrow.extractFunTypes(funArgCoercedTy, 2).map(_.resTy)
subtype.join(vTys)
}
def mapValueType(argMapTy: Type, argValTy: Type): Type = argMapTy match {
Expand Down Expand Up @@ -418,7 +418,7 @@ class ElabApplyCustom(pipelineContext: PipelineContext) {
.map((clause, occEnv) => elab.elabClause(clause, List(keyTy, valTy), occEnv, Set.empty)._1)
case _ =>
val funArgCoercedTy = coerce(funArg, funArgTy, expFunTy)
narrow.asFunType(funArgCoercedTy, 2).get.map(_.resTy)
narrow.extractFunTypes(funArgCoercedTy, 2).map(_.resTy)
}
def funResultsToValTy(tys: Iterable[Type], defaultTy: Type, pos: Pos): Type =
tys.foldLeft(NoneType: Type)((memo, ty) => subtype.join(memo, funResultToValTy(ty, defaultTy, pos)))
Expand Down
32 changes: 0 additions & 32 deletions eqwalizer/src/main/scala/com/whatsapp/eqwalizer/tc/Narrow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -232,38 +232,6 @@ class Narrow(pipelineContext: PipelineContext) {
List()
}

def asFunType(ty: Type, arity: Int): Option[List[FunType]] =
asFunTypeAux(ty, arity)

def asFunTypeAux(ty: Type, arity: Int): Option[List[FunType]] = ty match {
case DynamicType =>
Some(List(FunType(List.empty, List.fill(arity)(DynamicType), DynamicType)))
case BoundedDynamicType(bound) =>
asFunTypeAux(bound, arity).map(fts =>
fts.map(ft => FunType(ft.forall, ft.argTys.map(BoundedDynamicType), BoundedDynamicType(ft.resTy)))
)
case AnyFunType =>
Some(List(FunType(List.empty, List.fill(arity)(DynamicType), DynamicType)))
case AnyArityFunType(resTy) =>
Some(List(FunType(List.empty, List.fill(arity)(DynamicType), resTy)))
case _ if subtype.isNoneType(ty) =>
Some(List())
case ft: FunType =>
if (ft.argTys.size == arity) Some(List(ft))
else None
case UnionType(tys) =>
val subResults = tys.map(asFunTypeAux(_, arity))
if (subResults.exists(_.isEmpty)) None
else Some(subResults.toList.flatMap(_.get))
case RemoteType(rid, args) =>
val body = util.getTypeDeclBody(rid, args)
asFunTypeAux(body, arity)
case OpaqueType(rid, _) =>
None
case _ =>
None
}

def extractFunTypes(ty: Type, arity: Int): Set[FunType] = ty match {
case DynamicType =>
Set(FunType(List.empty, List.fill(arity)(DynamicType), DynamicType))
Expand Down

0 comments on commit 114a361

Please sign in to comment.