diff --git a/crates/compiler/mono/src/decision_tree.rs b/crates/compiler/mono/src/decision_tree.rs index 7cbb39c9f6..bb61192edd 100644 --- a/crates/compiler/mono/src/decision_tree.rs +++ b/crates/compiler/mono/src/decision_tree.rs @@ -977,9 +977,16 @@ fn needs_tests(pattern: &Pattern) -> bool { match pattern { Identifier(_) | Underscore => false, + NewtypeDestructure { arguments, .. } => { + // Newtypes that only have one argument decay immediately into that argument, so no + // test for them is needed. + // + // Newtypes with > 1 argument decay into a struct of those arguments, and we'll need to + // read the appropriate argument in the struct when constructing the decision tree. + arguments.len() != 1 + } RecordDestructure(_, _) - | NewtypeDestructure { .. } | AppliedTag { .. } | OpaqueUnwrap { .. } | BitLiteral { .. } diff --git a/crates/compiler/solve/tests/solve_expr.rs b/crates/compiler/solve/tests/solve_expr.rs index f586cfa85f..4bc0ad0196 100644 --- a/crates/compiler/solve/tests/solve_expr.rs +++ b/crates/compiler/solve/tests/solve_expr.rs @@ -7418,7 +7418,9 @@ mod solve_expr { Wrapper (AlternatePayload str) -> str "# ), - &[r#"Wrapper (Payload "err") : [Wrapper [AlternatePayload Str, Payload Str]]"#,] + @r###" + Wrapper (Payload "err") : [Wrapper [AlternatePayload Str, Payload Str]] + "### ) } } diff --git a/crates/compiler/test_gen/src/gen_tags.rs b/crates/compiler/test_gen/src/gen_tags.rs index 15bb82d1e1..7acc0917d2 100644 --- a/crates/compiler/test_gen/src/gen_tags.rs +++ b/crates/compiler/test_gen/src/gen_tags.rs @@ -1805,3 +1805,19 @@ fn instantiate_annotated_as_recursive_alias_multiple_polymorphic_expr() { i64 ) } + +#[test] +#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))] +fn issue_3560_nested_tag_constructor_is_newtype() { + assert_evals_to!( + indoc!( + r#" + when Wrapper (Payload "err") is + Wrapper (Payload str) -> str + Wrapper (AlternatePayload str) -> str + "# + ), + RocStr::from("err"), + RocStr + ) +} diff --git a/crates/compiler/test_mono/generated/issue_3560_nested_tag_constructor_is_newtype.txt b/crates/compiler/test_mono/generated/issue_3560_nested_tag_constructor_is_newtype.txt new file mode 100644 index 0000000000..f9ef100d57 --- /dev/null +++ b/crates/compiler/test_mono/generated/issue_3560_nested_tag_constructor_is_newtype.txt @@ -0,0 +1,13 @@ +procedure Test.0 (): + let Test.10 : Str = "err"; + let Test.9 : [C Str, C Str] = TagId(1) Test.10; + joinpoint Test.8: + let Test.4 : Str = UnionAtIndex (Id 0) (Index 0) Test.9; + inc Test.4; + dec Test.9; + ret Test.4; + in + let Test.3 : Str = UnionAtIndex (Id 1) (Index 0) Test.9; + inc Test.3; + dec Test.9; + ret Test.3; diff --git a/crates/compiler/test_mono/src/tests.rs b/crates/compiler/test_mono/src/tests.rs index 9734cbbcc5..ac925f1023 100644 --- a/crates/compiler/test_mono/src/tests.rs +++ b/crates/compiler/test_mono/src/tests.rs @@ -1886,17 +1886,12 @@ fn encode_derived_tag_two_payloads_string() { } #[mono_test] -fn issue_3560_nested_tag_union() { +fn issue_3560_nested_tag_constructor_is_newtype() { indoc!( r#" - app "test" - imports [Encode.{ toEncoder }, Json] - provides [main] to "./platform" - - main = - when Wrapper (Payload "err") is - Wrapper (Payload str) -> str - Wrapper (AlternatePayload str) -> str + when Wrapper (Payload "err") is + Wrapper (Payload str) -> str + Wrapper (AlternatePayload str) -> str "# ) }