Skip to content

Commit 5119208

Browse files
committed
Auto merge of #121904 - matthiaskrgr:rollup-qcq0d3h, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #121666 (Use the OS thread name by default if `THREAD_INFO` has not been initialized) - #121758 (Move thread local implementation to `sys`) - #121759 (attempt to further clarify addr_of docs) - #121855 (Correctly generate item info of trait items) - #121888 (style library/core/src/error.rs) - #121892 (The ordinary lowering of `thir::ExprKind::Let` is unreachable) - #121895 (avoid collecting into vecs in some places) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 5257aee + fcf5805 commit 5119208

File tree

33 files changed

+269
-107
lines changed

33 files changed

+269
-107
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+16-20
Original file line numberDiff line numberDiff line change
@@ -357,31 +357,27 @@ fn add_unused_functions(cx: &CodegenCx<'_, '_>) {
357357

358358
let ignore_unused_generics = tcx.sess.instrument_coverage_except_unused_generics();
359359

360-
let eligible_def_ids: Vec<DefId> = tcx
361-
.mir_keys(())
362-
.iter()
363-
.filter_map(|local_def_id| {
364-
let def_id = local_def_id.to_def_id();
365-
let kind = tcx.def_kind(def_id);
366-
// `mir_keys` will give us `DefId`s for all kinds of things, not
367-
// just "functions", like consts, statics, etc. Filter those out.
368-
// If `ignore_unused_generics` was specified, filter out any
369-
// generic functions from consideration as well.
370-
if !matches!(kind, DefKind::Fn | DefKind::AssocFn | DefKind::Closure) {
371-
return None;
372-
}
373-
if ignore_unused_generics && tcx.generics_of(def_id).requires_monomorphization(tcx) {
374-
return None;
375-
}
376-
Some(local_def_id.to_def_id())
377-
})
378-
.collect();
360+
let eligible_def_ids = tcx.mir_keys(()).iter().filter_map(|local_def_id| {
361+
let def_id = local_def_id.to_def_id();
362+
let kind = tcx.def_kind(def_id);
363+
// `mir_keys` will give us `DefId`s for all kinds of things, not
364+
// just "functions", like consts, statics, etc. Filter those out.
365+
// If `ignore_unused_generics` was specified, filter out any
366+
// generic functions from consideration as well.
367+
if !matches!(kind, DefKind::Fn | DefKind::AssocFn | DefKind::Closure) {
368+
return None;
369+
}
370+
if ignore_unused_generics && tcx.generics_of(def_id).requires_monomorphization(tcx) {
371+
return None;
372+
}
373+
Some(local_def_id.to_def_id())
374+
});
379375

380376
let codegenned_def_ids = codegenned_and_inlined_items(tcx);
381377

382378
// For each `DefId` that should have coverage instrumentation but wasn't
383379
// codegenned, add it to the function coverage map as an unused function.
384-
for def_id in eligible_def_ids.into_iter().filter(|id| !codegenned_def_ids.contains(id)) {
380+
for def_id in eligible_def_ids.filter(|id| !codegenned_def_ids.contains(id)) {
385381
// Skip any function that didn't have coverage data added to it by the
386382
// coverage instrumentor.
387383
let body = tcx.instance_mir(ty::InstanceDef::Item(def_id));

compiler/rustc_hir_analysis/src/astconv/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -860,10 +860,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
860860
traits with associated type `{name}`, you could use the \
861861
fully-qualified path",
862862
),
863-
traits
864-
.iter()
865-
.map(|trait_str| format!("<Example as {trait_str}>::{name}"))
866-
.collect::<Vec<_>>(),
863+
traits.iter().map(|trait_str| format!("<Example as {trait_str}>::{name}")),
867864
Applicability::HasPlaceholders,
868865
);
869866
}

compiler/rustc_hir_typeck/src/expr.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -2156,10 +2156,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21562156
err.span_suggestions(
21572157
span.shrink_to_hi().with_hi(expr_span.hi()),
21582158
"you might have meant to use an associated function to build this type",
2159-
items
2160-
.iter()
2161-
.map(|(_, name, args)| suggestion(name, *args))
2162-
.collect::<Vec<String>>(),
2159+
items.iter().map(|(_, name, args)| suggestion(name, *args)),
21632160
Applicability::MaybeIncorrect,
21642161
);
21652162
}

compiler/rustc_mir_build/src/build/expr/into.rs

