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

cleanup and dedupe CTFE and Miri error reporting #104317

Merged
merged 1 commit into from
Nov 16, 2022

Conversation

RalfJung
Copy link
Member

It looks like most of the time, this error raised from const_prop_lint is just redundant -- it duplicates the error reported when evaluating the const-eval query. This lets us make ConstEvalErr private to the const_eval module which I think is a good step.

The Miri change mostly replaces a match by if let, and dedupes the "this error is impossible in Miri" checks.

r? @oli-obk
Fixes #75461

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 12, 2022
@rustbot
Copy link
Collaborator

rustbot commented Nov 12, 2022

The Miri subtree was changed

cc @rust-lang/miri

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rust-log-analyzer

This comment has been minimized.

@RalfJung RalfJung force-pushed the ctfe-error-reporting branch 3 times, most recently from 5618924 to 2068b3b Compare November 12, 2022 15:26
@rustbot
Copy link
Collaborator

rustbot commented Nov 12, 2022

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Comment on lines 19 to 21
black_box((S::<i32>::FOO, S::<u32>::FOO));
//~^ ERROR erroneous constant
//~| ERROR erroneous constant
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically the reason the sometimes-duplicate error was added. Without it, it becomes unclear what caused the error

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still an error pointing at FOO. But the problem is the missing instantiation site?

This is a general problem with our const-eval errors -- they don't show where the const was used. Not sure what the best way is to add that information. After all the query is only evaluated once, without knowing the use sites. A separate lint is kind of a hack. Making this part of const_prop_lint is also a hack, this is an entirely separate concern. (Seems fine to keep it there I guess but there should be a comment explaining all this.)

@RalfJung
Copy link
Member Author

All right, attempt #2. :D

|
LL | const CONST: usize = <&Self>::CONST;
| ^^^^^^^^^^^^^^

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is strange, the build actually succeeds despite this error...

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

err.report_as_error(tcx, "erroneous constant used");
None
let res = self.use_ecx(source_info, |this| this.ecx.const_to_op(&c.literal, None));
if res.is_none() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do need the error to avoid emitting the note in case of TooGeneric and similar "nonfatal const prop failures"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Urgh but that's a terrible abstraction violation. :/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's what report_as_error was doing before. So you could add a maybe_report_erroneous_const() method to the error type and call it where we called report_as_error before

@RalfJung
Copy link
Member Author

I think what we really want is a const_eval query where we pass in a span that points to where the constant is used, so that the error message can incorporate that. Unfortunately we cannot do this on the query level, because then evaluating the same constant at different locations would re-evaluate the constant each time since an input changes.

So the 2nd best thing I can think of is some function that wraps the query. It first calls the query and then if that errors also emits a span for the usage location.

I don't quite understand how we propagate error information out of the query though... looks like all we get there is ErrorHandled. const_prop_lint currently circumvents this by calling const_to_op which does a bunch of checks before invoking the query.

So maybe const_to_op should be that wrapper function? Maybe instead of raising an InterpError it should emit a diagnostic itself, complementing the query it fired off? That would also make things more consistent between consts used in const-context vs runtime-context. It feels strange to have this wrapper be inside InterpCx though, this does not really need a full interpreter.

@rust-log-analyzer

This comment has been minimized.

@oli-obk
Copy link
Contributor

oli-obk commented Nov 15, 2022

Unfortunately we cannot do this on the query level, because then evaluating the same constant at different locations would re-evaluate the constant each time since an input changes.

We could do that by having an Option<Span> argument in the query key that we only set to Some if we are erroring. So basically invoke it once with None, causing no error to be emitted, but an error to be returned, if you get back an error, invoke again with Some(span) and now the query can error inside.

@RalfJung
Copy link
Member Author

Hm that sounds like it would make the error code more complicated again because it'd have to suppress the error the first time around. I'll leave that to later, feel free to open an issue tracking that -- it's a nice idea!

For now I think I am going with the 2nd variant I described above.

@rustbot
Copy link
Collaborator

