Support stubbing trait method implementations#4587
Merged
feliperodri merged 3 commits intomodel-checking:mainfrom Apr 24, 2026
Merged
Support stubbing trait method implementations#4587feliperodri merged 3 commits intomodel-checking:mainfrom
feliperodri merged 3 commits intomodel-checking:mainfrom
Conversation
f08d019 to
d04da30
Compare
…g#1997) Trait method stubbing with fully-qualified syntax like `#[kani::stub(<X as A>::foo, stub_fn)]` already worked for non-generic traits. This change extends support to generic traits like `#[kani::stub(<MyType as Convert<u32>>::convert, stub_fn)]`. The fix extracts generic type arguments from the trait path segment (e.g., `<u32>` from `Convert<u32>`) and includes them alongside the Self type when resolving the trait implementation via Instance::resolve. Previously, only the Self type was passed, causing resolution to fail for any trait with generic parameters. Changes: - resolve.rs: Extract trait generic args from the second-to-last path segment and pass them to resolve_in_trait_impl. - resolve_in_trait_impl: Accept optional trait generic args, resolve them as types, and include in the GenericArgs for Instance::resolve. Tests: - stub_trait_method.rs: Three harnesses testing <X as A>::foo, <X as A>::bar, and <X as B>::bar with disambiguating syntax. - stub_generic_trait_method.rs: Stubbing <MyType as Convert<u32>>::convert while leaving Convert<bool> unaffected. Signed-off-by: Felipe R. Monteiro <felisous@amazon.com>
Replace the 'Trait method stubbing not supported' limitation section in stubbing.md with feature documentation showing the fully-qualified syntax for both simple and generic traits. Add fixme_stub_trait_default_method.rs documenting that default trait methods (with body in the trait definition) fail validation because the body uses Self instead of the concrete type. Apply rustfmt to test files. Signed-off-by: Felipe R. Monteiro <felisous@amazon.com>
d04da30 to
fca0f58
Compare
adpaco-aws
approved these changes
Apr 24, 2026
…s, and overridden defaults Verify that trait method stubbing works correctly for: - Dynamic dispatch through &dyn Trait and Box<dyn Trait> - Methods defined in supertraits - Default trait methods that are overridden in the impl Update documentation to list supported trait patterns and refine the known limitation to only non-overridden default methods. Remove 'Traits' from the unsupported items list.
fca0f58 to
9c4219d
Compare
rajath-mk
approved these changes
Apr 24, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Enable stubbing of trait method implementations using fully-qualified syntax (
<Type as Trait>::method), including generic traits. Previously, Kani's stub resolution only searched inherentimplblocks and trait method implementations were silently ignored.Resolves #1997.
Problem
Users could not stub trait method implementations. Writing
#[kani::stub(<MyType as MyTrait>::method, mock_method)]failed because the resolution algorithm did not search through trait implementations for the concrete type. This was a significant limitation since many Rust APIs are trait-based.Solution
Resolution (
resolve.rs)resolve_in_trait_implnow correctly resolves trait method implementations by:<u32>fromConvert<u32>)GenericArgswith the Self type + trait generic parametersInstancefor the concrete type's implementationThis handles both simple traits (
<Vec<u8> as MyTrait>::method) and generic traits (<MyType as Convert<u32>>::convert).Documentation (
stubbing.md)Updated the user-facing docs to document trait method stubbing with examples covering:
Testing
5 passing tests:
stub_trait_method.rs— basic<Type as Trait>::methodstubbingstub_generic_trait_method.rs— generic trait<Type as Trait<T>>::methodstub_trait_dyn_dispatch.rs— dynamic dispatch through&dyn TraitandBox<dyn Trait>stub_trait_supertrait.rs— methods defined in supertraitsstub_trait_default_overridden.rs— default trait methods that are overridden in the impl1 fixme test:
fixme_stub_trait_default_method.rs— non-overridden default methods cause a type mismatch during stub validation (the default body usesSelfas a placeholder type)By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.