about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEthan Brierley <ethanboxx@gmail.com>2020-12-06 21:30:30 +0000
committerEthan Brierley <ethanboxx@gmail.com>2020-12-06 21:30:30 +0000
commit67db0ea4a7a58abc3687abf8fe55073af3ceff9e (patch)
treec15552a5879db92067630b026242a9ac7ed9e0a5
parent0c13a9c020b1a70ad3d33b4de706567d46955e5d (diff)
downloadrust-67db0ea4a7a58abc3687abf8fe55073af3ceff9e.tar.gz
rust-67db0ea4a7a58abc3687abf8fe55073af3ceff9e.zip
suggestions from camelid review
-rw-r--r--compiler/rustc_typeck/src/coherence/inherent_impls.rs55
-rw-r--r--src/test/ui/error-codes/E0390.stderr6
-rw-r--r--src/test/ui/kinds-of-primitive-impl.stderr31
-rw-r--r--src/test/ui/single-primitive-inherent-impl.stderr8
4 files changed, 32 insertions, 68 deletions
diff --git a/compiler/rustc_typeck/src/coherence/inherent_impls.rs b/compiler/rustc_typeck/src/coherence/inherent_impls.rs
index 05f387883f1..0c1578498b8 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 414ec20ae80..be47e93d19a 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 14aed00c005..d19c85b17f9 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 0daae91956d..50a0d5bef86 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