-
Notifications
You must be signed in to change notification settings - Fork 1.7k
rust-analyzer doesn't like zerocopy::KnownLayout
on DSTs
#18682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
If you don't get any complaints elsewhere from "rust-analyzer.procMacro.ignored": {
"zerocopy_derive": ["KnownLayout"]
}, |
For help in debugging this issue, here's the CodeSource: use zerocopy::KnownLayout;
#[derive(KnownLayout)]
#[repr(C)]
struct Flexible {
header: u32,
body: [u8],
} Output of #![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use zerocopy::KnownLayout;
#[repr(C)]
struct Flexible {
header: u32,
body: [u8],
}
const _: () = {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::KnownLayout for Flexible
where
[u8]: ::zerocopy::KnownLayout,
{
fn only_derive_is_allowed_to_implement_this_trait() {}
type PointerMetadata = <[u8] as ::zerocopy::KnownLayout>::PointerMetadata;
type MaybeUninit = __ZerocopyKnownLayoutMaybeUninit;
const LAYOUT: ::zerocopy::DstLayout = {
use ::zerocopy::util::macro_util::core_reexport::num::NonZeroUsize;
use ::zerocopy::{DstLayout, KnownLayout};
let repr_align = ::zerocopy::util::macro_util::core_reexport::option::Option::None;
let repr_packed = ::zerocopy::util::macro_util::core_reexport::option::Option::None;
DstLayout::new_zst(repr_align)
.extend(DstLayout::for_type::<u32>(), repr_packed)
.extend(<[u8] as KnownLayout>::LAYOUT, repr_packed)
.pad_to_align()
};
#[inline(always)]
fn raw_from_ptr_len(
bytes: ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<u8>,
meta: Self::PointerMetadata,
) -> ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<Self> {
use ::zerocopy::KnownLayout;
let trailing = <[u8] as KnownLayout>::raw_from_ptr_len(bytes, meta);
let slf = trailing.as_ptr() as *mut Self;
unsafe {
::zerocopy::util::macro_util::core_reexport::ptr::NonNull::new_unchecked(
slf,
)
}
}
#[inline(always)]
fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata {
<[u8]>::pointer_to_metadata(ptr as *mut _)
}
}
#[allow(non_camel_case_types)]
struct __Zerocopy_Field_header;
#[allow(non_camel_case_types)]
struct __Zerocopy_Field_body;
unsafe impl ::zerocopy::util::macro_util::Field<__Zerocopy_Field_header>
for Flexible {
type Type = u32;
}
unsafe impl ::zerocopy::util::macro_util::Field<__Zerocopy_Field_body> for Flexible {
type Type = [u8];
}
#[repr(C)]
#[doc(hidden)]
#[allow(private_bounds)]
struct __ZerocopyKnownLayoutMaybeUninit(
::zerocopy::util::macro_util::core_reexport::mem::MaybeUninit<
<Flexible as ::zerocopy::util::macro_util::Field<
__Zerocopy_Field_header,
>>::Type,
>,
<<Flexible as ::zerocopy::util::macro_util::Field<
__Zerocopy_Field_body,
>>::Type as ::zerocopy::KnownLayout>::MaybeUninit,
)
where
<Flexible as ::zerocopy::util::macro_util::Field<
__Zerocopy_Field_body,
>>::Type: ::zerocopy::KnownLayout;
unsafe impl ::zerocopy::KnownLayout for __ZerocopyKnownLayoutMaybeUninit
where
<Flexible as ::zerocopy::util::macro_util::Field<
__Zerocopy_Field_body,
>>::Type: ::zerocopy::KnownLayout,
{
#[allow(clippy::missing_inline_in_public_items)]
fn only_derive_is_allowed_to_implement_this_trait() {}
type PointerMetadata = <Flexible as ::zerocopy::KnownLayout>::PointerMetadata;
type MaybeUninit = Self;
const LAYOUT: ::zerocopy::DstLayout = <Flexible as ::zerocopy::KnownLayout>::LAYOUT;
#[inline(always)]
fn raw_from_ptr_len(
bytes: ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<u8>,
meta: Self::PointerMetadata,
) -> ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<Self> {
use ::zerocopy::KnownLayout;
let trailing = <<<Flexible as ::zerocopy::util::macro_util::Field<
__Zerocopy_Field_body,
>>::Type as ::zerocopy::KnownLayout>::MaybeUninit as KnownLayout>::raw_from_ptr_len(
bytes,
meta,
);
let slf = trailing.as_ptr() as *mut Self;
unsafe {
::zerocopy::util::macro_util::core_reexport::ptr::NonNull::new_unchecked(
slf,
)
}
}
#[inline(always)]
fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata {
<<<Flexible as ::zerocopy::util::macro_util::Field<
__Zerocopy_Field_body,
>>::Type as ::zerocopy::KnownLayout>::MaybeUninit>::pointer_to_metadata(
ptr as *mut _,
)
}
}
}; |
I've been planning to resolve this issue, but couldn't make time for it 😅 I'll work on this today |
Sorry for the inconveniences and delayed fix |
There's still a problem with use zerocopy::KnownLayout;
#[derive(KnownLayout)]
#[repr(C)]
pub struct Dst([u8]);
#[derive(KnownLayout)]
#[repr(C)]
pub struct Struct {
pub dst: Dst,
}
No problem with rustc. |
I've addressed a new issue for this and a new PR fixing it |
rust-analyzer version:
rust-analyzer 1.83.0
viarust-analyzer-1.83.0-1.fc41.x86_64
rustc version:
rustc 1.83.0 (90b35a623 2024-11-26) (Fedora 1.83.0-1.fc41)
viarust-1.83.0-1.fc41.x86_64
editor or extension: nvim from Fedora 41:
neovim-0.10.2-1.fc41.x86_64
relevant settings:
code snippet to reproduce:
This gets you
but with
cargo build
it's fine.This is caused by Flexible being a DST (dynamically sized type). Dropping the variable-sized
body: [u8]
field makes the problem go away.I also tried with nightly (
rust-analyzer 1.85.0-nightly (d4025ee 2024-12-12)
) without improvement.The text was updated successfully, but these errors were encountered: