Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions src/v3/compiler/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1453,15 +1453,10 @@ impl<'a> Ctx<'a> {
});
}
};
let variant_name = variant_parent_info(self.dag, variant_id)
.map(|(_, variant_name)| variant_name)
.unwrap_or_else(|| {
self.dag
.declaration(variant_id)
.name
.clone()
.unwrap_or_else(|| "UnknownVariant".to_string())
});
let Some((_parent_name, variant_name)) = variant_parent_info(self.dag, variant_id)
else {
return Err(EmitError::VariantParentNotFound { variant_id });
};
let mut arm_locals = locals.clone();
if let Some(binding) = &path.binding {
let elide = matches!(
Expand Down Expand Up @@ -3300,6 +3295,41 @@ mod tests {
assert!(!rendered.contains("payload := v.inner;"), "got: {rendered}");
}

#[test]
fn go_sum_branch_fails_when_variant_parent_is_missing() {
let mut dag = compile_to_dag(
"type Choice = Left | Right\nfn pick(choice: Choice) -> Int = match choice { Left => 1, Right => 2 }\n",
"missing_variant_parent.v3",
)
.expect("compiles");
let choice_decl = dag.declaration_by_name("Choice").expect("Choice decl").id;
let right_variant = match &dag.declaration(choice_decl).connective {
TypeConnective::Disj { variants } => {
variants
.iter()
.find(|v| v.label == "Right")
.expect("Right variant in Choice")
.ty
}
_ => panic!("Choice should lower to a Disj"),
};
let TypeConnective::Disj { variants } = &mut dag.declaration_mut(choice_decl).connective
else {
panic!("Choice should lower to a Disj");
};
variants.retain(|variant| variant.ty != right_variant);

let err = emit_module(&dag, EmitTarget::Go).expect_err("missing parent must fail closed");
assert!(
matches!(
err,
EmitDispatchError::Core(EmitError::VariantParentNotFound { variant_id })
if variant_id == right_variant
),
"expected VariantParentNotFound for Right, got {err:?}"
);
}

#[test]
fn behavior_result_port_returns_the_behavior_output_port() {
let mut dag = Dag::new();
Expand Down
4 changes: 4 additions & 0 deletions src/v3/compiler/src/emit/rust_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ pub enum EmitError {
/// inference — either inference didn't run or the scrutinee's
/// Disj has no matching variant.
UnresolvedBranchPattern { variant_name: String },
/// A Branch arm resolved to a variant declaration that no longer
/// has a Disj parent. Emit must not fabricate a plausible target
/// identifier when the substrate cannot identify the parent enum.
VariantParentNotFound { variant_id: DeclarationId },
/// A Branch's scrutinee resolved to a Disj that isn't the v3
/// `Classical` (Bool) sum — PR-B only emits boolean branches.
/// Carries the scrutinee's resolved variant ids so callers can
Expand Down
Loading