Eliminate run-time errors from PatternMatch (#1126)

This commit is contained in:
Geoff Romer
2022-03-10 16:46:43 -08:00
committed by GitHub
parent 9384077abd
commit 93842ad878
5 changed files with 59 additions and 10 deletions
@@ -178,11 +178,7 @@ auto PatternMatch(Nonnull<const Value*> p, Nonnull<const Value*> v,
std::optional<Nonnull<RuntimeScope*>> bindings) -> bool {
switch (p->kind()) {
case Value::Kind::BindingPlaceholderValue: {
if (!bindings.has_value()) {
// TODO: move this to typechecker.
FATAL_COMPILATION_ERROR(source_loc)
<< "Name bindings are not supported in this context";
}
CHECK(bindings.has_value());
const auto& placeholder = cast<BindingPlaceholderValue>(*p);
if (placeholder.value_node().has_value()) {
(*bindings)->Initialize(*placeholder.value_node(), v);
@@ -194,11 +190,7 @@ auto PatternMatch(Nonnull<const Value*> p, Nonnull<const Value*> v,
case Value::Kind::TupleValue: {
const auto& p_tup = cast<TupleValue>(*p);
const auto& v_tup = cast<TupleValue>(*v);
if (p_tup.elements().size() != v_tup.elements().size()) {
FATAL_PROGRAM_ERROR(source_loc)
<< "arity mismatch in tuple pattern match:\n pattern: "
<< p_tup << "\n value: " << v_tup;
}
CHECK(p_tup.elements().size() == v_tup.elements().size());
for (size_t i = 0; i < p_tup.elements().size(); ++i) {
if (!PatternMatch(p_tup.elements()[i], v_tup.elements()[i],
source_loc, bindings)) {