simplify pattern match

This commit is contained in:
Folkert 2022-04-23 21:17:05 +02:00
parent 4668cf506b
commit 44c08779c3
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C

View file

@ -1358,21 +1358,23 @@ fn canonicalize_pending_value_def_new<'a>(
// which also implies it's not a self tail call! // which also implies it's not a self tail call!
// //
// Only defs of the form (foo = ...) can be closure declarations or self tail calls. // Only defs of the form (foo = ...) can be closure declarations or self tail calls.
if let Pattern::Identifier(symbol)
| Pattern::AbilityMemberSpecialization { ident: symbol, .. } = loc_can_pattern.value match (&loc_can_pattern.value, &loc_can_expr.value) {
{ (
if let Closure(ClosureData { Pattern::Identifier(symbol)
function_type, | Pattern::AbilityMemberSpecialization { ident: symbol, .. },
closure_type, Closure(ClosureData {
closure_ext_var, function_type,
return_type, closure_type,
name: ref closure_name, closure_ext_var,
ref arguments, return_type,
loc_body: ref body, name: closure_name,
ref captured_symbols, arguments,
.. loc_body: body,
}) = loc_can_expr.value captured_symbols,
{ ..
}),
) => {
// Since everywhere in the code it'll be referred to by its defined name, // Since everywhere in the code it'll be referred to by its defined name,
// remove its generated name from the closure map. (We'll re-insert it later.) // remove its generated name from the closure map. (We'll re-insert it later.)
let closure_references = env.closures.remove(closure_name).unwrap_or_else(|| { let closure_references = env.closures.remove(closure_name).unwrap_or_else(|| {
@ -1384,17 +1386,16 @@ fn canonicalize_pending_value_def_new<'a>(
// The closure is self tail recursive iff it tail calls itself (by defined name). // The closure is self tail recursive iff it tail calls itself (by defined name).
let is_recursive = match can_output.tail_call { let is_recursive = match can_output.tail_call {
Some(tail_symbol) if tail_symbol == symbol => Recursive::TailRecursive, Some(tail_symbol) if tail_symbol == *symbol => Recursive::TailRecursive,
_ => Recursive::NotRecursive, _ => Recursive::NotRecursive,
}; };
// renamed_closure_def = Some(&symbol);
loc_can_expr.value = Closure(ClosureData { loc_can_expr.value = Closure(ClosureData {
function_type, function_type: *function_type,
closure_type, closure_type: *closure_type,
closure_ext_var, closure_ext_var: *closure_ext_var,
return_type, return_type: *return_type,
name: symbol, name: *symbol,
captured_symbols: captured_symbols.clone(), captured_symbols: captured_symbols.clone(),
recursive: is_recursive, recursive: is_recursive,
arguments: arguments.clone(), arguments: arguments.clone(),
@ -1416,7 +1417,8 @@ fn canonicalize_pending_value_def_new<'a>(
references: DefReferences::Function(closure_references), references: DefReferences::Function(closure_references),
def, def,
} }
} else { }
_ => {
let refs = can_output.references.clone(); let refs = can_output.references.clone();
let def = single_can_def( let def = single_can_def(
@ -1435,24 +1437,6 @@ fn canonicalize_pending_value_def_new<'a>(
def, def,
} }
} }
} else {
let refs = can_output.references.clone();
let def = single_can_def(
loc_can_pattern,
loc_can_expr,
expr_var,
Some(Loc::at(loc_ann.region, type_annotation)),
vars_by_symbol.clone(),
);
output.union(can_output);
TempOutput {
output,
references: DefReferences::Value(refs),
def,
}
} }
} }
// If we have a pattern, then the def has a body (that is, it's not a // If we have a pattern, then the def has a body (that is, it's not a
@ -1490,40 +1474,42 @@ fn canonicalize_pending_value_def_new<'a>(
// which also implies it's not a self tail call! // which also implies it's not a self tail call!
// //
// Only defs of the form (foo = ...) can be closure declarations or self tail calls. // Only defs of the form (foo = ...) can be closure declarations or self tail calls.
if let Pattern::Identifier(symbol) = loc_can_pattern.value { match (&loc_can_pattern.value, &loc_can_expr.value) {
if let Closure(ClosureData { (
function_type, Pattern::Identifier(symbol),
closure_type, Closure(ClosureData {
closure_ext_var,
return_type,
name: ref closure_name,
ref arguments,
loc_body: ref body,
ref captured_symbols,
..
}) = loc_can_expr.value
{
// Since everywhere in the code it'll be referred to by its defined name,
// remove its generated name from the closure map. (We'll re-insert it later.)
let closure_references = env.closures.remove(closure_name).unwrap_or_else(|| {
panic!(
"Tried to remove symbol {:?} from procedures, but it was not found: {:?}",
closure_name, env.closures
)
});
// The closure is self tail recursive iff it tail calls itself (by defined name).
let is_recursive = match can_output.tail_call {
Some(tail_symbol) if tail_symbol == symbol => Recursive::TailRecursive,
_ => Recursive::NotRecursive,
};
loc_can_expr.value = Closure(ClosureData {
function_type, function_type,
closure_type, closure_type,
closure_ext_var, closure_ext_var,
return_type, return_type,
name: symbol, name: closure_name,
arguments,
loc_body: body,
captured_symbols,
..
}),
) => {
// Since everywhere in the code it'll be referred to by its defined name,
// remove its generated name from the closure map. (We'll re-insert it later.)
let closure_references = env.closures.remove(closure_name).unwrap_or_else(|| {
panic!(
"Tried to remove symbol {:?} from procedures, but it was not found: {:?}",
closure_name, env.closures
)
});
// The closure is self tail recursive iff it tail calls itself (by defined name).
let is_recursive = match can_output.tail_call {
Some(tail_symbol) if tail_symbol == *symbol => Recursive::TailRecursive,
_ => Recursive::NotRecursive,
};
loc_can_expr.value = Closure(ClosureData {
function_type: *function_type,
closure_type: *closure_type,
closure_ext_var: *closure_ext_var,
return_type: *return_type,
name: *symbol,
captured_symbols: captured_symbols.clone(), captured_symbols: captured_symbols.clone(),
recursive: is_recursive, recursive: is_recursive,
arguments: arguments.clone(), arguments: arguments.clone(),
@ -1545,7 +1531,8 @@ fn canonicalize_pending_value_def_new<'a>(
references: DefReferences::Function(closure_references), references: DefReferences::Function(closure_references),
def, def,
} }
} else { }
_ => {
let refs = can_output.references.clone(); let refs = can_output.references.clone();
let def = single_can_def( let def = single_can_def(
@ -1564,25 +1551,6 @@ fn canonicalize_pending_value_def_new<'a>(
def, def,
} }
} }
} else {
let refs = can_output.references.clone();
let def = single_can_def(
loc_can_pattern,
loc_can_expr,
expr_var,
None,
vars_by_symbol.clone(),
);
output.union(can_output);
TempOutput {
output,
references: refs,
closure_references: None,
def,
}
} }
} }
} }