Implement stub sets for reusable stub groups#4594
Open
feliperodri wants to merge 4 commits intomodel-checking:mainfrom
Open
Implement stub sets for reusable stub groups#4594feliperodri wants to merge 4 commits intomodel-checking:mainfrom
feliperodri wants to merge 4 commits intomodel-checking:mainfrom
Conversation
bfe1c5b to
af1c2d1
Compare
Add kani::stub_set! macro and #[kani::use_stub_set] attribute for defining reusable groups of stubs that can be applied to multiple proof harnesses. Stub sets support: - Grouping multiple stub(original, replacement) pairs under a name - Composing stub sets via use_stub_set(other_set) entries - Visibility modifiers for cross-module access (pub) - Combining with individual #[kani::stub] attributes - Different stub sets per harness - Circular reference detection Implementation: - stub_set! generates a const with #[kanitool::stub_set(...)] attribute - use_stub_set resolves the const, reads the attribute, and expands the stub pairs into the harness's stub list - Paths in stub sets are resolved relative to the harness module Resolves model-checking#2096
Mark stub_set! doc examples as `ignore` since kanitool is only registered when running under the Kani compiler, not plain rustc. Add a doc comment to the pub macro arm so pub stub sets appear meaningfully in rustdoc. The non-pub arm already correctly applies #[doc(hidden)].
Add a test that verifies circular stub set references (set_a → set_b → set_a) are detected and reported as errors. This exercises the DFS stack-based cycle detection in collect_stubs_from_set.
Add expected-output test for invalid entries in stub_set! (e.g., unknown_entry(foo)) to verify the error path in collect_stubs_from_set. Replace bare unreachable!() in the StubSet arm of harness_attributes with a descriptive message for easier debugging if the invariant is ever violated.
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.
Add
kani::stub_set!macro and#[kani::use_stub_set]attribute for defining reusable groups of stubs that can be applied to multiple proof harnesses without repeating individual#[kani::stub]annotations.Resolves #2096.
Problem
When multiple proof harnesses need the same set of stubs (e.g., mocking I/O, randomness, or network calls), users must duplicate
#[kani::stub(orig, repl)]annotations on every harness. This is verbose, error-prone, and makes it hard to maintain consistent stub configurations across a test suite.Solution
kani::stub_set!macroDefines a named, reusable group of stub pairs:
#[kani::use_stub_set]attributeApplies a stub set to a harness:
Features
use_stub_set(other_set)entries to compose setspubmodifier for cross-module access#[kani::use_stub_set]works alongside individual#[kani::stub]attributesuse_stub_setreferencesImplementation
stub_set!generates aconstwith a#[kanitool::stub_set(...)]attribute encoding the stub pairsuse_stub_setresolution inattributes.rsreads the attribute, resolves paths relative to the harness module, and expands the pairs into the harness's stub listresolve.rsextended to look upstub_setconstsTesting
5 tests covering:
stub_set_basic.rs— basic stub set definition and usagestub_set_composed.rs— composing stub sets viause_stub_setentriesstub_set_module.rs— cross-module access withpubvisibilitystub_set_per_harness.rs— different stub sets per harnessstub_set_with_individual.rs— combining stub sets with individual#[kani::stub]By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.