Skip to content

Commit

Permalink
Add missing lowering of Spicy unit ctor to HILTI struct ctor.
Browse files Browse the repository at this point in the history
Closes #1275.
  • Loading branch information
bbannier committed Jun 26, 2023
1 parent b8d2000 commit f7ec2fd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions spicy/toolchain/src/compiler/codegen/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ struct VisitorPass2 : public hilti::visitor::PreOrder<void, VisitorPass2> {
replaceNode(&p, std::move(x));
}

result_t operator()(const spicy::ctor::Unit& c, position_t p) {
// Replace unit ctor with an equivalent struct ctor.
auto n = hilti::ctor::Struct(c.fields().copy(), c.meta());
replaceNode(&p, n);
}

result_t operator()(const operator_::unit::ConnectFilter& n, position_t p) {
auto x = builder::call("spicy_rt::filter_connect", {n.op0(), argument(n.op2(), 0)});
replaceNode(&p, std::move(x));
Expand Down
7 changes: 7 additions & 0 deletions tests/Baseline/spicy.types.unit.var-struct-init/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
foo::Y {
x: foo::X {
x: 4
}
y: 2
}
15 changes: 15 additions & 0 deletions tests/spicy/types/unit/var-struct-init.spicy
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# @TEST-DOC: Validates that unit vars can be assigned to with struct initialization. Regression test for #1275.
#
# @TEST-EXEC: spicyc -dj %INPUT -o foo.hlto
# @TEST-EXEC: printf '\x02' | spicy-dump foo.hlto >output 2>&1
# @TEST-EXEC: btest-diff output
module foo;

type X = unit {
x: uint8;
};

public type Y = unit {
var x: X;
y: uint8 { self.x = [$x = $$ * 2]; }
};

0 comments on commit f7ec2fd

Please sign in to comment.