rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.96k stars 12.69k forks source link

Function monomorphization site that causes post-monomorphization errors is never shown #73961

Closed oli-cosmian closed 3 years ago

oli-cosmian commented 4 years ago

The following code does not highlight the foo::<u32>() call, even though removing it will remove the error.

#[warn(const_err)]

trait ZstAssert: Sized {
    const ASSERT: () = [()][(std::mem::size_of::<Self>() != 0) as usize];
}

impl<T> ZstAssert for T {}

fn foo<T>() {
    #[allow(path_statements)]
    {
        T::ASSERT
    }
}

fn main() {
    foo::<u32>();
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
warning: any use of this value will cause an error
 --> src/main.rs:4:24
  |
4 |     const ASSERT: () = [()][(std::mem::size_of::<Self>() != 0) as usize];
  |     -------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
  |                        |
  |                        index out of bounds: the len is 1 but the index is 1
  |
note: the lint level is defined here
 --> src/main.rs:1:8
  |
1 | #[warn(const_err)]
  |        ^^^^^^^^^

error: erroneous constant encountered
  --> src/main.rs:12:9
   |
12 |         T::ASSERT
   |         ^^^^^^^^^

error: aborting due to previous error; 1 warning emitted

error: could not compile `playground`.

To learn more, run the command again with --verbose.
oli-obk commented 3 years ago

closing in favour of the more detailed #85155