Skip to content

Commit c5a4094

Browse files
committed
Fix unreachable expression warning
Invert the order that we pass the arguments to the `contract_check_ensures` function to avoid the warning when the tail of the function is unreachable. Note that the call itself is also unreachable, but we have already handled that case by ignoring unreachable call for contract calls.
1 parent d96e3e7 commit c5a4094

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

Diff for: core/src/contracts.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22
33
pub use crate::macros::builtin::{contracts_ensures as ensures, contracts_requires as requires};
44

5-
/// Emitted by rustc as a desugaring of `#[ensures(PRED)] fn foo() -> R { ... [return R;] ... }`
6-
/// into: `fn foo() { let _check = build_check_ensures(|ret| PRED) ... [return _check(R);] ... }`
7-
/// (including the implicit return of the tail expression, if any).
5+
/// This is an identity function used as part of the desugaring of the `#[ensures]` attribute.
86
///
9-
/// This call helps with type inference for the predicate.
7+
/// This is an existing hack to allow users to omit the type of the return value in their ensures
8+
/// attribute.
9+
///
10+
/// Ideally, rustc should be able to generate the type annotation.
11+
/// The existing lowering logic makes it rather hard to add the explicit type annotation,
12+
/// while the function call is fairly straight forward.
1013
#[unstable(feature = "contracts_internals", issue = "128044" /* compiler-team#759 */)]
14+
// Similar to `contract_check_requires`, we need to use the user-facing
15+
// `contracts` feature rather than the perma-unstable `contracts_internals`.
16+
// Const-checking doesn't honor allow internal unstable logic used by contract expansion.
1117
#[rustc_const_unstable(feature = "contracts", issue = "128044")]
1218
#[lang = "contract_build_check_ensures"]
13-
#[track_caller]
1419
pub const fn build_check_ensures<Ret, C>(cond: C) -> C
1520
where
1621
C: Fn(&Ret) -> bool + Copy + 'static,

Diff for: core/src/intrinsics/mod.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -3453,6 +3453,10 @@ pub const fn contract_checks() -> bool {
34533453
///
34543454
/// Note that this function is a no-op during constant evaluation.
34553455
#[unstable(feature = "contracts_internals", issue = "128044")]
3456+
// Calls to this function get inserted by an AST expansion pass, which uses the equivalent of
3457+
// `#[allow_internal_unstable]` to allow using `contracts_internals` functions. Const-checking
3458+
// doesn't honor `#[allow_internal_unstable]`, so for the const feature gate we use the user-facing
3459+
// `contracts` feature rather than the perma-unstable `contracts_internals`
34563460
#[rustc_const_unstable(feature = "contracts", issue = "128044")]
34573461
#[lang = "contract_check_requires"]
34583462
#[rustc_intrinsic]
@@ -3478,12 +3482,15 @@ pub const fn contract_check_requires<C: Fn() -> bool + Copy>(cond: C) {
34783482
/// Note that this function is a no-op during constant evaluation.
34793483
#[cfg(not(bootstrap))]
34803484
#[unstable(feature = "contracts_internals", issue = "128044")]
3485+
// Similar to `contract_check_requires`, we need to use the user-facing
3486+
// `contracts` feature rather than the perma-unstable `contracts_internals`.
3487+
// Const-checking doesn't honor allow internal unstable logic used by contract expansion.
34813488
#[rustc_const_unstable(feature = "contracts", issue = "128044")]
34823489
#[lang = "contract_check_ensures"]
34833490
#[rustc_intrinsic]
3484-
pub const fn contract_check_ensures<Ret, C: Fn(&Ret) -> bool + Copy>(ret: Ret, cond: C) -> Ret {
3491+
pub const fn contract_check_ensures<C: Fn(&Ret) -> bool + Copy, Ret>(cond: C, ret: Ret) -> Ret {
34853492
const_eval_select!(
3486-
@capture[Ret, C: Fn(&Ret) -> bool + Copy] { ret: Ret, cond: C } -> Ret :
3493+
@capture[C: Fn(&Ret) -> bool + Copy, Ret] { cond: C, ret: Ret } -> Ret :
34873494
if const {
34883495
// Do nothing
34893496
ret

0 commit comments

Comments
 (0)