Skip to content

Commit 58ea9b3

Browse files
committed
Auto merge of #141879 - matthiaskrgr:rollup-jqjk26e, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #140715 (Clarify &mut-methods' docs on sync::OnceLock) - #141309 (x86 (32/64): go back to passing SIMD vectors by-ptr) - #141677 (Async drop - type instead of async drop fn, fixes #140484) - #141733 (C-variadic functions must be unsafe) - #141858 (Fix typo in `StructuralPartialEq` docs) - #141874 (add f16_epsilon and f128_epsilon diagnostic items) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 52882f6 + bb56268 commit 58ea9b3

File tree

24 files changed

+268
-93
lines changed

24 files changed

+268
-93
lines changed

compiler/rustc_ast_passes/messages.ftl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ ast_passes_auto_super_lifetime = auto traits cannot have super traits or lifetim
2424
.label = {ast_passes_auto_super_lifetime}
2525
.suggestion = remove the super traits or lifetime bounds
2626
27-
ast_passes_bad_c_variadic = only foreign, `unsafe extern "C"`, or `unsafe extern "C-unwind"` functions may have a C-variadic arg
28-
2927
ast_passes_bare_fn_invalid_safety = function pointers cannot be declared with `safe` safety qualifier
3028
.suggestion = remove safe from this item
3129
@@ -36,6 +34,13 @@ ast_passes_body_in_extern = incorrect `{$kind}` inside `extern` block
3634
3735
ast_passes_bound_in_context = bounds on `type`s in {$ctx} have no effect
3836
37+
ast_passes_c_variadic_bad_calling_convention =
38+
only foreign, `unsafe extern "C"`, or `unsafe extern "C-unwind"` functions may have a C-variadic arg
39+
40+
ast_passes_c_variadic_safe_foreign_function =
41+
foreign functions with a C-variadic argument cannot be safe
42+
.suggestion = remove the `safe` keyword from this definition
43+
3944
ast_passes_const_and_c_variadic = functions cannot be both `const` and C-variadic
4045
.const = `const` because of this
4146
.variadic = C-variadic because of this

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,21 @@ impl<'a> AstValidator<'a> {
554554
}
555555

556556
match (fk.ctxt(), fk.header()) {
557-
(Some(FnCtxt::Foreign), _) => return,
557+
(Some(FnCtxt::Foreign), Some(header)) => match header.safety {
558+
Safety::Default | Safety::Unsafe(_) => return,
559+
Safety::Safe(span) => {
560+
self.dcx().emit_err(errors::CVariadicSafeForeignFunction {
561+
// The span of the "safe " string that should be removed.
562+
safe_span: self
563+
.sess
564+
.psess
565+
.source_map()
566+
.span_until_non_whitespace(span.until(fk.decl().output.span())),
567+
});
568+
return;
569+
}
570+
},
571+
558572
(Some(FnCtxt::Free), Some(header)) => match header.ext {
559573
Extern::Explicit(StrLit { symbol_unescaped: sym::C, .. }, _)
560574
| Extern::Explicit(StrLit { symbol_unescaped: sym::C_dash_unwind, .. }, _)

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,25 @@ pub(crate) struct ExternItemAscii {
308308
}
309309

310310
#[derive(Diagnostic)]
311-
#[diag(ast_passes_bad_c_variadic)]
311+
#[diag(ast_passes_c_variadic_bad_calling_convention)]
312312
pub(crate) struct BadCVariadic {
313313
#[primary_span]
314314
pub span: Vec<Span>,
315315
}
316316

317+
#[derive(Diagnostic)]
318+
#[diag(ast_passes_c_variadic_safe_foreign_function)]
319+
pub(crate) struct CVariadicSafeForeignFunction {
320+
#[primary_span]
321+
#[suggestion(
322+
ast_passes_suggestion,
323+
applicability = "machine-applicable",
324+
code = "",
325+
style = "verbose"
326+
)]
327+
pub safe_span: Span,
328+
}
329+
317330
#[derive(Diagnostic)]
318331
#[diag(ast_passes_item_underscore)]
319332
pub(crate) struct ItemUnderscore<'a> {

