Fix bug that caused extraneous assignment in IR generation

Previously we would expand optional record fields to assignments when
converting record patterns to "when" expressions. This resulted in
incorrect code being generated.
This commit is contained in:
ayazhafiz 2022-01-13 18:30:36 -05:00
parent 76d4d5d664
commit 9f78eb2e01
4 changed files with 3 additions and 33 deletions

View file

@ -1806,33 +1806,9 @@ fn patterns_to_when<'a>(
for (pattern_var, pattern) in patterns.into_iter() { for (pattern_var, pattern) in patterns.into_iter() {
let context = crate::exhaustive::Context::BadArg; let context = crate::exhaustive::Context::BadArg;
let mono_pattern = match from_can_pattern(env, layout_cache, &pattern.value) { let mono_pattern = match from_can_pattern(env, layout_cache, &pattern.value) {
Ok((pat, assignments)) => { Ok((pat, _assignments)) => {
for (symbol, variable, expr) in assignments.into_iter().rev() { // Don't apply any assignments (e.g. to initialize optional variables) yet.
if let Ok(old_body) = body { // We'll take care of that later when expanding the new "when" branch.
let def = roc_can::def::Def {
annotation: None,
expr_var: variable,
loc_expr: Loc::at(pattern.region, expr),
loc_pattern: Loc::at(
pattern.region,
roc_can::pattern::Pattern::Identifier(symbol),
),
pattern_vars: std::iter::once((symbol, variable)).collect(),
};
let new_expr = roc_can::expr::Expr::LetNonRec(
Box::new(def),
Box::new(old_body),
variable,
);
let new_body = Loc {
region: pattern.region,
value: new_expr,
};
body = Ok(new_body);
}
}
pat pat
} }
Err(runtime_error) => { Err(runtime_error) => {

View file

@ -587,9 +587,7 @@ fn optional_field_function_use_default() {
#[test] #[test]
#[cfg(any(feature = "gen-llvm"))] #[cfg(any(feature = "gen-llvm"))]
#[ignore]
fn optional_field_function_no_use_default() { fn optional_field_function_no_use_default() {
// blocked on https://github.com/rtfeldman/roc/issues/786
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
@ -608,9 +606,7 @@ fn optional_field_function_no_use_default() {
#[test] #[test]
#[cfg(any(feature = "gen-llvm"))] #[cfg(any(feature = "gen-llvm"))]
#[ignore]
fn optional_field_function_no_use_default_nested() { fn optional_field_function_no_use_default_nested() {
// blocked on https://github.com/rtfeldman/roc/issues/786
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"

View file

@ -5,7 +5,6 @@ procedure Num.24 (#Attr.2, #Attr.3):
procedure Test.1 (Test.4): procedure Test.1 (Test.4):
let Test.2 = StructAtIndex 0 Test.4; let Test.2 = StructAtIndex 0 Test.4;
let Test.3 = StructAtIndex 1 Test.4; let Test.3 = StructAtIndex 1 Test.4;
let Test.2 = 10i64;
let Test.7 = CallByName Num.24 Test.2 Test.3; let Test.7 = CallByName Num.24 Test.2 Test.3;
ret Test.7; ret Test.7;

View file

@ -3,7 +3,6 @@ procedure Num.24 (#Attr.2, #Attr.3):
ret Test.8; ret Test.8;
procedure Test.1 (Test.4): procedure Test.1 (Test.4):
let Test.2 = 10i64;
let Test.2 = 10i64; let Test.2 = 10i64;
let Test.7 = CallByName Num.24 Test.2 Test.4; let Test.7 = CallByName Num.24 Test.2 Test.4;
ret Test.7; ret Test.7;