You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Support stubbing trait method implementations (#4587)
Enable stubbing of trait method implementations using fully-qualified
syntax (`<Type as Trait>::method`), including generic traits.
Previously, Kani's stub resolution only searched inherent `impl` blocks
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_impl` now correctly resolves trait method
implementations by:
1. Extracting generic arguments from the trait segment (e.g., `<u32>`
from `Convert<u32>`)
2. Building the full `GenericArgs` with the Self type + trait generic
parameters
3. Resolving the `Instance` for the concrete type's implementation
This 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:
- Basic trait method stubbing syntax
- Generic trait methods
- Supported patterns (dyn dispatch, supertraits, overridden defaults)
- Known limitation: non-overridden default methods
## Testing
**5 passing tests:**
- `stub_trait_method.rs` — basic `<Type as Trait>::method` stubbing
- `stub_generic_trait_method.rs` — generic trait `<Type as
Trait<T>>::method`
- `stub_trait_dyn_dispatch.rs` — dynamic dispatch through `&dyn Trait`
and `Box<dyn Trait>`
- `stub_trait_supertrait.rs` — methods defined in supertraits
- `stub_trait_default_overridden.rs` — default trait methods that are
overridden in the impl
**1 fixme test:**
- `fixme_stub_trait_default_method.rs` — non-overridden default methods
cause a type mismatch during stub validation (the default body uses
`Self` as 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.
---------
Signed-off-by: Felipe R. Monteiro <felisous@amazon.com>
When two traits define methods with the same name, the fully-qualified syntax
203
+
disambiguates which implementation to stub.
204
+
205
+
The following trait patterns are supported:
206
+
207
+
-**Supertrait methods:** Stubbing a method defined in a supertrait (e.g., `<MyType as Base>::method`) works independently of subtrait methods.
208
+
-**Overridden default methods:** If a type overrides a trait's default method, the override can be stubbed normally.
209
+
-**Dynamic dispatch:** Stubs apply even when the method is called through a trait object (`&dyn Trait` or `Box<dyn Trait>`).
210
+
211
+
**Known limitation:** Stubbing a trait's default method that is *not* overridden by the implementing type is not currently supported ([#4588](https://github.com/model-checking/kani/issues/4588)). The default method body uses `Self` as a placeholder type, which causes a type mismatch during stub validation. This applies only to default methods that are inherited as-is (i.e., the `impl` block does not provide its own definition).
212
+
213
+
**Known limitation:** Traits with const generic parameters (e.g., `<Type as Buf<16>>::write`) or associated type constraints (e.g., `<Type as Iterator<Item = u32>>::next`) are not currently supported and will produce a resolution error.
180
214
181
215
### Usage restrictions
182
216
@@ -191,7 +225,6 @@ Support for stubbing is currently **limited to functions and methods**. All othe
191
225
The following are examples of items that could be good candidates for stubbing, but aren't supported:
192
226
- Types
193
227
- Macros
194
-
- Traits
195
228
- Intrinsics
196
229
197
230
We acknowledge that support for method stubbing isn't as ergonomic as it could be.
0 commit comments