Skip to content

Commit

Permalink
Fix when input redirection becomes visible.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rsmmr committed Sep 30, 2024
1 parent 7347a2b commit ea2deb1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
3 changes: 1 addition & 2 deletions spicy/toolchain/src/compiler/codegen/parser-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions tests/Baseline/spicy.types.unit.size-with-offset/output
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions tests/spicy/types/unit/size-with-offset.spicy
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit ea2deb1

Please # to comment.