+6-32
Original file line numberDiff line numberDiff line change
@@ -109,38 +109,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
109109
this.cfg.goto(else_blk, source_info, join_block);
110110
join_block.unit()
111111
}
112-
ExprKind::Let { expr, ref pat } => {
113-
let scope = this.local_scope();
114-
let (true_block, false_block) = this.in_if_then_scope(scope, expr_span, |this| {
115-
this.lower_let_expr(block, expr, pat, scope, None, expr_span, true)
116-
});
117-
118-
this.cfg.push_assign_constant(
119-
true_block,
120-
source_info,
121-
destination,
122-
ConstOperand {
123-
span: expr_span,
124-
user_ty: None,
125-
const_: Const::from_bool(this.tcx, true),
126-
},
127-
);
128-
129-
this.cfg.push_assign_constant(
130-
false_block,
131-
source_info,
132-
destination,
133-
ConstOperand {
134-
span: expr_span,
135-
user_ty: None,
136-
const_: Const::from_bool(this.tcx, false),
137-
},
138-
);
139-
140-
let join_block = this.cfg.start_new_block();
141-
this.cfg.goto(true_block, source_info, join_block);
142-
this.cfg.goto(false_block, source_info, join_block);
143-
join_block.unit()
112+
ExprKind::Let { .. } => {
113+
// After desugaring, `let` expressions should only appear inside `if`
114+
// expressions or `match` guards, possibly nested within a let-chain.
115+
// In both cases they are specifically handled by the lowerings of
116+
// those expressions, so this case is currently unreachable.
117+
span_bug!(expr_span, "unexpected let expression outside of if or match-guard");
144118
}
145119
ExprKind::NeverToAny { source } => {
146120
let source_expr = &this.thir[source];

compiler/rustc_resolve/src/late/diagnostics.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1884,10 +1884,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
18841884
err.span_suggestions_with_style(
18851885
path_span.shrink_to_hi().with_hi(call_span.hi()),
18861886
"you might have meant to use an associated function to build this type",
1887-
items
1888-
.iter()
1889-
.map(|(_, name, len)| suggestion(name, *len))
1890-
.collect::<Vec<String>>(),
1887+
items.iter().map(|(_, name, len)| suggestion(name, *len)),
18911888
Applicability::MaybeIncorrect,
18921889
SuggestionStyle::ShowAlways,
18931890
);

compiler/rustc_trait_selection/src/solve/fulfill.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'tcx> ObligationStorage<'tcx> {
5757

5858
fn take_pending(&mut self) -> Vec<PredicateObligation<'tcx>> {
5959
let mut obligations = mem::take(&mut self.pending);
60-
obligations.extend(self.overflowed.drain(..));
60+
obligations.append(&mut self.overflowed);
6161
obligations
6262
}
6363

library/core/src/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ pub trait Error: Debug + Display {
183183
#[allow(unused_variables)]
184184
fn provide<'a>(&'a self, request: &mut Request<'a>) {}
185185
}
186+
186187
mod private {
187188
// This is a hack to prevent `type_id` from being overridden by `Error`
188189
// implementations, since that can enable unsound downcasting.

library/core/src/ptr/mod.rs

+63-15
Original file line numberDiff line numberDiff line change
@@ -2071,11 +2071,16 @@ impl<F: FnPtr> fmt::Debug for F {
20712071
/// as all other references. This macro can create a raw pointer *without* creating
20722072
/// a reference first.
20732073
///
2074-
/// The `expr` in `addr_of!(expr)` is evaluated as a place expression, but never loads
2075-
/// from the place or requires the place to be dereferenceable. This means that
2076-
/// `addr_of!(*ptr)` is defined behavior even if `ptr` is null, dangling, or misaligned.
2077-
/// Note however that `addr_of!((*ptr).field)` still requires the projection to
2078-
/// `field` to be in-bounds, using the same rules as [`offset`].
2074+
/// See [`addr_of_mut`] for how to create a pointer to uninitialized data.
2075+
/// Doing that with `addr_of` would not make much sense since one could only
2076+
/// read the data, and that would be Undefined Behavior.
2077+
///
2078+
/// # Safety
2079+
///
2080+
/// The `expr` in `addr_of!(expr)` is evaluated as a place expression, but never loads from the
2081+
/// place or requires the place to be dereferenceable. This means that `addr_of!((*ptr).field)`
2082+
/// still requires the projection to `field` to be in-bounds, using the same rules as [`offset`].
2083+
/// However, `addr_of!(*ptr)` is defined behavior even if `ptr` is null, dangling, or misaligned.
20792084
///
20802085
/// Note that `Deref`/`Index` coercions (and their mutable counterparts) are applied inside
20812086
/// `addr_of!` like everywhere else, in which case a reference is created to call `Deref::deref` or
@@ -2086,6 +2091,8 @@ impl<F: FnPtr> fmt::Debug for F {
20862091
///
20872092
/// # Example
20882093
///
2094+
/// **Correct usage: Creating a pointer to unaligned data**
2095+
///
20892096
/// ```
20902097
/// use std::ptr;
20912098
///
@@ -2101,9 +2108,27 @@ impl<F: FnPtr> fmt::Debug for F {
21012108
/// assert_eq!(unsafe { raw_f2.read_unaligned() }, 2);
21022109
/// ```
21032110
///
2104-
/// See [`addr_of_mut`] for how to create a pointer to uninitialized data.
2105-
/// Doing that with `addr_of` would not make much sense since one could only
2106-
/// read the data, and that would be Undefined Behavior.
2111+
/// **Incorrect usage: Out-of-bounds fields projection**
2112+
///
2113+
/// ```rust,no_run
2114+
/// use std::ptr;
2115+
///
2116+
/// #[repr(C)]
2117+
/// struct MyStruct {
2118+
/// field1: i32,
2119+
/// field2: i32,
2120+
/// }
2121+
///
2122+
/// let ptr: *const MyStruct = ptr::null();
2123+
/// let fieldptr = unsafe { ptr::addr_of!((*ptr).field2) }; // Undefined Behavior ⚠️
2124+
/// ```
2125+
///
2126+
/// The field projection `.field2` would offset the pointer by 4 bytes,
2127+
/// but the pointer is not in-bounds of an allocation for 4 bytes,
2128+
/// so this offset is Undefined Behavior.
2129+
/// See the [`offset`] docs for a full list of requirements for inbounds pointer arithmetic; the
2130+
/// same requirements apply to field projections, even inside `addr_of!`. (In particular, it makes
2131+
/// no difference whether the pointer is null or dangling.)
21072132
#[stable(feature = "raw_ref_macros", since = "1.51.0")]
21082133
#[rustc_macro_transparency = "semitransparent"]
21092134
#[allow_internal_unstable(raw_ref_op)]
@@ -2120,11 +2145,12 @@ pub macro addr_of($place:expr) {
21202145
/// as all other references. This macro can create a raw pointer *without* creating
21212146
/// a reference first.
21222147
///
2123-
/// The `expr` in `addr_of_mut!(expr)` is evaluated as a place expression, but never loads
2124-
/// from the place or requires the place to be dereferenceable. This means that
2125-
/// `addr_of_mut!(*ptr)` is defined behavior even if `ptr` is null, dangling, or misaligned.
2126-
/// Note however that `addr_of_mut!((*ptr).field)` still requires the projection to
2127-
/// `field` to be in-bounds, using the same rules as [`offset`].
2148+
/// # Safety
2149+
///
2150+
/// The `expr` in `addr_of_mut!(expr)` is evaluated as a place expression, but never loads from the
2151+
/// place or requires the place to be dereferenceable. This means that `addr_of_mut!((*ptr).field)`
2152+
/// still requires the projection to `field` to be in-bounds, using the same rules as [`offset`].
2153+
/// However, `addr_of_mut!(*ptr)` is defined behavior even if `ptr` is null, dangling, or misaligned.
21282154
///
21292155
/// Note that `Deref`/`Index` coercions (and their mutable counterparts) are applied inside
21302156
/// `addr_of_mut!` like everywhere else, in which case a reference is created to call `Deref::deref`
@@ -2135,7 +2161,7 @@ pub macro addr_of($place:expr) {
21352161
///
21362162
/// # Examples
21372163
///
2138-
/// **Creating a pointer to unaligned data:**
2164+
/// **Correct usage: Creating a pointer to unaligned data**
21392165
///
21402166
/// ```
21412167
/// use std::ptr;
@@ -2153,7 +2179,7 @@ pub macro addr_of($place:expr) {
21532179
/// assert_eq!({packed.f2}, 42); // `{...}` forces copying the field instead of creating a reference.
21542180
/// ```
21552181
///
2156-
/// **Creating a pointer to uninitialized data:**
2182+
/// **Correct usage: Creating a pointer to uninitialized data**
21572183
///
21582184
/// ```rust
21592185
/// use std::{ptr, mem::MaybeUninit};
@@ -2169,6 +2195,28 @@ pub macro addr_of($place:expr) {
21692195
/// unsafe { f1_ptr.write(true); }
21702196
/// let init = unsafe { uninit.assume_init() };
21712197
/// ```
2198+
///
2199+
/// **Incorrect usage: Out-of-bounds fields projection**
2200+
///
2201+
/// ```rust,no_run
2202+
/// use std::ptr;
2203+
///
2204+
/// #[repr(C)]
2205+
/// struct MyStruct {
2206+
/// field1: i32,
2207+
/// field2: i32,
2208+
/// }
2209+
///
2210+
/// let ptr: *mut MyStruct = ptr::null_mut();
2211+
/// let fieldptr = unsafe { ptr::addr_of_mut!((*ptr).field2) }; // Undefined Behavior ⚠️
2212+
/// ```
2213+
///
2214+
/// The field projection `.field2` would offset the pointer by 4 bytes,
2215+
/// but the pointer is not in-bounds of an allocation for 4 bytes,
2216+
/// so this offset is Undefined Behavior.
2217+
/// See the [`offset`] docs for a full list of requirements for inbounds pointer arithmetic; the
2218+
/// same requirements apply to field projections, even inside `addr_of_mut!`. (In particular, it
2219+
/// makes no difference whether the pointer is null or dangling.)
21722220
#[stable(feature = "raw_ref_macros", since = "1.51.0")]
21732221
#[rustc_macro_transparency = "semitransparent"]
21742222
#[allow_internal_unstable(raw_ref_op)]

library/std/src/sys/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ pub mod cmath;
99
pub mod locks;
1010
pub mod os_str;
1111
pub mod path;
12+
#[allow(dead_code)]
13+
#[allow(unused_imports)]
14+
pub mod thread_local;
1215

1316
// FIXME(117276): remove this, move feature implementations into individual
1417
// submodules.

library/std/src/sys/pal/common/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
pub mod alloc;
1414
pub mod small_c_string;
15-
#[allow(unused_imports)]
16-
pub mod thread_local;
1715

1816
#[cfg(test)]
1917
mod tests;

library/std/src/sys/pal/hermit/thread.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use super::abi;
44
use super::thread_local_dtor::run_dtors;
5-
use crate::ffi::CStr;
5+
use crate::ffi::{CStr, CString};
66
use crate::io;
77
use crate::mem;
88
use crate::num::NonZero;
@@ -71,6 +71,10 @@ impl Thread {
7171
// nope
7272
}
7373

74+
pub fn get_name() -> Option<CString> {
75+
None
76+
}
77+
7478
#[inline]
7579
pub fn sleep(dur: Duration) {
7680
unsafe {

library/std/src/sys/pal/itron/thread.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use super::{
88
};
99
use crate::{
1010
cell::UnsafeCell,
11-
ffi::CStr,
11+
ffi::{CStr, CString},
1212
hint, io,
1313
mem::ManuallyDrop,
1414
num::NonZero,
@@ -204,6 +204,10 @@ impl Thread {
204204
// nope
205205
}
206206

207+
pub fn get_name() -> Option<CString> {
208+
None
209+
}
210+
207211
pub fn sleep(dur: Duration) {
208212
for timeout in dur2reltims(dur) {
209213
expect_success(unsafe { abi::dly_tsk(timeout) }, &"dly_tsk");

library/std/src/sys/pal/sgx/thread.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![cfg_attr(test, allow(dead_code))] // why is this necessary?
22
use super::unsupported;
3-
use crate::ffi::CStr;
3+
use crate::ffi::{CStr, CString};
44
use crate::io;
55
use crate::num::NonZero;
66
use crate::time::Duration;
@@ -133,6 +133,10 @@ impl Thread {
133133
// which succeeds as-is with the SGX target.
134134
}
135135

136+
pub fn get_name() -> Option<CString> {
137+
None
138+
}
139+
136140
pub fn sleep(dur: Duration) {
137141
usercalls::wait_timeout(0, dur, || true);
138142
}

library/std/src/sys/pal/teeos/thread.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::convert::TryInto;
22

33
use crate::cmp;
4-
use crate::ffi::CStr;
4+
use crate::ffi::{CStr, CString};
55
use crate::io;
66
use crate::mem;
77
use crate::num::NonZero;
@@ -101,6 +101,10 @@ impl Thread {
101101
// contact the teeos rustzone team.
102102
}
103103

104+
pub fn get_name() -> Option<CString> {
105+
None
106+
}
107+
104108
/// only main thread could wait for sometime in teeos
105109
pub fn sleep(dur: Duration) {
106110
let sleep_millis = dur.as_millis();

library/std/src/sys/pal/uefi/thread.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::unsupported;
2-
use crate::ffi::CStr;
2+
use crate::ffi::{CStr, CString};
33
use crate::io;
44
use crate::num::NonZero;
55
use crate::ptr::NonNull;
@@ -23,6 +23,10 @@ impl Thread {
2323
// nope
2424
}
2525

26+
pub fn get_name() -> Option<CString> {
27+
None
28+
}
29+
2630
pub fn sleep(dur: Duration) {
2731
let boot_services: NonNull<r_efi::efi::BootServices> =
2832
crate::os::uefi::env::boot_services().expect("can't sleep").cast();

0 commit comments

Comments
 (0)