Skip to content

Commit 67db0ea

Browse files
committed
suggestions from camelid review
1 parent 0c13a9c commit 67db0ea

File tree

4 files changed

+32
-68
lines changed

4 files changed

+32
-68
lines changed

compiler/rustc_typeck/src/coherence/inherent_impls.rs

+27-28
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,32 @@ impl InherentCollect<'tcx> {
403403
// OK
404404
}
405405
_ => {
406+
let to_implement = if assoc_items.len() == 0 {
407+
String::new()
408+
} else {
409+
let plural = assoc_items.len() > 1;
410+
let assoc_items_kind = {
411+
let item_types = assoc_items.iter().map(|x| x.kind);
412+
if item_types.clone().all(|x| x == hir::AssocItemKind::Const) {
413+
"constant"
414+
} else if item_types
415+
.clone()
416+
.all(|x| matches! {x, hir::AssocItemKind::Fn{ .. } })
417+
{
418+
"method"
419+
} else {
420+
"associated item"
421+
}
422+
};
423+
424+
format!(
425+
" to implement {} {}{}",
426+
if plural { "these" } else { "this" },
427+
assoc_items_kind,
428+
if plural { "s" } else { "" }
429+
)
430+
};
431+
406432
struct_span_err!(
407433
self.tcx.sess,
408434
span,
@@ -412,34 +438,7 @@ impl InherentCollect<'tcx> {
412438
lang,
413439
ty
414440
)
415-
.span_help(
416-
span,
417-
&format!("consider using a trait{}", {
418-
if assoc_items.len() == 0 {
419-
String::new()
420-
} else {
421-
let plural = assoc_items.len() > 1;
422-
format!(
423-
" to implement {} {}{}",
424-
if plural { "these" } else { "this" },
425-
{
426-
let item_types = assoc_items.iter().map(|x| x.kind);
427-
if item_types.clone().all(|x| x == hir::AssocItemKind::Const) {
428-
"constant"
429-
} else if item_types
430-
.clone()
431-
.all(|x| matches! {x, hir::AssocItemKind::Fn{ .. } })
432-
{
433-
"method"
434-
} else {
435-
"associated item"
436-
}
437-
},
438-
if plural { "s" } else { "" }
439-
)
440-
}
441-
}),
442-
)
441+
.help(&format!("consider using a trait{}", to_implement))
443442
.emit();
444443
}
445444
}

src/test/ui/error-codes/E0390.stderr

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ error[E0390]: only a single inherent implementation marked with `#[lang = "mut_p
44
LL | impl *mut Foo {}
55
| ^^^^^^^^^^^^^^^^
66
|
7-
help: consider using a trait
8-
--> $DIR/E0390.rs:5:1
9-
|
10-
LL | impl *mut Foo {}
11-
| ^^^^^^^^^^^^^^^^
7+
= help: consider using a trait
128

139
error: aborting due to previous error
1410

src/test/ui/kinds-of-primitive-impl.stderr

+3-28
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,7 @@ LL | | pub const B: u8 = 0;
77
LL | | }
88
| |_^
99
|
10-
help: consider using a trait to implement this constant
11-
--> $DIR/kinds-of-primitive-impl.rs:4:1
12-
|
13-
LL | / impl u8 {
14-
LL | |
15-
LL | | pub const B: u8 = 0;
16-
LL | | }
17-
| |_^
10+
= help: consider using a trait to implement this constant
1811

1912
error[E0390]: only a single inherent implementation marked with `#[lang = "str"]` is allowed for the `str` primitive
2013
--> $DIR/kinds-of-primitive-impl.rs:9:1
@@ -26,15 +19,7 @@ LL | | fn bar(self) {}
2619
LL | | }
2720
| |_^
2821
|
29-
help: consider using a trait to implement these methods
30-
--> $DIR/kinds-of-primitive-impl.rs:9:1
31-
|
32-
LL | / impl str {
33-
LL | |
34-
LL | | fn foo() {}
35-
LL | | fn bar(self) {}
36-
LL | | }
37-
| |_^
22+
= help: consider using a trait to implement these methods
3823

3924
error[E0390]: only a single inherent implementation marked with `#[lang = "char"]` is allowed for the `char` primitive
4025
--> $DIR/kinds-of-primitive-impl.rs:15:1
@@ -48,17 +33,7 @@ LL | | fn bar(self) {}
4833
LL | | }
4934
| |_^
5035
|
51-
help: consider using a trait to implement these associated items
52-
--> $DIR/kinds-of-primitive-impl.rs:15:1
53-
|
54-
LL | / impl char {
55-
LL | |
56-
LL | | pub const B: u8 = 0;
57-
LL | | pub const C: u8 = 0;
58-
LL | | fn foo() {}
59-
LL | | fn bar(self) {}
60-
LL | | }
61-
| |_^
36+
= help: consider using a trait to implement these associated items
6237

6338
error: aborting due to 3 previous errors
6439

src/test/ui/single-primitive-inherent-impl.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ LL | |
66
LL | | }
77
| |_^
88
|
9-
help: consider using a trait
10-
--> $DIR/single-primitive-inherent-impl.rs:11:1
11-
|
12-
LL | / impl str {
13-
LL | |
14-
LL | | }
15-
| |_^
9+
= help: consider using a trait
1610

1711
error: aborting due to previous error
1812

0 commit comments

Comments
 (0)