compiler/rustc_mir_transform/src/elaborate_drop.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{fmt, iter, mem};
22

33
use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
4+
use rustc_hir::def::DefKind;
45
use rustc_hir::lang_items::LangItem;
56
use rustc_index::Idx;
67
use rustc_middle::mir::*;
@@ -254,8 +255,19 @@ where
254255
// impl_item_refs may be empty if drop fn is not implemented in 'impl AsyncDrop for ...'
255256
// (#140974).
256257
// Such code will report error, so just generate sync drop here and return
257-
let Some(drop_fn_def_id) =
258-
tcx.associated_item_def_ids(drop_trait).into_iter().nth(0).copied()
258+
let Some(drop_fn_def_id) = tcx
259+
.associated_item_def_ids(drop_trait)
260+
.first()
261+
.and_then(|def_id| {
262+
if tcx.def_kind(def_id) == DefKind::AssocFn
263+
&& tcx.check_args_compatible(*def_id, trait_args)
264+
{
265+
Some(def_id)
266+
} else {
267+
None
268+
}
269+
})
270+
.copied()
259271
else {
260272
tcx.dcx().span_delayed_bug(
261273
self.elaborator.body().span,

compiler/rustc_monomorphize/src/mono_checks/abi_check.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ fn do_check_simd_vector_abi<'tcx>(
3535
is_call: bool,
3636
loc: impl Fn() -> (Span, HirId),
3737
) {
38-
// We check this on all functions, including those using the "Rust" ABI.
39-
// For the "Rust" ABI it would be a bug if the lint ever triggered, but better safe than sorry.
4038
let feature_def = tcx.sess.target.features_for_correct_vector_abi();
4139
let codegen_attrs = tcx.codegen_fn_attrs(def_id);
4240
let have_feature = |feat: Symbol| {
@@ -123,8 +121,9 @@ fn do_check_wasm_abi<'tcx>(
123121
is_call: bool,
124122
loc: impl Fn() -> (Span, HirId),
125123
) {
126-
// Only proceed for `extern "C" fn` on wasm32-unknown-unknown (same check as what `adjust_for_foreign_abi` uses to call `compute_wasm_abi_info`),
127-
// and only proceed if `wasm_c_abi_opt` indicates we should emit the lint.
124+
// Only proceed for `extern "C" fn` on wasm32-unknown-unknown (same check as what
125+
// `adjust_for_foreign_abi` uses to call `compute_wasm_abi_info`), and only proceed if
126+
// `wasm_c_abi_opt` indicates we should emit the lint.
128127
if !(tcx.sess.target.arch == "wasm32"
129128
&& tcx.sess.target.os == "unknown"
130129
&& tcx.wasm_c_abi_opt() == WasmCAbi::Legacy { with_lint: true }
@@ -157,8 +156,15 @@ fn check_instance_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) {
157156
else {
158157
// An error will be reported during codegen if we cannot determine the ABI of this
159158
// function.
159+
tcx.dcx().delayed_bug("ABI computation failure should lead to compilation failure");
160160
return;
161161
};
162+
// Unlike the call-site check, we do also check "Rust" ABI functions here.
163+
// This should never trigger, *except* if we start making use of vector registers
164+
// for the "Rust" ABI and the user disables those vector registers (which should trigger a
165+
// warning as that's clearly disabling a "required" target feature for this target).
166+
// Using such a function is where disabling the vector register actually can start leading
167+
// to soundness issues, so erroring here seems good.
162168
let loc = || {
163169
let def_id = instance.def_id();
164170
(
@@ -179,7 +185,8 @@ fn check_call_site_abi<'tcx>(
179185
loc: impl Fn() -> (Span, HirId) + Copy,
180186
) {
181187
if callee.fn_sig(tcx).abi().is_rustic_abi() {
182-
// we directly handle the soundness of Rust ABIs
188+
// We directly handle the soundness of Rust ABIs -- so let's skip the majority of
189+
// call sites to avoid a perf regression.
183190
return;
184191
}
185192
let typing_env = ty::TypingEnv::fully_monomorphized();

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,8 +937,10 @@ symbols! {
937937
external_doc,
938938
f,
939939
f128,
940+
f128_epsilon,
940941
f128_nan,
941942
f16,
943+
f16_epsilon,
942944
f16_nan,
943945
f16c_target_feature,
944946
f32,

compiler/rustc_target/src/callconv/mod.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_abi::{
88
};
99
use rustc_macros::HashStable_Generic;
1010

11-
use crate::spec::{HasTargetSpec, HasWasmCAbiOpt, HasX86AbiOpt, RustcAbi, WasmCAbi};
11+
use crate::spec::{HasTargetSpec, HasWasmCAbiOpt, HasX86AbiOpt, WasmCAbi};
1212

1313
mod aarch64;
1414
mod amdgpu;
@@ -733,24 +733,6 @@ impl<'a, Ty> FnAbi<'a, Ty> {
733733
_ => {}
734734
};
735735

736-
// Decides whether we can pass the given SIMD argument via `PassMode::Direct`.
737-
// May only return `true` if the target will always pass those arguments the same way,
738-
// no matter what the user does with `-Ctarget-feature`! In other words, whatever
739-
// target features are required to pass a SIMD value in registers must be listed in
740-
// the `abi_required_features` for the current target and ABI.
741-
let can_pass_simd_directly = |arg: &ArgAbi<'_, Ty>| match &*spec.arch {
742-
// On x86, if we have SSE2 (which we have by default for x86_64), we can always pass up
743-
// to 128-bit-sized vectors.
744-
"x86" if spec.rustc_abi == Some(RustcAbi::X86Sse2) => arg.layout.size.bits() <= 128,
745-
"x86_64" if spec.rustc_abi != Some(RustcAbi::X86Softfloat) => {
746-
// FIXME once https://github.com/bytecodealliance/wasmtime/issues/10254 is fixed
747-
// accept vectors up to 128bit rather than vectors of exactly 128bit.
748-
arg.layout.size.bits() == 128
749-
}
750-
// So far, we haven't implemented this logic for any other target.
751-
_ => false,
752-
};
753-
754736
for (arg_idx, arg) in self
755737
.args
756738
.iter_mut()
@@ -850,9 +832,10 @@ impl<'a, Ty> FnAbi<'a, Ty> {
850832
// target feature sets. Some more information about this
851833
// issue can be found in #44367.
852834
//
853-
// Note that the intrinsic ABI is exempt here as those are not
854-
// real functions anyway, and the backend expects very specific types.
855-
if spec.simd_types_indirect && !can_pass_simd_directly(arg) {
835+
// We *could* do better in some cases, e.g. on x86_64 targets where SSE2 is
836+
// required. However, it turns out that that makes LLVM worse at optimizing this
837+
// code, so we pass things indirectly even there. See #139029 for more on that.
838+
if spec.simd_types_indirect {
856839
arg.make_indirect();
857840
}
858841
}

library/core/src/marker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ pub trait Unsize<T: ?Sized> {
200200
///
201201
/// Constants are only allowed as patterns if (a) their type implements
202202
/// `PartialEq`, and (b) interpreting the value of the constant as a pattern
203-
/// is equialent to calling `PartialEq`. This ensures that constants used as
203+
/// is equivalent to calling `PartialEq`. This ensures that constants used as
204204
/// patterns cannot expose implementation details in an unexpected way or
205205
/// cause semver hazards.
206206
///

library/core/src/num/f128.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ impl f128 {
171171
/// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon
172172
/// [`MANTISSA_DIGITS`]: f128::MANTISSA_DIGITS
173173
#[unstable(feature = "f128", issue = "116909")]
174+
#[rustc_diagnostic_item = "f128_epsilon"]
174175
pub const EPSILON: f128 = 1.92592994438723585305597794258492732e-34_f128;
175176

176177
/// Smallest finite `f128` value.

library/core/src/num/f16.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ impl f16 {
168168
/// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon
169169
/// [`MANTISSA_DIGITS`]: f16::MANTISSA_DIGITS
170170
#[unstable(feature = "f16", issue = "116909")]
171+
#[rustc_diagnostic_item = "f16_epsilon"]
171172
pub const EPSILON: f16 = 9.7656e-4_f16;
172173

173174
/// Smallest finite `f16` value.

library/std/src/sync/once_lock.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,11 @@ impl<T> OnceLock<T> {
159159

160160
/// Gets the mutable reference to the underlying value.
161161
///
162-
/// Returns `None` if the cell is uninitialized, or being initialized.
163-
/// This method never blocks.
162+
/// Returns `None` if the cell is uninitialized.
163+
///
164+
/// This method never blocks. Since it borrows the `OnceLock` mutably,
165+
/// it is statically guaranteed that no active borrows to the `OnceLock`
166+
/// exist, including from other threads.
164167
#[inline]
165168
#[stable(feature = "once_cell", since = "1.70.0")]
166169
pub fn get_mut(&mut self) -> Option<&mut T> {
@@ -315,7 +318,9 @@ impl<T> OnceLock<T> {
315318
/// Gets the mutable reference of the contents of the cell, initializing
316319
/// it to `f()` if the cell was uninitialized.
317320
///
318-
/// This method never blocks.
321+
/// This method never blocks. Since it borrows the `OnceLock` mutably,
322+
/// it is statically guaranteed that no active borrows to the `OnceLock`
323+
/// exist, including from other threads.
319324
///
320325
/// # Panics
321326
///
@@ -405,7 +410,9 @@ impl<T> OnceLock<T> {
405410
/// it to `f()` if the cell was uninitialized. If the cell was uninitialized
406411
/// and `f()` failed, an error is returned.
407412
///
408-
/// This method never blocks.
413+
/// This method never blocks. Since it borrows the `OnceLock` mutably,
414+
/// it is statically guaranteed that no active borrows to the `OnceLock`
415+
/// exist, including from other threads.
409416
///
410417
/// # Panics
411418
///
@@ -469,7 +476,8 @@ impl<T> OnceLock<T> {
469476
///
470477
/// Has no effect and returns `None` if the `OnceLock` was uninitialized.
471478
///
472-
/// Safety is guaranteed by requiring a mutable reference.
479+
/// Since this method borrows the `OnceLock` mutably, it is statically guaranteed that
480+
/// no active borrows to the `OnceLock` exist, including from other threads.
473481
///
474482
/// # Examples
475483
///

tests/codegen/abi-x86-sse.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ trait Copy {}
2727
#[repr(simd)]
2828
pub struct Sse([f32; 4]);
2929

30-
// x86-64: <4 x float> @sse_id(<4 x float> {{[^,]*}})
31-
// x86-32: <4 x float> @sse_id(<4 x float> {{[^,]*}})
30+
// FIXME: due to #139029 we are passing them all indirectly.
31+
// x86-64: void @sse_id(ptr{{( [^,]*)?}} sret([16 x i8]){{( .*)?}}, ptr{{( [^,]*)?}})
32+
// x86-32: void @sse_id(ptr{{( [^,]*)?}} sret([16 x i8]){{( .*)?}}, ptr{{( [^,]*)?}})
3233
// x86-32-nosse: void @sse_id(ptr{{( [^,]*)?}} sret([16 x i8]){{( .*)?}}, ptr{{( [^,]*)?}})
3334
#[no_mangle]
3435
pub fn sse_id(x: Sse) -> Sse {

tests/codegen/simd-intrinsic/simd-intrinsic-transmute-array.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
//
22
//@ compile-flags: -C no-prepopulate-passes
3-
// LLVM IR isn't very portable and the one tested here depends on the ABI
4-
// which is different between x86 (where we use SSE registers) and others.
5-
// `x86-64` and `x86-32-sse2` are identical, but compiletest does not support
6-
// taking the union of multiple `only` annotations.
7-
//@ revisions: x86-64 x86-32-sse2 other
8-
//@[x86-64] only-x86_64
9-
//@[x86-32-sse2] only-rustc_abi-x86-sse2
10-
//@[other] ignore-rustc_abi-x86-sse2
11-
//@[other] ignore-x86_64
123

134
#![crate_type = "lib"]
145
#![allow(non_camel_case_types)]
@@ -47,9 +38,7 @@ pub fn build_array_s(x: [f32; 4]) -> S<4> {
4738
#[no_mangle]
4839
pub fn build_array_transmute_s(x: [f32; 4]) -> S<4> {
4940
// CHECK: %[[VAL:.+]] = load <4 x float>, ptr %x, align [[ARRAY_ALIGN]]
50-
// x86-32: ret <4 x float> %[[VAL:.+]]
51-
// x86-64: ret <4 x float> %[[VAL:.+]]
52-
// other: store <4 x float> %[[VAL:.+]], ptr %_0, align [[VECTOR_ALIGN]]
41+
// CHECK: store <4 x float> %[[VAL:.+]], ptr %_0, align [[VECTOR_ALIGN]]
5342
unsafe { std::mem::transmute(x) }
5443
}
5544

@@ -64,8 +53,6 @@ pub fn build_array_t(x: [f32; 4]) -> T {
6453
#[no_mangle]
6554
pub fn build_array_transmute_t(x: [f32; 4]) -> T {
6655
// CHECK: %[[VAL:.+]] = load <4 x float>, ptr %x, align [[ARRAY_ALIGN]]
67-
// x86-32: ret <4 x float> %[[VAL:.+]]
68-
// x86-64: ret <4 x float> %[[VAL:.+]]
69-
// other: store <4 x float> %[[VAL:.+]], ptr %_0, align [[VECTOR_ALIGN]]
56+
// CHECK: store <4 x float> %[[VAL:.+]], ptr %_0, align [[VECTOR_ALIGN]]
7057
unsafe { std::mem::transmute(x) }
7158
}

tests/codegen/simd/packed-simd.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,18 @@ fn load<T, const N: usize>(v: PackedSimd<T, N>) -> FullSimd<T, N> {
3030
}
3131
}
3232

33-
// CHECK-LABEL: define <3 x float> @square_packed_full(ptr{{[a-z_ ]*}} align 4 {{[^,]*}})
33+
// CHECK-LABEL: square_packed_full
34+
// CHECK-SAME: ptr{{[a-z_ ]*}} sret([[RET_TYPE:[^)]+]]) [[RET_ALIGN:align (8|16)]]{{[^%]*}} [[RET_VREG:%[_0-9]*]]
35+
// CHECK-SAME: ptr{{[a-z_ ]*}} align 4
3436
#[no_mangle]
3537
pub fn square_packed_full(x: PackedSimd<f32, 3>) -> FullSimd<f32, 3> {
36-
// The unoptimized version of this is not very interesting to check
37-
// since `load` does not get inlined.
38-
// opt3-NEXT: start:
39-
// opt3-NEXT: load <3 x float>
38+
// CHECK-NEXT: start
39+
// noopt: alloca [[RET_TYPE]], [[RET_ALIGN]]
40+
// CHECK: load <3 x float>
4041
let x = load(x);
41-
// opt3-NEXT: [[VREG:%[a-z0-9_]+]] = fmul <3 x float>
42-
// opt3-NEXT: ret <3 x float> [[VREG:%[a-z0-9_]+]]
42+
// CHECK: [[VREG:%[a-z0-9_]+]] = fmul <3 x float>
43+
// CHECK-NEXT: store <3 x float> [[VREG]], ptr [[RET_VREG]], [[RET_ALIGN]]
44+
// CHECK-NEXT: ret void
4345
unsafe { intrinsics::simd_mul(x, x) }
4446
}
4547

tests/crashes/140484.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/crashes/140500.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)