diff --git a/compiler/mono/src/ir.rs b/compiler/mono/src/ir.rs index a1f595fcf0..364dafcbdf 100644 --- a/compiler/mono/src/ir.rs +++ b/compiler/mono/src/ir.rs @@ -4131,17 +4131,17 @@ fn convert_tag_union<'a>( hole, ), ByteUnion(tag_names) => { - let tag_id = tag_names - .iter() - .position(|key| key == &tag_name) - .expect("tag must be in its own type"); + let opt_tag_id = tag_names.iter().position(|key| key == &tag_name); - Stmt::Let( - assigned, - Expr::Literal(Literal::Byte(tag_id as u8)), - Layout::Builtin(Builtin::Int8), - hole, - ) + match opt_tag_id { + Some(tag_id) => Stmt::Let( + assigned, + Expr::Literal(Literal::Byte(tag_id as u8)), + Layout::Builtin(Builtin::Int8), + hole, + ), + None => Stmt::RuntimeError("tag must be in its own type"), + } } Newtype { diff --git a/compiler/test_gen/src/gen_tags.rs b/compiler/test_gen/src/gen_tags.rs index 776c06ff07..cfd11b807f 100644 --- a/compiler/test_gen/src/gen_tags.rs +++ b/compiler/test_gen/src/gen_tags.rs @@ -1033,3 +1033,20 @@ fn applied_tag_function_linked_list() { i64 ); } + +#[test] +#[should_panic(expected = "")] +fn tag_must_be_its_own_type() { + assert_evals_to!( + indoc!( + r#" + z : [ A, B, C ] + z = Z + + z + "# + ), + 1, + i64 + ); +}