Skip to content

Commit 88c2f4f

Browse files
committed
Auto merge of rust-lang#123385 - matthiaskrgr:rollup-v69vjbn, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#123198 (Add fn const BuildHasherDefault::new) - rust-lang#123226 (De-LLVM the unchecked shifts [MCP#693]) - rust-lang#123302 (Make sure to insert `Sized` bound first into clauses list) - rust-lang#123348 (rustdoc: add a couple of regression tests) - rust-lang#123362 (Check that nested statics in thread locals are duplicated per thread.) - rust-lang#123368 (CFI: Support non-general coroutines) - rust-lang#123375 (rustdoc: synthetic auto trait impls: accept unresolved region vars for now) - rust-lang#123378 (Update sysinfo to 0.30.8) Failed merges: - rust-lang#123349 (Fix capture analysis for by-move closure bodies) r? `@ghost` `@rustbot` modify labels: rollup
2 parents a77322c + 31900b4 commit 88c2f4f

File tree

52 files changed

+679
-596
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+679
-596
lines changed

Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -5370,9 +5370,9 @@ dependencies = [
53705370

53715371
[[package]]
53725372
name = "sysinfo"
5373-
version = "0.30.7"
5373+
version = "0.30.8"
53745374
source = "registry+https://github.com/rust-lang/crates.io-index"
5375-
checksum = "0c385888ef380a852a16209afc8cfad22795dd8873d69c9a14d2e2088f118d18"
5375+
checksum = "4b1a378e48fb3ce3a5cf04359c456c9c98ff689bcf1c1bc6e6a31f247686f275"
53765376
dependencies = [
53775377
"cfg-if",
53785378
"core-foundation-sys",

compiler/rustc_codegen_ssa/src/base.rs

+31-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::back::write::{
55
compute_per_cgu_lto_type, start_async_codegen, submit_codegened_module_to_llvm,
66
submit_post_lto_module_to_llvm, submit_pre_lto_module_to_llvm, ComputedLtoType, OngoingCodegen,
77
};
8-
use crate::common::{IntPredicate, RealPredicate, TypeKind};
8+
use crate::common::{self, IntPredicate, RealPredicate, TypeKind};
99
use crate::errors;
1010
use crate::meth;
1111
use crate::mir;
@@ -33,7 +33,7 @@ use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, MonoItem};
3333
use rustc_middle::query::Providers;
3434
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout};
3535
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
36-
use rustc_session::config::{self, CrateType, EntryFnType, OutputType};
36+
use rustc_session::config::{self, CrateType, EntryFnType, OptLevel, OutputType};
3737
use rustc_session::Session;
3838
use rustc_span::symbol::sym;
3939
use rustc_span::Symbol;
@@ -300,14 +300,35 @@ pub fn coerce_unsized_into<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
300300
}
301301
}
302302

303-
pub fn cast_shift_expr_rhs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
303+
/// Returns `rhs` sufficiently masked, truncated, and/or extended so that
304+
/// it can be used to shift `lhs`.
305+
///
306+
/// Shifts in MIR are all allowed to have mismatched LHS & RHS types.
307+
/// The shift methods in `BuilderMethods`, however, are fully homogeneous
308+
/// (both parameters and the return type are all the same type).
309+
///
310+
/// If `is_unchecked` is false, this masks the RHS to ensure it stays in-bounds,
311+
/// as the `BuilderMethods` shifts are UB for out-of-bounds shift amounts.
312+
/// For 32- and 64-bit types, this matches the semantics
313+
/// of Java. (See related discussion on #1877 and #10183.)
314+
///
315+
/// If `is_unchecked` is true, this does no masking, and adds sufficient `assume`
316+
/// calls or operation flags to preserve as much freedom to optimize as possible.
317+
pub fn build_shift_expr_rhs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
304318
bx: &mut Bx,
305319
lhs: Bx::Value,
306-
rhs: Bx::Value,
320+
mut rhs: Bx::Value,
321+
is_unchecked: bool,
307322
) -> Bx::Value {
308323
// Shifts may have any size int on the rhs
309324
let mut rhs_llty = bx.cx().val_ty(rhs);
310325
let mut lhs_llty = bx.cx().val_ty(lhs);
326+
327+
let mask = common::shift_mask_val(bx, lhs_llty, rhs_llty, false);
328+
if !is_unchecked {
329+
rhs = bx.and(rhs, mask);
330+
}
331+
311332
if bx.cx().type_kind(rhs_llty) == TypeKind::Vector {
312333
rhs_llty = bx.cx().element_type(rhs_llty)
313334
}
@@ -317,6 +338,12 @@ pub fn cast_shift_expr_rhs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
317338
let rhs_sz = bx.cx().int_width(rhs_llty);
318339
let lhs_sz = bx.cx().int_width(lhs_llty);
319340
if lhs_sz < rhs_sz {
341+
if is_unchecked && bx.sess().opts.optimize != OptLevel::No {
342+
// FIXME: Use `trunc nuw` once that's available
343+
let inrange = bx.icmp(IntPredicate::IntULE, rhs, mask);
344+
bx.assume(inrange);
345+
}
346+
320347
bx.trunc(rhs, lhs_llty)
321348
} else if lhs_sz > rhs_sz {
322349
// We zero-extend even if the RHS is signed. So e.g. `(x: i32) << -1i8` will zero-extend the

compiler/rustc_codegen_ssa/src/common.rs

+1-40
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
use rustc_hir::LangItem;
44
use rustc_middle::mir;
55
use rustc_middle::ty::Instance;
6-
use rustc_middle::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
6+
use rustc_middle::ty::{self, layout::TyAndLayout, TyCtxt};
77
use rustc_span::Span;
88

9-
use crate::base;
109
use crate::traits::*;
1110

1211
#[derive(Copy, Clone)]
@@ -128,44 +127,6 @@ pub fn build_langcall<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
128127
(bx.fn_abi_of_instance(instance, ty::List::empty()), bx.get_fn_addr(instance), instance)
129128
}
130129

131-
// To avoid UB from LLVM, these two functions mask RHS with an
132-
// appropriate mask unconditionally (i.e., the fallback behavior for
133-
// all shifts). For 32- and 64-bit types, this matches the semantics
134-
// of Java. (See related discussion on #1877 and #10183.)
135-
136-
pub fn build_masked_lshift<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
137-
bx: &mut Bx,
138-
lhs: Bx::Value,
139-
rhs: Bx::Value,
140-
) -> Bx::Value {
141-
let rhs = base::cast_shift_expr_rhs(bx, lhs, rhs);
142-
// #1877, #10183: Ensure that input is always valid
143-
let rhs = shift_mask_rhs(bx, rhs);
144-
bx.shl(lhs, rhs)
145-
}
146-
147-
pub fn build_masked_rshift<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
148-
bx: &mut Bx,
149-
lhs_t: Ty<'tcx>,
150-
lhs: Bx::Value,
151-
rhs: Bx::Value,
152-
) -> Bx::Value {
153-
let rhs = base::cast_shift_expr_rhs(bx, lhs, rhs);
154-
// #1877, #10183: Ensure that input is always valid
155-
let rhs = shift_mask_rhs(bx, rhs);
156-
let is_signed = lhs_t.is_signed();
157-
if is_signed { bx.ashr(lhs, rhs) } else { bx.lshr(lhs, rhs) }
158-
}
159-
160-
fn shift_mask_rhs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
161-
bx: &mut Bx,
162-
rhs: Bx::Value,
163-
) -> Bx::Value {
164-
let rhs_llty = bx.val_ty(rhs);
165-
let shift_val = shift_mask_val(bx, rhs_llty, rhs_llty, false);
166-
bx.and(rhs, shift_val)
167-
}
168-
169130
pub fn shift_mask_val<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
170131
bx: &mut Bx,
171132
llty: Bx::Type,

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::place::PlaceRef;
33
use super::{FunctionCx, LocalRef};
44

55
use crate::base;
6-
use crate::common::{self, IntPredicate};
6+
use crate::common::IntPredicate;
77
use crate::traits::*;
88
use crate::MemFlags;
99

