google / google/zerocopy

Invalid `derive(FromBytes)` is not eagerly checked for lifetime-generic `struct`

Open
#2,311 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
2.6k
Forks
179
Avg merge
1d 19h
Merged PRs (30d)
29

Description

**Example:**

```rust
use zerocopy::FromBytes;

#[repr(C)]
#[derive(FromBytes)]
struct Foo<'a> {
x: &'a [u8],
}
```

**What I expect to happen:** This `derive` will be rejected as soon as it is declared, since `&[T]` is never `FromBytes`. Since it would never be valid for a type to conditionally `impl FromBytes` based on an internal lifetime, the lifetime parameter should not need to be taken into account to reject the `derive` before its first use.

**What actually happens:** The failure to `derive(FromBytes)` is only evident when you try to use it (e.g. `transmute!([0x01; size_of::()])`). If I change it to `struct Foo { x: &'static [u8] }`, the `derive` eagerly rejects as expected.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.