Skip to content

Bug-review fixes: backend, constant folding, lexer/parser, diagnostics; octal and hex literals - #24

Merged
johnhringiv merged 6 commits into
mainfrom
latent-bug-fixes
Oct 27, 2026
Merged

johnhringiv merged 6 commits into
mainfrom
latent-bug-fixes

Conversation

@johnhringiv

Copy link
Copy Markdown
Owner

Fixes every bug from the July and October bug reviews, plus three small language additions: octal and hex literals, and line splicing. Every fix has a regression test that fails on the commit before it. A differential fuzzer compared NCC with gcc -w -fwrapv -O0 on about 30,000 random programs and found no remaining mismatches.

Wrong code (backend)

  • B1: shift counts in memory are loaded at 4 bytes, not the shifted value's width. Before, this read past the end of the slot.
  • B2: SetCC on a register encodes that register instead of always AL.
  • B3: double → unsigned int uses the destination type. Out-of-range values and NaN give 0, and codegen and constant folding agree.
  • B4/F3: INT_MIN / -1 and INT_MIN % -1 wrap instead of crashing with SIGFPE.
  • B5: each function's symbol size ends at the next function, not at the end of .text.
  • B6: the impossible xorps-from-stack case is now unreachable!().
  • B7: comparison results are zeroed at int width, not the operands' width.
  • B8: stack slots are sized from the symbol table. Truncate and long shift counts gave a long a 4-byte slot, so later 8-byte writes overwrote neighbouring variables.
  • B9: the 16-byte-aligned xorps negation mask is padded to 16 bytes in .rodata.

Constant folding (frontend)

  • F1: ?: converts the chosen branch to the common type, so (1 ? -6 : 0u) / 2 is 2147483645 in static initializers and case labels.
  • F2: NaN → unsigned long folds to 2^63, the same value the runtime conversion gives.
  • F4: 1.0 / 0 folds to infinity instead of being rejected; operands are converted before the zero check.
  • F5: &&, || and ?: skip operands that aren't evaluated, so static int a = 1 || 1 / 0; is accepted, and overflow in a skipped operand doesn't warn.
  • binary_types holds the binary-operator type rules. typecheck_exp and constant folding both use it, so compile-time and runtime results can't drift apart.

