-
Notifications
You must be signed in to change notification settings - Fork 143
Support stubbing trait method implementations #4587
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 3 commits into
model-checking:main
from
feliperodri:stub-trait-methods
Apr 24, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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,32 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| // | ||
| // kani-flags: -Z stubbing | ||
| // | ||
| //! Test stubbing a trait's default method implementation. | ||
| //! | ||
| //! FIXME: Default trait methods use `Self` in their body types, which | ||
| //! doesn't match the concrete type in the stub. The validation compares | ||
| //! the trait definition's body (with `&Self`) against the stub (with | ||
| //! `&MyType`), causing a false type mismatch. | ||
| //! Tracked in: https://github.com/model-checking/kani/issues/4588 | ||
|
|
||
| trait HasDefault { | ||
| fn default_method(&self) -> u32 { | ||
| 0 | ||
| } | ||
| } | ||
|
|
||
| struct MyType; | ||
| impl HasDefault for MyType {} | ||
|
|
||
| fn stub_default(_x: &MyType) -> u32 { | ||
| 42 | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| #[kani::stub(<MyType as HasDefault>::default_method, stub_default)] | ||
| fn check_stub_default_trait_method() { | ||
| let x = MyType; | ||
| assert_eq!(x.default_method(), 42); | ||
| } |
9 changes: 3 additions & 6 deletions
9
...function-contracts/generic_slice_index.rs → ...kani/Stubbing/stub_generic_slice_index.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
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,38 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| // | ||
| // kani-flags: -Z stubbing | ||
| // | ||
| //! Test stubbing generic trait method implementations. | ||
| //! Regression test for https://github.com/model-checking/kani/issues/1997 | ||
|
|
||
| trait Convert<T> { | ||
| fn convert(&self) -> T; | ||
| } | ||
|
|
||
| struct MyType; | ||
|
|
||
| impl Convert<u32> for MyType { | ||
| fn convert(&self) -> u32 { | ||
| 100 | ||
| } | ||
| } | ||
|
|
||
| impl Convert<bool> for MyType { | ||
| fn convert(&self) -> bool { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| fn stub_convert_u32(_x: &MyType) -> u32 { | ||
| 42 | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| #[kani::stub(<MyType as Convert<u32>>::convert, stub_convert_u32)] | ||
| fn check_generic_trait_stub() { | ||
| let m = MyType; | ||
| assert_eq!(<MyType as Convert<u32>>::convert(&m), 42); | ||
| // The bool impl should NOT be affected | ||
| assert_eq!(<MyType as Convert<bool>>::convert(&m), false); | ||
| } |
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,32 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| // | ||
| // kani-flags: -Z stubbing | ||
| // | ||
| //! Test stubbing a trait default method that IS overridden in the impl. | ||
| //! The stub should replace the overridden implementation, not the default. | ||
|
|
||
| trait HasDefault { | ||
| fn method(&self) -> u32 { | ||
| 0 | ||
| } | ||
| } | ||
|
|
||
| struct MyType; | ||
|
|
||
| impl HasDefault for MyType { | ||
| fn method(&self) -> u32 { | ||
| 100 | ||
| } | ||
| } | ||
|
|
||
| fn stub_method(_x: &MyType) -> u32 { | ||
| 42 | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| #[kani::stub(<MyType as HasDefault>::method, stub_method)] | ||
| fn check_stub_overridden_default() { | ||
| let x = MyType; | ||
| assert_eq!(x.method(), 42); | ||
| } |
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,38 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| // | ||
| // kani-flags: -Z stubbing | ||
| // | ||
| //! Test stubbing a trait method called through dynamic dispatch (trait objects). | ||
|
|
||
| trait Animal { | ||
| fn sound(&self) -> u32; | ||
| } | ||
|
|
||
| struct Dog; | ||
|
|
||
| impl Animal for Dog { | ||
| fn sound(&self) -> u32 { | ||
| 100 | ||
| } | ||
| } | ||
|
|
||
| fn stub_sound(_x: &Dog) -> u32 { | ||
| 42 | ||
| } | ||
|
|
||
| fn call_via_dyn(a: &dyn Animal) -> u32 { | ||
| a.sound() | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| #[kani::stub(<Dog as Animal>::sound, stub_sound)] | ||
| fn check_stub_dyn_dispatch() { | ||
| let dog = Dog; | ||
| // Direct call — should be stubbed | ||
| assert_eq!(dog.sound(), 42); | ||
| // Call through trait object — stub applies because Kani replaces the | ||
| // function body globally, which is used regardless of dispatch mechanism. | ||
| let result = call_via_dyn(&dog); | ||
| assert_eq!(result, 42); | ||
| } |
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,70 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| // | ||
| // kani-flags: -Z stubbing | ||
| // | ||
| //! Test stubbing trait method implementations using fully-qualified syntax. | ||
| //! Regression test for https://github.com/model-checking/kani/issues/1997 | ||
|
|
||
| trait A { | ||
| fn foo(&self) -> u32; | ||
| fn bar(&self) -> u32; | ||
| } | ||
|
|
||
| trait B { | ||
| fn bar(&self) -> u32; | ||
| } | ||
|
|
||
| struct X {} | ||
|
|
||
| impl X { | ||
| fn new() -> Self { | ||
| Self {} | ||
| } | ||
| } | ||
|
|
||
| impl A for X { | ||
| fn foo(&self) -> u32 { | ||
| 100 | ||
| } | ||
| fn bar(&self) -> u32 { | ||
| 200 | ||
| } | ||
| } | ||
|
|
||
| impl B for X { | ||
| fn bar(&self) -> u32 { | ||
| 300 | ||
| } | ||
| } | ||
|
|
||
| fn stub_1(_x: &X) -> u32 { | ||
| 1 | ||
| } | ||
| fn stub_2(_x: &X) -> u32 { | ||
| 2 | ||
| } | ||
| fn stub_3(_x: &X) -> u32 { | ||
| 3 | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| #[kani::stub(<X as A>::foo, stub_1)] | ||
| fn check_stub_trait_a_foo() { | ||
| let x = X::new(); | ||
| assert_eq!(x.foo(), 1); | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| #[kani::stub(<X as A>::bar, stub_2)] | ||
| fn check_stub_trait_a_bar() { | ||
| let x = X::new(); | ||
| assert_eq!(A::bar(&x), 2); | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| #[kani::stub(<X as B>::bar, stub_3)] | ||
| fn check_stub_trait_b_bar() { | ||
| let x = X::new(); | ||
| assert_eq!(<X as B>::bar(&x), 3); | ||
| } |
Oops, something went wrong.
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.