Skip to content

Commit

Permalink
simpler forms
Browse files Browse the repository at this point in the history
Summary:
This simplifies the hierarchy of forms - making InvalidForm internal only.
Expanding of forms is refactored to return `Either[ErrorForm, ExpandedForm]`

Reviewed By: VLanvin

Differential Revision: D42247248

fbshipit-source-id: 0438e84113af922af5a150cb38ffcd6bb8f35a9c
  • Loading branch information
ilya-klyuchnikov authored and facebook-github-bot committed Dec 28, 2022
1 parent 241e338 commit bf0a77f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,15 @@ object Forms {
case class OpaqueTypeDecl(id: Id)(val pos: Pos) extends InternalForm
case class TypeDecl(id: Id, params: List[VarType], body: Type)(val pos: Pos) extends InternalForm

sealed trait UtilForm extends ExternalForm with InternalForm

sealed trait InvalidForm extends UtilForm {
sealed trait InvalidForm extends InternalForm {
val te: TypeError
}
case class InvalidTypeDecl(id: Id, te: Invalid)(val pos: Pos) extends InvalidForm
case class InvalidFunSpec(id: Id, te: Invalid)(val pos: Pos) extends InvalidForm
case class InvalidRecDecl(name: String, te: Invalid)(val pos: Pos) extends InvalidForm
case class InvalidConvertTypeInRecDecl(name: String, te: Invalid)(val pos: Pos) extends InvalidForm

case class NoSpecFuncDecl(id: Id)(val pos: Pos) extends UtilForm
case class NoSpecFuncDecl(id: Id)(val pos: Pos) extends InternalForm
case class FuncDecl(id: Id, errors: List[TypeError])(val pos: Pos) extends InternalForm
case class MisBehaviour(te: BehaviourError)(val pos: Pos) extends InternalForm

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import com.whatsapp.eqwalizer.config
// Expands spec constraints, applies rewrite rules, and validates
private class Expand(module: String) {

def expandFunSpec(funSpec: ExternalFunSpec): ExternalForm = {
def expandFunSpec(funSpec: ExternalFunSpec): Either[InvalidFunSpec, ExternalFunSpec] = {
try {
funSpec.copy(types = funSpec.types.map(expandCft))(funSpec.pos)
Right(funSpec.copy(types = funSpec.types.map(expandCft))(funSpec.pos))
} catch {
case e: InvalidDiagnostics.Invalid => InvalidFunSpec(funSpec.id, e)(funSpec.pos)
case e: InvalidDiagnostics.Invalid =>
Left(InvalidFunSpec(funSpec.id, e)(funSpec.pos))
}
}

Expand All @@ -37,30 +38,32 @@ private class Expand(module: String) {
ConstrainedFunType(ft1, List.empty)(cft.pos)
}

def expandCallback(cb: ExternalCallback): ExternalForm = {
def expandCallback(cb: ExternalCallback): Either[InvalidFunSpec, ExternalCallback] = {
try {
cb.copy(types = cb.types.map(expandCft))(cb.pos)
Right(cb.copy(types = cb.types.map(expandCft))(cb.pos))
} catch {
case e: InvalidDiagnostics.Invalid => InvalidFunSpec(cb.id, e)(cb.pos)
case e: InvalidDiagnostics.Invalid =>
Left(InvalidFunSpec(cb.id, e)(cb.pos))
}
}

def expandTypeDecl(decl: ExternalTypeDecl): ExternalForm = {
def expandTypeDecl(decl: ExternalTypeDecl): Either[InvalidTypeDecl, ExternalTypeDecl] = {
try {
validateTypeVars(decl.pos, decl.body, decl.params)
decl.copy(body = expand(decl.body))(decl.pos)
Right(decl.copy(body = expand(decl.body))(decl.pos))
} catch {
case e: InvalidDiagnostics.Invalid =>
InvalidTypeDecl(decl.id, e)(decl.pos)
Left(InvalidTypeDecl(decl.id, e)(decl.pos))
}
}

def expandOpaqueDecl(decl: ExternalOpaqueDecl): ExternalForm = {
def expandOpaqueDecl(decl: ExternalOpaqueDecl): Either[InvalidTypeDecl, ExternalOpaqueDecl] = {
try {
validateTypeVars(decl.pos, decl.body, decl.params)
decl.copy(body = expand(decl.body))(decl.pos)
Right(decl.copy(body = expand(decl.body))(decl.pos))
} catch {
case e: InvalidDiagnostics.Invalid => InvalidTypeDecl(decl.id, e)(decl.pos)
case e: InvalidDiagnostics.Invalid =>
Left(InvalidTypeDecl(decl.id, e)(decl.pos))
}
}

Expand Down Expand Up @@ -93,10 +96,12 @@ private class Expand(module: String) {
case None => ()
}

def expandRecDecl(decl: ExternalRecDecl): ExternalForm =
try decl.copy(fields = decl.fields.map(expandRecField))(decl.pos)
catch {
case e: InvalidDiagnostics.Invalid => InvalidRecDecl(decl.name, e)(decl.pos)
def expandRecDecl(decl: ExternalRecDecl): Either[InvalidRecDecl, ExternalRecDecl] =
try {
Right(decl.copy(fields = decl.fields.map(expandRecField))(decl.pos))
} catch {
case e: InvalidDiagnostics.Invalid =>
Left(InvalidRecDecl(decl.name, e)(decl.pos))
}

private def expand(t: ExtType): ExtType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private object Expander {

def typeDecl(t: ExternalTypeDecl): Unit =
expand.expandTypeDecl(t) match {
case decl: ExternalTypeDecl =>
case Right(decl) =>
convertTypes.convertTypeDecl(decl) match {
case tDecl: TypeDecl =>
types.addOne(tDecl)
Expand All @@ -49,18 +49,14 @@ private object Expander {
throw new IllegalStateException()
// $COVERAGE-ON$
}
case invalid: InvalidForm =>
case Left(invalid) =>
if (currentFile == moduleFile)
invalidForms.addOne(invalid)
// $COVERAGE-OFF$
case _ =>
throw new IllegalStateException()
// $COVERAGE-ON$
}

def opaqueDecl(t: ExternalOpaqueDecl): Unit =
expand.expandOpaqueDecl(t) match {
case decl: ExternalOpaqueDecl =>
case Right(decl) =>
publicOpaques.addOne(convertTypes.convertOpaqueDeclPublic(decl))
convertTypes.convertOpaquePrivate(decl) match {
case tDecl: TypeDecl =>
Expand All @@ -73,18 +69,14 @@ private object Expander {
throw new IllegalStateException()
// $COVERAGE-ON$
}
case invalid: InvalidForm =>
case Left(invalid) =>
if (currentFile == moduleFile)
invalidForms.addOne(invalid)
// $COVERAGE-OFF$
case _ =>
throw new IllegalStateException()
// $COVERAGE-ON$
}

def recordDecl(r: ExternalRecDecl): Unit =
expand.expandRecDecl(r) match {
case decl: ExternalRecDecl =>
case Right(decl) =>
convertTypes.convertRecDecl(decl) match {
case recDecl: RecDecl =>
records.addOne(recDecl)
Expand All @@ -96,42 +88,30 @@ private object Expander {
throw new IllegalStateException()
// $COVERAGE-ON$
}
case invalid: InvalidForm =>
case Left(invalid) =>
if (currentFile == moduleFile)
invalidForms.addOne(invalid)
// $COVERAGE-OFF$
case _ =>
throw new IllegalStateException()
// $COVERAGE-ON$
}

def spec(s: ExternalFunSpec): Unit =
expand.expandFunSpec(s) match {
case s1: ExternalFunSpec =>
case Right(s1) =>
if (s1.types.size == 1)
specs.addOne(convertTypes.convertSpec(s1))
else
overloadedSpecs.addOne(convertTypes.convertOverloadedSpec(s1))
case invalid: InvalidForm =>
case Left(invalid) =>
if (currentFile == moduleFile)
invalidForms.addOne(invalid)
// $COVERAGE-OFF$
case _ =>
throw new IllegalStateException()
// $COVERAGE-ON$
}

def callback(cb: ExternalCallback): Unit =
expand.expandCallback(cb) match {
case cb1: ExternalCallback =>
case Right(cb1) =>
callbacks.addOne(convertTypes.convertCallback(cb1))
case invalid: InvalidForm =>
case Left(invalid) =>
if (currentFile == moduleFile)
invalidForms.addOne(invalid)
// $COVERAGE-OFF$
case _ =>
throw new IllegalStateException()
// $COVERAGE-ON$
}

for (f <- stub.forms)
Expand Down

0 comments on commit bf0a77f

Please sign in to comment.