@@ -861,14 +861,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
861861
bx.inbounds_gep(llty, lhs, &[rhs])
862862
}
863863
}
864-
mir::BinOp::Shl => common::build_masked_lshift(bx, lhs, rhs),
865-
mir::BinOp::ShlUnchecked => {
866-
let rhs = base::cast_shift_expr_rhs(bx, lhs, rhs);
864+
mir::BinOp::Shl | mir::BinOp::ShlUnchecked => {
865+
let rhs = base::build_shift_expr_rhs(bx, lhs, rhs, op == mir::BinOp::ShlUnchecked);
867866
bx.shl(lhs, rhs)
868867
}
869-
mir::BinOp::Shr => common::build_masked_rshift(bx, input_ty, lhs, rhs),
870-
mir::BinOp::ShrUnchecked => {
871-
let rhs = base::cast_shift_expr_rhs(bx, lhs, rhs);
868+
mir::BinOp::Shr | mir::BinOp::ShrUnchecked => {
869+
let rhs = base::build_shift_expr_rhs(bx, lhs, rhs, op == mir::BinOp::ShrUnchecked);
872870
if is_signed { bx.ashr(lhs, rhs) } else { bx.lshr(lhs, rhs) }
873871
}
874872
mir::BinOp::Ne

compiler/rustc_const_eval/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ const_eval_mut_deref =
222222
223223
const_eval_mutable_ptr_in_final = encountered mutable pointer in final value of {const_eval_intern_kind}
224224
225+
const_eval_nested_static_in_thread_local = #[thread_local] does not support implicit nested statics, please create explicit static items and refer to them instead
225226
const_eval_non_const_fmt_macro_call =
226227
cannot call non-const formatting macro in {const_eval_const_context}s
227228

compiler/rustc_const_eval/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ pub(crate) struct DanglingPtrInFinal {
2525
pub kind: InternKind,
2626
}
2727

28+
#[derive(Diagnostic)]
29+
#[diag(const_eval_nested_static_in_thread_local)]
30+
pub(crate) struct NestedStaticInThreadLocal {
31+
#[primary_span]
32+
pub span: Span,
33+
}
34+
2835
#[derive(LintDiagnostic)]
2936
#[diag(const_eval_mutable_ptr_in_final)]
3037
pub(crate) struct MutablePtrInFinal {

compiler/rustc_const_eval/src/interpret/intern.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_span::sym;
2828

2929
use super::{AllocId, Allocation, InterpCx, MPlaceTy, Machine, MemoryKind, PlaceTy};
3030
use crate::const_eval;
31-
use crate::errors::{DanglingPtrInFinal, MutablePtrInFinal};
31+
use crate::errors::{DanglingPtrInFinal, MutablePtrInFinal, NestedStaticInThreadLocal};
3232

3333
pub trait CompileTimeMachine<'mir, 'tcx: 'mir, T> = Machine<
3434
'mir,
@@ -108,6 +108,10 @@ fn intern_as_new_static<'tcx>(
108108
);
109109
tcx.set_nested_alloc_id_static(alloc_id, feed.def_id());
110110

111+
if tcx.is_thread_local_static(static_id.into()) {
112+
tcx.dcx().emit_err(NestedStaticInThreadLocal { span: tcx.def_span(static_id) });
113+
}
114+
111115
// These do not inherit the codegen attrs of the parent static allocation, since
112116
// it doesn't make sense for them to inherit their `#[no_mangle]` and `#[link_name = ..]`
113117
// and the like.

compiler/rustc_hir_analysis/src/bounds.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,20 @@ impl<'tcx> Bounds<'tcx> {
5454
span: Span,
5555
polarity: ty::PredicatePolarity,
5656
) {
57-
self.clauses.push((
57+
let clause = (
5858
trait_ref
5959
.map_bound(|trait_ref| {
6060
ty::ClauseKind::Trait(ty::TraitPredicate { trait_ref, polarity })
6161
})
6262
.to_predicate(tcx),
6363
span,
64-
));
64+
);
65+
// FIXME(-Znext-solver): We can likely remove this hack once the new trait solver lands.
66+
if tcx.lang_items().sized_trait() == Some(trait_ref.def_id()) {
67+
self.clauses.insert(0, clause);
68+
} else {
69+
self.clauses.push(clause);
70+
}
6571
}
6672

6773
pub fn push_projection_bound(

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,8 @@ pub fn check_intrinsic_type(
459459
sym::unchecked_div | sym::unchecked_rem | sym::exact_div => {
460460
(1, 0, vec![param(0), param(0)], param(0))
461461
}
462-
sym::unchecked_shl | sym::unchecked_shr | sym::rotate_left | sym::rotate_right => {
463-
(1, 0, vec![param(0), param(0)], param(0))
464-
}
462+
sym::unchecked_shl | sym::unchecked_shr => (2, 0, vec![param(0), param(1)], param(0)),
463+
sym::rotate_left | sym::rotate_right => (1, 0, vec![param(0), param(0)], param(0)),
465464
sym::unchecked_add | sym::unchecked_sub | sym::unchecked_mul => {
466465
(1, 0, vec![param(0), param(0)], param(0))
467466
}

compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -1218,22 +1218,35 @@ pub fn typeid_for_instance<'tcx>(
12181218
let trait_id = tcx.fn_trait_kind_to_def_id(closure_args.kind()).unwrap();
12191219
let tuple_args =
12201220
tcx.instantiate_bound_regions_with_erased(closure_args.sig()).inputs()[0];
1221-
(trait_id, tuple_args)
1221+
(trait_id, Some(tuple_args))
12221222
}
1223-
ty::Coroutine(..) => (
1224-
tcx.require_lang_item(LangItem::Coroutine, None),
1225-
instance.args.as_coroutine().resume_ty(),
1226-
),
1223+
ty::Coroutine(..) => match tcx.coroutine_kind(instance.def_id()).unwrap() {
1224+
hir::CoroutineKind::Coroutine(..) => (
1225+
tcx.require_lang_item(LangItem::Coroutine, None),
1226+
Some(instance.args.as_coroutine().resume_ty()),
1227+
),
1228+
hir::CoroutineKind::Desugared(desugaring, _) => {
1229+
let lang_item = match desugaring {
1230+
hir::CoroutineDesugaring::Async => LangItem::Future,
1231+
hir::CoroutineDesugaring::AsyncGen => LangItem::AsyncIterator,
1232+
hir::CoroutineDesugaring::Gen => LangItem::Iterator,
1233+
};
1234+
(tcx.require_lang_item(lang_item, None), None)
1235+
}
1236+
},
12271237
ty::CoroutineClosure(..) => (
12281238
tcx.require_lang_item(LangItem::FnOnce, None),
1229-
tcx.instantiate_bound_regions_with_erased(
1230-
instance.args.as_coroutine_closure().coroutine_closure_sig(),
1231-
)
1232-
.tupled_inputs_ty,
1239+
Some(
1240+
tcx.instantiate_bound_regions_with_erased(
1241+
instance.args.as_coroutine_closure().coroutine_closure_sig(),
1242+
)
1243+
.tupled_inputs_ty,
1244+
),
12331245
),
12341246
x => bug!("Unexpected type kind for closure-like: {x:?}"),
12351247
};
1236-
let trait_ref = ty::TraitRef::new(tcx, trait_id, [closure_ty, inputs]);
1248+
let concrete_args = tcx.mk_args_trait(closure_ty, inputs.map(Into::into));
1249+
let trait_ref = ty::TraitRef::new(tcx, trait_id, concrete_args);
12371250
let invoke_ty = trait_object_ty(tcx, ty::Binder::dummy(trait_ref));
12381251
let abstract_args = tcx.mk_args_trait(invoke_ty, trait_ref.args.into_iter().skip(1));
12391252
// There should be exactly one method on this trait, and it should be the one we're

library/core/src/hash/mod.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,18 @@ pub trait BuildHasher {
752752
#[stable(since = "1.7.0", feature = "build_hasher")]
753753
pub struct BuildHasherDefault<H>(marker::PhantomData<fn() -> H>);
754754

755+
impl<H> BuildHasherDefault<H> {
756+
/// Creates a new BuildHasherDefault for Hasher `H`.
757+
#[unstable(
758+
feature = "build_hasher_default_const_new",
759+
issue = "123197",
760+
reason = "recently added"
761+
)]
762+
pub const fn new() -> Self {
763+
BuildHasherDefault(marker::PhantomData)
764+
}
765+
}
766+
755767
#[stable(since = "1.9.0", feature = "core_impl_debug")]
756768
impl<H> fmt::Debug for BuildHasherDefault<H> {
757769
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -778,7 +790,7 @@ impl<H> Clone for BuildHasherDefault<H> {
778790
#[stable(since = "1.7.0", feature = "build_hasher")]
779791
impl<H> Default for BuildHasherDefault<H> {
780792
fn default() -> BuildHasherDefault<H> {
781-
BuildHasherDefault(marker::PhantomData)
793+
Self::new()
782794
}
783795
}
784796

library/core/src/intrinsics.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2236,18 +2236,20 @@ extern "rust-intrinsic" {
22362236
/// Safe wrappers for this intrinsic are available on the integer
22372237
/// primitives via the `checked_shl` method. For example,
22382238
/// [`u32::checked_shl`]
2239+
#[cfg(not(bootstrap))]
22392240
#[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
22402241
#[rustc_nounwind]
2241-
pub fn unchecked_shl<T: Copy>(x: T, y: T) -> T;
2242+
pub fn unchecked_shl<T: Copy, U: Copy>(x: T, y: U) -> T;
22422243
/// Performs an unchecked right shift, resulting in undefined behavior when
22432244
/// `y < 0` or `y >= N`, where N is the width of T in bits.
22442245
///
22452246
/// Safe wrappers for this intrinsic are available on the integer
22462247
/// primitives via the `checked_shr` method. For example,
22472248
/// [`u32::checked_shr`]
2249+
#[cfg(not(bootstrap))]
22482250
#[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
22492251
#[rustc_nounwind]
2250-
pub fn unchecked_shr<T: Copy>(x: T, y: T) -> T;
2252+
pub fn unchecked_shr<T: Copy, U: Copy>(x: T, y: U) -> T;
22512253

22522254
/// Returns the result of an unchecked addition, resulting in
22532255
/// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`.

library/core/src/num/int_macros.rs

+24-8
Original file line numberDiff line numberDiff line change
@@ -1227,10 +1227,18 @@ macro_rules! int_impl {
12271227
#[inline(always)]
12281228
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
12291229
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
1230-
// SAFETY: the caller must uphold the safety contract for
1231-
// `unchecked_shl`.
1232-
// Any legal shift amount is losslessly representable in the self type.
1233-
unsafe { intrinsics::unchecked_shl(self, conv_rhs_for_unchecked_shift!($SelfT, rhs)) }
1230+
#[cfg(bootstrap)]
1231+
{
1232+
// For bootstrapping, just use built-in primitive shift.
1233+
// panicking is a legal manifestation of UB
1234+
self << rhs
1235+
}
1236+
#[cfg(not(bootstrap))]
1237+
{
1238+
// SAFETY: the caller must uphold the safety contract for
1239+
// `unchecked_shl`.
1240+
unsafe { intrinsics::unchecked_shl(self, rhs) }
1241+
}
12341242
}
12351243

12361244
/// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
@@ -1310,10 +1318,18 @@ macro_rules! int_impl {
13101318
#[inline(always)]
13111319
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
13121320
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
1313-
// SAFETY: the caller must uphold the safety contract for
1314-
// `unchecked_shr`.
1315-
// Any legal shift amount is losslessly representable in the self type.
1316-
unsafe { intrinsics::unchecked_shr(self, conv_rhs_for_unchecked_shift!($SelfT, rhs)) }
1321+
#[cfg(bootstrap)]
1322+
{
1323+
// For bootstrapping, just use built-in primitive shift.
1324+
// panicking is a legal manifestation of UB
1325+
self >> rhs
1326+
}
1327+
#[cfg(not(bootstrap))]
1328+
{
1329+
// SAFETY: the caller must uphold the safety contract for
1330+
// `unchecked_shr`.
1331+
unsafe { intrinsics::unchecked_shr(self, rhs) }
1332+
}
13171333
}
13181334

13191335
/// Checked absolute value. Computes `self.abs()`, returning `None` if

0 commit comments

Comments
 (0)