Repository navigation
self-host: gen-c writes - -x as --x, a pre-decrement #6046
Copy link
Copy link
Closed
Labels
owner-approved-foreignOwner-approved exception to the only-t27 rule: hand-written foreign code allowed in this PROwner-approved exception to the only-t27 rule: hand-written foreign code allowed in this PR
Description
Profile (read first)
You are working as Gamma (Compiler Core) on epic #5980: rewrite t27c in t27 itself.
The self-hosted core (specs/compiler/core/t27core.t27, PR #6041) must write exactly the
bytes t27c gen-c writes, so every gen-c defect is a defect the core has to refuse instead of
learn. Fixing it here is what lets the core grow. Rules for this task:
- Change the Rust emitter only inside the Boundary below.
gen/is generated: never hand-edit it. - Build with
cargo build --release -p t27cand use./target/release/t27cfrom the repository
root, not at27con PATH. - Write the regression test first and watch it fail on the unfixed compiler. Then fix, and
revert the fix once to watch the test go red again. Say in the PR that you did both. - Any other spec whose generated C changes must be listed in the PR by path, with one line on
why the new bytes are right. A silent corpus change is a failed task. - The PR body says
Closes #plus this issue's number andPart of #5980. ASCII only in source files.
Context
A unary minus applied to a unary minus is emitted without a space or parentheses, so - -x becomes the C pre-decrement --x. The function then MUTATES its argument and returns x - 1 instead of x. The self-hosted core refuses this shape (E_SYNTAX) rather than copy it.
Reproduce
cargo build --release -p t27c
printf 'module m;\nfn f(x: i64) -> i64 {\n return - -x;\n}\n' > /tmp/neg.t27
./target/release/t27c gen-c /tmp/neg.t27
Today the relevant output is:
int64_t f(int64_t x) {
return --x;
}
Acceptance criteria
-
./target/release/t27c gen-c /tmp/neg.t27 | grep -c -- '--x'prints0(today1)
-
./target/release/t27c gen-c /tmp/neg.t27 > /tmp/neg.c && printf '#include "neg.c"\nint main(void){return f(3)==3?0:1;}\n' > /tmp/negm.c && cc -w -o /tmp/negm /tmp/negm.c && /tmp/negm; echo $?prints0
-
cargo test --release -p t27c --test genc_double_negationpasses
Boundary
bootstrap/src/compiler.rsbootstrap/tests/genc_double_negation.rs
User Scenarios & Testing
- Given
/tmp/neg.t27written by the Reproduce block,
when./target/release/t27c gen-c /tmp/neg.t27runs and its output is compiled withcc,
thenf(3)returns3, and the same holds for- -1,-(-x)and- - -x(which returns-x).
Requirements
- FR-001: a unary operator whose operand starts with the same operator character MUST NOT be glued to it (
- -x,- -1,! !b,~ ~k). - FR-900:
cargo test --release -p t27c --test genc_double_negationMUST pass, and MUST fail with the fix reverted. - FR-901: if
bootstrap/tests/core_selfhost.rsexists on your base,cargo test --release -p t27c --test core_selfhostMUST still pass.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
owner-approved-foreignOwner-approved exception to the only-t27 rule: hand-written foreign code allowed in this PROwner-approved exception to the only-t27 rule: hand-written foreign code allowed in this PR
Activity
fix(gen-c): keep `- -x` apart instead of emitting the decrement `--x` (…
fix(gen-c): refuse text after `endmodule` instead of dropping it (Closes
fix(gen-c): a const initializer is the whole expression, not its firs…
fix(gen-c): emit a char const instead of dropping it (Closes #6049)
fix(gen-c): a global `** N` repeat keeps its value instead of zeros (C…
fix(gen-c): include <assert.h> where `assert` is used, not only with …
fix(gen-c): a test's write to a module var writes the global (Closes #…
fix(gen-c): keep `- -x` apart instead of emitting the decrement `--x` (…
fix(gen-c): refuse text after `endmodule` instead of dropping it (Closes
fix(gen-c): a const initializer is the whole expression, not its firs…
fix(gen-c): emit a char const instead of dropping it (Closes #6049)
fix(gen-c): a global `** N` repeat keeps its value instead of zeros (C…
fix(gen-c): include <assert.h> where `assert` is used, not only with …
fix(gen-c): a test's write to a module var writes the global (Closes #…
fix(gen-c): keep `- -x` apart instead of emitting the decrement `--x` (…
fix(gen-c): refuse text after `endmodule` instead of dropping it (Closes
fix(gen-c): a const initializer is the whole expression, not its firs…
fix(gen-c): emit a char const instead of dropping it (Closes #6049)
fix(gen-c): a global `** N` repeat keeps its value instead of zeros (C…
fix(gen-c): include <assert.h> where `assert` is used, not only with …
fix(gen-c): a test's write to a module var writes the global (Closes #…
Merge remote-tracking branch 'origin/master' into feat/tri-night (Refs …
Merge remote-tracking branch 'origin/master' into pr-6695 (Refs #6664)
Merge remote-tracking branch 'origin/master' into HEAD (Refs #6936)