Skip to content

Commit

Permalink
Auto merge of rust-lang#132468 - cuviper:beta-next, r=cuviper
Browse files Browse the repository at this point in the history
[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
  • Loading branch information
bors committed Nov 2, 2024
2 parents f41c7ed + 0f95b22 commit 67512de
Show file tree
Hide file tree
Showing 19 changed files with 245 additions and 69 deletions.
4 changes: 3 additions & 1 deletion compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
}
}

if let AggregateTy::Def(_, _) = ty
// unsound: https://github.com/rust-lang/rust/issues/132353
if tcx.sess.opts.unstable_opts.unsound_mir_opts
&& let AggregateTy::Def(_, _) = ty
&& let Some(value) =
self.simplify_aggregate_to_copy(rvalue, location, &fields, variant_index)
{
Expand Down
6 changes: 3 additions & 3 deletions library/Cargo.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
version = 4

[[package]]
name = "addr2line"
Expand Down Expand Up @@ -158,9 +158,9 @@ dependencies = [

[[package]]
name = "libc"
version = "0.2.159"
version = "0.2.161"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5"
checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1"
dependencies = [
"rustc-std-workspace-core",
]
Expand Down
2 changes: 1 addition & 1 deletion library/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ miniz_oxide = { version = "0.7.0", optional = true, default-features = false }
addr2line = { version = "0.22.0", optional = true, default-features = false }

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

Expand Down
31 changes: 15 additions & 16 deletions library/std/src/sys/thread_local/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,21 @@ pub use lazy::Storage as LazyStorage;
#[unstable(feature = "thread_local_internals", issue = "none")]
#[rustc_macro_transparency = "semitransparent"]
pub macro thread_local_inner {
// used to generate the `LocalKey` value for const-initialized thread locals
// NOTE: we cannot import `LocalKey`, `LazyStorage` or `EagerStorage` with a `use` because that
// can shadow user provided type or type alias with a matching name. Please update the shadowing
// test in `tests/thread.rs` if these types are renamed.

// Used to generate the `LocalKey` value for const-initialized thread locals.
(@key $t:ty, const $init:expr) => {{
const __INIT: $t = $init;

unsafe {
use $crate::mem::needs_drop;
use $crate::thread::LocalKey;
use $crate::thread::local_impl::EagerStorage;

LocalKey::new(const {
if needs_drop::<$t>() {
$crate::thread::LocalKey::new(const {
if $crate::mem::needs_drop::<$t>() {
|_| {
#[thread_local]
static VAL: EagerStorage<$t> = EagerStorage::new(__INIT);
static VAL: $crate::thread::local_impl::EagerStorage<$t>
= $crate::thread::local_impl::EagerStorage::new(__INIT);
VAL.get()
}
} else {
Expand All @@ -84,21 +85,19 @@ pub macro thread_local_inner {
}

unsafe {
use $crate::mem::needs_drop;
use $crate::thread::LocalKey;
use $crate::thread::local_impl::LazyStorage;

LocalKey::new(const {
if needs_drop::<$t>() {
$crate::thread::LocalKey::new(const {
if $crate::mem::needs_drop::<$t>() {
|init| {
#[thread_local]
static VAL: LazyStorage<$t, ()> = LazyStorage::new();
static VAL: $crate::thread::local_impl::LazyStorage<$t, ()>
= $crate::thread::local_impl::LazyStorage::new();
VAL.get_or_init(init, __init)
}
} else {
|init| {
#[thread_local]
static VAL: LazyStorage<$t, !> = LazyStorage::new();
static VAL: $crate::thread::local_impl::LazyStorage<$t, !>
= $crate::thread::local_impl::LazyStorage::new();
VAL.get_or_init(init, __init)
}
}
Expand Down
17 changes: 11 additions & 6 deletions library/std/src/sys/thread_local/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,24 @@ pub macro thread_local_inner {
$crate::thread::local_impl::thread_local_inner!(@key $t, { const INIT_EXPR: $t = $init; INIT_EXPR })
},

// used to generate the `LocalKey` value for `thread_local!`
// NOTE: we cannot import `Storage` or `LocalKey` with a `use` because that can shadow user
// provided type or type alias with a matching name. Please update the shadowing test in
// `tests/thread.rs` if these types are renamed.

// used to generate the `LocalKey` value for `thread_local!`.
(@key $t:ty, $init:expr) => {{
#[inline]
fn __init() -> $t { $init }

// NOTE: this cannot import `LocalKey` or `Storage` with a `use` because that can shadow
// user provided type or type alias with a matching name. Please update the shadowing test
// in `tests/thread.rs` if these types are renamed.
unsafe {
use $crate::thread::LocalKey;
use $crate::thread::local_impl::Storage;

// Inlining does not work on windows-gnu due to linking errors around
// dllimports. See https://github.com/rust-lang/rust/issues/109797.
LocalKey::new(#[cfg_attr(windows, inline(never))] |init| {
static VAL: Storage<$t> = Storage::new();
$crate::thread::LocalKey::new(#[cfg_attr(windows, inline(never))] |init| {
static VAL: $crate::thread::local_impl::Storage<$t>
= $crate::thread::local_impl::Storage::new();
VAL.get(init, __init)
})
}
Expand Down
23 changes: 23 additions & 0 deletions library/std/tests/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ fn thread_local_containing_const_statements() {
assert_eq!(REFCELL.take(), 1);
}

#[test]
fn thread_local_hygeiene() {
// Previously `thread_local_inner!` had use imports for `LocalKey`, `Storage`, `EagerStorage`
// and `LazyStorage`. The use imports will shadow a user-provided type or type alias if the
// user-provided type or type alias has the same name. Make sure that this does not happen. See
// <https://github.com/rust-lang/rust/issues/131863>.
//
// NOTE: if the internal implementation details change (i.e. get renamed), this test should be
// updated.

#![allow(dead_code)]
type LocalKey = ();
type Storage = ();
type LazyStorage = ();
type EagerStorage = ();
thread_local! {
static A: LocalKey = const { () };
static B: Storage = const { () };
static C: LazyStorage = const { () };
static D: EagerStorage = const { () };
}
}

#[test]
// Include an ignore list on purpose, so that new platforms don't miss it
#[cfg_attr(
Expand Down
2 changes: 2 additions & 0 deletions tests/codegen/clone_as_copy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//@ revisions: DEBUGINFO NODEBUGINFO
//@ compile-flags: -Zunsound-mir-opts
// FIXME: see <https://github.com/rust-lang/rust/issues/132353>
//@ compile-flags: -O -Cno-prepopulate-passes
//@ [DEBUGINFO] compile-flags: -Cdebuginfo=full

Expand Down
14 changes: 4 additions & 10 deletions tests/codegen/try_question_mark_nop.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//@ compile-flags: -O -Z merge-functions=disabled --edition=2021
//@ only-x86_64
// FIXME: Remove the `min-llvm-version`.
//@ revisions: NINETEEN TWENTY
//@[NINETEEN] min-llvm-version: 19
//@[NINETEEN] ignore-llvm-version: 20-99
//@[TWENTY] min-llvm-version: 20
//@ min-llvm-version: 19

#![crate_type = "lib"]
#![feature(try_blocks)]
Expand All @@ -16,12 +13,9 @@ use std::ptr::NonNull;
#[no_mangle]
pub fn option_nop_match_32(x: Option<u32>) -> Option<u32> {
// CHECK: start:
// NINETEEN-NEXT: [[TRUNC:%.*]] = trunc nuw i32 %0 to i1
// NINETEEN-NEXT: [[FIRST:%.*]] = select i1 [[TRUNC]], i32 %0
// NINETEEN-NEXT: insertvalue { i32, i32 } poison, i32 [[FIRST]], 0
// TWENTY-NEXT: insertvalue { i32, i32 } poison, i32 %0, 0
// CHECK-NEXT: insertvalue { i32, i32 }
// CHECK-NEXT: ret { i32, i32 }
// CHECK-NEXT: [[REG1:%.*]] = insertvalue { i32, i32 } poison, i32 %0, 0
// CHECK-NEXT: [[REG2:%.*]] = insertvalue { i32, i32 } [[REG1]], i32 %1, 1
// CHECK-NEXT: ret { i32, i32 } [[REG2]]
match x {
Some(x) => Some(x),
None => None,
Expand Down
2 changes: 2 additions & 0 deletions tests/mir-opt/gvn_clone.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@ compile-flags: -Zunsound-mir-opts
// FIXME: see <https://github.com/rust-lang/rust/issues/132353>
//@ test-mir-pass: GVN
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-before-inline

Expand Down
6 changes: 3 additions & 3 deletions tests/mir-opt/gvn_clone.{impl#0}-clone.GVN.diff
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
- // MIR for `<impl at $DIR/gvn_clone.rs:12:10: 12:15>::clone` before GVN
+ // MIR for `<impl at $DIR/gvn_clone.rs:12:10: 12:15>::clone` after GVN
- // MIR for `<impl at $DIR/gvn_clone.rs:14:10: 14:15>::clone` before GVN
+ // MIR for `<impl at $DIR/gvn_clone.rs:14:10: 14:15>::clone` after GVN

fn <impl at $DIR/gvn_clone.rs:12:10: 12:15>::clone(_1: &AllCopy) -> AllCopy {
fn <impl at $DIR/gvn_clone.rs:14:10: 14:15>::clone(_1: &AllCopy) -> AllCopy {
debug self => _1;
let mut _0: AllCopy;
let mut _2: i32;
Expand Down
2 changes: 2 additions & 0 deletions tests/mir-opt/gvn_copy_aggregate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@ compile-flags: -Zunsound-mir-opts
// FIXME: see <https://github.com/rust-lang/rust/issues/132353.
//@ test-mir-pass: GVN
//@ compile-flags: -Cpanic=abort

Expand Down
2 changes: 2 additions & 0 deletions tests/mir-opt/pre-codegen/clone_as_copy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@ compile-flags: -Zunsound-mir-opts
// FIXME: see <https://github.com/rust-lang/rust/issues/132353>
//@ compile-flags: -Cdebuginfo=full

// Check if we have transformed the nested clone to the copy in the complete pipeline.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
fn <impl at $DIR/no_inlined_clone.rs:9:10: 9:15>::clone(_1: &Foo) -> Foo {
debug self => _1;
let mut _0: Foo;
let mut _2: i32;

bb0: {
_0 = copy (*_1);
StorageLive(_2);
_2 = copy ((*_1).0: i32);
_0 = Foo { a: move _2 };
StorageDead(_2);
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ fn old(_1: Result<T, E>) -> Result<T, E> {
}

bb1: {
_3 = copy ((_1 as Ok).0: T);
_0 = copy _1;
_3 = move ((_1 as Ok).0: T);
_0 = Result::<T, E>::Ok(copy _3);
goto -> bb3;
}

bb2: {
_4 = copy ((_1 as Err).0: E);
_0 = copy _1;
_4 = move ((_1 as Err).0: E);
_0 = Result::<T, E>::Err(copy _4);
goto -> bb3;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
debug self => _1;
scope 2 (inlined Vec::<u8>::as_slice) {
debug self => _1;
let mut _6: usize;
let mut _7: usize;
scope 3 (inlined Vec::<u8>::as_ptr) {
debug self => _1;
let mut _2: &alloc::raw_vec::RawVec<u8>;
Expand All @@ -16,6 +16,7 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
let mut _3: &alloc::raw_vec::RawVecInner;
scope 5 (inlined alloc::raw_vec::RawVecInner::ptr::<u8>) {
debug self => _3;
let mut _6: std::ptr::NonNull<u8>;
scope 6 (inlined alloc::raw_vec::RawVecInner::non_null::<u8>) {
debug self => _3;
let mut _4: std::ptr::NonNull<u8>;
Expand All @@ -31,20 +32,20 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
}
}
scope 10 (inlined Unique::<u8>::as_non_null_ptr) {
debug ((self: Unique<u8>).0: std::ptr::NonNull<u8>) => _4;
debug ((self: Unique<u8>).0: std::ptr::NonNull<u8>) => _6;
debug ((self: Unique<u8>).1: std::marker::PhantomData<u8>) => const PhantomData::<u8>;
}
}
scope 11 (inlined NonNull::<u8>::as_ptr) {
debug self => _4;
debug self => _6;
}
}
}
}
scope 12 (inlined std::slice::from_raw_parts::<'_, u8>) {
debug data => _5;
debug len => _6;
let _7: *const [u8];
debug len => _7;
let _8: *const [u8];
scope 13 (inlined core::ub_checks::check_language_ub) {
scope 14 (inlined core::ub_checks::check_language_ub::runtime) {
}
Expand All @@ -55,10 +56,10 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
}
scope 17 (inlined slice_from_raw_parts::<u8>) {
debug data => _5;
debug len => _6;
debug len => _7;
scope 18 (inlined std::ptr::from_raw_parts::<[u8], u8>) {
debug data_pointer => _5;
debug metadata => _6;
debug metadata => _7;
}
}
}
Expand All @@ -70,17 +71,22 @@ fn vec_deref_to_slice(_1: &Vec<u8>) -> &[u8] {
_2 = &((*_1).0: alloc::raw_vec::RawVec<u8>);
StorageLive(_3);
_3 = &(((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner);
StorageLive(_6);
StorageLive(_4);
_4 = copy (((((*_1).0: alloc::raw_vec::RawVec<u8>).0: alloc::raw_vec::RawVecInner).0: std::ptr::Unique<u8>).0: std::ptr::NonNull<u8>);
_5 = copy (_4.0: *const u8);
_6 = NonNull::<u8> { pointer: copy _5 };
StorageDead(_4);
StorageDead(_6);
StorageDead(_3);
StorageDead(_2);
StorageLive(_6);
_6 = copy ((*_1).1: usize);
StorageLive(_7);
_7 = *const [u8] from (copy _5, copy _6);
_0 = &(*_7);
_7 = copy ((*_1).1: usize);
StorageLive(_8);
_8 = *const [u8] from (copy _5, copy _7);
_0 = &(*_8);
StorageDead(_8);
StorageDead(_7);
StorageDead(_6);
return;
}
}
Loading

0 comments on commit 67512de

Please sign in to comment.