This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 7 files changed +71
-21
lines changed
rustc_error_codes/src/error_codes
rustc_typeck/src/coherence Expand file tree Collapse file tree 7 files changed +71
-21
lines changed Original file line number Diff line number Diff line change 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, an
2+ enum, a union or a trait object .
33
44Erroneous 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+ ```
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 1+ struct Foo ;
2+
3+ impl & mut Foo {
4+ //~^ ERROR E0118
5+ fn bar ( self ) { }
6+ }
7+
8+ fn main ( ) { }
Original file line number Diff line number Diff line change 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`.
Original file line number Diff line number Diff line change 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 |
44LL | 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
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 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 |
44LL | 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 |
1212LL | 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
You can’t perform that action at this time.
0 commit comments