rust/tests/ui/consts/const-eval/simd/insert_extract.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

75 lines
2.2 KiB
Rust
Raw Normal View History

//@ run-pass
#![feature(repr_simd)]
#![feature(intrinsics, core_intrinsics, rustc_attrs)]
#![feature(staged_api)]
#![stable(feature = "foo", since = "1.3.37")]
#![allow(non_camel_case_types)]
// FIXME these intrinsics are not marked as const fn
// use std::intrinsics::simd::{simd_extract, simd_insert};
#[stable(feature = "foo", since = "1.3.37")]
#[rustc_const_stable(feature = "foo", since = "1.3.37")]
#[rustc_intrinsic]
const unsafe fn simd_insert<T, U>(_x: T, _idx: u32, _val: U) -> T {
unimplemented!()
}
Re-do recursive const stability checks Fundamentally, we have *three* disjoint categories of functions: 1. const-stable functions 2. private/unstable functions that are meant to be callable from const-stable functions 3. functions that can make use of unstable const features This PR implements the following system: - `#[rustc_const_stable]` puts functions in the first category. It may only be applied to `#[stable]` functions. - `#[rustc_const_unstable]` by default puts functions in the third category. The new attribute `#[rustc_const_stable_indirect]` can be added to such a function to move it into the second category. - `const fn` without a const stability marker are in the second category if they are still unstable. They automatically inherit the feature gate for regular calls, it can now also be used for const-calls. Also, several holes in recursive const stability checking are being closed. There's still one potential hole that is hard to avoid, which is when MIR building automatically inserts calls to a particular function in stable functions -- which happens in the panic machinery. Those need to *not* be `rustc_const_unstable` (or manually get a `rustc_const_stable_indirect`) to be sure they follow recursive const stability. But that's a fairly rare and special case so IMO it's fine. The net effect of this is that a `#[unstable]` or unmarked function can be constified simply by marking it as `const fn`, and it will then be const-callable from stable `const fn` and subject to recursive const stability requirements. If it is publicly reachable (which implies it cannot be unmarked), it will be const-unstable under the same feature gate. Only if the function ever becomes `#[stable]` does it need a `#[rustc_const_unstable]` or `#[rustc_const_stable]` marker to decide if this should also imply const-stability. Adding `#[rustc_const_unstable]` is only needed for (a) functions that need to use unstable const lang features (including intrinsics), or (b) `#[stable]` functions that are not yet intended to be const-stable. Adding `#[rustc_const_stable]` is only needed for functions that are actually meant to be directly callable from stable const code. `#[rustc_const_stable_indirect]` is used to mark intrinsics as const-callable and for `#[rustc_const_unstable]` functions that are actually called from other, exposed-on-stable `const fn`. No other attributes are required.
2024-10-06 17:59:19 +00:00
#[stable(feature = "foo", since = "1.3.37")]
#[rustc_const_stable(feature = "foo", since = "1.3.37")]
#[rustc_intrinsic]
const unsafe fn simd_extract<T, U>(_x: T, _idx: u32) -> U {
unimplemented!()
}
// repr(simd) now only supports array types
#[repr(simd)]
struct i8x1([i8; 1]);
#[repr(simd)]
struct u16x2([u16; 2]);
#[repr(simd)]
struct f32x4([f32; 4]);
fn main() {
2019-09-24 15:02:01 +00:00
{
2024-08-22 08:28:20 +00:00
const U: i8x1 = i8x1([13]);
2019-09-24 15:02:01 +00:00
const V: i8x1 = unsafe { simd_insert(U, 0_u32, 42_i8) };
2021-11-18 16:05:30 +00:00
const X0: i8 = V.0[0];
const Y0: i8 = unsafe { simd_extract(V, 0) };
assert_eq!(X0, 42);
assert_eq!(Y0, 42);
}
2019-09-24 15:02:01 +00:00
{
2024-08-22 08:28:20 +00:00
const U: u16x2 = u16x2([13, 14]);
2019-09-24 15:02:01 +00:00
const V: u16x2 = unsafe { simd_insert(U, 1_u32, 42_u16) };
2024-08-22 08:28:20 +00:00
const X0: u16 = V.0[0];
const X1: u16 = V.0[1];
2019-09-24 15:02:01 +00:00
const Y0: u16 = unsafe { simd_extract(V, 0) };
const Y1: u16 = unsafe { simd_extract(V, 1) };
assert_eq!(X0, 13);
assert_eq!(X1, 42);
assert_eq!(Y0, 13);
assert_eq!(Y1, 42);
}
{
const U: f32x4 = f32x4([13., 14., 15., 16.]);
const V: f32x4 = unsafe { simd_insert(U, 1_u32, 42_f32) };
const X0: f32 = V.0[0];
const X1: f32 = V.0[1];
const X2: f32 = V.0[2];
const X3: f32 = V.0[3];
2019-09-24 15:02:01 +00:00
const Y0: f32 = unsafe { simd_extract(V, 0) };
const Y1: f32 = unsafe { simd_extract(V, 1) };
const Y2: f32 = unsafe { simd_extract(V, 2) };
const Y3: f32 = unsafe { simd_extract(V, 3) };
2019-09-24 15:02:01 +00:00
assert_eq!(X0, 13.);
assert_eq!(X1, 42.);
assert_eq!(X2, 15.);
assert_eq!(X3, 16.);
2019-09-24 15:02:01 +00:00
assert_eq!(Y0, 13.);
assert_eq!(Y1, 42.);
assert_eq!(Y2, 15.);
assert_eq!(Y3, 16.);
2019-09-24 15:02:01 +00:00
}
}