Repository navigation
Instantiate path generic arguments during type resolution #4865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
feliperodri
merged 1 commit into
model-checking:main
from
kasimte:kani1997-instantiate-args
Sep 30, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
tests/expected/function-contract/generic_arg_unimplemented.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| unable to find implementation of associated function `Generic::generic` for S |
33 changes: 33 additions & 0 deletions
33
tests/expected/function-contract/generic_arg_unimplemented.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| // kani-flags: -Zfunction-contracts | ||
|
|
||
| // A proof_for_contract on an instantiation that is NOT implemented (`Generic<Wrap<u16>>`, | ||
| // where only `Generic<Wrap<u8>>` exists) must fail to resolve. Guards that instantiating | ||
| // the path's generic arguments (kani#1997) does not over-resolve to the wrong impl. | ||
|
|
||
| struct S(u32); | ||
| struct Wrap<T>(T); | ||
|
|
||
| trait Generic<T> { | ||
| fn generic(&self) -> u32; | ||
| } | ||
|
|
||
| impl Generic<Wrap<u8>> for S { | ||
| #[kani::requires(self.0 < 100)] | ||
| #[kani::ensures(|r| *r == self.0)] | ||
| fn generic(&self) -> u32 { | ||
| self.0 | ||
| } | ||
| } | ||
|
|
||
| #[cfg(kani)] | ||
| mod verify { | ||
| use super::*; | ||
|
|
||
| #[kani::proof_for_contract(<S as Generic<Wrap<u16>>>::generic)] | ||
| fn check_unimplemented() { | ||
| let s = S(kani::any()); | ||
| let _ = Generic::<Wrap<u8>>::generic(&s); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
tests/expected/function-contract/generic_method_unimplemented.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| unable to find implementation of associated function `Compute::compute` for S |
33 changes: 33 additions & 0 deletions
33
tests/expected/function-contract/generic_method_unimplemented.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| // kani-flags: -Zfunction-contracts | ||
|
|
||
| // A `proof_for_contract` on a trait method that carries its OWN generic parameter | ||
| // (`fn compute<T>`) is the case kani#1997 still does not support. Instantiating the | ||
| // path's generic arguments (this change) binds the self type and the trait's arguments, | ||
| // not a method-level type parameter, so this must still fail to resolve. | ||
|
|
||
| struct S(u32); | ||
|
|
||
| trait Compute { | ||
| fn compute<T>(&self, x: T) -> u32; | ||
| } | ||
|
|
||
| impl Compute for S { | ||
| #[kani::requires(self.0 < 100)] | ||
| #[kani::ensures(|r| *r == self.0)] | ||
| fn compute<T>(&self, _x: T) -> u32 { | ||
| self.0 | ||
| } | ||
| } | ||
|
|
||
| #[cfg(kani)] | ||
| mod verify { | ||
| use super::*; | ||
|
|
||
| #[kani::proof_for_contract(<S as Compute>::compute)] | ||
| fn check_generic_method() { | ||
| let s = S(kani::any()); | ||
| let _ = s.compute(0u8); | ||
| } | ||
| } |
156 changes: 156 additions & 0 deletions
156
tests/kani/FunctionContracts/generic_argument_instantiation.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| // kani-flags: -Zfunction-contracts | ||
|
|
||
| // `proof_for_contract` on a target whose type carries generic arguments — the remaining | ||
| // generic-parameter case from https://github.com/model-checking/kani/issues/1997. | ||
| // `resolve_ty` now instantiates a path type's generic arguments before resolution, so | ||
| // these all resolve and verify: | ||
| // * a generic *argument* that is itself generic (`Generic<Wrap<u8>>`), | ||
| // * a generic *self* type with an associated-type return and a `where`-bounded method, | ||
| // * a concrete DST self type with a generic argument (the `<CStr as Index<RangeFrom>>` shape), | ||
| // * a generic *impl* (`impl<T> Get for W<T>`) queried at a concrete instantiation, | ||
| // * a type with a lifetime parameter beside a type parameter (`Borrowed<'a, T>`) — lifetime erased. | ||
| // `check_primitive_arg` guards that the already-working concrete-argument case is unchanged. | ||
|
|
||
| struct S(u32); | ||
| struct Wrap<T>(T); | ||
|
|
||
| trait Generic<T> { | ||
| fn generic(&self) -> u32; | ||
| } | ||
|
|
||
| impl Generic<u8> for S { | ||
| #[kani::requires(self.0 < 100)] | ||
| #[kani::ensures(|r| *r == self.0)] | ||
| fn generic(&self) -> u32 { | ||
| self.0 | ||
| } | ||
| } | ||
|
|
||
| // Distinct body from the `Generic<u8>` impl above, so the harness only verifies | ||
|
feliperodri marked this conversation as resolved.
|
||
| // if resolution lands on *this* impl rather than the sibling. | ||
| impl Generic<Wrap<u8>> for S { | ||
| #[kani::requires(self.0 < 100)] | ||
| #[kani::ensures(|r| *r == self.0 + 1)] | ||
| fn generic(&self) -> u32 { | ||
| self.0 + 1 | ||
| } | ||
| } | ||
|
|
||
| trait Access {} | ||
|
|
||
| struct W<T>(T); | ||
|
|
||
| impl Access for W<u8> {} | ||
|
|
||
| trait Indexed { | ||
| type Item; | ||
| unsafe fn get_unchecked(&self, i: usize) -> Self::Item | ||
| where | ||
| Self: Access; | ||
| } | ||
|
|
||
| impl Indexed for W<u8> { | ||
| type Item = u8; | ||
| #[kani::requires(i < 1)] | ||
| #[kani::ensures(|r| *r == 0)] | ||
| unsafe fn get_unchecked(&self, i: usize) -> u8 | ||
| where | ||
| Self: Access, | ||
| { | ||
| let _ = i; | ||
| 0 | ||
| } | ||
| } | ||
|
|
||
| trait Get { | ||
| fn get(&self) -> u32; | ||
| } | ||
|
|
||
| // A generic impl: the query `<W<u8> as Get>::get` supplies the concrete instantiation. | ||
| impl<T> Get for W<T> { | ||
| #[kani::requires(true)] | ||
| #[kani::ensures(|r| *r == 7)] | ||
| fn get(&self) -> u32 { | ||
| 7 | ||
| } | ||
| } | ||
|
|
||
| #[repr(transparent)] | ||
| struct Dst([u8]); | ||
|
|
||
| trait Peek<T> { | ||
| fn peek(&self) -> u32; | ||
| } | ||
|
|
||
| impl Peek<Wrap<u8>> for Dst { | ||
| #[kani::requires(true)] | ||
| #[kani::ensures(|r| *r as usize == self.0.len())] | ||
| fn peek(&self) -> u32 { | ||
| self.0.len() as u32 | ||
| } | ||
| } | ||
|
|
||
| struct Borrowed<'a, T>(&'a T); | ||
|
|
||
| trait Held { | ||
| fn held(&self) -> u32; | ||
| } | ||
|
|
||
| impl<'a, T> Held for Borrowed<'a, T> { | ||
| #[kani::requires(true)] | ||
| #[kani::ensures(|r| *r == 0)] | ||
| fn held(&self) -> u32 { | ||
| 0 | ||
| } | ||
| } | ||
|
|
||
| #[cfg(kani)] | ||
| mod verify { | ||
| use super::*; | ||
|
|
||
| // Concrete (primitive) argument — resolved before this change; must still resolve. | ||
| #[kani::proof_for_contract(<S as Generic<u8>>::generic)] | ||
| fn check_primitive_arg() { | ||
| let s = S(kani::any()); | ||
| let _ = Generic::<u8>::generic(&s); | ||
| } | ||
|
|
||
| // Generic argument that is itself generic — now resolves. | ||
| #[kani::proof_for_contract(<S as Generic<Wrap<u8>>>::generic)] | ||
| fn check_generic_arg() { | ||
| let s = S(kani::any()); | ||
| let _ = Generic::<Wrap<u8>>::generic(&s); | ||
| } | ||
|
|
||
| // Generic self type + associated-type return + `where`-bounded method — now resolves. | ||
| #[kani::proof_for_contract(<W<u8> as Indexed>::get_unchecked)] | ||
| fn check_generic_self() { | ||
| let w = W(0u8); | ||
| let _ = unsafe { <W<u8> as Indexed>::get_unchecked(&w, kani::any()) }; | ||
| } | ||
|
|
||
| // Concrete DST self with a generic argument — now resolves. | ||
| #[kani::proof_for_contract(<Dst as Peek<Wrap<u8>>>::peek)] | ||
| fn check_dst_self() { | ||
| let bytes = [1u8, 2, 3]; | ||
| let dst: &Dst = unsafe { &*(&bytes[..] as *const [u8] as *const Dst) }; | ||
| let _ = dst.peek(); | ||
| } | ||
|
|
||
| // Generic impl queried at a concrete instantiation — now resolves. | ||
| #[kani::proof_for_contract(<W<u8> as Get>::get)] | ||
| fn check_generic_impl() { | ||
| let w = W(0u8); | ||
| let _ = w.get(); | ||
| } | ||
|
|
||
| // Lifetime parameter beside a type parameter — lifetime erased, type substituted. | ||
| #[kani::proof_for_contract(<Borrowed<'static, u8> as Held>::held)] | ||
| fn check_lifetime_param() { | ||
| let x = 0u8; | ||
| let b = Borrowed(&x); | ||
| let _ = b.held(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.