From d2464e0dbfdf0c14fe7e7132ec284a9ef071ede8 Mon Sep 17 00:00:00 2001 From: Kai Schmidt Date: Sat, 28 Dec 2024 13:31:00 -0800 Subject: [PATCH] fix more data def stuff fixes #640 --- src/compile/data.rs | 14 ++++++-------- src/compile/invert/un.rs | 1 + src/primitive/mod.rs | 10 +++++++--- tests/data_defs.ua | 13 +++++++++++++ 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/compile/data.rs b/src/compile/data.rs index c1c09f537..ff8eafe01 100644 --- a/src/compile/data.rs +++ b/src/compile/data.rs @@ -330,14 +330,7 @@ impl Compiler { let mut inner = Node::default(); for field in fields.iter().rev() { let mut arg = if let Some(sn) = &field.init { - let mut arg = sn.clone(); - if !boxed { - arg.node.push(Node::ImplPrim( - ImplPrimitive::ValidateNonBoxedVariant, - field.span, - )); - } - arg + sn.clone() } else { self.code_meta .global_references @@ -346,6 +339,11 @@ impl Compiler { }; if boxed { arg.node.push(Node::Label(field.name.clone(), span)); + } else { + arg.node.push(Node::ImplPrim( + ImplPrimitive::ValidateNonBoxedVariant, + field.span, + )) } if !inner.is_empty() { for _ in 0..arg.sig.args { diff --git a/src/compile/invert/un.rs b/src/compile/invert/un.rs index aa6f7131c..ca9cb43a0 100644 --- a/src/compile/invert/un.rs +++ b/src/compile/invert/un.rs @@ -977,6 +977,7 @@ impl InvertPattern for Trivial { } [node @ SetOutputComment { .. }, input @ ..] => Ok((input, node.clone())), [Call(f, _), input @ ..] => Ok((input, asm[f].un_inverse(asm).map_err(|e| e.func(f))?)), + [ImplPrim(ValidateNonBoxedVariant, _), input @ ..] => Ok((input, Node::empty())), _ => generic(), } } diff --git a/src/primitive/mod.rs b/src/primitive/mod.rs index 1cfef01ab..3ed635818 100644 --- a/src/primitive/mod.rs +++ b/src/primitive/mod.rs @@ -1673,11 +1673,11 @@ impl ImplPrimitive { let val = env.pop(1)?; if !matches!(val, Value::Num(_) | Value::Byte(_) | Value::Box(_)) { return Err(env.error(format!( - "Variant field must be numbers or boxes, but it is {}", + "Non-boxed variant field must be numbers or boxes, but it is {}", val.type_name_plural() ))); } - if val.rank() > 1 { + if val.rank() > 0 { return Err(env.error(format!( "Non-boxed variant field must be rank 0 or 1, but it is rank {}", val.rank() @@ -1697,7 +1697,11 @@ impl ImplPrimitive { let (head, tail) = val.unjoin(env).unwrap(); let set_tag = head.unboxed(); if tag != set_tag { - return Err(env.error(format!("Variant tag is {set_tag} instead of {tag}"))); + return Err(env.error(if set_tag.rank() == 0 { + format!("Variant tag is {set_tag} instead of {tag}") + } else { + format!("Variant tag is rank {} instead of {tag}", set_tag.rank()) + })); } env.push(tail); } diff --git a/tests/data_defs.ua b/tests/data_defs.ua index 42964047a..56569d718 100644 --- a/tests/data_defs.ua +++ b/tests/data_defs.ua @@ -51,3 +51,16 @@ S~PushB ~Foo {A: °0type|B: °0type} ⍤⤙≍ [7 42] ⊟ °Foo Foo 7 42 +~Foo [A: °0type|B: °0type] +⍤⤙≍ [7 42] ⊟ °Foo Foo 7 42 + +┌─╴Foo + |Bar [A] + |Baz [A] + |Qux +└─╴ +⍤⤙≍ 1 /↥⌕"Non-boxed variant" ⍣Foo~Bar⋅∘ @a +⍤⤙≍ 1 /↥⌕"Non-boxed variant" ⍣Foo~Baz⋅∘ [1 2 3] +⍤⤙≍ [0 5] Foo~Bar 5 +⍤⤙≍ [1 5] Foo~Baz 5 +⍤⤙≍ 2 Foo~Qux