Skip to content

Commit 57a3300

Browse files
committedJun 13, 2019
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 25 additions & 25 deletions
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

Lines changed: 0 additions & 81 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 0 additions & 35 deletions
This file was deleted.

‎src/librustc_metadata/dynamic_lib.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 0 additions & 36 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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 {

‎src/libstd/sys_common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub mod bytestring;
6565
pub mod process;
6666
pub mod fs;
6767

68-
cfg_if! {
68+
cfg_if::cfg_if! {
6969
if #[cfg(any(target_os = "cloudabi",
7070
target_os = "l4re",
7171
target_os = "redox",

‎src/libunwind/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ doc = false
1919
core = { path = "../libcore" }
2020
libc = { version = "0.2.43", features = ['rustc-dep-of-std'], default-features = false }
2121
compiler_builtins = "0.1.0"
22+
cfg-if = "0.1.8"
2223

2324
[build-dependencies]
2425
cc = { optional = true, version = "1.0.1" }

‎src/libunwind/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111

1212
#![cfg_attr(not(target_env = "msvc"), feature(libc))]
1313

14-
#[macro_use]
15-
mod macros;
16-
17-
cfg_if! {
14+
cfg_if::cfg_if! {
1815
if #[cfg(target_env = "msvc")] {
1916
// no extra unwinder support needed
2017
} else if #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] {

‎src/libunwind/libunwind.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
#![allow(nonstandard_style)]
22

3-
macro_rules! cfg_if {
4-
( $( if #[cfg( $meta:meta )] { $($it1:item)* } else { $($it2:item)* } )* ) =>
5-
( $( $( #[cfg($meta)] $it1)* $( #[cfg(not($meta))] $it2)* )* )
6-
}
7-
83
use libc::{c_int, c_void, uintptr_t};
94

105
#[repr(C)]
@@ -82,7 +77,7 @@ extern "C" {
8277
pub fn _Unwind_GetDataRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
8378
}
8479

85-
cfg_if! {
80+
cfg_if::cfg_if! {
8681
if #[cfg(all(any(target_os = "ios", target_os = "netbsd", not(target_arch = "arm"))))] {
8782
// Not ARM EHABI
8883
#[repr(C)]
@@ -206,7 +201,9 @@ if #[cfg(all(any(target_os = "ios", target_os = "netbsd", not(target_arch = "arm
206201
pc
207202
}
208203
}
204+
} // cfg_if!
209205

206+
cfg_if::cfg_if! {
210207
if #[cfg(not(all(target_os = "ios", target_arch = "arm")))] {
211208
// Not 32-bit iOS
212209
extern "C" {

‎src/libunwind/macros.rs

Lines changed: 0 additions & 35 deletions
This file was deleted.

‎src/test/run-pass/issues/issue-39367.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ fn arena() -> &'static ArenaSet<Vec<u8>> {
1111
ArenaSet(vec![], &Z)
1212
}
1313
unsafe {
14-
use std::sync::{Once, ONCE_INIT};
14+
use std::sync::Once;
1515
fn require_sync<T: Sync>(_: &T) { }
1616
unsafe fn __stability() -> &'static ArenaSet<Vec<u8>> {
1717
use std::mem::transmute;
1818
static mut DATA: *const ArenaSet<Vec<u8>> = 0 as *const ArenaSet<Vec<u8>>;
1919

20-
static mut ONCE: Once = ONCE_INIT;
20+
static mut ONCE: Once = Once::new();
2121
ONCE.call_once(|| {
2222
DATA = transmute
2323
::<Box<ArenaSet<Vec<u8>>>, *const ArenaSet<Vec<u8>>>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
&{[1, 2, 3][4]};
3+
//~^ ERROR index out of bounds
4+
//~| ERROR reaching this expression at runtime will panic or abort
5+
//~| ERROR this expression will panic at runtime
6+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error: index out of bounds: the len is 3 but the index is 4
2+
--> $DIR/array-literal-index-oob.rs:2:7
3+
|
4+
LL | &{[1, 2, 3][4]};
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: #[deny(const_err)] on by default
8+
9+
error: this expression will panic at runtime
10+
--> $DIR/array-literal-index-oob.rs:2:5
11+
|
12+
LL | &{[1, 2, 3][4]};
13+
| ^^^^^^^^^^^^^^^ index out of bounds: the len is 3 but the index is 4
14+
15+
error: reaching this expression at runtime will panic or abort
16+
--> $DIR/array-literal-index-oob.rs:2:7
17+
|
18+
LL | &{[1, 2, 3][4]};
19+
| --^^^^^^^^^^^^-
20+
| |
21+
| index out of bounds: the len is 3 but the index is 4
22+
23+
error: aborting due to 3 previous errors
24+

‎src/tools/clippy

0 commit comments

Comments
 (0)
Please sign in to comment.