about summary refs log tree commit diff
diff options
context:
space:
mode:
authormejrs <59372212+mejrs@users.noreply.github.com>2025-03-29 13:48:14 +0100
committermejrs <59372212+mejrs@users.noreply.github.com>2025-04-14 00:12:37 +0200
commit10ec5cbe96b8b1ba96fbacc207d6a2f0d19ca9e2 (patch)
tree3acb27b9f50e6c4d23e7cd8671bcd33079e2e59f
parent9abaa9d4dffeb897a1dbff97d32d3b6ac190be21 (diff)
downloadrust-10ec5cbe96b8b1ba96fbacc207d6a2f0d19ca9e2.tar.gz
rust-10ec5cbe96b8b1ba96fbacc207d6a2f0d19ca9e2.zip
Raise errors on bad rustc_on_unimplemented format strings again
-rw-r--r--compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented.rs38
-rw-r--r--tests/ui/on-unimplemented/bad-annotation.rs2
-rw-r--r--tests/ui/on-unimplemented/bad-annotation.stderr14
3 files changed, 41 insertions, 13 deletions
diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented.rs
index ca51f177f96..4c4491269b7 100644
--- a/compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented.rs
+++ b/compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented.rs
@@ -18,7 +18,9 @@ use {rustc_attr_parsing as attr, rustc_hir as hir};
 use super::{ObligationCauseCode, PredicateObligation};
 use crate::error_reporting::TypeErrCtxt;
 use crate::error_reporting::traits::on_unimplemented_condition::{Condition, ConditionOptions};
-use crate::error_reporting::traits::on_unimplemented_format::{Ctx, FormatArgs, FormatString};
+use crate::error_reporting::traits::on_unimplemented_format::{
+    Ctx, FormatArgs, FormatString, FormatWarning,
+};
 use crate::errors::{
     EmptyOnClauseInOnUnimplemented, InvalidOnClauseInOnUnimplemented, NoValueInOnUnimplemented,
 };
@@ -806,8 +808,38 @@ impl<'tcx> OnUnimplementedFormatString {
             // Warnings about format specifiers, deprecated parameters, wrong parameters etc.
             // In other words we'd like to let the author know, but we can still try to format the string later
             Ok(FormatString { warnings, .. }) => {
-                for w in warnings {
-                    w.emit_warning(tcx, trait_def_id)
+                if self.is_diagnostic_namespace_variant {
+                    for w in warnings {
+                        w.emit_warning(tcx, trait_def_id)
+                    }
+                } else {
+                    for w in warnings {
+                        match w {
+                            FormatWarning::UnknownParam { argument_name, span } => {
+                                let reported = struct_span_code_err!(
+                                    tcx.dcx(),
+                                    span,
+                                    E0230,
+                                    "cannot find parameter {} on this trait",
+                                    argument_name,
+                                )
+                                .emit();
+                                result = Err(reported);
+                            }
+                            FormatWarning::PositionalArgument { span, .. } => {
+                                let reported = struct_span_code_err!(
+                                    tcx.dcx(),
+                                    span,
+                                    E0231,
+                                    "positional format arguments are not allowed here"
+                                )
+                                .emit();
+                                result = Err(reported);
+                            }
+                            FormatWarning::InvalidSpecifier { .. }
+                            | FormatWarning::FutureIncompat { .. } => {}
+                        }
+                    }
                 }
             }
             // Errors from the underlying `rustc_parse_format::Parser`
diff --git a/tests/ui/on-unimplemented/bad-annotation.rs b/tests/ui/on-unimplemented/bad-annotation.rs
index 0936b25847c..f2b97865759 100644
--- a/tests/ui/on-unimplemented/bad-annotation.rs
+++ b/tests/ui/on-unimplemented/bad-annotation.rs
@@ -20,7 +20,7 @@ trait BadAnnotation1
 {}
 
 #[rustc_on_unimplemented = "Unimplemented trait error on `{Self}` with params `<{A},{B},{C}>`"]
-//~^ WARNING there is no parameter `C` on trait `BadAnnotation2`
+//~^ ERROR cannot find parameter C on this trait
 trait BadAnnotation2<A,B>
 {}
 
diff --git a/tests/ui/on-unimplemented/bad-annotation.stderr b/tests/ui/on-unimplemented/bad-annotation.stderr
index 1fefd93aa7e..afd737dc85e 100644
--- a/tests/ui/on-unimplemented/bad-annotation.stderr
+++ b/tests/ui/on-unimplemented/bad-annotation.stderr
@@ -11,22 +11,17 @@ LL | #[rustc_on_unimplemented = "message"]
 LL | #[rustc_on_unimplemented(/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...")]
    |                         ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
-warning: there is no parameter `C` on trait `BadAnnotation2`
+error[E0230]: cannot find parameter C on this trait
   --> $DIR/bad-annotation.rs:22:90
    |
 LL | #[rustc_on_unimplemented = "Unimplemented trait error on `{Self}` with params `<{A},{B},{C}>`"]
    |                                                                                          ^
-   |
-   = help: expect either a generic argument name or `{Self}` as format argument
-   = note: `#[warn(unknown_or_malformed_diagnostic_attributes)]` on by default
 
-warning: positional format arguments are not allowed here
+error[E0231]: positional format arguments are not allowed here
   --> $DIR/bad-annotation.rs:27:90
    |
 LL | #[rustc_on_unimplemented = "Unimplemented trait error on `{Self}` with params `<{A},{B},{}>`"]
    |                                                                                          ^
-   |
-   = help: only named format arguments with the name of one of the generic types are allowed in this context
 
 error[E0232]: this attribute must have a valid value
   --> $DIR/bad-annotation.rs:32:26
@@ -82,6 +77,7 @@ LL | #[rustc_on_unimplemented(on(desugared, on(desugared, message="x")), message
    |
    = note: eg `#[rustc_on_unimplemented(message="foo")]`
 
-error: aborting due to 8 previous errors; 2 warnings emitted
+error: aborting due to 10 previous errors
 
-For more information about this error, try `rustc --explain E0232`.
+Some errors have detailed explanations: E0230, E0231, E0232.
+For more information about an error, try `rustc --explain E0230`.