Lokathor/bytemuck

`TransparentWrapper` derive emits unnecessary bound on generic structs.

zachs18 opened this issue · 1 comments

#[derive(bytemuck::TransparentWrapper)]
#[repr(transparent)]
pub struct GenericWrapper<T>(pub T);

This emits

unsafe impl<T> ::bytemuck::TransparentWrapper<T> for GenericWrapper<T> where
    T: ::bytemuck::TransparentWrapper<T>
{
}

The where T: ::bytemuck::TransparentWrapper<T> is not necessary, and is not implemented by default (there is no blanket impl<T> TransparentWrapper<T> for T), so it makes derive(TransparentWrapper) on generic structs not very useful.


Adding a blanket impl<T> TransparentWrapper<T> for T or removing the where T: bytemuck::TransparentWrapper<T> bound from the derived impl would solve the issue.

I think adding blanket would be a breaking change, but removing the bound on the derived impl should not be.

I belive this regressed in commit 518baf9c0b73c92b4ea4406fe15e005c6d71535a (which is newer than the version of bytemuck_derive on play.rust-lang.org).

// src/lib.rs
use bytemuck::TransparentWrapper;
#[derive(bytemuck::TransparentWrapper)]
#[repr(transparent)]
pub struct Generic1<T>(pub T);

pub fn test1<T>(x: T) -> Generic1<T> { Generic1::wrap(x) }

With

# Cargo.toml
[package]
name = "bytemuck-transparent"
version = "0.1.0"
edition = "2021"

[dependencies.bytemuck]
features = ["derive"]
git = "https://github.com/Lokathor/bytemuck"
#rev = "3f2e91d3a6a496fd93b0a7b63ab68242a65d4cd5" # bytemuck_derive 1.2.1
rev = "7b67524a43aeee8c8e07ad8f4486cfe81e51591c" # last commit that works
#rev = "518baf9c0b73c92b4ea4406fe15e005c6d71535a" # regression
#rev = "5c9d81ca1a17ef2077fdbcdeb05bd14d6328062f" # bytemuck_derive 1.3.0

there is no error, but with

# Cargo.toml
[package]
name = "bytemuck-transparent"
version = "0.1.0"
edition = "2021"

[dependencies.bytemuck]
features = ["derive"]
git = "https://github.com/Lokathor/bytemuck"
#rev = "3f2e91d3a6a496fd93b0a7b63ab68242a65d4cd5" # bytemuck_derive 1.2.1
#rev = "7b67524a43aeee8c8e07ad8f4486cfe81e51591c" # last commit that works
rev = "518baf9c0b73c92b4ea4406fe15e005c6d71535a" # regression
#rev = "5c9d81ca1a17ef2077fdbcdeb05bd14d6328062f" # bytemuck_derive 1.3.0

cargo build gives

   Compiling bytemuck-transparent v0.1.0 (/home/zachary/Programming/rusttesting/bytemuck-transparent)
error[E0277]: the trait bound `T: TransparentWrapper<T>` is not satisfied
 --> src/lib.rs:6:55
  |
6 | pub fn test1<T>(x: T) -> Generic1<T> { Generic1::wrap(x) }
  |                                        -------------- ^ the trait `TransparentWrapper<T>` is not implemented for `T`
  |                                        |
  |                                        required by a bound introduced by this call
  |
note: required for `Generic1<T>` to implement `TransparentWrapper<T>`
 --> src/lib.rs:2:10
  |
2 | #[derive(bytemuck::TransparentWrapper)]
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 | #[repr(transparent)]
4 | pub struct Generic1<T>(pub T);
  |            ^^^^^^^^^^^
  = note: this error originates in the derive macro `bytemuck::TransparentWrapper` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider restricting type parameter `T`
  |
6 | pub fn test1<T: bytemuck::TransparentWrapper<T>>(x: T) -> Generic1<T> { Generic1::wrap(x) }
  |               +++++++++++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `bytemuck-transparent` due to previous error