Skip to content

Reject const-generic self types at trait resolution instead of panicking - #4999

Open
kasimte wants to merge 6 commits into
model-checking:mainfrom
kasimte:fix-const-generic-contract-path
Open

kasimte wants to merge 6 commits into
model-checking:mainfrom
kasimte:fix-const-generic-contract-path

Conversation

@kasimte

@kasimte kasimte commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #4998

A proof_for_contract or stub target whose self type has a const generic parameter made kani-compiler panic during compilation instead of reporting an error. For example, with struct Buf<const N: usize>, the target <Buf<4> as Get>::get hit an internal cannot find N/#0 in param-env panic.

The panic is in Instance::resolve: this resolver does not instantiate const arguments, so a const-generic self type reaches instance resolution with a free const parameter and ICEs.

The guard sits at that call site, in resolve_in_trait_impl. A const-generic self type is refused there with the resolver's existing UnsupportedPath error. Inherent-method targets (<Buf<4>>::method) resolve without Instance::resolve and are left untouched, so they keep resolving.

This errors gracefully. It does not add support for instantiating const arguments.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@kasimte
kasimte requested review from a team as code owners October 12, 2026 16:58
@github-actions github-actions Bot added Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-CompilerBenchCI Tag a PR to run benchmark CI labels Oct 12, 2026

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

The shared resolver guard also rejects previously supported qualified inherent-method targets that do not require trait-instance resolution.

1 open issue
What changed in this PR

This PR addresses #4998 by reporting unsupported const-generic contract target paths as resolution errors instead of compiler panics.

Changes:

  • Makes instantiate_path_args return a Result and reject unsupported const-generic paths.
  • Adds regression cases for explicit, omitted, and mixed type/const arguments.
File Description
tests/​expected/​function-contract/​const_generic_path.rs Tests an explicit const argument in a trait-method target.
tests/​expected/​function-contract/​const_generic_path.expected Specifies the expected unsupported-path diagnostic.
tests/​expected/​function-contract/​const_generic_omitted.rs Tests an omitted argument on a const-only type.
tests/​expected/​function-contract/​const_generic_omitted.expected Specifies the diagnostic for omitted const arguments.
tests/​expected/​function-contract/​const_generic_mixed.rs Tests explicit arguments on a mixed type/const-generic type.
tests/​expected/​function-contract/​const_generic_mixed.expected Specifies the diagnostic for mixed generic arguments.
kani-compiler/​src/​kani_middle/​resolve/​type_resolution.rs Adds fallible path instantiation and const-generic rejection.

Review effort: Balanced

…the shared resolver

Qualified inherent-method targets on const-generic types resolve again; the
const-generic trait/contract rejection (the panic fix) is unchanged, just moved
to the Instance::resolve call site where the ICE originates. Adds a regression
test for the qualified inherent spelling.
Covers `<Buf<4>>::get`: the qualified inherent spelling resolves and its
contract verifies, confirming the guard does not over-reject the inherent path.
@kasimte kasimte changed the title Reject const generic arguments in contract target paths instead of panicking Reject const-generic self types at trait resolution instead of panicking Oct 24, 2026
@kasimte

kasimte commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

@feliperodri Thanks for running the Copilot review. I addressed the comment and updated the branch.

@feliperodri
feliperodri requested a balanced review from Copilot November 6, 2026 18:53
@feliperodri feliperodri added this to the Contracts milestone Nov 6, 2026

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

2 open issues
1 resolved since last review

Review effort: Lite

Comment on lines +461 to +468
// A const-generic self type would reach `Instance::resolve` below with a free const
// parameter (this resolver does not instantiate const arguments) and ICE. Reject it first.
if let TyKind::RigidTy(RigidTy::Adt(_, self_args)) = ty.kind() {
let has_const = self_args.0.iter().any(|arg| matches!(arg, GenericArgKind::Const(_)));
if has_const {
return Err(ResolveError::UnsupportedPath { kind: "const generic arguments" });
}
}
}
}

pub fn bar<const C: usize>(x: &Foo<C>) -> bool {

@feliperodri feliperodri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approach looks right: the error is raised at the call site, and inherent-method targets are untouched.

Before merge:

  1. regression (ubuntu-24.04) fails on this head. Please check the log. If it's the unused x in main.rs that Copilot flagged, fix it and re-run.
  2. The guard only checks ADTs. Please test a const-generic array self type (e.g. impl Trait for [u8; N]), or explain why it can't reach Instance::resolve.
  3. This is an ICE, so please add [C] Bug and [F] Crash.
  4. The branch is behind main. Please update it.

@feliperodri feliperodri assigned kasimte and unassigned feliperodri Nov 7, 2026
wodex1nhaoIeng pushed a commit to wodex1nhaoIeng/kani that referenced this pull request Nov 10, 2026
…cking#5135)

## Summary

Fix trait method resolution for stubs and function contracts when the
method declares its own generic parameters.

Previously, the resolver could identify a concrete trait implementation
but failed to construct the complete generic argument list required by
`Instance::resolve`. Method-level type, const, and lifetime parameters
could therefore prevent valid trait method targets from resolving.

## Changes

- Include method-level generic arguments when resolving trait method
implementations.
- Reconstruct the complete generic argument layout, including trait
defaults and erased lifetime slots, using `ctor_args_with_lifetimes`.
- Return the resolved implementation's function definition.
- Improve diagnostics by distinguishing invalid generic arguments,
unsupported trait-level const generics, and genuinely missing trait
implementations. Remove the outdated note claiming generic trait methods
are unsupported.
- Add positive and negative regression tests for trait/method generics,
method-level const generics, lifetimes, trait defaults, contracts, and
stub signature validation.
- Add regressions for standard-library `FromIterator<char>` and
`Extend<char>` methods on `String`.
- Add generic `stub_verified` tests, including multiple method
instantiations and precondition failures.
- Cover the specialization/`FnMut` case from model-checking#4908, including a negative
test demonstrating that a weak contract does not guarantee sorting.

## Testing

- Local validation: **807 passed, 18 previously ignored, 0 failures**
across the compiler unit tests, Expected/UI suites, and relevant
Stubbing/FunctionContracts tests.
- Standard-library, verified-stub, and specialization regressions were
checked with the solver, including expected negative results.
- Confirmed that the corresponding reproductions fail during resolution
on the unmodified upstream baseline.
- Build, formatting, and Clippy checks pass locally.
- Full repository CI has not been run locally.

## Related issues and work

Closes model-checking#4908.

This resolves the trait method generic-argument resolution limitation
affecting `#[kani::stub]`, `#[kani::stub_verified]`, and
`#[kani::proof_for_contract]`. This is a follow-up to model-checking#4587, which
introduced trait method implementation stubbing. It is separate from
model-checking#4999 (const-generic self-type ICE handling), model-checking#4986 (trait-object
argument resolution), and model-checking#5116 (contract attributes on specialization
`default fn` methods).

Trait-level const generic arguments remain unsupported in this
resolution path. Paths still require appropriate qualification or
imports.

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.

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

Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ICE: proof_for_contract target with a const generic parameter panics instead of erroring

3 participants