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

Insufficient diagnostic info when casting usize to fat pointer #92125

Closed
Noratrieb opened this issue Dec 20, 2021 · 3 comments · Fixed by #92150
Closed

Insufficient diagnostic info when casting usize to fat pointer #92125

Noratrieb opened this issue Dec 20, 2021 · 3 comments · Fixed by #92150
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Noratrieb
Copy link
Member

Given the following code: playground

fn main() {
    maybe_sized_t::<()>();
}

fn maybe_sized_t<T: ?Sized>() {
    let _ptr = 0 as *mut T; 
}

The current output is:

error[E0606]: casting `usize` as `*mut T` is invalid
 --> src/main.rs:6:16
  |
6 |     let _ptr = 0 as *mut T; 
  |                ^^^^^^^^^^^

For more information about this error, try `rustc --explain E0606`.
error: could not compile `playground` due to previous error
error[E0606]: casting `usize` as `*mut T` is invalid
 --> src/main.rs:6:16
  |
6 |     let _ptr = 0 as *mut T; 
  |                ^^^^^^^^^^^

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

note: `*mut T` is a fat pointer, and can therefore not be built from a `usize`

Casting a usize to a regular pointer is possible, so it's a bit confusing at first that this doesn't work for fat pointers, especially if it's not obvious that *mut T is a fat pointer here.

@Noratrieb Noratrieb added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 20, 2021
@compiler-errors
Copy link
Member

Perhaps it should say "*mut T may be a fat pointer", since it's ?Sized (and so it could either be 1xusize or 2xusize depending on the choice of T).

Another possibly useful thing to point out for the programmer: tracking down the definition of the pointee type, and if it's a generic parameter, pointing to the ?Sized bound.

@BGR360
Copy link
Contributor

BGR360 commented Dec 21, 2021

@rustbot label +D-terse +D-confusing +A-coercions

@rustbot rustbot added A-coercions Area: implicit and explicit `expr as Type` coercions D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Dec 21, 2021
@bors bors closed this as completed in 7473750 Mar 10, 2022
@Noratrieb
Copy link
Member Author

Nice, thank you for your work @compiler-errors!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. 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