Skip to content

Commit 67512de

Browse files
committed
Auto merge of rust-lang#132468 - cuviper:beta-next, r=cuviper
[beta] backports - Bump libc to 0.2.161 rust-lang#131823 - Avoid use imports in `thread_local_inner!` rust-lang#131866 - Mark `simplify_aggregate_to_copy` mir-opt as unsound rust-lang#132356 r? cuviper
2 parents f41c7ed + 0f95b22 commit 67512de

19 files changed

+245
-69
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
10791079
}
10801080
}
10811081

1082-
if let AggregateTy::Def(_, _) = ty
1082+
// unsound: https://github.com/rust-lang/rust/issues/132353
1083+
if tcx.sess.opts.unstable_opts.unsound_mir_opts
1084+
&& let AggregateTy::Def(_, _) = ty
10831085
&& let Some(value) =
10841086
self.simplify_aggregate_to_copy(rvalue, location, &fields, variant_index)
10851087
{

library/Cargo.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is automatically @generated by Cargo.
22
# It is not intended for manual editing.
3-
version = 3
3+
version = 4
44

55
[[package]]
66
name = "addr2line"
@@ -158,9 +158,9 @@ dependencies = [
158158

159159
[[package]]
160160
name = "libc"
161-
version = "0.2.159"
161+
version = "0.2.161"
162162
source = "registry+https://github.com/rust-lang/crates.io-index"
163-
checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5"
163+
checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1"
164164
dependencies = [
165165
"rustc-std-workspace-core",
166166
]

library/std/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ miniz_oxide = { version = "0.7.0", optional = true, default-features = false }
3939
addr2line = { version = "0.22.0", optional = true, default-features = false }
4040

4141
[target.'cfg(not(all(windows, target_env = "msvc")))'.dependencies]
42-
libc = { version = "0.2.156", default-features = false, features = [
42+
libc = { version = "0.2.161", default-features = false, features = [
4343
'rustc-dep-of-std',
4444
], public = true }
4545

library/std/src/sys/thread_local/native/mod.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,21 @@ pub use lazy::Storage as LazyStorage;
4949
#[unstable(feature = "thread_local_internals", issue = "none")]
5050
#[rustc_macro_transparency = "semitransparent"]
5151
pub macro thread_local_inner {
52-
// used to generate the `LocalKey` value for const-initialized thread locals
52+
// NOTE: we cannot import `LocalKey`, `LazyStorage` or `EagerStorage` with a `use` because that
53+
// can shadow user provided type or type alias with a matching name. Please update the shadowing
54+
// test in `tests/thread.rs` if these types are renamed.
55+
56+
// Used to generate the `LocalKey` value for const-initialized thread locals.
5357
(@key $t:ty, const $init:expr) => {{
5458
const __INIT: $t = $init;
5559

5660
unsafe {
57-
use $crate::mem::needs_drop;
58-
use $crate::thread::LocalKey;
59-
use $crate::thread::local_impl::EagerStorage;
60-
61-
LocalKey::new(const {
62-
if needs_drop::<$t>() {
61+
$crate::thread::LocalKey::new(const {
62+
if $crate::mem::needs_drop::<$t>() {
6363
|_| {
6464
#[thread_local]
65-
static VAL: EagerStorage<$t> = EagerStorage::new(__INIT);
65+
static VAL: $crate::thread::local_impl::EagerStorage<$t>
66+
= $crate::thread::local_impl::EagerStorage::new(__INIT);
6667
VAL.get()
6768
}
6869
} else {
@@ -84,21 +85,19 @@ pub macro thread_local_inner {
8485
}
8586

8687
unsafe {
87-
use $crate::mem::needs_drop;
88-
use $crate::thread::LocalKey;
89-
use $crate::thread::local_impl::LazyStorage;
90-
91-
LocalKey::new(const {
92-
if needs_drop::<$t>() {
88+
$crate::thread::LocalKey::new(const {
89+
if $crate::mem::needs_drop::<$t>() {
9390
|init| {
9491
#[thread_local]
95-
static VAL: LazyStorage<$t, ()> = LazyStorage::new();
92+
static VAL: $crate::thread::local_impl::LazyStorage<$t, ()>
93+
= $crate::thread::local_impl::LazyStorage::new();
9694
VAL.get_or_init(init, __init)
9795
}
9896
} else {
9997
|init| {
10098
#[thread_local]
101-
static VAL: LazyStorage<$t, !> = LazyStorage::new();
99+
static VAL: $crate::thread::local_impl::LazyStorage<$t, !>
100+
= $crate::thread::local_impl::LazyStorage::new();
102101
VAL.get_or_init(init, __init)
103102
}
104103
}

library/std/src/sys/thread_local/os.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@ pub macro thread_local_inner {
1515
$crate::thread::local_impl::thread_local_inner!(@key $t, { const INIT_EXPR: $t = $init; INIT_EXPR })
1616
},
1717

18-
// used to generate the `LocalKey` value for `thread_local!`
18+
// NOTE: we cannot import `Storage` or `LocalKey` with a `use` because that can shadow user
19+
// provided type or type alias with a matching name. Please update the shadowing test in
20+
// `tests/thread.rs` if these types are renamed.
21+
22+
// used to generate the `LocalKey` value for `thread_local!`.
1923
(@key $t:ty, $init:expr) => {{
2024
#[inline]
2125
fn __init() -> $t { $init }
2226

27+
// NOTE: this cannot import `LocalKey` or `Storage` with a `use` because that can shadow
28+
// user provided type or type alias with a matching name. Please update the shadowing test
29+
// in `tests/thread.rs` if these types are renamed.
2330
unsafe {
24-
use $crate::thread::LocalKey;
25-
use $crate::thread::local_impl::Storage;
26-
2731
// Inlining does not work on windows-gnu due to linking errors around
2832
// dllimports. See https://github.com/rust-lang/rust/issues/109797.
29-
LocalKey::new(#[cfg_attr(windows, inline(never))] |init| {
30-
static VAL: Storage<$t> = Storage::new();
33+
$crate::thread::LocalKey::new(#[cfg_attr(windows, inline(never))] |init| {
34+
static VAL: $crate::thread::local_impl::Storage<$t>
35+
= $crate::thread::local_impl::Storage::new();
3136
VAL.get(init, __init)
3237
})
3338
}

library/std/tests/thread.rs

+23
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,29 @@ fn thread_local_containing_const_statements() {
3838
assert_eq!(REFCELL.take(), 1);
3939
}
4040

41+
#[test]
42+
fn thread_local_hygeiene() {
43+
// Previously `thread_local_inner!` had use imports for `LocalKey`, `Storage`, `EagerStorage`
44+
// and `LazyStorage`. The use imports will shadow a user-provided type or type alias if the
45+
// user-provided type or type alias has the same name. Make sure that this does not happen. See
46+
// <https://github.com/rust-lang/rust/issues/131863>.
47+
//
48+
// NOTE: if the internal implementation details change (i.e. get renamed), this test should be
49+
// updated.
50+
51+
#![allow(dead_code)]
52+
type LocalKey = ();
53+
type Storage = ();
54+
type LazyStorage = ();
55+
type EagerStorage = ();
56+
thread_local! {
57+
static A: LocalKey = const { () };
58+
static B: Storage = const { () };
59+
static C: LazyStorage = const { () };
60+
static D: EagerStorage = const { () };
61+
}
62+
}
63+
4164
#[test]
4265
// Include an ignore list on purpose, so that new platforms don't miss it
4366
#[cfg_attr(

tests/codegen/clone_as_copy.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//@ revisions: DEBUGINFO NODEBUGINFO
2+
//@ compile-flags: -Zunsound-mir-opts
3+
// FIXME: see <https://github.com/rust-lang/rust/issues/132353>
24
//@ compile-flags: -O -Cno-prepopulate-passes
35
//@ [DEBUGINFO] compile-flags: -Cdebuginfo=full
46

tests/codegen/try_question_mark_nop.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
//@ compile-flags: -O -Z merge-functions=disabled --edition=2021
22
//@ only-x86_64
33
// FIXME: Remove the `min-llvm-version`.
4-
//@ revisions: NINETEEN TWENTY
5-
//@[NINETEEN] min-llvm-version: 19
6-
//@[NINETEEN] ignore-llvm-version: 20-99
7-
//@[TWENTY] min-llvm-version: 20
4+
//@ min-llvm-version: 19
85

96
#![crate_type = "lib"]
107
#![feature(try_blocks)]
@@ -16,12 +13,9 @@ use std::ptr::NonNull;
1613
#[no_mangle]
1714
pub fn option_nop_match_32(x: Option<u32>) -> Option<u32> {
1815
// CHECK: start:
19-
// NINETEEN-NEXT: [[TRUNC:%.*]] = trunc nuw i32 %0 to i1
20-
// NINETEEN-NEXT: [[FIRST:%.*]] = select i1 [[TRUNC]], i32 %0
21-
// NINETEEN-NEXT: insertvalue { i32, i32 } poison, i32 [[FIRST]], 0
22-
// TWENTY-NEXT: insertvalue { i32, i32 } poison, i32 %0, 0
23-
// CHECK-NEXT: insertvalue { i32, i32 }
24-
// CHECK-NEXT: ret { i32, i32 }
16+
// CHECK-NEXT: [[REG1:%.*]] = insertvalue { i32, i32 } poison, i32 %0, 0
17+
// CHECK-NEXT: [[REG2:%.*]] = insertvalue { i32, i32 } [[REG1]], i32 %1, 1
18+
// CHECK-NEXT: ret { i32, i32 } [[REG2]]
2519
match x {
2620
Some(x) => Some(x),
2721
None => None,

tests/mir-opt/gvn_clone.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ compile-flags: -Zunsound-mir-opts
2+
// FIXME: see <https://github.com/rust-lang/rust/issues/132353>
13
//@ test-mir-pass: GVN
24
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-before-inline
35

tests/mir-opt/gvn_clone.{impl#0}-clone.GVN.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
- // MIR for `<impl at $DIR/gvn_clone.rs:12:10: 12:15>::clone` before GVN
2-
+ // MIR for `<impl at $DIR/gvn_clone.rs:12:10: 12:15>::clone` after GVN
1+
- // MIR for `<impl at $DIR/gvn_clone.rs:14:10: 14:15>::clone` before GVN
2+
+ // MIR for `<impl at $DIR/gvn_clone.rs:14:10: 14:15>::clone` after GVN
33

4-
fn <impl at $DIR/gvn_clone.rs:12:10: 12:15>::clone(_1: &AllCopy) -> AllCopy {
4+
fn <impl at $DIR/gvn_clone.rs:14:10: 14:15>::clone(_1: &AllCopy) -> AllCopy {
55
debug self => _1;
66
let mut _0: AllCopy;
77
let mut _2: i32;

tests/mir-opt/gvn_copy_aggregate.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ compile-flags: -Zunsound-mir-opts
2+
// FIXME: see <https://github.com/rust-lang/rust/issues/132353.
13
//@ test-mir-pass: GVN
24
//@ compile-flags: -Cpanic=abort
35

tests/mir-opt/pre-codegen/clone_as_copy.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ compile-flags: -Zunsound-mir-opts
2+
// FIXME: see <https://github.com/rust-lang/rust/issues/132353>
13
//@ compile-flags: -Cdebuginfo=full
24

35
// Check if we have transformed the nested clone to the copy in the complete pipeline.

tests/mir-opt/pre-codegen/no_inlined_clone.{impl#0}-clone.PreCodegen.after.mir

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
fn <impl at $DIR/no_inlined_clone.rs:9:10: 9:15>::clone(_1: &Foo) -> Foo {
44
debug self => _1;
55
let mut _0: Foo;
6+
let mut _2: i32;
67

78
bb0: {
8-
_0 = copy (*_1);
9+
StorageLive(_2);
10+
_2 = copy ((*_1).0: i32);
11+
_0 = Foo { a: move _2 };
12+
StorageDead(_2);
913
return;
1014
}
1115
}

tests/mir-opt/pre-codegen/try_identity.old.PreCodegen.after.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ fn old(_1: Result<T, E>) -> Result<T, E> {
1919
}
2020

2121
bb1: {
22-
_3 = copy ((_1 as Ok).0: T);
23-
_0 = copy _1;
22+
_3 = move ((_1 as Ok).0: T);
23+
_0 = Result::<T, E>::Ok(copy _3);
2424
goto -> bb3;
2525
}
2626

2727
bb2: {
28-
_4 = copy ((_1 as Err).0: E);
29-
_0 = copy _1;
28+
_4 = move ((_1 as Err).0: E);
29+
_0 = Result::<T, E>::Err(copy _4);
3030
goto -> bb3;
3131
}
3232

tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir

+18-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
77
debug self => _1;
88
scope 2 (inlined Vec::<u8>::as_slice) {
99
debug self => _1;
10-
let mut _6: usize;
10+
let mut _7: usize;
1111
scope 3 (inlined Vec::<u8>::as_ptr) {
1212
debug self => _1;
1313
let mut _2: &alloc::raw_vec::RawVec<u8>;
@@ -16,6 +16,7 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
1616
let mut _3: &alloc::raw_vec::RawVecInner;
1717
scope 5 (inlined alloc::raw_vec::RawVecInner::ptr::<u8>) {
1818
debug self => _3;
19+
let mut _6: std::ptr::NonNull<u8>;
1920
scope 6 (inlined alloc::raw_vec::RawVecInner::non_null::<u8>) {
2021
debug self => _3;
2122
let mut _4: std::ptr::NonNull<u8>;
@@ -31,20 +32,20 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
3132
}
3233
}
3334
scope 10 (inlined Unique::<u8>::as_non_null_ptr) {
34-
debug ((self: Unique<u8>).0: std::ptr::NonNull<u8>) => _4;
35+
debug ((self: Unique<u8>).0: std::ptr::NonNull<u8>) => _6;
3536
debug ((self: Unique<u8>).1: std::marker::PhantomData<u8>) => const PhantomData::<u8>;
3637
}
3738
}
3839
scope 11 (inlined NonNull::<u8>::as_ptr) {
39-
debug self => _4;
40+
debug self => _6;
4041
}
4142
}
4243
}
4344
}
4445
scope 12 (inlined std::slice::from_raw_parts::<'_, u8>) {
4546
debug data => _5;
46-
debug len => _6;
47-
let _7: *const [u8];
47+
debug len => _7;
48+
let _8: *const [u8];
4849
scope 13 (inlined core::ub_checks::check_language_ub) {
4950
scope 14 (inlined core::ub_checks::check_language_ub::runtime) {
5051
}
@@ -55,10 +56,10 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
5556
}
5657
scope 17 (inlined slice_from_raw_parts::<u8>) {
5758
debug data => _5;
58-
debug len => _6;
59+
debug len => _7;
5960
scope 18 (inlined std::ptr::from_raw_parts::<[u8], u8>) {
6061
debug data_pointer => _5;
61-
debug metadata => _6;
62+
debug metadata => _7;
6263
}
6364
}
6465
}
@@ -70,17 +71,22 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
7071
_2 = &((*_1).0: alloc::raw_vec::RawVec<u8>);
7172
StorageLive(_3);
7273
_3 = &(((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner);
74+
StorageLive(_6);
75+
StorageLive(_4);
7376
_4 = copy (((((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner).0: std::ptr::Unique<u8>).0: std::ptr::NonNull<u8>);
7477
_5 = copy (_4.0: *const u8);
78+
_6 = NonNull::<u8> { pointer: copy _5 };
79+
StorageDead(_4);
80+
StorageDead(_6);
7581
StorageDead(_3);
7682
StorageDead(_2);
77-
StorageLive(_6);
78-
_6 = copy ((*_1).1: usize);
7983
StorageLive(_7);
80-
_7 = *const [u8] from (copy _5, copy _6);
81-
_0 = &(*_7);
84+
_7 = copy ((*_1).1: usize);
85+
StorageLive(_8);
86+
_8 = *const [u8] from (copy _5, copy _7);
87+
_0 = &(*_8);
88+
StorageDead(_8);
8289
StorageDead(_7);
83-
StorageDead(_6);
8490
return;
8591
}
8692
}

0 commit comments

Comments
 (0)