Skip to content

CTFE error shown thrice #52922

Closed
Closed

Description

@RalfJung

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

Activity

  1. RalfJung commented on Jul 31, 2018

    @RalfJung
    MemberAuthor

    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
    
  2. RalfJung commented on Aug 1, 2018

    @RalfJung
    MemberAuthor

    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.

  3. RalfJung commented on Nov 9, 2018

    @RalfJung
    MemberAuthor

    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.

  4. oli-obk commented on Nov 9, 2018

    @oli-obk
    Contributor

    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.

  5. estebank commented on Jan 8, 2019

    @estebank
    Contributor

    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
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions