rust/clippy_utils/src
León Orell Valerian Liehr 4c11087e51 Rollup merge of #128045 - pnkfelix:rustc-contracts, r=oli-obk
#[contracts::requires(...)]  + #[contracts::ensures(...)]

cc https://github.com/rust-lang/rust/issues/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 (https://github.com/rust-lang/compiler-team/issues/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.
2025-02-05 05:03:01 +01:00
..
ast_utils Express contracts as part of function header and lower it to the contract lang items 2025-02-03 12:54:00 -08:00
mir Merge commit '51d49c1ae2' into clippy-subtree-update 2025-01-28 19:33:54 +01:00
ty Rollup merge of #135902 - compiler-errors:item-non-self-bound-in-new-solver, r=lcnr 2025-01-29 03:12:19 +01:00
attrs.rs Merge commit '609cd310be' into clippy-subtree-update 2024-12-26 15:15:54 +01:00
check_proc_macro.rs Merge commit '51d49c1ae2' into clippy-subtree-update 2025-01-28 19:33:54 +01:00
comparisons.rs Merge commit 'd9c24d1b1e' into clippyup 2023-07-17 10:22:32 +02:00
consts.rs Merge commit '51d49c1ae2' into clippy-subtree-update 2025-01-28 19:33:54 +01:00
diagnostics.rs Clippy: cfg out validate_diag on release builds 2024-11-08 10:49:54 +01:00
eager_or_lazy.rs Fix tools 2024-12-12 16:43:36 +00:00
higher.rs Merge commit '51d49c1ae2' into clippy-subtree-update 2025-01-28 19:33:54 +01:00
hir_utils.rs Use a different hir type for patterns in pattern types than we use in match patterns 2025-02-03 08:18:30 +00:00
lib.rs Eliminate PatKind::Path 2025-01-29 15:45:13 +00:00
macros.rs Merge commit '51d49c1ae2' into clippy-subtree-update 2025-01-28 19:33:54 +01:00
msrvs.rs Merge commit '51d49c1ae2' into clippy-subtree-update 2025-01-28 19:33:54 +01:00
numeric_literal.rs Merge commit '51d49c1ae2' into clippy-subtree-update 2025-01-28 19:33:54 +01:00
paths.rs Merge commit '19e305bb57' into clippy-subtree-update 2025-01-09 18:57:00 +01:00
ptr.rs Merge commit '7901289135' into clippy-subtree-update 2024-09-24 11:58:04 +02:00
qualify_min_const_fn.rs Contracts core intrinsics. 2025-02-03 12:53:57 -08:00
source.rs Avoid another &Lrc<..> in a return value. 2024-10-07 13:59:50 +11:00
str_utils.rs Merge commit '7901289135' into clippy-subtree-update 2024-09-24 11:58:04 +02:00
sugg.rs Fix tools 2024-12-12 16:43:36 +00:00
sym_helper.rs Merge commit '928e72dd10' into clippyup 2021-02-25 11:25:22 +01:00
usage.rs Merge commit 'ff4a26d442' into clippy-subtree-update 2024-11-28 19:38:59 +01:00
visitors.rs Merge commit '51d49c1ae2' into clippy-subtree-update 2025-01-28 19:33:54 +01:00