Skip to content

Commit

Permalink
Fix parsing for vectors of literals.
Browse files Browse the repository at this point in the history
This was broken in two ways:

1. with the `(LITERAL)[]` syntax, the parser would not recognize literals
   using type constructors
2. with the syntax `LITERAL[]`, we'd try to store the parsed value
   into a vector

Closes #1860.

(cherry picked from commit f3a0290)
  • Loading branch information
rsmmr committed Sep 11, 2024
1 parent fbd3b70 commit 52db333
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion spicy/toolchain/src/compiler/codegen/parsers/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct TypeParser {
if ( dst )
return dst;

if ( meta.field() )
if ( meta.field() && meta.isFieldProduction() )
return builder()->addTmp("x", meta.field()->parseType());

return builder()->addTmp("x", t);
Expand Down
2 changes: 1 addition & 1 deletion spicy/toolchain/src/compiler/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ unit_field_ctor
| INT64 '(' const_sint ')' { $$ = builder->ctorSignedInteger($3, 64, __loc__); }

unit_field_in_container
: ctor opt_unit_field_args opt_attributes
: unit_field_ctor opt_unit_field_args opt_attributes
{ $$ = builder->typeUnitItemUnresolvedField({}, std::move($1), false, std::move($2), {}, {}, std::move($3), {}, {}, __loc__); }
| scoped_id opt_unit_field_args opt_unit_field_repeat opt_attributes
{ $$ = builder->typeUnitItemUnresolvedField({}, std::move($1), false, std::move($2), std::move($3), {}, std::move($4), {}, {}, __loc__); }
Expand Down
2 changes: 2 additions & 0 deletions tests/Baseline/spicy.types.vector.ctor-vector/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
[$x1=[0, 0], $x2=[1, 1], $x3=[b"abc", b"abc"]]
22 changes: 22 additions & 0 deletions tests/spicy/types/vector/ctor-vector.spicy
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# @TEST-EXEC: ${SCRIPTS}/printf '\x00\x00\x01\x01abcabc' | spicy-driver -d %INPUT >output
# @TEST-EXEC: btest-diff output
#
# @TEST-DOC: Test parsing vectors of constants; regression test for #1860.

module Test;

public type X = unit {
x1: (uint8(0))[] foreach {
assert $$ == 0: "unreachable";
}

x2: uint8(1)[] foreach {
assert $$ == 1: "unreachable";
}

x3: b"abc"[] foreach {
assert $$ == b"abc": "unreachable";
}

on %done { print self; }
};

0 comments on commit 52db333

Please sign in to comment.