Skip to content

Commit 857b066

Browse files
committed
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.
1 parent 8e0b68e commit 857b066

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

library/core/src/contracts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub use crate::macros::builtin::{contracts_ensures as ensures, contracts_require
1818
#[lang = "contract_build_check_ensures"]
1919
pub const fn build_check_ensures<Ret, C>(cond: C) -> C
2020
where
21-
C: Fn(&Ret) -> bool + Copy + 'static,
21+
C: Fn(&Ret) -> bool + Copy,
2222
{
2323
cond
2424
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//@ run-pass
2+
//@ compile-flags: -Zcontract-checks=yes
3+
4+
#![feature(core_intrinsics)]
5+
#![feature(contracts)]
6+
//~^ WARN the feature `contracts` is incomplete and may not be safe to use
7+
//and/or cause compiler crashes [incomplete_features]
8+
#![allow(unused)]
9+
10+
// Regression test to allow contract `ensures` clauses to reference non-static
11+
// references and non-static types. Previously, contracts in the below functions
12+
// would raise type/lifetime errors due a `'static` bound on the `ensures`
13+
// closure.
14+
15+
extern crate core;
16+
use core::contracts::ensures;
17+
18+
#[ensures(|_| { x; true })]
19+
pub fn noop<T>(x: &T) {}
20+
21+
#[ensures(move |_| { x; true })]
22+
pub fn noop_mv<T>(x: &T) {}
23+
24+
#[ensures(|_| { x; true })]
25+
pub fn noop_ptr<T>(x: *const T) {}
26+
27+
#[ensures(move |_| { x; true })]
28+
pub fn noop_ptr_mv<T>(x: *const T) {}
29+
30+
fn main() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/ensures-lifetime.rs:5:12
3+
|
4+
LL | #![feature(contracts)]
5+
| ^^^^^^^^^
6+
|
7+
= note: see issue #128044 <https://github.com/rust-lang/rust/issues/128044> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
warning: 1 warning emitted
11+

0 commit comments

Comments
 (0)