Commit 5efd8b6
Carolyn Zech
Verify contracts/stubs for generic types with multiple inherent implementations (#3829)
Extends Kani's contracts/stubbing logic to handle simple paths with
generic arguments. I'll explain the changes commit-by-commit:
## Commit 690ba97: Error for multiple implementations
Prior to this PR, given the code from #3773:
```rust
struct NonZero<T>(T);
impl NonZero<u32> {
#[kani::requires(true)]
fn unchecked_mul(self, x: u32) {}
}
impl NonZero<i32> {
#[kani::requires(true)]
fn unchecked_mul(self, x: i32) {}
}
#[kani::proof_for_contract(NonZero::unchecked_mul)]
fn verify_unchecked_mul() {
let x: NonZero<i32> = NonZero(-1);
x.unchecked_mul(-2);
}
```
When resolving the target, Kani would return the first `unchecked_mul`
implementation that it found for NonZero. If that happens to be the
`u32` implementation, then we get an error later that the target
`NonZero::unchecked_mul` isn't reachable from the harness, which is
confusing because the harness does call `unchecked_mul`.
The real problem is that the target needs to specify which
implementation it wants to verify, so now we throw this error:
```
Failed to resolve checking function NonZero::unchecked_mul because there are multiple implementations of unchecked_mul in struct `NonZero`. Found:
NonZero::<u32>::unchecked_mul
NonZero::<i32>::unchecked_mul
|
| #[kani::proof_for_contract(NonZero::unchecked_mul)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: Replace NonZero::unchecked_mul with a specific implementation.
```
## Commit 62b66de: Path resolution for simple paths with generic
arguments
If the user took our suggestion and changed their proof for contract to
be `NonZero::<i32>::unchecked_mul`, they would still get the same error.
The `syn::Path` is this:
```
Path {
leading_colon: None,
segments: [
PathSegment {
ident: Ident(
NonZero,
),
arguments: PathArguments::AngleBracketed {
colon2_token: Some(
PathSep,
),
lt_token: Lt,
args: [
GenericArgument::Type(
Type::Path {
qself: None,
path: Path {
leading_colon: None,
segments: [
PathSegment {
ident: Ident(
i32,
),
arguments: PathArguments::None,
},
],
},
},
),
],
gt_token: Gt,
},
},
PathSep,
PathSegment {
ident: Ident(
unchecked_mul,
),
arguments: PathArguments::None,
},
],
```
Kani's path resolution would skip over the `PathArguments` for the base
type NonZero, so it would still try to find `NonZero::unchecked_mul` and
run into the same problem. So, this commit adds a `base_path_args` field
to our `Path` representation to store these arguments, which we use to
select the specified implementation.
Resolves #3773
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 6fda222 commit 5efd8b6
File tree
9 files changed
+277
-19
lines changed- kani-compiler/src/kani_middle
- tests
- kani/FunctionContracts
- ui
- function-contracts
- stubbing/invalid-path
9 files changed
+277
-19
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
213 | 213 | | |
214 | 214 | | |
215 | 215 | | |
216 | | - | |
| 216 | + | |
217 | 217 | | |
218 | 218 | | |
219 | 219 | | |
220 | 220 | | |
221 | 221 | | |
222 | | - | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
223 | 230 | | |
224 | 231 | | |
225 | 232 | | |
| |||
242 | 249 | | |
243 | 250 | | |
244 | 251 | | |
245 | | - | |
| 252 | + | |
246 | 253 | | |
247 | 254 | | |
248 | 255 | | |
249 | 256 | | |
250 | 257 | | |
251 | | - | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
252 | 266 | | |
253 | 267 | | |
254 | 268 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
153 | 153 | | |
154 | 154 | | |
155 | 155 | | |
156 | | - | |
| 156 | + | |
157 | 157 | | |
158 | 158 | | |
159 | 159 | | |
| |||
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
| 173 | + | |
| 174 | + | |
173 | 175 | | |
174 | 176 | | |
175 | 177 | | |
| |||
208 | 210 | | |
209 | 211 | | |
210 | 212 | | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
211 | 225 | | |
212 | 226 | | |
213 | 227 | | |
| |||
232 | 246 | | |
233 | 247 | | |
234 | 248 | | |
235 | | - | |
| 249 | + | |
236 | 250 | | |
237 | 251 | | |
238 | 252 | | |
239 | 253 | | |
240 | 254 | | |
| 255 | + | |
241 | 256 | | |
242 | 257 | | |
243 | 258 | | |
| |||
271 | 286 | | |
272 | 287 | | |
273 | 288 | | |
274 | | - | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
275 | 294 | | |
276 | 295 | | |
277 | 296 | | |
| |||
292 | 311 | | |
293 | 312 | | |
294 | 313 | | |
295 | | - | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
296 | 319 | | |
297 | 320 | | |
298 | 321 | | |
| |||
320 | 343 | | |
321 | 344 | | |
322 | 345 | | |
323 | | - | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
324 | 351 | | |
325 | 352 | | |
326 | 353 | | |
| |||
350 | 377 | | |
351 | 378 | | |
352 | 379 | | |
353 | | - | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
354 | 385 | | |
355 | 386 | | |
356 | 387 | | |
| |||
529 | 560 | | |
530 | 561 | | |
531 | 562 | | |
532 | | - | |
| 563 | + | |
533 | 564 | | |
534 | 565 | | |
535 | 566 | | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
536 | 571 | | |
537 | 572 | | |
538 | 573 | | |
539 | 574 | | |
| 575 | + | |
540 | 576 | | |
541 | 577 | | |
542 | 578 | | |
543 | | - | |
544 | | - | |
545 | 579 | | |
546 | | - | |
| 580 | + | |
| 581 | + | |
547 | 582 | | |
548 | 583 | | |
549 | 584 | | |
550 | | - | |
551 | | - | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
552 | 643 | | |
553 | 644 | | |
554 | 645 | | |
| |||
601 | 692 | | |
602 | 693 | | |
603 | 694 | | |
604 | | - | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
605 | 700 | | |
606 | 701 | | |
607 | 702 | | |
| |||
612 | 707 | | |
613 | 708 | | |
614 | 709 | | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
Lines changed: 18 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
0 commit comments