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

Only invoke decorate if the diag can eventually be emitted #122578

Merged
merged 3 commits into from
Mar 17, 2024

Conversation

jieyouxu
Copy link
Member

Lints can call trimmed_def_paths, such as through manual implementations of LintDiagnostic and calling def_path_str.

// Needed because of def_path_str
impl<'a> LintDiagnostic<'a, ()> for UnusedDef<'_, '_> {
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
diag.arg("pre", self.pre);
diag.arg("post", self.post);
diag.arg("def", self.cx.tcx.def_path_str(self.def_id));

The emission of a lint eventually relies on TyCtxt::node_lint, which has a decorate closure which is responsible for decorating the diagnostic with "lint stuff". node_lint in turn relies on lint_level. Within lint_level, decorate is eventually called just before Diag::emit is called to decorate the diagnostic. However, if -A warnings or --cap-lint=allow are set, or if the unused_must_use lint is explicitly allowed, then decorate would be called, which would call def_path_str, but the diagnostic would never be emitted and hence would trigger the must_produce_diag ICE.

To avoid calling decorate when we don't eventually emit the diagnostic, we check that:

  • if --force-warn is specified, then call decorate; otherwise
  • if we can emit warnings (or higher), then call decorate.

Fixes #121774.

@rustbot
Copy link
Collaborator

rustbot commented Mar 16, 2024

r? @fee1-dead

rustbot has assigned @fee1-dead.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 Mar 16, 2024
Comment on lines 401 to 416
// Finally, run `decorate`. This is guarded by a `can_emit_warnings()` check so that any
// `def_path_str` called within `decorate` won't trigger a `must_produce_diag` ICE if the
// `err` isn't eventually emitted (e.g. due to `-A warnings`). If an `err` is force-warn,
// it's going to be emitted anyway.
if matches!(err_level, rustc_errors::Level::ForceWarning(_))
|| sess.dcx().can_emit_warnings()
{
decorate(&mut err);
}

Copy link
Member

Choose a reason for hiding this comment

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

This isn't entirely correct. The test attached below ICEs after your PR because decorate is incorrectly skipped. can_emit_warnings is only relevant when the error level is Warning.

//@ compile-flags: -Dunused_must_use -Awarnings --crate-type=lib
//@ check-pass

#[must_use]
fn f() {}

pub fn g() {
    f();
}

Copy link
Member

Choose a reason for hiding this comment

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

Also please add a test with --force-warn as well.

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 isn't entirely correct. The test attached below ICEs after your PR because decorate is incorrectly skipped. can_emit_warnings is only relevant when the error level is Warning.

Thanks for pointing that out. I forgor to also invoke decorate for the actual Level::Error case (which lint levels Deny and Forbid correspond to).

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a test case with --force-warn.

Comment on lines 1 to 3
// Checks that compiling this file with
// `-Dunused_must_use -Awarnings --cap-lints=warn --crate-type=lib` does not ICE when emitting
// diagnostics.
Copy link
Member

Choose a reason for hiding this comment

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

Seems a bit redundant given compile-flags below. It would probably be better to write some notes explaining why there is an ICE.

Copy link
Member Author

@jieyouxu jieyouxu Mar 16, 2024

Choose a reason for hiding this comment

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

I added some notes explaining why the ICE is triggered in the tests/ui/lint/decorate-ice/decorate-def-path-str-ice.rs test instead of repeating what the compile-flags say (lol).

@rustbot rustbot 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-review Status: Awaiting review from the assignee but also interested parties. labels Mar 16, 2024
@rust-log-analyzer

This comment has been minimized.

@jieyouxu
Copy link
Member Author

I need to think about how to guard the decorate invocation a bit more.

Comment on lines 413 to 414
if matches!(err_level, ELevel::ForceWarning(_) | ELevel::Error)
|| sess.dcx().can_emit_warnings()
Copy link
Member Author

@jieyouxu jieyouxu Mar 16, 2024

Choose a reason for hiding this comment

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

This check seems to pass the ui test suite including the -Dunused_must_use case, but I'm still not very confident in this check.

Copy link
Member

Choose a reason for hiding this comment

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

I think it would be best if we instead used an if statement to consider when to skip calling decorate.

Something like

let skip = err_level == rustc_errors::Warning && !sess.dcx().can_emit_warnings();
if !skip {
    decorate(&mut err);
}

Which would have better maintainability.

@jieyouxu
Copy link
Member Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 16, 2024
Comment on lines 413 to 414
if matches!(err_level, ELevel::ForceWarning(_) | ELevel::Error)
|| sess.dcx().can_emit_warnings()
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be best if we instead used an if statement to consider when to skip calling decorate.

Something like

let skip = err_level == rustc_errors::Warning && !sess.dcx().can_emit_warnings();
if !skip {
    decorate(&mut err);
}

Which would have better maintainability.

@rustbot rustbot 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-review Status: Awaiting review from the assignee but also interested parties. labels Mar 17, 2024
@jieyouxu
Copy link
Member Author

Changed the guard for decorate to be based on when not to skip instead.

Copy link
Member

@fee1-dead fee1-dead left a comment

Choose a reason for hiding this comment

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

Thanks!

@bors r+

@fee1-dead
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Mar 17, 2024

📌 Commit bdab02c has been approved by fee1-dead

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 Mar 17, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 17, 2024
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#120640 (Mark UEFI std support as WIP)
 - rust-lang#121862 (Add release notes for 1.77.0)
 - rust-lang#122572 (add test for rust-lang#122301 to cover behavior that's on stable)
 - rust-lang#122578 (Only invoke `decorate` if the diag can eventually be emitted)
 - rust-lang#122615 (Mention Zalathar for coverage changes)
 - rust-lang#122636 (some minor code simplifications)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 8e748c0 into rust-lang:master Mar 17, 2024
11 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Mar 17, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 17, 2024
Rollup merge of rust-lang#122578 - jieyouxu:guard-decorate, r=fee1-dead

Only invoke `decorate` if the diag can eventually be emitted

Lints can call [`trimmed_def_paths`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/print/fn.trimmed_def_paths.html#), such as through manual implementations of `LintDiagnostic` and calling `def_path_str`.

https://github.com/rust-lang/rust/blob/05a2be3def211255dc7640b006ac10f0f02baf5c/compiler/rustc_lint/src/lints.rs#L1834-L1839

The emission of a lint eventually relies on [`TyCtxt::node_lint`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.node_lint), which has a `decorate` closure which is responsible for decorating the diagnostic with "lint stuff". `node_lint` in turn relies on [`lint_level`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/lint/fn.lint_level.html). Within `lint_level`, `decorate` is eventually called just before `Diag::emit` is called to decorate the diagnostic. However, if `-A warnings` or `--cap-lint=allow` are set, or if the unused_must_use lint is explicitly allowed, then `decorate` would be called, which would call `def_path_str`, but the diagnostic would never be emitted and hence would trigger the `must_produce_diag` ICE.

To avoid calling `decorate` when we don't eventually emit the diagnostic, we check that:

- if `--force-warn` is specified, then call `decorate`; otherwise
- if we can emit warnings (or higher), then call `decorate`.

Fixes rust-lang#121774.
@jieyouxu jieyouxu deleted the guard-decorate branch May 28, 2024 03:23
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: must_produce_diag: trimmed_def_paths called but no diagnostics emitted
5 participants