Skip to content

Commit 4870c48

Browse files
author
Carolyn Zech
committed
extend generic fn note to show for generic traits too
1 parent 4ed5d8e commit 4870c48

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

kani-compiler/src/kani_middle/attributes.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,12 @@ impl<'tcx> KaniAttributes<'tcx> {
688688
}
689689
ResolveError::MissingTraitImpl { tcx: _, trait_fn_id, ty: _ } => {
690690
let generics = self.tcx.generics_of(trait_fn_id);
691-
if !generics.own_params.is_empty() {
692-
err = err.with_help(
691+
let parent_generics =
692+
generics.parent.map(|parent| self.tcx.generics_of(parent));
693+
if !generics.own_params.is_empty()
694+
|| parent_generics.is_some_and(|generics| !generics.own_params.is_empty())
695+
{
696+
err = err.with_note(
693697
"Kani does not currently support stubs or function contracts on generic functions in traits.\n \
694698
See https://github.com/model-checking/kani/issues/1997#issuecomment-3134614734 for more information.",
695699
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: failed to resolve `<usize as SliceIndex < [i32] >>::get_unchecked`: unable to find implementation of associated function `SliceIndex::get_unchecked` for usize\
2+
generic_slice_index.rs\
3+
|\
4+
| #[kani::proof_for_contract(<usize as SliceIndex<[i32]>>::get_unchecked)]\
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\
6+
|\
7+
= note: Kani does not currently support stubs or function contracts on generic functions in traits.\
8+
See https://github.com/model-checking/kani/issues/1997#issuecomment-3134614734 for more information.
9+
10+
11+
error: aborting due to 1 previous error
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
// kani-flags: -Zfunction-contracts
4+
//! Test function contracts on generic trait implementations based on SliceIndex,
5+
//! c.f. https://github.com/model-checking/kani/issues/4084
6+
//! This `proof_for_contract` should work,
7+
//! but we do not yet support stubbing/contracts on trait fns with generic arguments
8+
//! c.f. https://github.com/model-checking/kani/issues/1997#issuecomment-3134614734.
9+
//! So for now, test that we emit a nice error message.
10+
11+
const INVALID_INDEX: usize = 10;
12+
13+
trait SliceIndex<T: ?Sized> {
14+
type Output: ?Sized;
15+
unsafe fn get_unchecked(self, slice: *const T) -> *const Self::Output;
16+
}
17+
18+
impl<T> SliceIndex<[T]> for usize {
19+
type Output = T;
20+
21+
#[kani::requires(!slice.is_null())]
22+
#[kani::requires(self < slice.len())]
23+
unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output {
24+
unsafe { (*slice).as_ptr().add(self) }
25+
}
26+
}
27+
28+
#[kani::proof_for_contract(<usize as SliceIndex<[i32]>>::get_unchecked)]
29+
fn test_generic_slice_contract() {
30+
let data = [1i32, 2, 3, 4, 5];
31+
let slice_ptr = &data as *const [i32];
32+
33+
unsafe {
34+
// This violates the contract precondition (index >= slice length)
35+
let _ptr = INVALID_INDEX.get_unchecked(slice_ptr);
36+
}
37+
}

tests/ui/stubbing/generic_trait_fn.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ generic_trait_fn.rs\
44
| #[kani::stub(<TestStruct as TraitX>::generic_fn, <TestStruct as TraitY>::generic_fn)]\
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\
66
|\
7-
= help: Kani does not currently support stubs or function contracts on generic functions in traits.\
7+
= note: Kani does not currently support stubs or function contracts on generic functions in traits.\
88
See https://github.com/model-checking/kani/issues/1997#issuecomment-3134614734 for more information.
99

1010
error: failed to resolve `<TestStruct as TraitY>::generic_fn`: unable to find implementation of associated function `TraitY::generic_fn` for TestStruct\
@@ -13,7 +13,7 @@ generic_trait_fn.rs\
1313
| #[kani::stub(<TestStruct as TraitX>::generic_fn, <TestStruct as TraitY>::generic_fn)]\
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\
1515
|\
16-
= help: Kani does not currently support stubs or function contracts on generic functions in traits.\
16+
= note: Kani does not currently support stubs or function contracts on generic functions in traits.\
1717
See https://github.com/model-checking/kani/issues/1997#issuecomment-3134614734 for more information.
1818

1919
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)