Skip to content

Commit

Permalink
Fix another assertion with anonymous struct constructor.
Browse files Browse the repository at this point in the history
The struct type's ID could end up not making it across the
Spicy-to-HILTI boundary.

Closes #1234.
  • Loading branch information
rsmmr committed Aug 18, 2022
1 parent 9cbc550 commit fcdc6f5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion hilti/toolchain/include/ast/ctors/struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class Struct : public NodeBase, public hilti::trait::isCtor {
return {};
}

void setType(const type::Struct& x) { children()[0] = x; }
void setType(const Type& t) { children()[0] = t; }
void setTypeID(ID id) { children()[0].as<Type>().setTypeID(std::move(id)); }

bool operator==(const Struct& other) const { return fields() == other.fields(); }

Expand Down
5 changes: 4 additions & 1 deletion hilti/toolchain/src/compiler/visitors/resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ struct Visitor : public visitor::PostOrder<void, Visitor> {
field_types.emplace_back(declaration::Field(f.id(), f.expression().type(), std::nullopt, f.id().meta()));
}

auto ntype = type::Struct(type::Struct::AnonymousStruct{}, std::move(field_types), u.meta());
Type ntype = type::Struct(type::Struct::AnonymousStruct{}, std::move(field_types), u.meta());
if ( auto id = u.type().typeID() )
ntype.setTypeID(*id);

logChange(p.node, ntype);
p.node.as<ctor::Struct>().setType(ntype);
modified = true;
Expand Down
3 changes: 3 additions & 0 deletions tests/Baseline/spicy.types.unit.struct-ctor-init-2/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
[error] <...>/struct-ctor-init-2.spicy:12:11: type cannot be used as key type for maps (because it is not sortable)
[error] spicyc: aborting after errors
15 changes: 15 additions & 0 deletions tests/spicy/types/unit/struct-ctor-init-2.spicy
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# @TEST-EXEC-FAIL: spicyc -j %INPUT >output 2>&1
# @TEST-EXEC: btest-diff output
#
# @TEST-DOC: Regression test for #1234.

module test;

type Key = unit {a: uint8;};

# The below will be rejeced because units can't be used a key types, but that's
# fine, we just want to make sure type resolution doesn't complain.
global m: map<Key, uint8>;
m[[$a=1]] = 1;

print m;

0 comments on commit fcdc6f5

Please sign in to comment.