diff --git a/compiler/rustc_middle/src/lint.rs b/compiler/rustc_middle/src/lint.rs index d8d6899f057..b83793df641 100644 --- a/compiler/rustc_middle/src/lint.rs +++ b/compiler/rustc_middle/src/lint.rs @@ -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() } diff --git a/tests/ui/lint/decorate-def-path-str-ice.rs b/tests/ui/lint/decorate-def-path-str-ice.rs new file mode 100644 index 00000000000..176f66ba1f4 --- /dev/null +++ b/tests/ui/lint/decorate-def-path-str-ice.rs @@ -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: . + +//@ compile-flags: -Dunused_must_use -Awarnings --cap-lints=warn --crate-type=lib +//@ check-pass + +#[must_use] +fn f() {} + +pub fn g() { + f(); +}