Skip to content

Commit e9af8b0

Browse files
committed
Auto merge of rust-lang#122975 - DianQK:simplify_ub_check, r=saethlin
Eliminate `UbChecks` for non-standard libraries The purpose of this PR is to allow other passes to treat `UbChecks` as constants in MIR for optimization after rust-lang#122629. r? RalfJung
2 parents f1695e5 + 5422044 commit e9af8b0

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
// Language features:
177177
// tidy-alphabetical-start
178178
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
179+
#![cfg_attr(not(bootstrap), rustc_preserve_ub_checks)]
179180
#![cfg_attr(not(test), feature(coroutine_trait))]
180181
#![cfg_attr(test, feature(panic_update_hook))]
181182
#![cfg_attr(test, feature(test))]

core/src/intrinsics.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -2686,12 +2686,14 @@ pub const unsafe fn typed_swap<T>(x: *mut T, y: *mut T) {
26862686
unsafe { ptr::swap_nonoverlapping(x, y, 1) };
26872687
}
26882688

2689-
/// Returns whether we should perform some UB-checking at runtime. This evaluate to the value of
2690-
/// `cfg!(debug_assertions)` during monomorphization.
2691-
///
2692-
/// This intrinsic is evaluated after monomorphization, which is relevant when mixing crates
2693-
/// compiled with and without debug_assertions. The common case here is a user program built with
2694-
/// debug_assertions linked against the distributed sysroot which is built without debug_assertions.
2689+
/// Returns whether we should perform some UB-checking at runtime. This eventually evaluates to
2690+
/// `cfg!(debug_assertions)`, but behaves different from `cfg!` when mixing crates built with different
2691+
/// flags: if the crate has debug assertions enabled or carries the `#[rustc_preserve_ub_checks]`
2692+
/// attribute, evaluation is delayed until monomorphization (or until the call gets inlined into
2693+
/// a crate that does not delay evaluation further); otherwise it can happen any time.
2694+
///
2695+
/// The common case here is a user program built with debug_assertions linked against the distributed
2696+
/// sysroot which is built without debug_assertions but with `#[rustc_preserve_ub_checks]`.
26952697
/// For code that gets monomorphized in the user crate (i.e., generic functions and functions with
26962698
/// `#[inline]`), gating assertions on `ub_checks()` rather than `cfg!(debug_assertions)` means that
26972699
/// assertions are enabled whenever the *user crate* has debug assertions enabled. However if the

core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
))]
9595
#![no_core]
9696
#![rustc_coherence_is_core]
97+
#![cfg_attr(not(bootstrap), rustc_preserve_ub_checks)]
9798
//
9899
// Lints:
99100
#![deny(rust_2021_incompatible_or_patterns)]

std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
//
222222
#![cfg_attr(not(feature = "restricted-std"), stable(feature = "rust1", since = "1.0.0"))]
223223
#![cfg_attr(feature = "restricted-std", unstable(feature = "restricted_std", issue = "none"))]
224+
#![cfg_attr(not(bootstrap), rustc_preserve_ub_checks)]
224225
#![doc(
225226
html_playground_url = "https://play.rust-lang.org/",
226227
issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",

0 commit comments

Comments
 (0)