Change guard to DestructType

This commit is contained in:
Richard Feldman 2020-07-18 12:42:04 -04:00
parent c9883be8a2
commit 43bca05cdb
4 changed files with 69 additions and 16 deletions

View file

@ -941,7 +941,7 @@ fn canonicalize_field<'a>(
match field {
// Both a label and a value, e.g. `{ name: "blah" }`
LabeledValue(label, _, loc_expr) => {
RequiredValue(label, _, loc_expr) => {
let field_var = var_store.fresh();
let (loc_can_expr, output) =
canonicalize_expr(env, var_store, scope, loc_expr.region, &loc_expr.value);
@ -954,6 +954,10 @@ fn canonicalize_field<'a>(
)
}
OptionalValue(_, _, _) => {
todo!("TODO gracefully handle an optional field being used in an Expr");
}
// A label with no value, e.g. `{ name }` (this is sugar for { name: name })
LabelOnly(_) => {
panic!("Somehow a LabelOnly record field was not desugared!");

View file

@ -44,7 +44,14 @@ pub struct RecordDestruct {
pub var: Variable,
pub label: Lowercase,
pub symbol: Symbol,
pub guard: Option<(Variable, Located<Pattern>)>,
pub typ: DestructType,
}
#[derive(Clone, Debug, PartialEq)]
pub enum DestructType {
Required,
Optional(Variable),
Guard(Variable, Located<Pattern>),
}
pub fn symbols_from_pattern(pattern: &Pattern) -> Vec<Symbol> {
@ -253,7 +260,7 @@ pub fn canonicalize_pattern<'a>(
var: var_store.fresh(),
label: Lowercase::from(label),
symbol,
guard: None,
typ: DestructType::Required,
},
});
}
@ -271,7 +278,8 @@ pub fn canonicalize_pattern<'a>(
}
};
}
RecordField(label, loc_guard) => {
RequiredField(label, loc_guard) => {
// a guard does not introduce the label into scope!
let symbol = scope.ignore(label.into(), &mut env.ident_ids);
let can_guard = canonicalize_pattern(
@ -289,7 +297,7 @@ pub fn canonicalize_pattern<'a>(
var: var_store.fresh(),
label: Lowercase::from(label),
symbol,
guard: Some((var_store.fresh(), can_guard)),
typ: DestructType::Guard(var_store.fresh(), can_guard),
},
});
}
@ -305,7 +313,8 @@ pub fn canonicalize_pattern<'a>(
destructs,
})
}
RecordField(_name, _loc_pattern) => {
RequiredField(_name, _loc_pattern) | OptionalField(_name, _loc_pattern) => {
unreachable!("should have been handled in RecordDestructure");
}