fix: disallow unknown type assignments and reads
This commit is contained in:
parent
277d2f66af
commit
bc5f28ab52
@ -99,12 +99,30 @@ object typeChecker {
|
||||
)
|
||||
case Assign(lhs, rhs) =>
|
||||
val lhsTy = checkValue(lhs, Constraint.Unconstrained)
|
||||
checkValue(rhs, Constraint.Is(lhsTy, s"assignment must have type $lhsTy"))
|
||||
case Read(lhs) =>
|
||||
checkValue(
|
||||
lhs,
|
||||
Constraint.IsEither(KnownType.Int, KnownType.Char, "read must be int or char")
|
||||
checkValue(rhs, Constraint.Is(lhsTy, s"assignment must have type $lhsTy")) match {
|
||||
case ? =>
|
||||
ctx.error(
|
||||
Error.SemanticError(lhs.pos, "assignment with both sides of unknown type is illegal")
|
||||
)
|
||||
case _ => ()
|
||||
}
|
||||
case Read(lhs) =>
|
||||
val lhsTy = checkValue(lhs, Constraint.Unconstrained)
|
||||
lhsTy match {
|
||||
case ? =>
|
||||
ctx.error(
|
||||
Error.SemanticError(lhs.pos, "cannot read into a destination with an unknown type")
|
||||
)
|
||||
case _ =>
|
||||
lhsTy.satisfies(
|
||||
Constraint.IsEither(
|
||||
KnownType.Int,
|
||||
KnownType.Char,
|
||||
"read must be applied to an int or char"
|
||||
),
|
||||
lhs.pos
|
||||
)
|
||||
}
|
||||
case Free(lhs) =>
|
||||
checkValue(
|
||||
lhs,
|
||||
@ -193,7 +211,8 @@ object typeChecker {
|
||||
elem,
|
||||
Constraint.Is(KnownType.Pair(?, ?), "fst must be applied to a pair")
|
||||
) match {
|
||||
case KnownType.Pair(left, _) => left.satisfies(constraint, elem.pos)
|
||||
case what @ KnownType.Pair(left, _) =>
|
||||
left.satisfies(constraint, elem.pos)
|
||||
case ? => ?.satisfies(constraint, elem.pos)
|
||||
case _ => ctx.error(Error.InternalError(elem.pos, "fst must be applied to a pair"))
|
||||
} // satisfies constraint
|
||||
|
@ -16,7 +16,7 @@ class ParallelExamplesSpec extends AnyFlatSpec with BeforeAndAfterAll with Paral
|
||||
(p.toString, List(200))
|
||||
} ++
|
||||
allWaccFiles("wacc-examples/invalid/whack").map { p =>
|
||||
(p.toString, List(0, 100, 200))
|
||||
(p.toString, List(100, 200))
|
||||
}
|
||||
|
||||
// tests go here
|
||||
@ -82,7 +82,7 @@ class ParallelExamplesSpec extends AnyFlatSpec with BeforeAndAfterAll with Paral
|
||||
// "wacc-examples/invalid/semanticErr/variables",
|
||||
// "wacc-examples/invalid/semanticErr/while",
|
||||
// invalid (whack)
|
||||
"wacc-examples/invalid/whack"
|
||||
// "wacc-examples/invalid/whack"
|
||||
// format: on
|
||||
// format: on
|
||||
).find(filename.contains).isDefined
|
||||
|
Loading…
x
Reference in New Issue
Block a user