Skip to content

Analyze the impl body a trait-dispatched call resolves to - #237

Merged
coord-e merged 2 commits into
mainfrom
claude/issue-190-fix-4m1wjh
Aug 26, 2026
Merged

Analyze the impl body a trait-dispatched call resolves to#237
coord-e merged 2 commits into
mainfrom
claude/issue-190-fix-4m1wjh

Conversation

@coord-e

@coord-e coord-e commented Aug 20, 2026

Copy link
Copy Markdown
Owner

Closes #190.

A generic impl of a trait method is registered as a deferred def, whose body is analyzed only when its type is asked for. A call dispatched through the trait takes its type from the trait method instead — that is where the spec is annotated — so nothing ever asked for the impl method's type: its body went unchecked while every caller assumed the trait's ensures, and an always-panicking program verified as safe.

Changes

  • src/analyze/basic_block.rs: when fn_def_ty answers from the registered callee type, it now also asks for the type of the impl the call resolves to, which is what analyzes that impl's body. The impl method's expected type is the trait's spec, so running its body against it is the missing check. The lookup sits at the call site rather than in def_ty_with_args so that it resolves in the caller body's typing env and reuses resolve_fn_def.
  • src/analyze/local_def.rs: trait_item_args builds the implemented trait method's arguments for the instantiation being analyzed — the instantiated trait ref followed by the impl method's own-parameter tail — and both the trait-item type lookup and the inherited requires/ensures extraction use it. Analyzer::new now defaults generic_args to the identity instantiation, which keeps that instantiation well-formed for a def whose generic arguments were never set.
  • tests/ui/{pass,fail}/trait_generic_impl.rs: a generic impl of a spec'd trait method, with the ensures satisfied and violated.
  • tests/ui/{pass,fail}/trait_generic_method.rs: the same for a trait method generic over a parameter of its own, which the newly reached path had to get right.

Verification

cargo test (330 UI tests), cargo fmt --all -- --check and cargo clippy -- -D warnings all pass, with Z3 5.0.0 and the CI-pinned COAR image.

Beyond the added test pairs, checked by hand: the issue's reproduction and its assert!(false)-with-unused-result variant are both rejected; a satisfied body, a bounded impl<T: Base>, two instantiations (W<i32> and W<bool>), a non-generic method in a generic impl, and the method passed as a function value all verify or are rejected as they should be.

@coord-e
coord-e force-pushed the claude/issue-190-fix-4m1wjh branch 2 times, most recently from 220e02c to be42861 Compare August 26, 2026 13:55
A generic impl of a trait method is registered as a deferred def, whose body
is analyzed only when its type is asked for. A call dispatched through the
trait takes its type from the trait method instead, where the spec is
annotated, so nothing ever asked for the impl method's type: its body went
unchecked while every caller assumed the trait's `ensures`.

Ask for it at the call site, and instantiate the trait ref in `trait_item_ty`
with the analyzed instantiation so the impl method's expected type is the
trait's spec for that instantiation rather than one carrying the impl's own
type parameters.

Closes #190

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PvA5f69BEE7xw9MR6SLeMX
@coord-e
coord-e force-pushed the claude/issue-190-fix-4m1wjh branch from be42861 to 1133f50 Compare August 26, 2026 13:56
@coord-e
coord-e marked this pull request as ready for review August 26, 2026 13:57
@coord-e
coord-e requested a balanced review from Copilot August 26, 2026 13:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Trait-item argument rebasing remains incomplete for generic methods and type-dependent contracts.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Fixes a soundness gap by analyzing generic trait implementation bodies against trait contracts at dispatched call sites.

Changes:

  • Resolves and analyzes the concrete implementation behind trait calls.
  • Propagates generic instantiations into trait contract analysis.
  • Adds passing and failing regression tests.
File summaries
File Description
src/analyze/basic_block.rs Triggers analysis of resolved implementation methods.
src/analyze/local_def.rs Propagates generic arguments during trait-type translation.
tests/ui/pass/trait_generic_impl.rs Covers a conforming generic implementation.
tests/ui/fail/trait_generic_impl.rs Rejects a contract-violating generic implementation.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/analyze/local_def.rs

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1133f502dc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/analyze/local_def.rs
`trait_item_ty` passed only the impl's trait ref arguments, which cover the
trait's generics but not the method's own. Analyzing the impl body of a
generic trait method therefore asked for the trait method's type with too few
arguments and panicked with `type parameter out of range`. Inherited
`requires`/`ensures` had the reverse problem: they were translated with the
impl method's arguments, whose layout maps `Self` to the impl's first generic
parameter.

Build the trait method's arguments once, and extract the inherited
annotations with them.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PvA5f69BEE7xw9MR6SLeMX
@coord-e

coord-e commented Aug 26, 2026

Copy link
Copy Markdown
Owner Author

@codex review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The soundness gap is addressed directly and covered by paired positive and negative regression tests.

Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: e1ed4f3a90

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@coord-e
coord-e merged commit 35eea46 into main Aug 26, 2026
7 checks passed
@coord-e
coord-e deleted the claude/issue-190-fix-4m1wjh branch August 26, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants