Only invoke decorate if the diag can eventually be emitted

This commit is contained in:
许杰友 Jieyou Xu (Joe) 2024-03-16 02:38:42 +00:00
parent a0c20d52e0
commit 772d8598d2
No known key found for this signature in database
GPG Key ID: 95DDEBD74A1DC2C0
2 changed files with 24 additions and 2 deletions

View File

@ -398,8 +398,16 @@ pub fn lint_level(
}
}
// Finally, run `decorate`.
decorate(&mut err);
// 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);
}
explain_lint_level_source(lint, level, src, &mut err);
err.emit()
}

View File

@ -0,0 +1,14 @@
// Checks that compiling this file with
// `-Dunused_must_use -Awarnings --cap-lints=warn --crate-type=lib` does not ICE when emitting
// diagnostics.
// Issue: <https://github.com/rust-lang/rust/issues/121774>.
//@ compile-flags: -Dunused_must_use -Awarnings --cap-lints=warn --crate-type=lib
//@ check-pass
#[must_use]
fn f() {}
pub fn g() {
f();
}