Skip to content

Commit 4c11087

Browse files
authored
Rollup merge of rust-lang#128045 - pnkfelix:rustc-contracts, r=oli-obk
#[contracts::requires(...)] + #[contracts::ensures(...)] cc rust-lang#128044 Updated contract support: attribute syntax for preconditions and postconditions, implemented via a series of desugarings that culminates in: 1. a compile-time flag (`-Z contract-checks`) that, similar to `-Z ub-checks`, attempts to ensure that the decision of enabling/disabling contract checks is delayed until the end user program is compiled, 2. invocations of lang-items that handle invoking the precondition, building a checker for the post-condition, and invoking that post-condition checker at the return sites for the function, and 3. intrinsics for the actual evaluation of pre- and post-condition predicates that third-party verification tools can intercept and reinterpret for their own purposes (e.g. creating shims of behavior that abstract away the function body and replace it solely with the pre- and post-conditions). Known issues: * My original intent, as described in the MCP (rust-lang/compiler-team#759) was to have a rustc-prefixed attribute namespace (like rustc_contracts::requires). But I could not get things working when I tried to do rewriting via a rustc-prefixed builtin attribute-macro. So for now it is called `contracts::requires`. * Our attribute macro machinery does not provide direct support for attribute arguments that are parsed like rust expressions. I spent some time trying to add that (e.g. something that would parse the attribute arguments as an AST while treating the remainder of the items as a token-tree), but its too big a lift for me to undertake. So instead I hacked in something approximating that goal, by semi-trivially desugaring the token-tree attribute contents into internal AST constucts. This may be too fragile for the long-term. * (In particular, it *definitely* breaks when you try to add a contract to a function like this: `fn foo1(x: i32) -> S<{ 23 }> { ... }`, because its token-tree based search for where to inject the internal AST constructs cannot immediately see that the `{ 23 }` is within a generics list. I think we can live for this for the short-term, i.e. land the work, and continue working on it while in parallel adding a new attribute variant that takes a token-tree attribute alongside an AST annotation, which would completely resolve the issue here.) * the *intent* of `-Z contract-checks` is that it behaves like `-Z ub-checks`, in that we do not prematurely commit to including or excluding the contract evaluation in upstream crates (most notably, `core` and `std`). But the current test suite does not actually *check* that this is the case. Ideally the test suite would be extended with a multi-crate test that explores the matrix of enabling/disabling contracts on both the upstream lib and final ("leaf") bin crates.
2 parents c1e4249 + 0a8331f commit 4c11087

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Diff for: clippy_utils/src/ast_utils/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -362,18 +362,21 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
362362
defaultness: ld,
363363
sig: lf,
364364
generics: lg,
365+
contract: lc,
365366
body: lb,
366367
}),
367368
Fn(box ast::Fn {
368369
defaultness: rd,
369370
sig: rf,
370371
generics: rg,
372+
contract: rc,
371373
body: rb,
372374
}),
373375
) => {
374376
eq_defaultness(*ld, *rd)
375377
&& eq_fn_sig(lf, rf)
376378
&& eq_generics(lg, rg)
379+
&& eq_opt_fn_contract(lc, rc)
377380
&& both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
378381
},
379382
(Mod(lu, lmk), Mod(ru, rmk)) => {
@@ -497,18 +500,21 @@ pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
497500
defaultness: ld,
498501
sig: lf,
499502
generics: lg,
503+
contract: lc,
500504
body: lb,
501505
}),
502506
Fn(box ast::Fn {
503507
defaultness: rd,
504508
sig: rf,
505509
generics: rg,
510+
contract: rc,
506511
body: rb,
507512
}),
508513
) => {
509514
eq_defaultness(*ld, *rd)
510515
&& eq_fn_sig(lf, rf)
511516
&& eq_generics(lg, rg)
517+
&& eq_opt_fn_contract(lc, rc)
512518
&& both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
513519
},
514520
(
@@ -559,18 +565,21 @@ pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
559565
defaultness: ld,
560566
sig: lf,
561567
generics: lg,
568+
contract: lc,
562569
body: lb,
563570
}),
564571
Fn(box ast::Fn {
565572
defaultness: rd,
566573
sig: rf,
567574
generics: rg,
575+
contract: rc,
568576
body: rb,
569577
}),
570578
) => {
571579
eq_defaultness(*ld, *rd)
572580
&& eq_fn_sig(lf, rf)
573581
&& eq_generics(lg, rg)
582+
&& eq_opt_fn_contract(lc, rc)
574583
&& both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
575584
},
576585
(
@@ -653,6 +662,17 @@ pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool {
653662
&& eq_ext(&l.ext, &r.ext)
654663
}
655664

665+
pub fn eq_opt_fn_contract(l: &Option<P<FnContract>>, r: &Option<P<FnContract>>) -> bool {
666+
match (l, r) {
667+
(Some(l), Some(r)) => {
668+
eq_expr_opt(l.requires.as_ref(), r.requires.as_ref())
669+
&& eq_expr_opt(l.ensures.as_ref(), r.ensures.as_ref())
670+
}
671+
(None, None) => true,
672+
(Some(_), None) | (None, Some(_)) => false,
673+
}
674+
}
675+
656676
pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
657677
over(&l.params, &r.params, eq_generic_param)
658678
&& over(&l.where_clause.predicates, &r.where_clause.predicates, |l, r| {

Diff for: clippy_utils/src/qualify_min_const_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ fn check_rvalue<'tcx>(
179179
))
180180
}
181181
},
182-
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::UbChecks, _)
182+
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::UbChecks | NullOp::ContractChecks, _)
183183
| Rvalue::ShallowInitBox(_, _) => Ok(()),
184184
Rvalue::UnaryOp(_, operand) => {
185185
let ty = operand.ty(body, tcx);

0 commit comments

Comments
 (0)