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

Do not assert in op_to_const. #117441

Merged
merged 6 commits into from
Nov 2, 2023
Merged

Do not assert in op_to_const. #117441

merged 6 commits into from
Nov 2, 2023

Conversation

cjgillot
Copy link
Contributor

op_to_const is used in try_destructure_mir_constant_for_diagnostics, which may encounter invalid constants created by optimizations and debugging.

r? @oli-obk

Fixes #117368

@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 Oct 31, 2023
@oli-obk
Copy link
Contributor

oli-obk commented Oct 31, 2023

Hmm... While you're at it, please also rename the hook to _for_user_output and adjust the documentation to explain that it is also used for mir dumps.

Can you write a mir-opt test demonstrating what is being rendered instead of the ui test? Or does it not actually end up showing up?

@rustbot
Copy link
Collaborator

rustbot commented Oct 31, 2023

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy


bb3: {
- _1 = move ((_2 as Some).0: std::alloc::Layout);
+ _1 = const Layout {{ size: Indirect { alloc_id: ALLOC0, offset: Size(4 bytes) }: usize, align: std::ptr::Alignment(Scalar(0x00000000): ptr::alignment::AlignmentEnum32) }};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@oli-obk this is what we were ICEing on. This is a fancy way of having an uninit byte range in a MIR constant. This bb is unreachable, so this does not matter if we output such degenerate constant.

@oli-obk
Copy link
Contributor

oli-obk commented Oct 31, 2023

@bors r+ rollup

Thanks! This helps me a lot

@bors
Copy link
Contributor

bors commented Oct 31, 2023

📌 Commit 6ca7bfd 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. labels Oct 31, 2023
@bors
Copy link
Contributor

bors commented Nov 1, 2023

☔ The latest upstream changes (presumably #117459) 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 1, 2023
@oli-obk
Copy link
Contributor

oli-obk commented Nov 1, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Nov 1, 2023

📌 Commit ef196ae 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 1, 2023
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Nov 1, 2023
Do not assert in op_to_const.

`op_to_const` is used in `try_destructure_mir_constant_for_diagnostics`, which may encounter invalid constants created by optimizations and debugging.

r? `@oli-obk`

Fixes rust-lang#117368
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 1, 2023
…llaumeGomez

Rollup of 3 pull requests

Successful merges:

 - rust-lang#117373 (Avoid the path trimming ICE lint in error reporting)
 - rust-lang#117441 (Do not assert in op_to_const.)
 - rust-lang#117488 (Update minifier-rs version to 0.3.0)

r? `@ghost`
`@rustbot` modify labels: rollup
@compiler-errors
Copy link
Member

@bors r-

This test failed in a rollup (#117490 (comment))

@GuillaumeGomez: This is the PR that failed, not #117373.

@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 1, 2023
@GuillaumeGomez
Copy link
Member

Ah I see. Thanks for the clarification. :)

@cjgillot
Copy link
Contributor Author

cjgillot commented Nov 1, 2023

@bors r=oli-obk

@bors
Copy link
Contributor

bors commented Nov 1, 2023

📌 Commit 0f8f77f 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 1, 2023
@RalfJung
Copy link
Member

RalfJung commented Nov 1, 2023

op_to_const is used in try_destructure_mir_constant_for_diagnostics, which may encounter invalid constants created by optimizations and debugging.

Maybe it shouldn't? I don't like this kind of silent fallback. We should never "catch" interpreter errors on any code path that is also used for regular consts.

@bors r-

@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 1, 2023
@@ -132,8 +132,8 @@ pub(super) fn op_to_const<'tcx>(
// functionality.)
_ => false,
};
let immediate = if force_as_immediate {
Right(ecx.read_immediate(op).expect("normalization works on validated constants"))
let immediate = if force_as_immediate && let Ok(imm) = ecx.read_immediate(op) {
Copy link
Member

Choose a reason for hiding this comment

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

This is "catching" an interpreter error, and this codepath is used for regular consts, not just for diagnostics. That's fragile, I don't think we should do this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IIUC, this code path is mostly an optimization. Nothing would break if we were to set force_as_immediate as false: we do not need to represent scalars as a ConstValue::Scalar, it's just more efficient.

Copy link
Member

@RalfJung RalfJung Nov 1, 2023

Choose a reason for hiding this comment

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

That's wrong; many parts of the compiler rely on try_to_scalar to work for integer types, and will ICE if we don't use the "immediate" code path here.

Copy link
Member

Choose a reason for hiding this comment

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

There's a comment just a few lines above explaining that:

    // All scalar types should be stored as `ConstValue::Scalar`. This is needed to make
    // `ConstValue::try_to_scalar` efficient; we want that to work for *all* constants of scalar
    // type (it's used throughout the compiler and having it work just on literals is not enough)
    // and we want it to be fast (i.e., don't go to an `Allocation` and reconstruct the `Scalar`
    // from its byte-serialized form).

Please suggest how we could word that more clearly to avoid such misunderstandings in the future.

@@ -132,8 +133,14 @@ pub(super) fn op_to_const<'tcx>(
// functionality.)
_ => false,
};
let immediate = if force_as_immediate && let Ok(imm) = ecx.read_immediate(op) {
Right(imm)
let immediate = if force_as_immediate {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder, would it work to just say if !force_as_immediate && force_as_immediate here? Then we could avoid "catching" entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried it. This would degrade diagnostics significantly, as we wont be able to output an Option<u32> as a Some(99_usize), just Some(Indirect { stuff }), which IMO is much harder to use.

Copy link
Member

@RalfJung RalfJung Nov 1, 2023

Choose a reason for hiding this comment

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

This could be alleviated by equipping the diagnostic code with support for printing integers that are Indirect. Not sure if that's worth it though.

@@ -110,6 +110,7 @@ pub(crate) fn mk_eval_cx<'mir, 'tcx>(
pub(super) fn op_to_const<'tcx>(
Copy link
Member

Choose a reason for hiding this comment

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

Please extend the doc comment (sadly github doesn't let me put a suggestion there...)

/// The `for_diagnostics` flag turns the usual rules for returning `ConstValue::Scalar` into a best-effort
/// attempt. This is not okay for use in const-eval sine it breaks invariants rustc relies on, but
/// it is okay for diagnostics which will just give up gracefully when they encounter an `Indirect` they
/// cannot handle.

@RalfJung
Copy link
Member

RalfJung commented Nov 1, 2023

Thanks!

@bors r=oli-obk,RalfJung

@bors
Copy link
Contributor

bors commented Nov 1, 2023

📌 Commit f512f91 has been approved by oli-obk,RalfJung

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 1, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 1, 2023
…RalfJung

Do not assert in op_to_const.

`op_to_const` is used in `try_destructure_mir_constant_for_diagnostics`, which may encounter invalid constants created by optimizations and debugging.

r? `@oli-obk`

Fixes rust-lang#117368
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 1, 2023
…iaskrgr

Rollup of 4 pull requests

Successful merges:

 - rust-lang#117298 (Recover from missing param list in function definitions)
 - rust-lang#117373 (Avoid the path trimming ICE lint in error reporting)
 - rust-lang#117441 (Do not assert in op_to_const.)
 - rust-lang#117488 (Update minifier-rs version to 0.3.0)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 4e437be into rust-lang:master Nov 2, 2023
11 checks passed
@rustbot rustbot added this to the 1.75.0 milestone Nov 2, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Nov 2, 2023
Rollup merge of rust-lang#117441 - cjgillot:diag-noassert, r=oli-obk,RalfJung

Do not assert in op_to_const.

`op_to_const` is used in `try_destructure_mir_constant_for_diagnostics`, which may encounter invalid constants created by optimizations and debugging.

r? ``@oli-obk``

Fixes rust-lang#117368
bors-ferrocene bot added a commit to ferrocene/ferrocene that referenced this pull request Nov 2, 2023
80: Automated pull from upstream `master` r=tshepang a=github-actions[bot]


This PR pulls the following changes from the upstream repository:

* rust-lang/rust#117498
  * rust-lang/rust#117488
  * rust-lang/rust#117441
  * rust-lang/rust#117373
  * rust-lang/rust#117298
* rust-lang/rust#117029
* rust-lang/rust#117289
* rust-lang/rust#117307
* rust-lang/rust#114208
* rust-lang/rust#117482
  * rust-lang/rust#117475
  * rust-lang/rust#117401
  * rust-lang/rust#117397
  * rust-lang/rust#115626
* rust-lang/rust#117436
* rust-lang/rust#115356
* rust-lang/rust#117422
* rust-lang/rust#116692



Co-authored-by: David CARLIER <[email protected]>
Co-authored-by: Taiki Endo <[email protected]>
Co-authored-by: ltdk <[email protected]>
Co-authored-by: Ryan Mehri <[email protected]>
@cjgillot cjgillot deleted the diag-noassert branch November 2, 2023 09:43
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.

ICE: normalization works on validated constants: InterpErrorInfo(InterpErrorInfoInner..
7 participants