Skip to content

rustc struggles to recognize legal vectors-of-pointers #85915

Closed
@workingjubilee

Description

@workingjubilee

rustc has trouble recognizing legal {tuples,arrays} of pointers for #[repr(simd)] to use.

What Does Work

The following is an example of a vector-of-pointers type that works.

#![feature(repr_simd)]
#[derive(Copy, Clone, Debug)]
#[repr(simd)]
pub struct ptrx2<T>(T, T);

fn main() {
    let v = vec![2, 3, 4];
    let x = ptrx2(v.as_ptr(), v.as_ptr());
}

Note that the pointer-ness here is only determined during monomorphization. This does not follow the same pattern as e.g. *const T being parameterized over <T>.

What Does Not Work

Attempts to define things like these give me errors.

#[repr(simd)]
pub struct ptrx2<T>(*const T, *const T);

or, in particular, the inciting incident:

#[repr(simd)]
struct SimdConst<T, const LANES: usize>([*const T; LANES]);

I expected rustc to recognize a legal definition of a vector-of-pointers and proceed to monomorphize it.
Instead, this happened:

error[E0077]: SIMD vector element type should be a primitive scalar (integer/float/pointer) type
  --> crates\core_simd\src\array.rs:76:1
   |
76 | struct SimdConst<T, const LANES: usize>([*const T; LANES]);
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Meta

rustc --version --verbose:

rustc 1.54.0-nightly (9111b8ae9 2021-05-26)
binary: rustc
commit-hash: 9111b8ae9793f18179a1336417618fc07a9cac85
commit-date: 2021-05-26
host: x86_64-pc-windows-msvc
release: 1.54.0-nightly
LLVM version: 12.0.1

Likely Causes

This is likely caused by the clauses in

match e.kind() {
ty::Param(_) => { /* struct<T>(T, T, T, T) is ok */ }
_ if e.is_machine() => { /* struct(u8, u8, u8, u8) is ok */ }
ty::Array(ty, _c) if ty.is_machine() => { /* struct([f32; 4]) */ }
_ => {
struct_span_err!(

A parameter is accepted, but not a pointer, even though &T and *const T (but not &[T] or fn() -> ()) are valid inputs to that parameter (i.e. they monomorphize successfully).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-SIMDArea: SIMD (Single Instruction Multiple Data)C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions