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

add test for unused ill-formed constant #1504

Merged
merged 2 commits into from
Aug 13, 2020

Conversation

RalfJung
Copy link
Member

Once rust-lang/rust#75339 lands, this test should pass.

Fixes #1382.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Aug 10, 2020
evaluate required_consts when pushing stack frame in Miri engine

[Just like codegen](https://github.com/rust-lang/rust/pull/70820/files#diff-32c57af5c8e23eb048f55d1e955e5cd5R194), Miri needs to make sure all `required_consts` evaluate successfully, to catch post-monomorphization errors.

While at it I also moved the const_eval error reporting logic into rustc_mir::const_eval::error; there is no reason it should be in `rustc_middle`. I kept this in a separate commit for easier reviewing.

Helps with rust-lang/miri#1382. I will add a test on the Miri side (done now: rust-lang/miri#1504).
r? @oli-obk
@RalfJung
Copy link
Member Author

RalfJung commented Aug 11, 2020

@oli-obk there are some strange things going on here... first of all, this is the error Miri shows:

warning: any use of this value will cause an error
 --> tests/compile-fail/erroneous_const.rs:9:21
  |
9 |     const VOID: ! = panic!(); //~ERROR any use of this value will cause an error
  |     ----------------^^^^^^^^-
  |                     |
  |                     the evaluated program panicked at 'explicit panic', tests/compile-fail/erroneous_const.rs:9:21
  |
note: the lint level is defined here
 --> tests/compile-fail/erroneous_const.rs:5:9
  |
5 | #![warn(const_err)]
  |         ^^^^^^^^^
  = note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: post-monomorphization error: referenced constant has errors
  --> tests/compile-fail/erroneous_const.rs:13:8
   |
13 |     if false {
   |        ^^^^^ referenced constant has errors
   |
   = note: inside `no_codegen::<i32>` at tests/compile-fail/erroneous_const.rs:13:8
note: inside `main` at tests/compile-fail/erroneous_const.rs:18:5
  --> tests/compile-fail/erroneous_const.rs:18:5
   |
18 |     no_codegen::<i32>();
   |     ^^^^^^^^^^^^^^^^^^^

Note the completely ridiculous span for "referenced constant has errors", it points at the false?!?

Second, even though we have a warning on line 9, compiletest insists that I need to make this an ERROR annotation. I forgot about our -Dwarnings in the compiletest runner.

@RalfJung
Copy link
Member Author

The span happens because we do this right after pushing the stack frame, when the current MIR statement to be executed is the first statement in the first block. But of course we don't want to point at that statement, we want to point at the required const that we evaluated.

@RalfJung
Copy link
Member Author

But how would we know this in generate_stacktrace? Constant comes with a span, but is this the span where the constant is used or where it is defined?

I think this is similar to why ConstProp has to explicitly pass a span to ConstEvalErr::new -- though that seems a bit strange since that should have the current statement set correctly.

@RalfJung
Copy link
Member Author

RalfJung commented Aug 11, 2020

I tried to follow our spans in const_eval/{eval_queries,error} and realized I do not have the slightest clue what is going on. There are so many spans overwriting each other and then sometimes being replaced again later and elsewhere we are pretty inconsistent with whether we use the span closest to the error or the outermost span of the DefId -- it's all just a huge mess...

For example, report_as_lint takes two spans (one via TyCtxtAt and one explicitly, though the latter is optional); it is entirely unclear what the role of these two spans is and different callers seem to use them inconsistently.

Also, even after lots of digging I could not figure out how we ever have more than one "primary span" in the MultiSpan of the const-err diagnostic, and yet the code report_as_lint clearly shows that this is what happens (or is that dead code?).

Lucky enough none of this matters for Miri error reporting^^ but still, it's quite the chaos.

@oli-obk
Copy link
Contributor

oli-obk commented Aug 11, 2020

Constant comes with a span, but is this the span where the constant is used or where it is defined?

where it is used. This is the span that should be used for linting about failed evaluation of a constant

also use better span in TopFrameInfo
@RalfJung
Copy link
Member Author

@bors r+

@bors
Copy link
Contributor

bors commented Aug 13, 2020

📌 Commit 925d607 has been approved by RalfJung

@bors
Copy link
Contributor

bors commented Aug 13, 2020

⌛ Testing commit 925d607 with merge 1bfb26d...

@bors
Copy link
Contributor

bors commented Aug 13, 2020

☀️ Test successful - checks-travis, status-appveyor
Approved by: RalfJung
Pushing 1bfb26d to master...

@bors bors merged commit 1bfb26d into rust-lang:master Aug 13, 2020
@RalfJung RalfJung deleted the ill-formed-const branch August 13, 2020 07:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Detect erroneous constants post-monomorphization
3 participants