Skip to content

Commit 57a3300

Browse files
committed
Auto merge of #61799 - Centril:rollup-vpm5uxr, r=Centril
Rollup of 5 pull requests Successful merges: - #61598 (Handle index out of bound errors during const eval without panic) - #61720 (std: Remove internal definitions of `cfg_if!` macro) - #61757 (Deprecate ONCE_INIT in future 1.38 release) - #61766 (submodules: update clippy from c0dbd34 to bd33a97) - #61791 (Small cleanup in `check_pat_path`) Failed merges: r? @ghost
2 parents 96636f3 + 8917b8e commit 57a3300

File tree

27 files changed

+97
-244
lines changed

27 files changed

+97
-244
lines changed

Cargo.lock

+3
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,7 @@ name = "panic_unwind"
18321832
version = "0.0.0"
18331833
dependencies = [
18341834
"alloc 0.0.0",
1835+
"cfg-if 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
18351836
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
18361837
"core 0.0.0",
18371838
"libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -3384,6 +3385,7 @@ dependencies = [
33843385
"alloc 0.0.0",
33853386
"backtrace 0.3.29 (registry+https://github.com/rust-lang/crates.io-index)",
33863387
"cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)",
3388+
"cfg-if 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
33873389
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
33883390
"core 0.0.0",
33893391
"dlmalloc 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -3982,6 +3984,7 @@ name = "unwind"
39823984
version = "0.0.0"
39833985
dependencies = [
39843986
"cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)",
3987+
"cfg-if 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
39853988
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
39863989
"core 0.0.0",
39873990
"libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)",

src/libcore/hint.rs

+25-25
Original file line numberDiff line numberDiff line change
@@ -111,31 +111,31 @@ pub fn spin_loop() {
111111
/// This function is a no-op, and does not even read from `dummy`.
112112
#[inline]
113113
#[unstable(feature = "test", issue = "27812")]
114+
#[allow(unreachable_code)] // this makes #[cfg] a bit easier below.
114115
pub fn black_box<T>(dummy: T) -> T {
115-
cfg_if! {
116-
if #[cfg(any(
117-
target_arch = "asmjs",
118-
all(
119-
target_arch = "wasm32",
120-
target_os = "emscripten"
121-
)
122-
))] {
123-
#[inline]
124-
unsafe fn black_box_impl<T>(d: T) -> T {
125-
// these targets do not support inline assembly
126-
let ret = crate::ptr::read_volatile(&d);
127-
crate::mem::forget(d);
128-
ret
129-
}
130-
} else {
131-
#[inline]
132-
unsafe fn black_box_impl<T>(d: T) -> T {
133-
// we need to "use" the argument in some way LLVM can't
134-
// introspect.
135-
asm!("" : : "r"(&d));
136-
d
137-
}
138-
}
116+
// We need to "use" the argument in some way LLVM can't introspect, and on
117+
// targets that support it we can typically leverage inline assembly to do
118+
// this. LLVM's intepretation of inline assembly is that it's, well, a black
119+
// box. This isn't the greatest implementation since it probably deoptimizes
120+
// more than we want, but it's so far good enough.
121+
#[cfg(not(any(
122+
target_arch = "asmjs",
123+
all(
124+
target_arch = "wasm32",
125+
target_os = "emscripten"
126+
)
127+
)))]
128+
unsafe {
129+
asm!("" : : "r"(&dummy));
130+
return dummy;
131+
}
132+
133+
// Not all platforms support inline assembly so try to do something without
134+
// inline assembly which in theory still hinders at least some optimizations
135+
// on those targets. This is the "best effort" scenario.
136+
unsafe {
137+
let ret = crate::ptr::read_volatile(&dummy);
138+
crate::mem::forget(dummy);
139+
ret
139140
}
140-
unsafe { black_box_impl(dummy) }
141141
}

src/libcore/internal_macros.rs

-81
Original file line numberDiff line numberDiff line change
@@ -117,84 +117,3 @@ macro_rules! impl_fn_for_zst {
117117
)+
118118
}
119119
}
120-
121-
/// A macro for defining `#[cfg]` if-else statements.
122-
///
123-
/// The macro provided by this crate, `cfg_if`, is similar to the `if/elif` C
124-
/// preprocessor macro by allowing definition of a cascade of `#[cfg]` cases,
125-
/// emitting the implementation which matches first.
126-
///
127-
/// This allows you to conveniently provide a long list `#[cfg]`'d blocks of code
128-
/// without having to rewrite each clause multiple times.
129-
///
130-
/// # Example
131-
///
132-
/// ```
133-
/// #[macro_use]
134-
/// extern crate cfg_if;
135-
///
136-
/// cfg_if! {
137-
/// if #[cfg(unix)] {
138-
/// fn foo() { /* unix specific functionality */ }
139-
/// } else if #[cfg(target_pointer_width = "32")] {
140-
/// fn foo() { /* non-unix, 32-bit functionality */ }
141-
/// } else {
142-
/// fn foo() { /* fallback implementation */ }
143-
/// }
144-
/// }
145-
///
146-
/// # fn main() {}
147-
/// ```
148-
macro_rules! cfg_if {
149-
// match if/else chains with a final `else`
150-
($(
151-
if #[cfg($($meta:meta),*)] { $($it:item)* }
152-
) else * else {
153-
$($it2:item)*
154-
}) => {
155-
cfg_if! {
156-
@__items
157-
() ;
158-
$( ( ($($meta),*) ($($it)*) ), )*
159-
( () ($($it2)*) ),
160-
}
161-
};
162-
163-
// match if/else chains lacking a final `else`
164-
(
165-
if #[cfg($($i_met:meta),*)] { $($i_it:item)* }
166-
$(
167-
else if #[cfg($($e_met:meta),*)] { $($e_it:item)* }
168-
)*
169-
) => {
170-
cfg_if! {
171-
@__items
172-
() ;
173-
( ($($i_met),*) ($($i_it)*) ),
174-
$( ( ($($e_met),*) ($($e_it)*) ), )*
175-
( () () ),
176-
}
177-
};
178-
179-
// Internal and recursive macro to emit all the items
180-
//
181-
// Collects all the negated cfgs in a list at the beginning and after the
182-
// semicolon is all the remaining items
183-
(@__items ($($not:meta,)*) ; ) => {};
184-
(@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), $($rest:tt)*) => {
185-
// Emit all items within one block, applying an approprate #[cfg]. The
186-
// #[cfg] will require all `$m` matchers specified and must also negate
187-
// all previous matchers.
188-
cfg_if! { @__apply cfg(all($($m,)* not(any($($not),*)))), $($it)* }
189-
190-
// Recurse to emit all other items in `$rest`, and when we do so add all
191-
// our `$m` matchers to the list of `$not` matchers as future emissions
192-
// will have to negate everything we just matched as well.
193-
cfg_if! { @__items ($($not,)* $($m,)*) ; $($rest)* }
194-
};
195-
196-
// Internal macro to Apply a cfg attribute to a list of items
197-
(@__apply $m:meta, $($it:item)*) => {
198-
$(#[$m] $it)*
199-
};
200-
}

src/libpanic_unwind/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ core = { path = "../libcore" }
1616
libc = { version = "0.2", default-features = false }
1717
unwind = { path = "../libunwind" }
1818
compiler_builtins = "0.1.0"
19+
cfg-if = "0.1.8"

src/libpanic_unwind/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ use core::mem;
3838
use core::raw;
3939
use core::panic::BoxMeUp;
4040

