Skip to content
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

Debug on packed structs causes error on non-Copy fields #117406

Closed
Super-Pizza opened this issue Oct 30, 2023 · 7 comments · Fixed by #117511
Closed

Debug on packed structs causes error on non-Copy fields #117406

Super-Pizza opened this issue Oct 30, 2023 · 7 comments · Fixed by #117511
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Super-Pizza
Copy link

Super-Pizza commented Oct 30, 2023

The following code causes a compilation error.

#[repr(packed)]
#[derive(Debug)]
struct B(String);
fn main(){}

Error

error[E0507]: cannot move out of `self` which is behind a shared reference
 --> src/main.rs:4:10
  |
3 | #[derive(Debug)]
  |          ----- in this derive macro expansion
4 | struct B(String);
  |          ^^^^^^ move occurs because `self.0` has type `String`, which does not implement the `Copy` trait
  |
  = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0507`.

Meta

rustc --version --verbose:

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2
@Super-Pizza Super-Pizza added the C-bug Category: This is a bug. label Oct 30, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 30, 2023
@Noratrieb
Copy link
Member

This is intended, it is an attempt to make derives and repr(packed) work in more cases. The derive cannot know whether the field is copy. It cannot take a reference to the field, since it's not aligned. So it tries to copy, which at least works for Copy types.

@Noratrieb Noratrieb added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) T-lang Relevant to the language team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-discussion Category: Discussion or questions that doesn't represent real issues. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. C-bug Category: This is a bug. labels Oct 30, 2023
@Super-Pizza
Copy link
Author

Should, or can this be fixed?

@Noratrieb
Copy link
Member

It cannot. repr(packed) sucks.

@Super-Pizza
Copy link
Author

Super-Pizza commented Oct 30, 2023

Atleast, we should modify the error message to make it clearer? Something like

error[E0507]: cannot move out of `self` which is behind a shared reference
 --> src/main.rs:4:10
  |
3 | #[derive(Debug)]
  |          ----- in this derive macro expansion
4 | struct B(String);
  |          ^^^^^^ move occurs because `self.0` has type `String`, which does not implement the `Copy` trait
  |
  = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
  = note: this struct is marked as `repr(packed)`, but unaligned field references are undefined behaviour.

For more information about this error, try `rustc --explain E0507`.

@Noratrieb
Copy link
Member

that's possible.

@Noratrieb Noratrieb added A-diagnostics Area: Messages for errors, warnings, and lints and removed T-lang Relevant to the language team, which will review and decide on the PR/issue. C-discussion Category: Discussion or questions that doesn't represent real issues. labels Oct 30, 2023
@gurry
Copy link
Contributor

gurry commented Oct 31, 2023

@rustbot claim

@Super-Pizza
Copy link
Author

I'll let others implement the diagnostic message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants