Skip to content

Commit

Permalink
Add validation of unit switch attributes.
Browse files Browse the repository at this point in the history
We previously silently ignored unsupported attributes; now errors are
raised.
  • Loading branch information
bbannier committed May 28, 2021
1 parent a4455d4 commit 9ffdf6a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spicy/toolchain/src/compiler/visitors/validator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <hilti/ast/statements/switch.h>
#include <hilti/base/logger.h>
#include <hilti/base/result.h>
#include <hilti/base/util.h>

#include <spicy/ast/all.h>
#include <spicy/ast/detail/visitor.h>
Expand Down Expand Up @@ -645,6 +646,15 @@ struct PreTransformVisitor : public hilti::visitor::PreOrder<void, PreTransformV

if ( defaults > 1 )
error("more than one default case", p);

if ( const auto& attrs = s.attributes() ) {
for ( const auto& attr : attrs->attributes() ) {
const auto& tag = attr.tag();

if ( tag != "&size" )
error(fmt("attribute '%s' is not supported here", tag), p);
}
}
}

void operator()(const spicy::type::unit::item::Variable& v, position_t p) {
Expand Down
4 changes: 4 additions & 0 deletions tests/Baseline/spicy.types.unit.switch-attributes-fail/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
[error] <...>/switch-attributes-fail.spicy:9:5-11:27: attribute '&eod' is not supported here
[error] <...>/switch-attributes-fail.spicy:9:5-11:27: attribute '&until' is not supported here
[error] spicyc: aborting after errors
12 changes: 12 additions & 0 deletions tests/spicy/types/unit/switch-attributes-fail.spicy
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# @TEST-EXEC-FAIL: spicyc -p %INPUT >output 2>&1
# @TEST-EXEC: btest-diff output

# @TEST-DOC: Tests that invalid switch attributes are rejected.

module Mini;

type X = unit {
switch ( 1 ) {
* -> : void;
} &eod &size=1 &until;
};

0 comments on commit 9ffdf6a

Please sign in to comment.