Generalize return vars

This commit is contained in:
Sam Mohr 2024-12-01 02:24:51 -08:00
parent 8b9f122047
commit febe95c228
No known key found for this signature in database
GPG key ID: EA41D161A3C1BC99
2 changed files with 23 additions and 2 deletions

View file

@ -1438,14 +1438,16 @@ pub fn constrain_expr(
return_value.region, return_value.region,
)); ));
constrain_expr( let return_con = constrain_expr(
types, types,
constraints, constraints,
env, env,
return_value.region, return_value.region,
&return_value.value, &return_value.value,
expected_return_value, expected_return_value,
) );
constraints.exists([*return_var], return_con)
} }
Tag { Tag {
tag_union_var: variant_var, tag_union_var: variant_var,

View file

@ -14726,6 +14726,25 @@ All branches in an `if` must have the same type!
"### "###
); );
test_report!(
function_with_early_return_generalizes,
indoc!(
r#"
parseItemsWith = \parser ->
when List.mapTry ["123", "456"] parser is
Ok ok -> Ok ok
Err err ->
return Err err
u64Nums = parseItemsWith Str.toU64
u8Nums = parseItemsWith Str.toU8
"$(Inspect.toStr u64Nums) $(Inspect.toStr u8Nums)"
"#
),
@"" // no errors
);
test_report!( test_report!(
leftover_statement, leftover_statement,
indoc!( indoc!(