-
Notifications
You must be signed in to change notification settings - Fork 183
Fill omitted defaulted generic parameters during type resolution #4915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
1 change: 1 addition & 0 deletions
1
tests/expected/function-contract/generic_default_const_param.expected
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| unable to find implementation of associated function `Probe::probe` for WithConst |
32 changes: 32 additions & 0 deletions
32
tests/expected/function-contract/generic_default_const_param.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| // kani-flags: -Zfunction-contracts | ||
|
|
||
| // A `proof_for_contract` path that omits a generic parameter with a declared default keeps | ||
| // the uninstantiated type when that parameter is a CONST parameter: only defaulted TYPE | ||
| // parameters are filled (see tests/kani/FunctionContracts/generic_default_argument_fill.rs). | ||
| // `WithConst<u8>` omits `N` (default 4); the const default is not filled, so the target | ||
| // keeps the uninstantiated type and fails to resolve. | ||
|
|
||
| struct WithConst<T, const N: usize = 4>([T; N]); | ||
|
|
||
| trait Probe { | ||
| fn probe(&self) -> u32; | ||
| } | ||
|
|
||
| impl Probe for WithConst<u8> { | ||
| #[kani::ensures(|r| *r == 0)] | ||
| fn probe(&self) -> u32 { | ||
| 0 | ||
| } | ||
| } | ||
|
|
||
| mod verify { | ||
| use super::*; | ||
|
|
||
| #[kani::proof_for_contract(<WithConst<u8> as Probe>::probe)] | ||
| fn check_const_default_not_filled() { | ||
| let w = WithConst([0u8; 4]); | ||
| let _ = w.probe(); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
tests/expected/function-contract/generic_default_missing_bare.expected
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| unable to find implementation of associated function `Probe::probe` for NoDefaultBare |
32 changes: 32 additions & 0 deletions
32
tests/expected/function-contract/generic_default_missing_bare.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| // kani-flags: -Zfunction-contracts | ||
|
|
||
| // A `proof_for_contract` path written with NO generic arguments (`NoDefaultBare`, not | ||
| // `NoDefaultBare<..>`) keeps the uninstantiated type when a parameter has no default. The | ||
| // no-arguments path is treated as an empty argument list, but the substitution loop still | ||
| // returns the identity type at the first parameter it cannot fill (see | ||
| // tests/kani/FunctionContracts/generic_default_argument_fill.rs). | ||
|
|
||
| struct NoDefaultBare<T>(T); | ||
|
|
||
| trait Probe { | ||
| fn probe(&self) -> u32; | ||
| } | ||
|
|
||
| impl Probe for NoDefaultBare<u8> { | ||
| #[kani::ensures(|r| *r == 2)] | ||
| fn probe(&self) -> u32 { | ||
| 2 | ||
| } | ||
| } | ||
|
|
||
| mod verify { | ||
| use super::*; | ||
|
|
||
| #[kani::proof_for_contract(<NoDefaultBare as Probe>::probe)] | ||
| fn check_missing_required_bare() { | ||
| let n = NoDefaultBare(1u8); | ||
| let _ = n.probe(); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
tests/expected/function-contract/generic_default_missing_required.expected
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| unable to find implementation of associated function `Probe::probe` for NoDefault |
31 changes: 31 additions & 0 deletions
31
tests/expected/function-contract/generic_default_missing_required.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| // kani-flags: -Zfunction-contracts | ||
|
|
||
| // A `proof_for_contract` path that omits a generic parameter WITHOUT a declared default | ||
| // (`NoDefault<u8>` for `NoDefault<T, U>`) keeps the uninstantiated type and fails to | ||
| // resolve. Only parameters with declared defaults are filled (see | ||
| // tests/kani/FunctionContracts/generic_default_argument_fill.rs). | ||
|
|
||
| struct NoDefault<T, U>(T, U); | ||
|
|
||
| trait Probe { | ||
| fn probe(&self) -> u32; | ||
| } | ||
|
|
||
| impl Probe for NoDefault<u8, u16> { | ||
| #[kani::ensures(|r| *r == 1)] | ||
| fn probe(&self) -> u32 { | ||
| 1 | ||
| } | ||
| } | ||
|
|
||
| mod verify { | ||
| use super::*; | ||
|
|
||
| #[kani::proof_for_contract(<NoDefault<u8> as Probe>::probe)] | ||
| fn check_missing_required_param() { | ||
| let n = NoDefault(1u8, 2u16); | ||
| let _ = n.probe(); | ||
| } | ||
| } |
97 changes: 97 additions & 0 deletions
97
tests/kani/FunctionContracts/generic_default_argument_fill.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| // kani-flags: -Zfunction-contracts | ||
|
|
||
| // `proof_for_contract` on a target that omits trailing generic parameters with declared | ||
| // defaults. An omitted trailing parameter is now filled from its declared default, | ||
| // instantiated with the arguments so far — what rustc does for omitted arguments. These | ||
| // all resolve and verify: | ||
| // * a std container default: `Vec<u8>` fills `A = Global`, | ||
| // * sibling discrimination under the fill: `Vec<u16>`'s impl has a distinct | ||
| // postcondition, so each harness only verifies if resolution picks its own impl, | ||
| // * a default referencing an earlier parameter: `Pair<u8>` fills `U = T` as `u8`, | ||
| // * the explicit spelling (`Vec<u16, std::alloc::Global>`) resolves unchanged, | ||
| // * an all-defaulted type written with no argument list: `AllDefault` fills `T = u8`. | ||
| // `tests/expected/function-contract/generic_default_missing_required.rs` guards that a | ||
| // missing parameter without a default still fails to resolve. | ||
|
|
||
| #![feature(allocator_api)] | ||
|
|
||
| trait Sum { | ||
| fn total(&self) -> usize; | ||
| } | ||
|
|
||
| impl Sum for Vec<u8> { | ||
| #[kani::requires(self.len() < 3)] | ||
| #[kani::ensures(|r| *r == self.len())] | ||
| fn total(&self) -> usize { | ||
| self.len() | ||
| } | ||
| } | ||
|
|
||
| // Distinct postcondition from the `Vec<u8>` impl, so a harness only verifies if | ||
| // resolution lands on *this* impl rather than the sibling. | ||
| impl Sum for Vec<u16> { | ||
| #[kani::requires(self.len() < 3)] | ||
| #[kani::ensures(|r| *r == self.len() + 1)] | ||
| fn total(&self) -> usize { | ||
| self.len() + 1 | ||
| } | ||
| } | ||
|
|
||
| // `U`'s default references the earlier parameter, so `Pair<u8>` must fill `U = u8`. | ||
| struct Pair<T, U = T>(T, U); | ||
|
|
||
| impl Sum for Pair<u8> { | ||
| #[kani::ensures(|r| *r == 2)] | ||
| fn total(&self) -> usize { | ||
| 2 | ||
| } | ||
| } | ||
|
|
||
| // All parameters defaulted, written with no argument list at all (`AllDefault`, not | ||
| // `AllDefault<..>`): the omitted list must still be filled from the default `T = u8`. | ||
| struct AllDefault<T = u8>(T); | ||
|
|
||
| impl Sum for AllDefault { | ||
| #[kani::ensures(|r| *r == 1)] | ||
| fn total(&self) -> usize { | ||
| 1 | ||
| } | ||
| } | ||
|
|
||
| mod verify { | ||
| use super::*; | ||
| // Kani's path resolver does not see the prelude; name `Vec` explicitly. | ||
| use std::vec::Vec; | ||
|
|
||
| #[kani::proof_for_contract(<Vec<u8> as Sum>::total)] | ||
| fn check_vec_default_filled() { | ||
| let v: Vec<u8> = vec![1, 2]; | ||
| let _ = v.total(); | ||
| } | ||
|
|
||
| #[kani::proof_for_contract(<Vec<u16> as Sum>::total)] | ||
| fn check_vec_sibling_discriminated() { | ||
| let v: Vec<u16> = vec![1]; | ||
| let _ = v.total(); | ||
| } | ||
|
|
||
| #[kani::proof_for_contract(<Pair<u8> as Sum>::total)] | ||
| fn check_default_from_earlier_param() { | ||
| let p = Pair(1u8, 2u8); | ||
| let _ = p.total(); | ||
| } | ||
|
|
||
| #[kani::proof_for_contract(<Vec<u16, std::alloc::Global> as Sum>::total)] | ||
| fn check_explicit_spelling_unchanged() { | ||
| let v: Vec<u16> = vec![1]; | ||
| let _ = v.total(); | ||
| } | ||
|
|
||
| #[kani::proof_for_contract(<AllDefault as Sum>::total)] | ||
| fn check_all_parameters_defaulted() { | ||
| let a = AllDefault(0u8); | ||
| let _ = a.total(); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.