Skip to content

Commit 6b9676b

Browse files
committed
Auto merge of rust-lang#131612 - tgross35:rollup-zy3yg4p, r=tgross35
Rollup of 7 pull requests Successful merges: - rust-lang#130870 (Add suggestion for removing invalid path sep `::` in fn def) - rust-lang#130954 (Stabilize const `ptr::write*` and `mem::replace`) - rust-lang#131233 (std: fix stdout-before-main) - rust-lang#131590 (yeet some clones) - rust-lang#131596 (mark InterpResult as must_use) - rust-lang#131597 (Take a display name for `tool_check_step!`) - rust-lang#131605 (`LLVMConstInt` only allows integer types) r? `@ghost` `@rustbot` modify labels: rollup
2 parents e200c7f + 1b98ae0 commit 6b9676b

File tree

26 files changed

+175
-58
lines changed

26 files changed

+175
-58
lines changed

compiler/rustc_borrowck/src/nll.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pub(crate) fn compute_regions<'a, 'tcx>(
165165
universe_causes,
166166
type_tests,
167167
liveness_constraints,
168-
elements.clone(),
168+
elements,
169169
);
170170

171171
// If requested: dump NLL facts, and run legacy polonius analysis.

compiler/rustc_builtin_macros/src/deriving/smart_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ pub(crate) fn expand_deriving_smart_ptr(
312312
impl_generics.params.insert(pointee_param_idx + 1, extra_param);
313313

314314
// Add the impl blocks for `DispatchFromDyn` and `CoerceUnsized`.
315-
let gen_args = vec![GenericArg::Type(alt_self_type.clone())];
315+
let gen_args = vec![GenericArg::Type(alt_self_type)];
316316
add_impl_block(impl_generics.clone(), sym::DispatchFromDyn, gen_args.clone());
317317
add_impl_block(impl_generics.clone(), sym::CoerceUnsized, gen_args);
318318
}

compiler/rustc_codegen_llvm/src/common.rs

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_abi as abi;
55
use rustc_abi::Primitive::Pointer;
66
use rustc_abi::{AddressSpace, HasDataLayout};
77
use rustc_ast::Mutability;
8+
use rustc_codegen_ssa::common::TypeKind;
89
use rustc_codegen_ssa::traits::*;
910
use rustc_data_structures::stable_hasher::{Hash128, HashStable, StableHasher};
1011
use rustc_hir::def_id::DefId;
@@ -146,6 +147,10 @@ impl<'ll, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
146147
}
147148

148149
fn const_int(&self, t: &'ll Type, i: i64) -> &'ll Value {
150+
debug_assert!(
151+
self.type_kind(t) == TypeKind::Integer,
152+
"only allows integer types in const_int"
153+
);
149154
unsafe { llvm::LLVMConstInt(t, i as u64, True) }
150155
}
151156

@@ -176,10 +181,18 @@ impl<'ll, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
176181
}
177182

178183
fn const_uint(&self, t: &'ll Type, i: u64) -> &'ll Value {
184+
debug_assert!(
185+
self.type_kind(t) == TypeKind::Integer,
186+
"only allows integer types in const_uint"
187+
);
179188
unsafe { llvm::LLVMConstInt(t, i, False) }
180189
}
181190

182191
fn const_uint_big(&self, t: &'ll Type, u: u128) -> &'ll Value {
192+
debug_assert!(
193+
self.type_kind(t) == TypeKind::Integer,
194+
"only allows integer types in const_uint_big"
195+
);
183196
unsafe {
184197
let words = [u as u64, (u >> 64) as u64];
185198
llvm::LLVMConstIntOfArbitraryPrecision(t, 2, words.as_ptr())

compiler/rustc_middle/src/mir/interpret/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ impl Drop for Guard {
754754
///
755755
/// We also make things panic if this type is ever implicitly dropped.
756756
#[derive(Debug)]
757+
#[must_use]
757758
pub struct InterpResult_<'tcx, T> {
758759
res: Result<T, InterpErrorInfo<'tcx>>,
759760
guard: Guard,

compiler/rustc_parse/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,9 @@ parse_invalid_meta_item = expected unsuffixed literal, found `{$token}`
422422
423423
parse_invalid_offset_of = offset_of expects dot-separated field and variant names
424424
425+
parse_invalid_path_sep_in_fn_definition = invalid path separator in function definition
426+
.suggestion = remove invalid path separator
427+
425428
parse_invalid_unicode_escape = invalid unicode character escape
426429
.label = invalid escape
427430
.help = unicode escape must {$surrogate ->

compiler/rustc_parse/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1755,6 +1755,14 @@ pub(crate) struct MissingFnParams {
17551755
pub span: Span,
17561756
}
17571757

1758+
#[derive(Diagnostic)]
1759+
#[diag(parse_invalid_path_sep_in_fn_definition)]
1760+
pub(crate) struct InvalidPathSepInFnDefinition {
1761+
#[primary_span]
1762+
#[suggestion(code = "", applicability = "machine-applicable", style = "verbose")]
1763+
pub span: Span,
1764+
}
1765+
17581766
#[derive(Diagnostic)]
17591767
#[diag(parse_missing_trait_in_trait_impl)]
17601768
pub(crate) struct MissingTraitInTraitImpl {

compiler/rustc_parse/src/parser/generics.rs

+7
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ impl<'a> Parser<'a> {
269269
/// | ( < lifetimes , typaramseq ( , )? > )
270270
/// where typaramseq = ( typaram ) | ( typaram , typaramseq )
271271
pub(super) fn parse_generics(&mut self) -> PResult<'a, ast::Generics> {
272+
// invalid path separator `::` in function definition
273+
// for example `fn invalid_path_separator::<T>() {}`
274+
if self.eat_noexpect(&token::PathSep) {
275+
self.dcx()
276+
.emit_err(errors::InvalidPathSepInFnDefinition { span: self.prev_token.span });
277+
}
278+
272279
let span_lo = self.token.span;
273280
let (params, span) = if self.eat_lt() {
274281
let params = self.parse_generic_params()?;

library/alloc/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@
173173
#![feature(allow_internal_unstable)]
174174
#![feature(cfg_sanitize)]
175175
#![feature(const_precise_live_drops)]
176-
#![feature(const_ptr_write)]
177176
#![feature(const_try)]
178177
#![feature(decl_macro)]
179178
#![feature(dropck_eyepatch)]

library/alloc/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#![feature(const_cow_is_borrowed)]
88
#![feature(const_heap)]
99
#![cfg_attr(bootstrap, feature(const_mut_refs))]
10-
#![feature(const_ptr_write)]
1110
#![feature(const_try)]
1211
#![feature(core_intrinsics)]
1312
#![feature(extract_if)]

library/core/src/intrinsics.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ extern "rust-intrinsic" {
10841084
/// it does not require an `unsafe` block.
10851085
/// Therefore, implementations must not require the user to uphold
10861086
/// any safety invariants.
1087-
#[rustc_const_unstable(feature = "const_intrinsic_forget", issue = "none")]
1087+
#[rustc_const_stable(feature = "const_intrinsic_forget", since = "CURRENT_RUSTC_VERSION")]
10881088
#[rustc_safe_intrinsic]
10891089
#[rustc_nounwind]
10901090
pub fn forget<T: ?Sized>(_: T);
@@ -2688,7 +2688,7 @@ extern "rust-intrinsic" {
26882688
/// This intrinsic can *only* be called where the pointer is a local without
26892689
/// projections (`write_via_move(ptr, x)`, not `write_via_move(*ptr, x)`) so
26902690
/// that it trivially obeys runtime-MIR rules about derefs in operands.
2691-
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
2691+
#[rustc_const_stable(feature = "const_ptr_write", since = "CURRENT_RUSTC_VERSION")]
26922692
#[rustc_nounwind]
26932693
pub fn write_via_move<T>(ptr: *mut T, value: T);
26942694

@@ -3525,13 +3525,13 @@ pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
35253525
#[doc(alias = "memset")]
35263526
#[stable(feature = "rust1", since = "1.0.0")]
35273527
#[rustc_allowed_through_unstable_modules]
3528-
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
3528+
#[rustc_const_stable(feature = "const_ptr_write", since = "CURRENT_RUSTC_VERSION")]
35293529
#[inline(always)]
35303530
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
35313531
#[rustc_diagnostic_item = "ptr_write_bytes"]
35323532
pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
35333533
extern "rust-intrinsic" {
3534-
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
3534+
#[rustc_const_stable(feature = "const_ptr_write", since = "CURRENT_RUSTC_VERSION")]
35353535
#[rustc_nounwind]
35363536
fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
35373537
}

library/core/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@
124124
#![feature(const_hash)]
125125
#![feature(const_heap)]
126126
#![feature(const_index_range_slice_index)]
127-
#![feature(const_intrinsic_forget)]
128127
#![feature(const_ipv4)]
129128
#![feature(const_ipv6)]
130129
#![feature(const_likely)]
@@ -138,9 +137,7 @@
138137
#![feature(const_pointer_is_aligned)]
139138
#![feature(const_ptr_is_null)]
140139
#![feature(const_ptr_sub_ptr)]
141-
#![feature(const_ptr_write)]
142140
#![feature(const_raw_ptr_comparison)]
143-
#![feature(const_replace)]
144141
#![feature(const_size_of_val)]
145142
#![feature(const_size_of_val_raw)]
146143
#![feature(const_strict_overflow_ops)]

library/core/src/mem/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,8 @@ pub fn take<T: Default>(dest: &mut T) -> T {
857857
#[inline]
858858
#[stable(feature = "rust1", since = "1.0.0")]
859859
#[must_use = "if you don't need the old value, you can just assign the new value directly"]
860-
#[rustc_const_unstable(feature = "const_replace", issue = "83164")]
860+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
861+
#[rustc_const_stable(feature = "const_replace", since = "CURRENT_RUSTC_VERSION")]
861862
#[cfg_attr(not(test), rustc_diagnostic_item = "mem_replace")]
862863
pub const fn replace<T>(dest: &mut T, src: T) -> T {
863864
// It may be tempting to use `swap` to avoid `unsafe` here. Don't!

library/core/src/ptr/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,8 @@ const unsafe fn swap_nonoverlapping_simple_untyped<T>(x: *mut T, y: *mut T, coun
12631263
/// ```
12641264
#[inline]
12651265
#[stable(feature = "rust1", since = "1.0.0")]
1266-
#[rustc_const_unstable(feature = "const_replace", issue = "83164")]
1266+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_mut_refs))]
1267+
#[rustc_const_stable(feature = "const_replace", since = "CURRENT_RUSTC_VERSION")]
12671268
#[rustc_diagnostic_item = "ptr_replace"]
12681269
pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
12691270
// SAFETY: the caller must guarantee that `dst` is valid to be
@@ -1611,7 +1612,7 @@ pub const unsafe fn read_unaligned<T>(src: *const T) -> T {
16111612
/// ```
16121613
#[inline]
16131614
#[stable(feature = "rust1", since = "1.0.0")]
1614-
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
1615+
#[rustc_const_stable(feature = "const_ptr_write", since = "CURRENT_RUSTC_VERSION")]
16151616
#[rustc_diagnostic_item = "ptr_write"]
16161617
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
16171618
pub const unsafe fn write<T>(dst: *mut T, src: T) {
@@ -1719,7 +1720,8 @@ pub const unsafe fn write<T>(dst: *mut T, src: T) {
17191720
/// ```
17201721
#[inline]
17211722
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
1722-
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
1723+
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_refs_to_cell))]
1724+
#[rustc_const_stable(feature = "const_ptr_write", since = "CURRENT_RUSTC_VERSION")]
17231725
#[rustc_diagnostic_item = "ptr_write_unaligned"]
17241726
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
17251727
pub const unsafe fn write_unaligned<T>(dst: *mut T, src: T) {

library/core/src/ptr/mut_ptr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1449,7 +1449,7 @@ impl<T: ?Sized> *mut T {
14491449
///
14501450
/// [`ptr::write`]: crate::ptr::write()
14511451
#[stable(feature = "pointer_methods", since = "1.26.0")]
1452-
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
1452+
#[rustc_const_stable(feature = "const_ptr_write", since = "CURRENT_RUSTC_VERSION")]
14531453
#[inline(always)]
14541454
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
14551455
pub const unsafe fn write(self, val: T)
@@ -1468,7 +1468,7 @@ impl<T: ?Sized> *mut T {
14681468
/// [`ptr::write_bytes`]: crate::ptr::write_bytes()
14691469
#[doc(alias = "memset")]
14701470
#[stable(feature = "pointer_methods", since = "1.26.0")]
1471-
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
1471+
#[rustc_const_stable(feature = "const_ptr_write", since = "CURRENT_RUSTC_VERSION")]
14721472
#[inline(always)]
14731473
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
14741474
pub const unsafe fn write_bytes(self, val: u8, count: usize)
@@ -1509,7 +1509,7 @@ impl<T: ?Sized> *mut T {
15091509
///
15101510
/// [`ptr::write_unaligned`]: crate::ptr::write_unaligned()
15111511
#[stable(feature = "pointer_methods", since = "1.26.0")]
1512-
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
1512+
#[rustc_const_stable(feature = "const_ptr_write", since = "CURRENT_RUSTC_VERSION")]
15131513
#[inline(always)]
15141514
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
15151515
pub const unsafe fn write_unaligned(self, val: T)

library/core/src/ptr/non_null.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ impl<T: ?Sized> NonNull<T> {
10131013
#[inline(always)]
10141014
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
10151015
#[stable(feature = "non_null_convenience", since = "1.80.0")]
1016-
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
1016+
#[rustc_const_stable(feature = "const_ptr_write", since = "CURRENT_RUSTC_VERSION")]
10171017
pub const unsafe fn write(self, val: T)
10181018
where
10191019
T: Sized,
@@ -1032,7 +1032,7 @@ impl<T: ?Sized> NonNull<T> {
10321032
#[doc(alias = "memset")]
10331033
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
10341034
#[stable(feature = "non_null_convenience", since = "1.80.0")]
1035-
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
1035+
#[rustc_const_stable(feature = "const_ptr_write", since = "CURRENT_RUSTC_VERSION")]
10361036
pub const unsafe fn write_bytes(self, val: u8, count: usize)
10371037
where
10381038
T: Sized,
@@ -1073,7 +1073,7 @@ impl<T: ?Sized> NonNull<T> {
10731073
#[inline(always)]
10741074
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
10751075
#[stable(feature = "non_null_convenience", since = "1.80.0")]
1076-
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
1076+
#[rustc_const_stable(feature = "const_ptr_write", since = "CURRENT_RUSTC_VERSION")]
10771077
pub const unsafe fn write_unaligned(self, val: T)
10781078
where
10791079
T: Sized,

library/core/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#![feature(const_option_ext)]
2929
#![feature(const_pin)]
3030
#![feature(const_pointer_is_aligned)]
31-
#![feature(const_ptr_write)]
3231
#![feature(const_three_way_compare)]
3332
#![feature(const_trait_impl)]
3433
#![feature(core_intrinsics)]

library/std/src/rt.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,24 @@ unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
102102
sys::init(argc, argv, sigpipe)
103103
};
104104

105-
// Set up the current thread to give it the right name.
106-
let thread = Thread::new_main();
107-
thread::set_current(thread);
105+
// Set up the current thread handle to give it the right name.
106+
//
107+
// When code running before main uses `ReentrantLock` (for example by
108+
// using `println!`), the thread ID can become initialized before we
109+
// create this handle. Since `set_current` fails when the ID of the
110+
// handle does not match the current ID, we should attempt to use the
111+
// current thread ID here instead of unconditionally creating a new
112+
// one. Also see #130210.
113+
let thread = Thread::new_main(thread::current_id());
114+
if let Err(_thread) = thread::set_current(thread) {
115+
// `thread::current` will create a new handle if none has been set yet.
116+
// Thus, if someone uses it before main, this call will fail. That's a
117+
// bad idea though, as we then cannot set the main thread name here.
118+
//
119+
// FIXME: detect the main thread in `thread::current` and use the
120+
// correct name there.
121+
rtabort!("code running before main must not use thread::current");
122+
}
108123
}
109124

110125
/// Clean up the thread-local runtime state. This *should* be run after all other

library/std/src/thread/current.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,24 @@ mod id {
110110
}
111111
}
112112

113-
/// Sets the thread handle for the current thread.
114-
///
115-
/// Aborts if the handle or the ID has been set already.
116-
pub(crate) fn set_current(thread: Thread) {
117-
if CURRENT.get() != NONE || id::get().is_some() {
118-
// Using `panic` here can add ~3kB to the binary size. We have complete
119-
// control over where this is called, so just abort if there is a bug.
120-
rtabort!("thread::set_current should only be called once per thread");
113+
/// Tries to set the thread handle for the current thread. Fails if a handle was
114+
/// already set or if the thread ID of `thread` would change an already-set ID.
115+
pub(crate) fn set_current(thread: Thread) -> Result<(), Thread> {
116+
if CURRENT.get() != NONE {
117+
return Err(thread);
121118
}
122119

123-
id::set(thread.id());
120+
match id::get() {
121+
Some(id) if id == thread.id() => {}
122+
None => id::set(thread.id()),
123+
_ => return Err(thread),
124+
}
124125

125126
// Make sure that `crate::rt::thread_cleanup` will be run, which will
126127
// call `drop_current`.
127128
crate::sys::thread_local::guard::enable();
128129
CURRENT.set(thread.into_raw().cast_mut());
130+
Ok(())
129131
}
130132

131133
/// Gets the id of the thread that invokes it.

library/std/src/thread/mod.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,14 @@ impl Builder {
519519

520520
let f = MaybeDangling::new(f);
521521
let main = move || {
522-
// Immediately store the thread handle to avoid setting it or its ID
523-
// twice, which would cause an abort.
524-
set_current(their_thread.clone());
522+
if let Err(_thread) = set_current(their_thread.clone()) {
523+
// Both the current thread handle and the ID should not be
524+
// initialized yet. Since only the C runtime and some of our
525+
// platform code run before this, this point shouldn't be
526+
// reachable. Use an abort to save binary size (see #123356).
527+
rtabort!("something here is badly broken!");
528+
}
529+
525530
if let Some(name) = their_thread.cname() {
526531
imp::Thread::set_name(name);
527532
}
@@ -1159,9 +1164,6 @@ pub fn park_timeout(dur: Duration) {
11591164
pub struct ThreadId(NonZero<u64>);
11601165

11611166
impl ThreadId {
1162-
// DO NOT rely on this value.
1163-
const MAIN_THREAD: ThreadId = ThreadId(unsafe { NonZero::new_unchecked(1) });
1164-
11651167
// Generate a new unique thread ID.
11661168
pub(crate) fn new() -> ThreadId {
11671169
#[cold]
@@ -1173,7 +1175,7 @@ impl ThreadId {
11731175
if #[cfg(target_has_atomic = "64")] {
11741176
use crate::sync::atomic::AtomicU64;
11751177

1176-
static COUNTER: AtomicU64 = AtomicU64::new(1);
1178+
static COUNTER: AtomicU64 = AtomicU64::new(0);
11771179

11781180
let mut last = COUNTER.load(Ordering::Relaxed);
11791181
loop {
@@ -1189,7 +1191,7 @@ impl ThreadId {
11891191
} else {
11901192
use crate::sync::{Mutex, PoisonError};
11911193

1192-
static COUNTER: Mutex<u64> = Mutex::new(1);
1194+
static COUNTER: Mutex<u64> = Mutex::new(0);
11931195

11941196
let mut counter = COUNTER.lock().unwrap_or_else(PoisonError::into_inner);
11951197
let Some(id) = counter.checked_add(1) else {
@@ -1326,9 +1328,9 @@ impl Thread {
13261328
Self::new_inner(id, ThreadName::Unnamed)
13271329
}
13281330

1329-
// Used in runtime to construct main thread
1330-
pub(crate) fn new_main() -> Thread {
1331-
Self::new_inner(ThreadId::MAIN_THREAD, ThreadName::Main)
1331+
/// Constructs the thread handle for the main thread.
1332+
pub(crate) fn new_main(id: ThreadId) -> Thread {
1333+
Self::new_inner(id, ThreadName::Main)
13321334
}
13331335

13341336
fn new_inner(id: ThreadId, name: ThreadName) -> Thread {

0 commit comments

Comments
 (0)