Skip to content

Support stubbing trait method implementations#4587

Merged
feliperodri merged 3 commits intomodel-checking:mainfrom
feliperodri:stub-trait-methods
Apr 24, 2026
Merged

Support stubbing trait method implementations#4587
feliperodri merged 3 commits intomodel-checking:mainfrom
feliperodri:stub-trait-methods

Conversation

@feliperodri
Copy link
Copy Markdown
Contributor

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.

@feliperodri feliperodri added this to the Contracts milestone Apr 22, 2026
@feliperodri feliperodri added [C] Feature / Enhancement A new feature request or enhancement to an existing feature. Z-Contracts Issue related to code contracts labels Apr 22, 2026
@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 Apr 22, 2026
@feliperodri feliperodri force-pushed the stub-trait-methods branch 2 times, most recently from f08d019 to d04da30 Compare April 22, 2026 22:39
@feliperodri feliperodri marked this pull request as ready for review April 22, 2026 22:43
@feliperodri feliperodri requested a review from a team as a code owner April 22, 2026 22:43
…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>
Copy link
Copy Markdown
Contributor

@adpaco-aws adpaco-aws left a comment

Choose a reason for hiding this comment

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

Thank you!

Comment thread kani-compiler/src/kani_middle/resolve.rs
Comment thread kani-compiler/src/kani_middle/resolve.rs Outdated
…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.
@feliperodri feliperodri enabled auto-merge April 24, 2026 19:02
@feliperodri feliperodri added this pull request to the merge queue Apr 24, 2026
Merged via the queue into model-checking:main with commit 6fa4467 Apr 24, 2026
34 checks passed
@feliperodri feliperodri deleted the stub-trait-methods branch April 24, 2026 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[C] Feature / Enhancement A new feature request or enhancement to an existing feature. Z-CompilerBenchCI Tag a PR to run benchmark CI Z-Contracts Issue related to code contracts Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resolve trait methods for stubs and function contracts

3 participants