Repository navigation
self-host: gen-c keeps only the first token of a top-level const initializer #6048
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 top-level const whose initializer is more than one token is cut to its first token: true and false becomes true, 5 as u8 becomes 5, and ~5 / !true emit no #define at all. Any later use of C or D is then an undeclared name. The self-hosted core refuses these (E_CONST).
Reproduce
cargo build --release -p t27c
printf 'module m;\nconst A: u8 = 5 as u8;\nconst B: bool = true and false;\nconst C: i64 = ~5;\nconst D: bool = !true;\n' > /tmp/consts.t27
./target/release/t27c gen-c /tmp/consts.t27
Today the relevant output is:
#define A 5
#define B true
(nothing for C or D)
Acceptance criteria
-
./target/release/t27c gen-c /tmp/consts.t27 | grep -cE '^#define (A|B|C|D) 'prints4(today2)
-
./target/release/t27c gen-c /tmp/consts.t27 > /tmp/consts.c && printf '#include "consts.c"\nint main(void){return (A==5 && B==0 && C==~5 && D==0)?0:1;}\n' > /tmp/constsm.c && cc -w -o /tmp/constsm /tmp/constsm.c && /tmp/constsm; echo $?prints0
-
cargo test --release -p t27c --test genc_const_initializerpasses
Boundary
bootstrap/src/compiler.rsbootstrap/tests/genc_const_initializer.rs
User Scenarios & Testing
- Given
/tmp/consts.t27written by the Reproduce block,
when./target/release/t27c gen-c /tmp/consts.t27runs and its output is compiled withcc,
thenA == 5,Bis false,C == ~5andDis false in C.
Requirements
- FR-001: the whole initializer expression MUST be emitted, parenthesised, through the same expression emitter functions use.
- FR-002: a
#defineMUST NOT be dropped for an initializer the emitter can lower. - FR-900:
cargo test --release -p t27c --test genc_const_initializerMUST 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): a const initializer is the whole expression, not its firs…
fix(gen-c): a const initializer is the whole expression, not its firs…
fix(gen-c): a const initializer is the whole expression, not its firs…