41-
#[macro_use]
42-
mod macros;
43-
44-
cfg_if! {
41+
cfg_if::cfg_if! {
4542
if #[cfg(target_os = "emscripten")] {
4643
#[path = "emcc.rs"]
4744
mod imp;

src/libpanic_unwind/macros.rs

-35
This file was deleted.

src/librustc_metadata/dynamic_lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ mod dl {
161161
pub fn check_for_errors_in<T, F>(f: F) -> Result<T, String> where
162162
F: FnOnce() -> T,
163163
{
164-
use std::sync::{Mutex, Once, ONCE_INIT};
165-
static INIT: Once = ONCE_INIT;
164+
use std::sync::{Mutex, Once};
165+
static INIT: Once = Once::new();
166166
static mut LOCK: *mut Mutex<()> = 0 as *mut _;
167167
unsafe {
168168
INIT.call_once(|| {

src/librustc_mir/interpret/place.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,12 @@ where
348348
offsets[usize::try_from(field).unwrap()],
349349
layout::FieldPlacement::Array { stride, .. } => {
350350
let len = base.len(self)?;
351-
assert!(field < len, "Tried to access element {} of array/slice with length {}",
352-
field, len);
351+
if field >= len {
352+
// This can be violated because this runs during promotion on code where the
353+
// type system has not yet ensured that such things don't happen.
354+
debug!("Tried to access element {} of array/slice with length {}", field, len);
355+
return err!(BoundsCheck { len, index: field });
356+
}
353357
stride * field
354358
}
355359
layout::FieldPlacement::Union(count) => {

src/librustc_typeck/check/_match.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1067,10 +1067,7 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
10671067
self.set_tainted_by_errors();
10681068
return tcx.types.err;
10691069
}
1070-
Res::Def(DefKind::Method, _) => {
1071-
report_unexpected_variant_res(tcx, res, pat.span, qpath);
1072-
return tcx.types.err;
1073-
}
1070+
Res::Def(DefKind::Method, _) |
10741071
Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) |
10751072
Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) => {
10761073
report_unexpected_variant_res(tcx, res, pat.span, qpath);

src/libstd/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ crate-type = ["dylib", "rlib"]
1515

1616
[dependencies]
1717
alloc = { path = "../liballoc" }
18+
cfg-if = "0.1.8"
1819
panic_unwind = { path = "../libpanic_unwind", optional = true }
1920
panic_abort = { path = "../libpanic_abort" }
2021
core = { path = "../libcore" }

src/libstd/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,12 @@ extern crate libc;
336336
#[allow(unused_extern_crates)]
337337
extern crate unwind;
338338

339+
// Only needed for now for the `std_detect` module until that crate changes to
340+
// use `cfg_if::cfg_if!`
341+
#[macro_use]
342+
#[cfg(not(test))]
343+
extern crate cfg_if;
344+
339345
// During testing, this crate is not actually the "real" std library, but rather
340346
// it links to the real std library, which was compiled from this same source
341347
// code. So any lang items std defines are conditionally excluded (or else they

src/libstd/macros.rs

-36
Original file line numberDiff line numberDiff line change
@@ -896,39 +896,3 @@ mod builtin {
896896
($cond:expr, $($arg:tt)+) => ({ /* compiler built-in */ });
897897
}
898898
}
899-
900-
/// Defines `#[cfg]` if-else statements.
901-
///
902-
/// This is similar to the `if/elif` C preprocessor macro by allowing definition
903-
/// of a cascade of `#[cfg]` cases, emitting the implementation which matches
904-
/// first.
905-
///
906-
/// This allows you to conveniently provide a long list `#[cfg]`'d blocks of code
907-
/// without having to rewrite each clause multiple times.
908-
macro_rules! cfg_if {
909-
($(
910-
if #[cfg($($meta:meta),*)] { $($it:item)* }
911-
) else * else {
912-
$($it2:item)*
913-
}) => {
914-
__cfg_if_items! {
915-
() ;
916-
$( ( ($($meta),*) ($($it)*) ), )*
917-
( () ($($it2)*) ),
918-
}
919-
}
920-
}
921-
922-
macro_rules! __cfg_if_items {
923-
(($($not:meta,)*) ; ) => {};
924-
(($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), $($rest:tt)*) => {
925-
__cfg_if_apply! { cfg(all(not(any($($not),*)), $($m,)*)), $($it)* }
926-
__cfg_if_items! { ($($not,)* $($m,)*) ; $($rest)* }
927-
}
928-
}
929-
930-
macro_rules! __cfg_if_apply {
931-
($m:meta, $($it:item)*) => {
932-
$(#[$m] $it)*
933-
}
934-
}

src/libstd/os/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![stable(feature = "os", since = "1.0.0")]
44
#![allow(missing_docs, nonstandard_style, missing_debug_implementations)]
55

6-
cfg_if! {
6+
cfg_if::cfg_if! {
77
if #[cfg(rustdoc)] {
88

99
// When documenting libstd we want to show unix/windows/linux modules as

src/libstd/sync/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ pub use self::condvar::{Condvar, WaitTimeoutResult};
163163
#[stable(feature = "rust1", since = "1.0.0")]
164164
pub use self::mutex::{Mutex, MutexGuard};
165165
#[stable(feature = "rust1", since = "1.0.0")]
166+
#[allow(deprecated)]
166167
pub use self::once::{Once, OnceState, ONCE_INIT};
167168
#[stable(feature = "rust1", since = "1.0.0")]
168169
pub use crate::sys_common::poison::{PoisonError, TryLockError, TryLockResult, LockResult};

src/libstd/sync/once.rs

+5
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ pub struct OnceState {
115115
/// static START: Once = ONCE_INIT;
116116
/// ```
117117
#[stable(feature = "rust1", since = "1.0.0")]
118+
#[rustc_deprecated(
119+
since = "1.38.0",
120+
reason = "the `new` function is now preferred",
121+
suggestion = "Once::new()",
122+
)]
118123
pub const ONCE_INIT: Once = Once::new();
119124

120125
// Four states that a Once can be in, encoded into the lower bits of `state` in

src/libstd/sys/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
2323
#![allow(missing_debug_implementations)]
2424

25-
cfg_if! {
25+
cfg_if::cfg_if! {
2626
if #[cfg(unix)] {
2727
mod unix;
2828
pub use self::unix::*;
@@ -54,7 +54,7 @@ cfg_if! {
5454
// Windows when we're compiling for Linux.
5555

5656
#[cfg(rustdoc)]
57-
cfg_if! {
57+
cfg_if::cfg_if! {
5858
if #[cfg(any(unix, target_os = "redox"))] {
5959
// On unix we'll document what's already available
6060
#[stable(feature = "rust1", since = "1.0.0")]
@@ -77,7 +77,7 @@ cfg_if! {
7777
}
7878

7979
#[cfg(rustdoc)]
80-
cfg_if! {
80+
cfg_if::cfg_if! {
8181
if #[cfg(windows)] {
8282
// On windows we'll just be documenting what's already available
8383
#[allow(missing_docs)]

src/libstd/sys/wasm/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub mod stdio;
4040

4141
pub use crate::sys_common::os_str_bytes as os_str;
4242

43-
cfg_if! {
43+
cfg_if::cfg_if! {
4444
if #[cfg(target_feature = "atomics")] {
4545
#[path = "condvar_atomics.rs"]
4646
pub mod condvar;

src/libstd/sys/wasm/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub mod guard {
5959
pub unsafe fn init() -> Option<Guard> { None }
6060
}
6161

62-
cfg_if! {
62+
cfg_if::cfg_if! {
6363
if #[cfg(all(target_feature = "atomics", feature = "wasm-bindgen-threads"))] {
6464
#[link(wasm_import_module = "__wbindgen_thread_xform__")]
6565
extern {

0 commit comments

Comments
 (0)