Skip to content

Commit 396cf1e

Browse files
committed
require simd_insert, simd_extract indices to be constants
1 parent 0b9f6ad commit 396cf1e

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

compiler/rustc_borrowck/messages.ftl

+7-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,13 @@ borrowck_returned_lifetime_wrong =
163163
borrowck_returned_ref_escaped =
164164
returns a reference to a captured variable which escapes the closure body
165165
166-
borrowck_simd_shuffle_last_const = last argument of `simd_shuffle` is required to be a `const` item
166+
borrowck_simd_intrinsic_arg_const =
167+
{$arg ->
168+
[1] 1st
169+
[2] 2nd
170+
[3] 3rd
171+
*[other] {$arg}th
172+
} argument of `{$intrinsic}` is required to be a `const` item
167173
168174
borrowck_suggest_create_freash_reborrow =
169175
consider reborrowing the `Pin` instead of moving it

compiler/rustc_borrowck/src/session_diagnostics.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,10 @@ pub(crate) enum TypeNoCopy<'a, 'tcx> {
454454
}
455455

456456
#[derive(Diagnostic)]
457-
#[diag(borrowck_simd_shuffle_last_const)]
458-
pub(crate) struct SimdShuffleLastConst {
457+
#[diag(borrowck_simd_intrinsic_arg_const)]
458+
pub(crate) struct SimdIntrinsicArgConst {
459459
#[primary_span]
460460
pub span: Span,
461+
pub arg: usize,
462+
pub intrinsic: String,
461463
}

compiler/rustc_borrowck/src/type_check/mod.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
4949
use rustc_mir_dataflow::move_paths::MoveData;
5050
use rustc_mir_dataflow::ResultsCursor;
5151

52-
use crate::session_diagnostics::{MoveUnsized, SimdShuffleLastConst};
52+
use crate::session_diagnostics::{MoveUnsized, SimdIntrinsicArgConst};
5353
use crate::{
5454
borrow_set::BorrowSet,
5555
constraints::{OutlivesConstraint, OutlivesConstraintSet},
@@ -1666,9 +1666,22 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
16661666

16671667
let func_ty = func.ty(body, self.infcx.tcx);
16681668
if let ty::FnDef(def_id, _) = *func_ty.kind() {
1669-
if let Some(sym::simd_shuffle) = self.tcx().intrinsic(def_id) {
1670-
if !matches!(args[2], Spanned { node: Operand::Constant(_), .. }) {
1671-
self.tcx().dcx().emit_err(SimdShuffleLastConst { span: term.source_info.span });
1669+
// Some of the SIMD intrinsics are special: they need a particular argument to be a constant.
1670+
// (Eventually this should use const-generics, but those are not up for the task yet:
1671+
// https://github.com/rust-lang/rust/issues/85229.)
1672+
if let Some(name @ (sym::simd_shuffle | sym::simd_insert | sym::simd_extract)) =
1673+
self.tcx().intrinsic(def_id)
1674+
{
1675+
let idx = match name {
1676+
sym::simd_shuffle => 2,
1677+
_ => 1,
1678+
};
1679+
if !matches!(args[idx], Spanned { node: Operand::Constant(_), .. }) {
1680+
self.tcx().dcx().emit_err(SimdIntrinsicArgConst {
1681+
span: term.source_info.span,
1682+
arg: idx + 1,
1683+
intrinsic: name.to_string(),
1684+
});
16721685
}
16731686
}
16741687
}

compiler/rustc_codegen_ssa/src/mir/block.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -864,8 +864,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
864864
.map(|(i, arg)| {
865865
// The indices passed to simd_shuffle in the
866866
// third argument must be constant. This is
867-
// checked by const-qualification, which also
868-
// promotes any complex rvalues to constants.
867+
// checked by the type-checker.
869868
if i == 2 && intrinsic == sym::simd_shuffle {
870869
if let mir::Operand::Constant(constant) = &arg.node {
871870
let (llval, ty) = self.simd_shuffle_indices(bx, constant);

0 commit comments

Comments
 (0)