google / google/zerocopy

Improve error message on missing `Immutable`

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

Description

## Progress

- [ ] https://github.com/rust-lang/rust/issues/130563
- [ ] Per [this comment](https://github.com/rust-lang/rust/issues/130563#issuecomment-2362990475), wait for `#[diagnostic::do_not_recommend]` to [stabilize](https://github.com/rust-lang/rust/issues/51992)
- [ ] Use `#[diagnostic::do_not_recommend]` on our `Immutable` impls for `&T` and `&mut T`

## Original text

Per https://github.com/google/zerocopy/discussions/1288#discussioncomment-9476006:

> The compiler errors when a type is missing `Immutable` are somewhat misleading, although I'm not sure there's much `zerocopy` can do about this. An example where the compiler recommends adding a reference rather than implementing `Immutable` (some were even more confusing than this):
>
>
> ```
> error[E0277]: the trait bound `virtio::wl::CtrlVfdNewDmabuf: zerocopy::Immutable` is not satisfied
> --> devices/src/virtio/wl.rs:317:20
> |
> 317 | .write_obj(ctrl_vfd_new_dmabuf)
> | --------- ^^^^^^^^^^^^^^^^^^^ the trait `zerocopy::Immutable` is not implemented for `virtio::wl::CtrlVfdNewDmabuf`
> | |
> | required by a bound introduced by this call
> |
> note: required by a bound in `virtio::descriptor_utils::Writer::write_obj`
> --> devices/src/virtio/descriptor_utils.rs:536:25
> |
> 536 | pub fn write_obj(&mut self, val: T) -> io::Result<()> {
> | ^^^^^^^^^ required by this bound in `Writer::write_obj`
> help: consider borrowing here
> |
> 317 | .write_obj(&ctrl_vfd_new_dmabuf)
> | +
> 317 | .write_obj(&mut ctrl_vfd_new_dmabuf)
> | ++++
> ```
>
> Taking the compiler's suggestion results in a different error with a recommendation to remove the reference (back to the original code).

It appears that this is a compiler issue: https://github.com/rust-lang/rust/issues/130563

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.