Skip to content

yrc: state that the epoch period is never 0 - #26884

Merged
Araq merged 1 commit into
nim-lang:develfrom
planetis-m:yrc-epoch-period-assume
Dec 4, 2026
Merged

Araq merged 1 commit into
nim-lang:develfrom
planetis-m:yrc-epoch-period-assume

Conversation

@planetis-m

Copy link
Copy Markdown
Contributor

finishCollection divides by YrcEpochLen * slotsInPlay(). slotsInPlay only ensures 0 <= result, but gParSlots starts at 1 and only grows, so the divisor is never 0. This adds a {.assume: 1 <= inPlay.} stating that, with no run-time cost.

Needed by nim-lang/nimony#3338. There, integer div/mod by zero becomes a staticContracts obligation, and nimony compiles this file (pinned through lib/std/system/upstream.commit) inside a staticContracts module. Without the assume, --mm:yrc fails with "cannot prove that the divisor 'YrcEpochLen * …' is not 0".

Tested:

🤖 Generated with Claude Code

finishCollection divides by YrcEpochLen * slotsInPlay(). slotsInPlay only
promises 0 <= result, but gParSlots starts at 1 and only grows, so the
divisor is never 0. Say so with an assume, so a prover (Nimony's
staticContracts, which now treats a divisor as an obligation) can discharge it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Araq
Araq merged commit 0128175 into nim-lang:devel Dec 4, 2026
19 checks passed
planetis-m added a commit to planetis-m/nimony that referenced this pull request Dec 4, 2026
yrc.nim's epoch period now states that it is never 0, which the
division-by-zero obligation needs under --mm:yrc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
planetis-m added a commit to planetis-m/nimony that referenced this pull request Dec 4, 2026
yrc.nim's epoch period now states that it is never 0, which the
division-by-zero obligation needs under --mm:yrc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Araq pushed a commit to nim-lang/nimony that referenced this pull request Dec 5, 2026
… divisor panics at run time (#3338)

Fixes planetis-m/nimony-bugs-llms#666.

Integer `div`/`mod`, signed and unsigned (so `/%` and `%%` too), now
owes `divisor != 0`. The contract prover judges that the way it judges
an array index:
- **Proven:** a non-zero constant, a `sizeof`, or a divisor that the
facts on the path keep away from zero (`if b > 0:`, `s.len > 0`). No
run-time check is emitted.
- **Disproven:** the divisor is known to be 0 on that path. This is a
compile-time error in every mode: `var z = 0; echo 1 div z` reports
"division by zero: 'z' is 0".
- **Undecided:** a run-time check is emitted, and `nimCheckDiv` panics
with `division by zero`. Under `{.feature: "staticContracts".}` it is an
error instead ("cannot prove that the divisor … is not 0").
`runtimeContracts` skips the judgement, and `-d:danger` removes the
check.

**IR:** `(div|mod T X X .X?)`. The trailing `.` marks a discharged
obligation, like the bounds of `(arrat …)`. Only
`contracts_fir.copyWithVerdicts` writes it, and only
`hexer/desugar.trDivMod` reads and drops it. `doc/tags.md` is updated.

**Stdlib:**
- `/%` and `%%` become templates, so the obligation is judged at the
call site, as for `div`.
- `dec2flt` guards its `IntPow10` divisor.

**Nim runtime pin:** `lib/std/system/upstream.commit` moves to
nim-lang/Nim#26884 (merged). Nim's `yrc.nim`, compiled here under
`staticContracts`, divides by `YrcEpochLen * slotsInPlay()`, and that
change adds the `{.assume: 1 <= inPlay.}` that discharges the
obligation.

**Not covered:** `if b != 0:` does not discharge the obligation, because
the prover has no disequality facts. Such a divisor keeps the run-time
check, or is an error under `staticContracts`. Guards written `b > 0`
work.

**Docs:** `doc/language.md` (the rule, after "Signed overflow"),
`doc/differences.md`, `doc/internals/failure_modes.md`, `doc/tags.md`.

**Tests:**
- New:
  - `rtchecks/tdivbyzero`: signed, a zero the prover cannot see.
  - `rtchecks/tdivbyzero_unsigned`: the same for unsigned.
  - `errmsgs/tdivbyzero`: a divisor known to be zero.
- `contracts/tdivproven`: constant, guarded and `sizeof` divisors in a
`staticContracts` module; the generated C has no check.
- The first three fail on master.
- Linux amd64: tests/nimony 1476/1476, tests/boot, `hastur native`
207/207.
- arm64 (under qemu): rtchecks and contracts pass.
- The full CI tester (`tools/tester.nim`) passes locally on Linux amd64:
tests 1530/1530, validator 21/21.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants