Skip to content

Commit ad36641

Browse files
authored
Handle const generics in stubbing code (#4323)
The recent support for stubbing trait impls #4250 assumed that generic args must be types which caused a crash when they're not. This PR takes into account that case. Resolves #4322 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
1 parent 12ef829 commit ad36641

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

kani-compiler/src/kani_middle/stubbing/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ fn generic_args_len_without_self(args: &GenericArgs) -> usize {
5959
return len;
6060
}
6161
let has_self = args.0.iter().any(|arg| {
62-
if let TyKind::Param(param_ty) = arg.expect_ty().kind() {
62+
if let Some(ty) = arg.ty()
63+
&& let TyKind::Param(param_ty) = ty.kind()
64+
{
6365
param_ty.name == "Self"
6466
} else {
6567
false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VERIFICATION:- SUCCESSFUL
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
// kani-flags: -Z stubbing
4+
//! Check that Kani supports stubbing methods for structs with const generics.
5+
//! This was previously crashing:
6+
//! https://github.com/model-checking/kani/issues/4322
7+
8+
struct Foo<const C: usize> {
9+
_a: [i32; C],
10+
x: bool,
11+
}
12+
13+
impl<const C: usize> Foo<C> {
14+
fn foo(&self) -> bool {
15+
self.x
16+
}
17+
}
18+
19+
pub fn bar<const C: usize>(x: &Foo<C>) -> bool {
20+
false
21+
}
22+
23+
#[kani::proof]
24+
#[kani::stub(Foo::foo, bar)]
25+
fn main() {
26+
let f = Foo { _a: [1, 2, 3], x: true };
27+
assert!(!f.foo());
28+
}

0 commit comments

Comments
 (0)