rust-gpu icon indicating copy to clipboard operation
rust-gpu copied to clipboard

Warn when something derives `Debug`

Open SiebenCorgie opened this issue 4 years ago • 4 comments

When a struct derives the Debug trait, compilation fails with a strange error. For instance this struct

#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct GpuCamera2d {
    pub location: [f32; 2],
    pub extent: [f32; 2],
    pub pad1: [f32; 3],
    pub rotation: f32,
}

Fails with the error:

     Compiling shared v0.1.0 (/../../shared)
  error: Cannot cast between pointer types
     --> /.../.../nightly-2021-08-27-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/array/mod.rs:219:9
      |
  219 |         Index::index(self as &[T], index)
      |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |
      = note: from: *[f32; 2]
      = note: to: *[f32]

  error: Cannot cast between pointer types
     --> /.../.../nightly-2021-08-27-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/array/mod.rs:219:9
      |
  219 |         Index::index(self as &[T], index)
      |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |
      = note: from: *[f32; 3]
      = note: to: *[f32]

  error: could not compile `shared` due to 2 previous errors

While the error is probably expected, I still searched for a while until I found the error. It would probably be nice to have an error like "Debug not supported".

SiebenCorgie avatar Sep 12 '21 18:09 SiebenCorgie

Unfortunately, this is a really difficult problem to solve (... as is everything else that we haven't fixed yet :stuck_out_tongue:). The issue is that rust-gpu never sees anything like #[derive(Debug)], but instead a heap of rust code that looks exactly like the user wrote it, with no way to tell from what macro it was generated from, or even if it's generated at all. All it sees is some super wacky code that definitely won't work on the GPU, and so the only thing it can do is error on the bad code. Because of this, unfortunately, this issue will likely not be fixed any time soon.

This issue is mentioned in #744.

khyperia avatar Sep 13 '21 07:09 khyperia

You could warn on any std::fmt usage, since that's what the Debug code generates and I don't think anything else in that module will work currently anyway.

XAMPPRocky avatar Sep 13 '21 08:09 XAMPPRocky

Unfortunately it's not that simple. We need to consider all sorts of edge cases - for example, saying "you can't use #[derive(Debug)]" when directly referencing something in std::fmt is incorrect, confusing, and misleading.

khyperia avatar Sep 13 '21 08:09 khyperia

We need to consider all sorts of edge cases - for example, saying "you can't use #[derive(Debug)]" when directly referencing something in std::fmt is incorrect, confusing, and misleading.

Right, that's how you phrase the warning, if the detection algorithm is coarse, so is the error message. FWIW I think something like the following would be enough, but you could be smarter about the message with a blocklist of all the std::fmt items to say which specific item is being used.

warning: Usage of std::fmt. This module will not compile on GPU platforms, remove or add #[cfg(not(target_arch="spirv")] to any std::fmt item, such as Debug, Display, format!, or write!.

XAMPPRocky avatar Sep 13 '21 08:09 XAMPPRocky

Closing this as a won't fix for now. I'll keep it in #744 as a reference.

oisyn avatar Nov 16 '22 15:11 oisyn