diff --git a/compiler/rustc_typeck/src/coherence/inherent_impls.rs b/compiler/rustc_typeck/src/coherence/inherent_impls.rs index 05f387883f14c..0c1578498b846 100644 --- a/compiler/rustc_typeck/src/coherence/inherent_impls.rs +++ b/compiler/rustc_typeck/src/coherence/inherent_impls.rs @@ -403,6 +403,32 @@ impl InherentCollect<'tcx> { // OK } _ => { + let to_implement = if assoc_items.len() == 0 { + String::new() + } else { + let plural = assoc_items.len() > 1; + let assoc_items_kind = { + let item_types = assoc_items.iter().map(|x| x.kind); + if item_types.clone().all(|x| x == hir::AssocItemKind::Const) { + "constant" + } else if item_types + .clone() + .all(|x| matches! {x, hir::AssocItemKind::Fn{ .. } }) + { + "method" + } else { + "associated item" + } + }; + + format!( + " to implement {} {}{}", + if plural { "these" } else { "this" }, + assoc_items_kind, + if plural { "s" } else { "" } + ) + }; + struct_span_err!( self.tcx.sess, span, @@ -412,34 +438,7 @@ impl InherentCollect<'tcx> { lang, ty ) - .span_help( - span, - &format!("consider using a trait{}", { - if assoc_items.len() == 0 { - String::new() - } else { - let plural = assoc_items.len() > 1; - format!( - " to implement {} {}{}", - if plural { "these" } else { "this" }, - { - let item_types = assoc_items.iter().map(|x| x.kind); - if item_types.clone().all(|x| x == hir::AssocItemKind::Const) { - "constant" - } else if item_types - .clone() - .all(|x| matches! {x, hir::AssocItemKind::Fn{ .. } }) - { - "method" - } else { - "associated item" - } - }, - if plural { "s" } else { "" } - ) - } - }), - ) + .help(&format!("consider using a trait{}", to_implement)) .emit(); } } diff --git a/src/test/ui/error-codes/E0390.stderr b/src/test/ui/error-codes/E0390.stderr index 414ec20ae803d..be47e93d19a9b 100644 --- a/src/test/ui/error-codes/E0390.stderr +++ b/src/test/ui/error-codes/E0390.stderr @@ -4,11 +4,7 @@ error[E0390]: only a single inherent implementation marked with `#[lang = "mut_p LL | impl *mut Foo {} | ^^^^^^^^^^^^^^^^ | -help: consider using a trait - --> $DIR/E0390.rs:5:1 - | -LL | impl *mut Foo {} - | ^^^^^^^^^^^^^^^^ + = help: consider using a trait error: aborting due to previous error diff --git a/src/test/ui/kinds-of-primitive-impl.stderr b/src/test/ui/kinds-of-primitive-impl.stderr index 14aed00c005e6..d19c85b17f9e7 100644 --- a/src/test/ui/kinds-of-primitive-impl.stderr +++ b/src/test/ui/kinds-of-primitive-impl.stderr @@ -7,14 +7,7 @@ LL | | pub const B: u8 = 0; LL | | } | |_^ | -help: consider using a trait to implement this constant - --> $DIR/kinds-of-primitive-impl.rs:4:1 - | -LL | / impl u8 { -LL | | -LL | | pub const B: u8 = 0; -LL | | } - | |_^ + = help: consider using a trait to implement this constant error[E0390]: only a single inherent implementation marked with `#[lang = "str"]` is allowed for the `str` primitive --> $DIR/kinds-of-primitive-impl.rs:9:1 @@ -26,15 +19,7 @@ LL | | fn bar(self) {} LL | | } | |_^ | -help: consider using a trait to implement these methods - --> $DIR/kinds-of-primitive-impl.rs:9:1 - | -LL | / impl str { -LL | | -LL | | fn foo() {} -LL | | fn bar(self) {} -LL | | } - | |_^ + = help: consider using a trait to implement these methods error[E0390]: only a single inherent implementation marked with `#[lang = "char"]` is allowed for the `char` primitive --> $DIR/kinds-of-primitive-impl.rs:15:1 @@ -48,17 +33,7 @@ LL | | fn bar(self) {} LL | | } | |_^ | -help: consider using a trait to implement these associated items - --> $DIR/kinds-of-primitive-impl.rs:15:1 - | -LL | / impl char { -LL | | -LL | | pub const B: u8 = 0; -LL | | pub const C: u8 = 0; -LL | | fn foo() {} -LL | | fn bar(self) {} -LL | | } - | |_^ + = help: consider using a trait to implement these associated items error: aborting due to 3 previous errors diff --git a/src/test/ui/single-primitive-inherent-impl.stderr b/src/test/ui/single-primitive-inherent-impl.stderr index 0daae91956d32..50a0d5bef86d3 100644 --- a/src/test/ui/single-primitive-inherent-impl.stderr +++ b/src/test/ui/single-primitive-inherent-impl.stderr @@ -6,13 +6,7 @@ LL | | LL | | } | |_^ | -help: consider using a trait - --> $DIR/single-primitive-inherent-impl.rs:11:1 - | -LL | / impl str { -LL | | -LL | | } - | |_^ + = help: consider using a trait error: aborting due to previous error