Skip to content

feat(codegen): a pure arithmetic tree is exact and narrowed only at its root (SPEC-wide-intermediates task 1) - #342

Merged
xzawed merged 3 commits into
mainfrom
feat/wide-intermediates
Sep 26, 2026
Merged

xzawed merged 3 commits into
mainfrom
feat/wide-intermediates

Conversation

@xzawed

@xzawed xzawed commented Sep 26, 2026 •

Copy link
Copy Markdown
Owner

What

SPEC-wide-intermediates §6 task 1 (DP-W1–W11, user-confirmed 2026-09-27). In a fallible context — a ! body or a checked copy — an i32 arithmetic tree whose leaves are variables, i32 constants, len, or calls to loop-free infallible functions without string arguments is computed in i64 with checked ops and narrowed once, where its value has to become an i32 (let, assignment, argument, index, as f64, return, an impure parent). A comparison of two pure sides compares exact values.

  • ir::loop_free_functions — infallible functions with no while anywhere they reach (DP-W2).
  • codegen::{is_pure, is_leaf_argument, emit_exact, emit_wide} — exhaustive matches (the crate denies wildcard arms).
  • Not leaves: array reads (a later -2 would overtake a real overflow), calls with while (hang), calls with string args and byte_len (bounded only by the host's NUL contract). Those operators keep today's per-operator check.
  • Unchanged: infallible bodies (DP-W10), fingerprint, ABI and bindings (DP-W8) — headers/units goldens are byte-identical; only generated.rs.txt moves.
  • Two pins moved with the rule: checked_arithmetic mid (2^30 now answers) and helper_check_propagation big (product now stored in a let, so it still narrows).

Docs: LANGUAGE.md (the rule, DP-O9 restated, the extraction price, the D19 C-backend note), HOST_ABI.md, SPEC status → 확정·구현 중 plus §2.6 helper-return price, index row, STATUS ▶ 1.

Evidence (E2)

  • Oracle wide_intermediates.rs, 7 tests: A (exact / -3 / i64 bound / x/0 = 0 / len leaf / narrowing at assignment, argument, index), B (ordering -3/-2/-2, call leaves incl. byte_len), C (no hang, 10 s thread deadline), D (comparison, as f64, literals, domain fail), E (infallible wraps, checked copy widens), and the price (x*x/x = 46341; let and sq(x)/x → -3).
  • Full local gate (MATHLESS_GATE_D/FPC/FPC_HOST=require): 704 passed, 0 failed on 7c2fe4e and again on the head 10fe91f.
  • Plants on the committed tree (--no-fail-fast), each caught by the test meant for it:
Plant Caught by
array read as a leaf B: idx_after −2 ≠ −3
while calls admitted B call_while (0 ≠ −3) + C deadline
string-arg check dropped B call_str
byte_len as a leaf B direct_blen
narrow with as i32 instead of try_from A, B, C, D, price
zero-divisor guard removed A quot_div + checked_arithmetic
exact comparison removed D cmp
as f64 widened D tof_over
infallible bodies widened E plain (100000 ≠ 14100)
len not a leaf A len_leaf

Narrowing at assignment / argument / index: probe under main's codegen gives -3 for all three, head gives the exact values.

Grok verify (single claims, REVIEW ONLY)

  • is_pure leaf set — CONFIRMED; is_leaf_argument string rule — CONFIRMED; emit_exact order / zero divisor / wrapping_rem / checked ops — CONFIRMED; emit_wide infallible no-op + root narrowing + comparison gate — CONFIRMED; bail threading after refactor — CONFIRMED; LANGUAGE values ↔ oracle asserts — CONFIRMED.
  • loop_free_functions — DISCREPANCY in my prompt (I wrote that only If nests statements; While does too, and has_while returns true on it) — the code was confirmed.
  • Unasked findings, re-checked against code: missing len/assignment/argument/index pins → added; emit_exact's unwrap_or_default bail → removed (bail passed in); rhs.ty unchecked in the comparison gate → typeck gives both sides one type (comment added); Len ignores an operand → Len has none (ir.rs 279); ConstStr non-Str type → cannot occur.

Task 2 (real C host on an existing example, 626-row re-measure, slice close) follows in a separate PR.

🤖 Generated with Claude Code

xzawed and others added 3 commits September 27, 2026 00:53
…ts root (SPEC-wide-intermediates task 1)

In a fallible context (a `!` body or a checked copy) an i32 arithmetic tree whose leaves are
variables, i32 constants, `len`, or calls to loop-free infallible functions without string
arguments is computed in i64 with checked ops and narrowed once, where its value has to
become an i32 (DP-W1..W3, W5). A comparison of two pure sides compares exact values (DP-W4);
`as f64` narrows first (DP-W6); DP-O9 is restated on exact values (DP-W7). Array reads, calls
with `while`, calls with string arguments and `byte_len` keep the per-operator check, so a
later -2 never overtakes a real overflow and no call can hang where today fails first.
Infallible bodies, the fingerprint, the ABI and the bindings are unchanged (DP-W8, DP-W10).

Oracle: wide_intermediates.rs (acceptance A-E and the extraction price). Two existing pins
moved with the rule: checked_arithmetic mid (2^30 now answers) and helper_check_propagation
big (the product is now stored in a let, so it still narrows).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
… and index; emit_exact takes its bail

Grok verify (unasked lines): LANGUAGE.md names `len` as a leaf and assignment, argument and
index as narrowing points, and the oracle asserted none of them. emit_exact computed its own
bail with unwrap_or_default, an empty arm that was unreachable only because emit_wide checks
first; the bail is now passed in, so there is no default to reach.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@xzawed

xzawed commented Sep 26, 2026

Copy link
Copy Markdown
Owner Author

⚠️ AI 리뷰가 완전 검증되지 않음 — 기본값이 적용됐습니다(비활성/API 키 없음/빈 diff). AI 배점은 측정값이 아니므로 총점을 부분 점수로 보세요.
ℹ️ 범용 검사기만 본 언어 포함: rust — 이 배포에서 해당 언어의 전담 분석기가 하나도 실행되지 않아, 다언어 범용 분석기 하나에만 기대었습니다. 그 규칙 밀도는 언어마다 편차가 큽니다 — 여기서의 만점은 "깨끗함" 이 아니라 "거의 검사 안 됨" 입니다 — AI 리뷰와 수동 검토로 보완하세요.

🔵 SCAManager 분석 결과

총점: 89/100 (등급 B)

점수 상세

항목 점수 만점
커밋 메시지 13 15
코드 품질 25 25
보안 20 20
구현 방향성 (AI) 21 25
테스트 10 15

@xzawed
xzawed merged commit b077cb9 into main Sep 26, 2026
2 checks passed
@xzawed
xzawed deleted the feat/wide-intermediates branch September 26, 2026 16:54
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