Lexer and parser

  • P1: /*/ no longer closes the comment it opens.
  • P2: malformed for header clauses are errors instead of being read as empty clauses.
  • P3: parameter and argument lists require , or ) between items.
  • P4: f(void) is no longer accepted as a call.
  • P5: -2147483648 is a long.
  • P6: octal literals: 010 was read as 10, and 08 was accepted.
    • Leading-0 literals are now read in base 8, with C11's type rules, so 037777777777 is unsigned int.
    • 8 and 9 are rejected in octal literals; 08.5 is still a double.
  • P7: form feed and vertical tab are whitespace.
  • P8: a backslash at the end of a line joins it to the next line before tokenizing, and error locations still point at the original source. # only starts a skipped line when it is the first non-blank character on the line.
  • New: hex literals (0x/0X), with the same type rules as octal. Hex floating literals aren't supported.

Semantics

  • V1: a block-scope extern that hides a local can't link to a file-scope static; that's now an error.
  • Clearer messages:
    • "conflicting types" vs "redeclared as different kind of symbol"
    • source names in errors (b, not b.1)
    • correct messages for %, shift and bitwise operators on doubles in constant expressions
    • a location for the switch-on-double error

Warnings (all checked against gcc)

  • -Wswitch-unreachable follows gcc's model of which code before the first case is reachable.
  • -Wsequence-point catches conflicts across &&, ||, ?: and call arguments.
  • -Wshadow:
    • stays silent for prototype parameters and for variables that shadow a function
    • warns for an extern that hides a local
  • -Wunused-parameter and parameter shadow warnings point at the parameter.
  • Shift-count warnings:
    • also fire in file-scope initializers
    • use width 64 for unsigned long (W1)
    • report a huge unsigned count as too wide, not negative
  • -Wconstant-conversion:
    • covers automatic locals with constant initializers
    • covers case values that change when converted to the switch type

Docs

  • README: supported features (integer literals, line splicing), the warning descriptions, and the exact double → unsigned long results.
  • CLAUDE.md: notes on line splicing and the unsigned long conversion results.

Known differences from gcc (left as is)

  • -Wdiv-by-zero still warns for 1 || 1 / 0.
  • static int a = 1 || x; is rejected, as the standard requires; gcc accepts it as an extension.

Testing

cargo test: 30 passed. cargo clippy -D warnings and cargo fmt --check are clean.

🤖 Generated with Claude Code

johnhringiv and others added 6 commits October 24, 2026 22:09
- B1: load memory shift counts at 4 bytes, not the shifted value's width
- B2: SetCC on a register now encodes the matched register, not always AL
- B3: double -> unsigned int keys on dst type; out-of-range/NaN -> 0
  (codegen and const-eval agree)
- B4/F3: INT_MIN / -1 and % -1 wrap instead of trapping with SIGFPE
- B5: function symbol sizes end at the next function, not end of .text
- B6: unreachable xorps-from-stack arm is now unreachable!()
- B7: zero comparison results at int width, not the operands' width

Each fix has a regression test in tests/c_programs/ or tests/runner.rs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Backend:
- B8: size stack slots from the symbol table, not the first instruction
  that touches them. Truncate and long shift counts gave longs 4-byte
  slots, so later 8-byte writes clobbered neighbours (miscompile).
- B9: pad the 16-aligned xorps negation mask to 16 bytes in .rodata

Lexer/parser:
- P1: `/*/` no longer closes the comment it opens
- P2: malformed for-header clauses are errors, not empty clauses
- P3: parameter/argument lists require `,` or `)` between items
- P4: `f(void)` is no longer accepted as a call
- P5: `-2147483648` is a long, not an int
- P7: form feed and vertical tab are whitespace

Warnings:
- W1: -Wshift-count-overflow uses width 64 for unsigned long

Every fix has a regression test that fails on the previous commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Language:
- Octal integer literals (leading 0), with C11 type ladders: unsuffixed
  octal can become unsigned (int -> unsigned int -> long -> unsigned
  long). 8 and 9 are rejected in octal; 08.5 stays a valid double.
- Backslash-newline splicing before tokenizing (translation phase 2),
  with a byte -> source position table so spans stay accurate. `#` only
  starts a skipped line as the first non-blank character of a line.

Semantics:
- A block-scope extern that hides a local can't bind to a file-scope
  static (it would need both linkages); now an error.

Diagnostics (all checked against gcc):
- -Wswitch-unreachable follows gcc's reachability model
- -Wsequence-point catches conflicts across &&/||/?: and call arguments
- -Wshadow: silent for prototype params and function names; warns for an
  extern hiding a local
- shift-count warnings in file-scope initializers
- -Wconstant-conversion for automatic locals with constant initializers
- parameter warnings point at the parameter; switch-on-double error has
  a location; correct messages for %/shift/bitwise on doubles in constant
  expressions; source names (not b.1) and "conflicting types" wording

Every fix has a test that fails on the previous commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Factor the binary-operator type rules into binary_types, shared by
typecheck_exp and eval_constant_expr, so compile-time folding converts
operands the same way runtime code does.

- F1: ?: converts the taken branch to the common type of both branches.
- F4: operands are converted before the zero-divisor check, so
  1.0 / 0 folds to infinity instead of being rejected.
- F5: && / || and the untaken ?: branch are folded as unevaluated
  operands: a zero divisor there is not an error and overflow there
  does not warn. They must still be constant expressions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
double_to_u64 took the subtract-2^63 path for NaN and folded to 0,
while the runtime lowering fails the range check and yields 2^63.
Docs now spell out the unsigned long conversion results.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Hex integer literals (0x/0X) with the same type ladders as octal; the
  lexer and parser share literal_radix. Hex floating literals are not
  supported.
- -Wconstant-conversion for a case value narrowed to the switch type
  (gcc reports these as -Woverflow). Sign-only changes don't warn.
- A huge unsigned shift count is reported as too wide, not negative.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.05520% with 28 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/parser.rs 89.91% 12 Missing ⚠️
src/emit_iced.rs 60.71% 11 Missing ⚠️
src/validate.rs 98.29% 3 Missing ⚠️
src/lexer.rs 97.05% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@johnhringiv
johnhringiv merged commit 2bca1f4 into main Oct 27, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant