Skip to content

Missing diagnostic for non-sized type of ptr::null_mut #61870

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

Open
tesuji opened this issue Jun 15, 2019 · 6 comments
Open

Missing diagnostic for non-sized type of ptr::null_mut #61870

tesuji opened this issue Jun 15, 2019 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tesuji
Copy link
Contributor

tesuji commented Jun 15, 2019

I tried this code:

trait Trait {}

// Make sure casts between thin-pointer <-> fat pointer obey RFC401
fn main() {
    let a: *mut (dyn Trait + 'static) = 0 as *mut _; //~ ERROR casting
    let x: *mut (dyn Trait + 'static) = std::ptr::null_mut();
}

When I ran this on rustc 1.32.0, I got this error message:

error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
 --> src/lib.rs:6:41
  |
6 |     let x: *mut (dyn Trait + 'static) = std::ptr::null_mut();
  |                                         ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `(dyn Trait + 'static)`
  = note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
  = note: required by `std::ptr::null_mut`

error[E0606]: casting `usize` as `*mut (dyn Trait + 'static)` is invalid
 --> src/lib.rs:5:41
  |
5 |     let a: *mut (dyn Trait + 'static) = 0 as *mut _; //~ ERROR casting
  |                                         ^^^^^^^^^^^

error: aborting due to 2 previous errors

Some errors occurred: E0277, E0606.
For more information about an error, try `rustc --explain E0277`.

But when I ran this on rustc nightly, I got this error message:

error[E0606]: casting `usize` as `*mut (dyn Trait + 'static)` is invalid
 --> src/lib.rs:5:41
  |
5 |     let a: *mut (dyn Trait + 'static) = 0 as *mut _; //~ ERROR casting
  |                                         ^^^^^^^^^^^

error: aborting due to previous error

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

Meta

% rustc +1.32.0 -Vv 
rustc 1.32.0 (9fda7c223 2019-01-16)
binary: rustc
commit-hash: 9fda7c2237db910e41d6a712e9a2139b352e558b
commit-date: 2019-01-16
host: x86_64-unknown-linux-gnu
release: 1.32.0
LLVM version: 8.0
% rustc +nightly -Vv
rustc 1.37.0-nightly (0e4a56b4b 2019-06-13)
binary: rustc
commit-hash: 0e4a56b4b04ea98bb16caada30cb2418dd06e250
commit-date: 2019-06-13
host: x86_64-unknown-linux-gnu
release: 1.37.0-nightly
LLVM version: 8.0
@tesuji

This comment has been minimized.

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 15, 2019
@rustbot rustbot added the E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc label Jun 17, 2019
@scottmcm
Copy link
Member

Note that if one comments out the let a line, there's an error about x:

error[E0282]: type annotations needed
 --> src/main.rs:5:41
  |
  |     let x: *mut (dyn Trait + 'static) = std::ptr::null_mut();
  |                                         ^^^^^^^^^^^^^^^^^^ cannot infer type for `T`

@tesuji
Copy link
Contributor Author

tesuji commented Jun 18, 2019

Godbolt link for that

@shepmaster shepmaster changed the title Missing diagnotisc for non-sized type of ptr::null_mut Missing diagnostic for non-sized type of ptr::null_mut Jun 26, 2019
@Skepfyr
Copy link
Contributor

Skepfyr commented Aug 29, 2019

Bisected to find regression in nightly-2018-12-21

@steffahn
Copy link
Member

@rustbot modify labels to -E-needs-bisection.

@rustbot rustbot removed the E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc label Mar 31, 2020
@estebank
Copy link
Contributor

Current output:

error[E0271]: type mismatch resolving `<dyn Trait as Pointee>::Metadata == ()`
   --> f71.rs:6:41
    |
6   |     let x: *mut (dyn Trait + 'static) = std::ptr::null_mut();
    |                                         ^^^^^^^^^^^^^^^^^^ expected `()`, found `DynMetadata<dyn Trait>`
    |
    = note: expected unit type `()`
                  found struct `DynMetadata<(dyn Trait + 'static)>`
    = note: required for `(dyn Trait + 'static)` to implement `Thin`
note: required by a bound in `null_mut`
   --> /home/gh-estebank/rust/library/core/src/ptr/mod.rs:543:35
    |
543 | pub const fn null_mut<T: ?Sized + Thin>() -> *mut T {
    |                                   ^^^^ required by this bound in `null_mut`

error[E0606]: cannot cast `usize` to a pointer that is wide
 --> f71.rs:5:46
  |
5 |     let a: *mut (dyn Trait + 'static) = 0 as *mut _; //~ ERROR casting
  |                                         -    ^^^^^^ creating a `*mut (dyn Trait + 'static)` requires both an address and a vtable
  |                                         |
  |                                         consider casting this expression to `*const ()`, then using `core::ptr::from_raw_parts`

Hiding T: Sized errors is somewhat intended, as they sometimes are knock down effect of other errors so we try to hide them if we infer they are "spurious".

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 C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants