From ea2deb1f94ef2cc32acb1cec1ec86a717041b009 Mon Sep 17 00:00:00 2001 From: Robin Sommer Date: Fri, 27 Sep 2024 13:00:28 +0200 Subject: [PATCH] Fix when input redirection becomes visible. With `&parse-at/from` we were updating the internal state on our current position immediately, meaning they were visible already when evaluating other attributes on the same field afterwards, which is unexpected. Closes #1842. --- .../src/compiler/codegen/parser-builder.cc | 3 +- .../spicy.types.unit.size-with-offset/output | 5 ++++ tests/spicy/types/unit/size-with-offset.spicy | 30 +++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 tests/Baseline/spicy.types.unit.size-with-offset/output create mode 100644 tests/spicy/types/unit/size-with-offset.spicy diff --git a/spicy/toolchain/src/compiler/codegen/parser-builder.cc b/spicy/toolchain/src/compiler/codegen/parser-builder.cc index 36a23a83a0..67f4f2542b 100644 --- a/spicy/toolchain/src/compiler/codegen/parser-builder.cc +++ b/spicy/toolchain/src/compiler/codegen/parser-builder.cc @@ -548,6 +548,7 @@ struct ProductionVisitor : public production::Visitor { auto etype = c->parseType()->type()->elementType(); auto container_element = builder()->addTmp("elem", etype); pushDestination(container_element); + pb->saveParsePosition(); // need to update position for container elements in case input is redirected } else if ( ! meta.isFieldProduction() ) @@ -1430,7 +1431,6 @@ struct ProductionVisitor : public production::Visitor { pstate.cur = builder()->addTmp("parse_cur", builder()->typeStreamView(), builder()->deref(tmp)); pstate.ncur = {}; pushState(std::move(pstate)); - pb->saveParsePosition(); } // Redirects input to be read from given stream position next. @@ -1446,7 +1446,6 @@ struct ProductionVisitor : public production::Visitor { pstate.cur = builder()->addTmp("parse_cur", cur); pstate.ncur = {}; pushState(std::move(pstate)); - pb->saveParsePosition(); } // Start sync and trial mode. diff --git a/tests/Baseline/spicy.types.unit.size-with-offset/output b/tests/Baseline/spicy.types.unit.size-with-offset/output new file mode 100644 index 0000000000..a60e44c0a4 --- /dev/null +++ b/tests/Baseline/spicy.types.unit.size-with-offset/output @@ -0,0 +1,5 @@ +### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. +x, 3 +x, \x01\x02\x03 +z, 4 +z, 1234 diff --git a/tests/spicy/types/unit/size-with-offset.spicy b/tests/spicy/types/unit/size-with-offset.spicy new file mode 100644 index 0000000000..a61cf980d5 --- /dev/null +++ b/tests/spicy/types/unit/size-with-offset.spicy @@ -0,0 +1,30 @@ +# @TEST-EXEC: ${SCRIPTS}/printf '\x01\x02\x03\x04\0x05' | spicy-driver %INPUT >output +# @TEST-EXEC: btest-diff output +# +# @TEST-EXED: Check that changing the input with `&parse-at` isn't visible to other attributes on same field; regression test for #1842. + +module Test; + +import spicy; + +public type X = unit { + a: uint8; + b: uint8; + c: uint8; + + x: bytes &size=self.offset() # yields 3 + &parse-at=self.input() { + print "x", self.offset(); # yields still 3 + print "x", $$; + } + + y: uint8; + + z: bytes &size=self.offset() # yields 4 + &parse-from=b"12345" { + print "z", self.offset(); # yields still 4 + print "z", $$; + } + + d: uint8; +};