rustbot commented Nov 15, 2022

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred in const_evaluatable.rs

cc @lcnr

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

--> $DIR/const-err-late.rs:19:16
|
LL | black_box((S::<i32>::FOO, S::<u32>::FOO));
| ^^^^^^^^^^^^^
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why errors are shown multiple times. But in production we deduplicate so users should see this only once.

@oli-obk
Copy link
Contributor

oli-obk commented Nov 15, 2022

@bors r+

@oli-obk
Copy link
Contributor

oli-obk commented Nov 15, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Nov 15, 2022

📌 Commit e7d9981eb83cdc7b6a2dc33194e016d99e7ef34b has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 15, 2022
@bors
Copy link
Contributor

bors commented Nov 15, 2022

☔ The latest upstream changes (presumably #104054) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 15, 2022
@RalfJung RalfJung force-pushed the ctfe-error-reporting branch 3 times, most recently from 8cb93db to 5787421 Compare November 16, 2022 08:05
@RalfJung
Copy link
Member Author

@bors r=oli-obk

@bors
Copy link
Contributor

bors commented Nov 16, 2022

📌 Commit 1115ec6 has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 16, 2022
@RalfJung
Copy link
Member Author

@bors rollup=maybe

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 16, 2022
…li-obk

cleanup and dedupe CTFE and Miri error reporting

It looks like most of the time, this error raised from const_prop_lint is just redundant -- it duplicates the error reported when evaluating the const-eval query. This lets us make `ConstEvalErr` private to the const_eval module which I think is a good step.

The Miri change mostly replaces a `match` by `if let`, and dedupes the "this error is impossible in Miri" checks.

r? `@oli-obk`
Fixes rust-lang#75461
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 16, 2022
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#103750 (Fix some misleading target feature aliases)
 - rust-lang#104137 (Issue error when -C link-self-contained option is used on unsupported platforms)
 - rust-lang#104317 (cleanup and dedupe CTFE and Miri error reporting)
 - rust-lang#104335 (Only do parser recovery on retried macro matching)
 - rust-lang#104394 (various cleanups to try to reduce the use of spans inside method resolution)
 - rust-lang#104459 (rustdoc: remove unused JS IIFE from main.js)
 - rust-lang#104462 (rustdoc: remove pointless CSS `.rightside { padding-right: 2px }`)
 - rust-lang#104466 (rustdoc: remove no-op CSS `#crate-search-div { display: inline-block }`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 353b915 into rust-lang:master Nov 16, 2022
@rustbot rustbot added this to the 1.67.0 milestone Nov 16, 2022
@RalfJung RalfJung deleted the ctfe-error-reporting branch November 17, 2022 08:00
write!(f, "encountered constants with type errors, stopping evaluation")
write!(
f,
"an error has already been reported elsewhere (this sould not usually be printed)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/sould/should

Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
…li-obk

cleanup and dedupe CTFE and Miri error reporting

It looks like most of the time, this error raised from const_prop_lint is just redundant -- it duplicates the error reported when evaluating the const-eval query. This lets us make `ConstEvalErr` private to the const_eval module which I think is a good step.

The Miri change mostly replaces a `match` by `if let`, and dedupes the "this error is impossible in Miri" checks.

r? ``@oli-obk``
Fixes rust-lang#75461
Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#103750 (Fix some misleading target feature aliases)
 - rust-lang#104137 (Issue error when -C link-self-contained option is used on unsupported platforms)
 - rust-lang#104317 (cleanup and dedupe CTFE and Miri error reporting)
 - rust-lang#104335 (Only do parser recovery on retried macro matching)
 - rust-lang#104394 (various cleanups to try to reduce the use of spans inside method resolution)
 - rust-lang#104459 (rustdoc: remove unused JS IIFE from main.js)
 - rust-lang#104462 (rustdoc: remove pointless CSS `.rightside { padding-right: 2px }`)
 - rust-lang#104466 (rustdoc: remove no-op CSS `#crate-search-div { display: inline-block }`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cleanup CTFE error reporting code & centralize promoted error reporting logic
7 participants