Skip to content

Model unsigned integers as non-negative - #268

Draft
coord-e wants to merge 1 commit into
mainfrom
claude/great-galileo-1zj0z2
Draft

coord-e wants to merge 1 commit into
mainfrom
claude/great-galileo-1zj0z2

Conversation

@coord-e

@coord-e coord-e commented Sep 19, 2026

Copy link
Copy Markdown
Owner

usize, u32 and u64 now have thrust_models::model::UInt as their model, and a value whose model is UInt is refined with v >= 0. The fact is an assumption where such a value is read and an obligation where one is produced, so unsigned subtraction that may underflow is rejected.

Analyzer

TypeBuilder::build yields a rty::RefinedType. The refinement describes the value as a whole: a refinement nested in a type has no counterpart on the value side, where the analyzer keeps the facts about the parts of a value in the formula of a PlaceType, so nested positions are left unrefined. Enum variant fields are the exception, as they are related against the field's own value, and EnumVariantDef records them as refinement types.

An annotated refinement is conjoined onto what the model already says about the value rather than replacing it. truncate_outer_fn_params asserted that every parameter but the last is unrefined; it now asserts that they carry no predicate variable.

Models

In annotations Int and UInt mix through model::Integer, and an Array of either integer model is indexed by both through model::IndexModel. Seq is indexed and measured by UInt, so Seq::len() returns one; the ghost_const pair used it as a source of an Int and now works over Seq<UInt>.

Tests

unsigned_sub pins the obligation direction (x - y without requires(x >= y) is Unsat) and unsigned_loop the assumption direction (while n > 0 { n -= 1 } ends at 0, which does not hold for i32). The UI suite passes: 346 tests.

Known gap

The refinement only reaches a value's root, so an unsigned value read out of a nested position has no fact to offer, and one flowing into an unsigned position raises an obligation that cannot be discharged:

position fact available
parameter, return value, local yes
enum variant field yes
struct or tuple field no
pointee of &u32 / &mut u32 no
element of Vec<u32>, Seq's length no

This costs verification of programs that used to pass: #[thrust::callable] fn get(x: &u32) -> u32 { *x } is now Unsat, as is returning s.n from a struct S { n: u32 }. No test in the suite has such a flow, which is why it is green. Closing the gap needs the unsignedness where a type is rebuilt from a PlaceType, i.e. in rty::Type rather than in a refinement.

🤖 Generated with Claude Code

https://claude.ai/code/session_01RiJqqjcQaJoLMyZ5PJZ1dM


Generated by Claude Code

`usize`, `u32` and `u64` now have `thrust_models::model::UInt` as their model,
and a value whose model is `UInt` is refined with `v >= 0`. The fact is an
assumption where such a value is read and an obligation where one is produced,
so unsigned subtraction that may underflow is now rejected.

`TypeBuilder::build` therefore yields a `rty::RefinedType`. The refinement
describes the value as a whole: a refinement nested in a type has no
counterpart on the value side, where the analyzer keeps the facts about the
parts of a value in the formula of a `PlaceType`, so nested positions are left
unrefined. Enum variant fields are the exception, as they are related against
the field's own value, and `EnumVariantDef` records them as refinement types.

In annotations `Int` and `UInt` mix through `model::Integer`, and an `Array` of
either integer model is indexed by both through `model::IndexModel`. An
annotated refinement is conjoined onto what the model already says about the
value rather than replacing it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RiJqqjcQaJoLMyZ5PJZ1dM
coeff-aij added a commit to coeff-aij/thrust that referenced this pull request Oct 1, 2026
…del as UInt

Follows coord-e#268 where the merge departed from it, and extends it
to the positions #286 makes reachable.

- The unsigned primitive types have model::UInt as their model, Seq::len
  returns UInt and Enumerate's count is UInt, as in #268, and build_refined
  conjoins the model's facts to each template again. Specifications that
  passed a usize where a predicate took Int now name UInt (the
  rustc_coroutine stages and drafts, derived_ord_newtype, ghost_const).
- The type builders place the model's facts on tuple elements, struct fields
  and the content of shared and mutable references. Subtyping lifts the
  refinement of an owned or shared pointer's content to the pointer, so a
  field fact is stated about the field's own term.
- On the content of a mutable reference the facts are an invariant of the
  reference. A mutable borrow takes its invariant from the box it borrows,
  so the box of a mutable local that the body borrows mutably carries the
  facts of the local's declared type, and the bound value has to satisfy
  them. Without this a fresh borrow of a struct local is related to a
  parameter whose content has the facts with no term for the content, an
  obligation that cannot be proved.

rustc_coroutine: BitMatrix::rows, DenseBitSet::clear_excess_bits and
Size::bits verify without #[thrust::trusted]; BitMatrix::range stays trusted
because num_words has no contract. map_over_filter_item's fail twin gets 300 s:
its refutation takes about 100 s on fptprove 804d76744 and 82865d8c7.

UI tests: 858 passed. The 8 failures are the 6 that fail on main 94e10d5
(fold, fold_fn, map_fn, map_no_closure, simple_loop_self_mut_noinv,
skip_step) and slice_split_first_loop and traits/multi_params, which time
out on the pinned coar:804d76744 and pass on fptprove develop 82865d8c7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
coeff-aij referenced this pull request in coeff-aij/thrust Oct 1, 2026
…ields and reference contents, usize and friends model as UInt

BitMatrix::rows, DenseBitSet::clear_excess_bits, Size::bits and
Size::checked_add in the rustc_coroutine stages verify without
#[thrust::trusted]; redundant non-negativity annotations are dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
coeff-aij referenced this pull request in coeff-aij/thrust Oct 1, 2026
…e growth of unsigned facts as refinements

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

This branch has not been deployed

No deployments
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