From 75caeab25aed9c2017a39ea513f2dfb4a5f3f5c5 Mon Sep 17 00:00:00 2001 From: Dawid Lachowicz Date: Thu, 6 Nov 2025 17:48:49 +0000 Subject: [PATCH] Remove `'static` bound on contract ensures closure The `'static` bound on the closure of `build_check_ensures` prevented some patterns of contracts from type checking, without a clear reason for doing so. As such, this change removes the `'static` bound. --- library/core/src/contracts.rs | 2 +- tests/ui/contracts/ensures-lifetime.rs | 30 ++++++++++++++++++++++ tests/ui/contracts/ensures-lifetime.stderr | 11 ++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/ui/contracts/ensures-lifetime.rs create mode 100644 tests/ui/contracts/ensures-lifetime.stderr diff --git a/library/core/src/contracts.rs b/library/core/src/contracts.rs index 495f84bce4bf2..b9d05755f6a80 100644 --- a/library/core/src/contracts.rs +++ b/library/core/src/contracts.rs @@ -18,7 +18,7 @@ pub use crate::macros::builtin::{contracts_ensures as ensures, contracts_require #[lang = "contract_build_check_ensures"] pub const fn build_check_ensures(cond: C) -> C where - C: Fn(&Ret) -> bool + Copy + 'static, + C: Fn(&Ret) -> bool + Copy, { cond } diff --git a/tests/ui/contracts/ensures-lifetime.rs b/tests/ui/contracts/ensures-lifetime.rs new file mode 100644 index 0000000000000..d621ff38c0355 --- /dev/null +++ b/tests/ui/contracts/ensures-lifetime.rs @@ -0,0 +1,30 @@ +//@ run-pass +//@ compile-flags: -Zcontract-checks=yes + +#![feature(core_intrinsics)] +#![feature(contracts)] +//~^ WARN the feature `contracts` is incomplete and may not be safe to use +//and/or cause compiler crashes [incomplete_features] +#![allow(unused)] + +// Regression test to allow contract `ensures` clauses to reference non-static +// references and non-static types. Previously, contracts in the below functions +// would raise type/lifetime errors due a `'static` bound on the `ensures` +// closure. + +extern crate core; +use core::contracts::ensures; + +#[ensures(|_| { x; true })] +pub fn noop(x: &T) {} + +#[ensures(move |_| { x; true })] +pub fn noop_mv(x: &T) {} + +#[ensures(|_| { x; true })] +pub fn noop_ptr(x: *const T) {} + +#[ensures(move |_| { x; true })] +pub fn noop_ptr_mv(x: *const T) {} + +fn main() {} diff --git a/tests/ui/contracts/ensures-lifetime.stderr b/tests/ui/contracts/ensures-lifetime.stderr new file mode 100644 index 0000000000000..e590d2faaa69e --- /dev/null +++ b/tests/ui/contracts/ensures-lifetime.stderr @@ -0,0 +1,11 @@ +warning: the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/ensures-lifetime.rs:5:12 + | +LL | #![feature(contracts)] + | ^^^^^^^^^ + | + = note: see issue #128044 for more information + = note: `#[warn(incomplete_features)]` on by default + +warning: 1 warning emitted +