Skip to content

Commit

Permalink
fix more data def stuff
Browse files Browse the repository at this point in the history
fixes #640
  • Loading branch information
kaikalii committed Dec 28, 2024
1 parent 765c5a4 commit d2464e0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
14 changes: 6 additions & 8 deletions src/compile/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/compile/invert/un.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}
Expand Down
13 changes: 13 additions & 0 deletions tests/data_defs.ua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d2464e0

Please sign in to comment.