about summary refs log tree commit diff
diff options
context:
space:
mode:
author许杰友 Jieyou Xu (Joe) <jieyouxu@outlook.com>2024-03-16 02:38:42 +0000
committer许杰友 Jieyou Xu (Joe) <jieyouxu@outlook.com>2024-03-17 14:41:36 +0000
commit772d8598d2a2d1b27f090dc2cbaf7f950b9ad4be (patch)
treea45b7e867769be61910e659f0b047198ad83712e
parenta0c20d52e0e83f0bdd5c4f24295def8b276de314 (diff)
downloadrust-772d8598d2a2d1b27f090dc2cbaf7f950b9ad4be.tar.gz
rust-772d8598d2a2d1b27f090dc2cbaf7f950b9ad4be.zip
Only invoke `decorate` if the diag can eventually be emitted
-rw-r--r--compiler/rustc_middle/src/lint.rs12
-rw-r--r--tests/ui/lint/decorate-def-path-str-ice.rs14
2 files changed, 24 insertions, 2 deletions
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: <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();
+}