Repository navigation
Bug-review fixes: backend, constant folding, lexer/parser, diagnostics; octal and hex literals - #24
Merged
Merged
Conversation
- 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 Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 -O0on about 30,000 random programs and found no remaining mismatches.Wrong code (backend)
SetCCon a register encodes that register instead of alwaysAL.double→unsigned intuses the destination type. Out-of-range values and NaN give 0, and codegen and constant folding agree.INT_MIN / -1andINT_MIN % -1wrap instead of crashing with SIGFPE..text.unreachable!().intwidth, not the operands' width.longa 4-byte slot, so later 8-byte writes overwrote neighbouring variables..rodata.Constant folding (frontend)
?:converts the chosen branch to the common type, so(1 ? -6 : 0u) / 2is 2147483645 in static initializers and case labels.unsigned longfolds to 2^63, the same value the runtime conversion gives.1.0 / 0folds to infinity instead of being rejected; operands are converted before the zero check.&&,||and?:skip operands that aren't evaluated, sostatic int a = 1 || 1 / 0;is accepted, and overflow in a skipped operand doesn't warn.binary_typesholds the binary-operator type rules.typecheck_expand constant folding both use it, so compile-time and runtime results can't drift apart.Lexer and parser
/*/no longer closes the comment it opens.forheader clauses are errors instead of being read as empty clauses.,or)between items.f(void)is no longer accepted as a call.-2147483648is along.010was read as 10, and08was accepted.0literals are now read in base 8, with C11's type rules, so037777777777isunsigned int.08.5is still a double.#only starts a skipped line when it is the first non-blank character on the line.0x/0X), with the same type rules as octal. Hex floating literals aren't supported.Semantics
externthat hides a local can't link to a file-scopestatic; that's now an error.b, notb.1)%, shift and bitwise operators on doubles in constant expressionsWarnings (all checked against gcc)
-Wswitch-unreachablefollows gcc's model of which code before the first case is reachable.-Wsequence-pointcatches conflicts across&&,||,?:and call arguments.-Wshadow:externthat hides a local-Wunused-parameterand parameter shadow warnings point at the parameter.unsigned long(W1)-Wconstant-conversion:Docs
double→unsigned longresults.unsigned longconversion results.Known differences from gcc (left as is)
-Wdiv-by-zerostill warns for1 || 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 warningsandcargo fmt --checkare clean.🤖 Generated with Claude Code