From 52db33320a19b12fc2deb8cd5d1f0513ed67fa70 Mon Sep 17 00:00:00 2001 From: Robin Sommer Date: Mon, 9 Sep 2024 08:50:05 +0200 Subject: [PATCH] Fix parsing for vectors of literals. 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 f3a02904c0ea1038b8ab7e1fa672e7e6347d6b5d) --- .../src/compiler/codegen/parsers/types.cc | 2 +- spicy/toolchain/src/compiler/parser/parser.yy | 2 +- .../spicy.types.vector.ctor-vector/output | 2 ++ tests/spicy/types/vector/ctor-vector.spicy | 22 +++++++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/Baseline/spicy.types.vector.ctor-vector/output create mode 100644 tests/spicy/types/vector/ctor-vector.spicy diff --git a/spicy/toolchain/src/compiler/codegen/parsers/types.cc b/spicy/toolchain/src/compiler/codegen/parsers/types.cc index dbdce24df..8a90e983d 100644 --- a/spicy/toolchain/src/compiler/codegen/parsers/types.cc +++ b/spicy/toolchain/src/compiler/codegen/parsers/types.cc @@ -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); diff --git a/spicy/toolchain/src/compiler/parser/parser.yy b/spicy/toolchain/src/compiler/parser/parser.yy index cc2b9d390..393bd46ed 100644 --- a/spicy/toolchain/src/compiler/parser/parser.yy +++ b/spicy/toolchain/src/compiler/parser/parser.yy @@ -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__); } diff --git a/tests/Baseline/spicy.types.vector.ctor-vector/output b/tests/Baseline/spicy.types.vector.ctor-vector/output new file mode 100644 index 000000000..4c7c295fe --- /dev/null +++ b/tests/Baseline/spicy.types.vector.ctor-vector/output @@ -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"]] diff --git a/tests/spicy/types/vector/ctor-vector.spicy b/tests/spicy/types/vector/ctor-vector.spicy new file mode 100644 index 000000000..ff19eeeb1 --- /dev/null +++ b/tests/spicy/types/vector/ctor-vector.spicy @@ -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; } +};