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

CTFE error shown thrice #52922

Closed
RalfJung opened this issue Jul 31, 2018 · 5 comments
Closed

CTFE error shown thrice #52922

RalfJung opened this issue Jul 31, 2018 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@RalfJung
Copy link
Member

This program

#![allow(const_err)]

union Foo {
    a: &'static u8,
    b: usize,
}

const BAD: usize = unsafe { Foo { a: &1 }.b * 2};
fn main() {
    let f = BAD;
}

leads to the following errors:

error[E0080]: referenced constant has errors
  --> src/main.rs:10:13
   |
8  | const BAD: usize = unsafe { Foo { a: &1 }.b * 2};
   |                             ------------------- "pointer arithmetic or comparison" needs an rfc before being allowed inside constants
9  | fn main() {
10 |     let f = BAD;
   |             ^^^

error[E0080]: could not evaluate constant
  --> src/main.rs:10:13
   |
10 |     let f = BAD;
   |             ^^^ referenced constant has errors

error[E0080]: constant evaluation error
 --> src/main.rs:8:1
  |
8 | const BAD: usize = unsafe { Foo { a: &1 }.b * 2};
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------^^
  |                             |
  |                             "pointer arithmetic or comparison" needs an rfc before being allowed inside constants

error: aborting due to 3 previous errors

Seems like overkill to say three times that the constant has errors. ;)

Cc @oli-obk

@RalfJung
Copy link
Member Author

It gets even better when having warn(const_err): Then we get if 4 times.

warning: this constant cannot be used
 --> src/main.rs:8:1
  |
8 | const BAD: usize = unsafe { Foo { a: &1 }.b * 2};
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------^^
  |                             |
  |                             "pointer arithmetic or comparison" needs an rfc before being allowed inside constants
  |
note: lint level defined here
 --> src/main.rs:1:9
  |
1 | #![warn(const_err)]
  |         ^^^^^^^^^

error[E0080]: referenced constant has errors
  --> src/main.rs:10:13
   |
8  | const BAD: usize = unsafe { Foo { a: &1 }.b * 2};
   |                             ------------------- "pointer arithmetic or comparison" needs an rfc before being allowed inside constants
9  | fn main() {
10 |     let f = BAD;
   |             ^^^

error[E0080]: could not evaluate constant
  --> src/main.rs:10:13
   |
10 |     let f = BAD;
   |             ^^^ referenced constant has errors

error[E0080]: constant evaluation error
 --> src/main.rs:8:1
  |
8 | const BAD: usize = unsafe { Foo { a: &1 }.b * 2};
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------^^
  |                             |
  |                             "pointer arithmetic or comparison" needs an rfc before being allowed inside constants

@RalfJung
Copy link
Member Author

RalfJung commented Aug 1, 2018

From conversation with @oli-obk:

The proper way to fix this is to move the errors/warnings into the query, instead of having to do this at all places where the query is made. This is both the right fix for #52849 and avoids duplicate errors.

So the const_eval query would be renamed to const_eval_raw and only used internally. There would be three new queries eval_const, eval_static, eval_promoted with different behavior wrt. showing an error or a warning, and whether to do validation of the final value.

The lint would just trigger all these queries. Thanks to caching, a warning triggered by a query is only ever shown once.

@RalfJung
Copy link
Member Author

RalfJung commented Nov 9, 2018

Thanks to @oli-obk's const-query-error PR, this is now fixed.

@oli-obk should this get a dedicated regression test? My impression is that we already have tons of tests like this.

@oli-obk
Copy link
Contributor

oli-obk commented Nov 9, 2018

Yea, there's enough things testing this. The only concern I see now is that if you allow(const_err) you have no idea anymore what's going on with the constant, rustc just tells you

error[E0080]: could not evaluate constant
  --> src/main.rs:10:13
   |
10 |     let f = BAD;
   |             ^^^ referenced constant has errors

without any further info.

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Jan 8, 2019
@estebank
Copy link
Contributor

estebank commented Jan 8, 2019

Closing as per #53821:

error: any use of this value will cause an error
 --> src/main.rs:8:1
  |
8 | const BAD: usize = unsafe { Foo { a: &1 }.b * 2};
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------^^
  |                             |
  |                             "pointer arithmetic or comparison" needs an rfc before being allowed inside constants
  |
  = note: #[deny(const_err)] on by default

@estebank estebank closed this as completed Jan 8, 2019
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
Projects
None yet
Development

No branches or pull requests

3 participants