Skip to content

Commit ec2c213

Browse files
committed
Raise errors on bad rustc_on_unimplemented format strings again
1 parent 6cc3ceb commit ec2c213

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented.rs

+35-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use {rustc_attr_parsing as attr, rustc_hir as hir};
1818
use super::{ObligationCauseCode, PredicateObligation};
1919
use crate::error_reporting::TypeErrCtxt;
2020
use crate::error_reporting::traits::on_unimplemented_condition::{Condition, ConditionOptions};
21-
use crate::error_reporting::traits::on_unimplemented_format::{Ctx, FormatArgs, FormatString};
21+
use crate::error_reporting::traits::on_unimplemented_format::{
22+
Ctx, FormatArgs, FormatString, FormatWarning,
23+
};
2224
use crate::errors::{
2325
EmptyOnClauseInOnUnimplemented, InvalidOnClauseInOnUnimplemented, NoValueInOnUnimplemented,
2426
};
@@ -806,8 +808,38 @@ impl<'tcx> OnUnimplementedFormatString {
806808
// Warnings about format specifiers, deprecated parameters, wrong parameters etc.
807809
// In other words we'd like to let the author know, but we can still try to format the string later
808810
Ok(FormatString { warnings, .. }) => {
809-
for w in warnings {
810-
w.emit_warning(tcx, trait_def_id)
811+
if self.is_diagnostic_namespace_variant {
812+
for w in warnings {
813+
w.emit_warning(tcx, trait_def_id)
814+
}
815+
} else {
816+
for w in warnings {
817+
match w {
818+
FormatWarning::UnknownParam { argument_name, span } => {
819+
let reported = struct_span_code_err!(
820+
tcx.dcx(),
821+
span,
822+
E0230,
823+
"cannot find parameter {} on this trait",
824+
argument_name,
825+
)
826+
.emit();
827+
result = Err(reported);
828+
}
829+
FormatWarning::PositionalArgument { span, .. } => {
830+
let reported = struct_span_code_err!(
831+
tcx.dcx(),
832+
span,
833+
E0231,
834+
"positional format arguments are not allowed here"
835+
)
836+
.emit();
837+
result = Err(reported);
838+
}
839+
FormatWarning::InvalidSpecifier { .. }
840+
| FormatWarning::FutureIncompat { .. } => {}
841+
}
842+
}
811843
}
812844
}
813845
// Errors from the underlying `rustc_parse_format::Parser`

tests/ui/on-unimplemented/bad-annotation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ trait BadAnnotation1
2020
{}
2121

2222
#[rustc_on_unimplemented = "Unimplemented trait error on `{Self}` with params `<{A},{B},{C}>`"]
23-
//~^ WARNING there is no parameter `C` on trait `BadAnnotation2`
23+
//~^ ERROR cannot find parameter C on this trait
2424
trait BadAnnotation2<A,B>
2525
{}
2626

tests/ui/on-unimplemented/bad-annotation.stderr

+5-9
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,17 @@ LL | #[rustc_on_unimplemented = "message"]
1111
LL | #[rustc_on_unimplemented(/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...")]
1212
| ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1313

14-
warning: there is no parameter `C` on trait `BadAnnotation2`
14+
error[E0230]: cannot find parameter C on this trait
1515
--> $DIR/bad-annotation.rs:22:90
1616
|
1717
LL | #[rustc_on_unimplemented = "Unimplemented trait error on `{Self}` with params `<{A},{B},{C}>`"]
1818
| ^
19-
|
20-
= help: expect either a generic argument name or `{Self}` as format argument
21-
= note: `#[warn(unknown_or_malformed_diagnostic_attributes)]` on by default
2219

23-
warning: positional format arguments are not allowed here
20+
error[E0231]: positional format arguments are not allowed here
2421
--> $DIR/bad-annotation.rs:27:90
2522
|
2623
LL | #[rustc_on_unimplemented = "Unimplemented trait error on `{Self}` with params `<{A},{B},{}>`"]
2724
| ^
28-
|
29-
= help: only named format arguments with the name of one of the generic types are allowed in this context
3025

3126
error[E0232]: this attribute must have a valid value
3227
--> $DIR/bad-annotation.rs:32:26
@@ -82,6 +77,7 @@ LL | #[rustc_on_unimplemented(on(desugared, on(desugared, message="x")), message
8277
|
8378
= note: eg `#[rustc_on_unimplemented(message="foo")]`
8479

85-
error: aborting due to 8 previous errors; 2 warnings emitted
80+
error: aborting due to 10 previous errors
8681

87-
For more information about this error, try `rustc --explain E0232`.
82+
Some errors have detailed explanations: E0230, E0231, E0232.
83+
For more information about an error, try `rustc --explain E0230`.

0 commit comments

Comments
 (0)