Skip to content

Commit 95386b6

Browse files
committed
Auto merge of #76028 - aticu:improve_e0118, r=estebank,jyn514,GuillaumeGomez
Improve E0118 - Changes the "base type" terminology to "nominal type" (according to the [reference](https://doc.rust-lang.org/stable/reference/items/implementations.html#inherent-implementations)). - Suggests removing a reference, if one is present on the type. - Clarify what is meant by a "nominal type". closes #69392 This is my first not-entirely-trivial PR, so please let me know if I missed anything or if something could be improved. Though I probably won't be able to fix anything in the upcoming week.
2 parents 285fc7d + 81161be commit 95386b6

File tree

7 files changed

+71
-21
lines changed

7 files changed

+71
-21
lines changed

compiler/rustc_error_codes/src/error_codes/E0118.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
An inherent implementation was defined for something which isn't a struct nor
2-
an enum.
1+
An inherent implementation was defined for something which isn't a struct,
2+
enum, union, or trait object.
33

44
Erroneous code example:
55

66
```compile_fail,E0118
7-
impl (u8, u8) { // error: no base type found for inherent implementation
7+
impl (u8, u8) { // error: no nominal type found for inherent implementation
88
fn get_state(&self) -> String {
99
// ...
1010
}
@@ -41,3 +41,24 @@ impl TypeWrapper {
4141
}
4242
}
4343
```
44+
45+
Instead of defining an inherent implementation on a reference, you could also
46+
move the reference inside the implementation:
47+
48+
```compile_fail,E0118
49+
struct Foo;
50+
51+
impl &Foo { // error: no nominal type found for inherent implementation
52+
fn bar(self, other: Self) {}
53+
}
54+
```
55+
56+
becomes
57+
58+
```
59+
struct Foo;
60+
61+
impl Foo {
62+
fn bar(&self, other: &Self) {}
63+
}
64+
```

compiler/rustc_typeck/src/coherence/inherent_impls.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -308,18 +308,25 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
308308
}
309309
ty::Error(_) => {}
310310
_ => {
311-
struct_span_err!(
311+
let mut err = struct_span_err!(
312312
self.tcx.sess,
313313
ty.span,
314314
E0118,
315-
"no base type found for inherent implementation"
316-
)
317-
.span_label(ty.span, "impl requires a base type")
318-
.note(
319-
"either implement a trait on it or create a newtype \
320-
to wrap it instead",
321-
)
322-
.emit();
315+
"no nominal type found for inherent implementation"
316+
);
317+
318+
err.span_label(ty.span, "impl requires a nominal type")
319+
.note("either implement a trait on it or create a newtype to wrap it instead");
320+
321+
if let ty::Ref(_, subty, _) = self_ty.kind() {
322+
err.note(&format!(
323+
"you could also try moving the reference to \
324+
uses of `{}` (such as `self`) within the implementation",
325+
subty
326+
));
327+
}
328+
329+
err.emit();
323330
}
324331
}
325332
}

src/test/ui/error-codes/E0118-2.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struct Foo;
2+
3+
impl &mut Foo {
4+
//~^ ERROR E0118
5+
fn bar(self) {}
6+
}
7+
8+
fn main() {}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0118]: no nominal type found for inherent implementation
2+
--> $DIR/E0118-2.rs:3:6
3+
|
4+
LL | impl &mut Foo {
5+
| ^^^^^^^^ impl requires a nominal type
6+
|
7+
= note: either implement a trait on it or create a newtype to wrap it instead
8+
= note: you could also try moving the reference to uses of `Foo` (such as `self`) within the implementation
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0118`.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0118]: no base type found for inherent implementation
1+
error[E0118]: no nominal type found for inherent implementation
22
--> $DIR/E0118.rs:1:6
33
|
44
LL | impl (u8, u8) {
5-
| ^^^^^^^^ impl requires a base type
5+
| ^^^^^^^^ impl requires a nominal type
66
|
77
= note: either implement a trait on it or create a newtype to wrap it instead
88

src/test/ui/privacy/private-in-public-ill-formed.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ mod aliases_pub {
1111
type AssocAlias = m::Pub3;
1212
}
1313

14-
impl <Priv as PrivTr>::AssocAlias { //~ ERROR no base type found for inherent implementation
14+
impl <Priv as PrivTr>::AssocAlias {
15+
//~^ ERROR no nominal type found for inherent implementation
1516
pub fn f(arg: Priv) {} // private type `aliases_pub::Priv` in public interface
1617
}
1718
}
@@ -27,7 +28,8 @@ mod aliases_priv {
2728
type AssocAlias = Priv3;
2829
}
2930

30-
impl <Priv as PrivTr>::AssocAlias { //~ ERROR no base type found for inherent implementation
31+
impl <Priv as PrivTr>::AssocAlias {
32+
//~^ ERROR no nominal type found for inherent implementation
3133
pub fn f(arg: Priv) {} // OK
3234
}
3335
}

src/test/ui/privacy/private-in-public-ill-formed.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error[E0118]: no base type found for inherent implementation
1+
error[E0118]: no nominal type found for inherent implementation
22
--> $DIR/private-in-public-ill-formed.rs:14:10
33
|
44
LL | impl <Priv as PrivTr>::AssocAlias {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a base type
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type
66
|
77
= note: either implement a trait on it or create a newtype to wrap it instead
88

9-
error[E0118]: no base type found for inherent implementation
10-
--> $DIR/private-in-public-ill-formed.rs:30:10
9+
error[E0118]: no nominal type found for inherent implementation
10+
--> $DIR/private-in-public-ill-formed.rs:31:10
1111
|
1212
LL | impl <Priv as PrivTr>::AssocAlias {
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a base type
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type
1414
|
1515
= note: either implement a trait on it or create a newtype to wrap it instead
1616

0 commit comments

Comments
 (0)