Skip to content

Commit db63a55

Browse files
Fix E0118
1 parent 2849ca6 commit db63a55

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

src/librustc_typeck/coherence/orphan.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,12 @@ impl<'cx, 'tcx> OrphanChecker<'cx, 'tcx> {
209209
return;
210210
}
211211
_ => {
212-
span_err!(self.tcx.sess, item.span, E0118,
213-
"no base type found for inherent implementation; \
214-
implement a trait or new type instead");
212+
struct_span_err!(self.tcx.sess, item.span, E0118,
213+
"no base type found for inherent implementation")
214+
.span_help(item.span,
215+
"either implement a trait on it or create a newtype to wrap it \
216+
instead")
217+
.emit();
215218
return;
216219
}
217220
}

src/librustc_typeck/diagnostics.rs

+33-9
Original file line numberDiff line numberDiff line change
@@ -1489,22 +1489,46 @@ For information on the design of the orphan rules, see [RFC 1023].
14891489
"##,
14901490

14911491
E0118: r##"
1492-
Rust can't find a base type for an implementation you are providing, or the type
1493-
cannot have an implementation. For example, only a named type or a trait can
1494-
have an implementation:
1492+
You're trying to write an inherent implementation for something which isn't a
1493+
struct nor an enum. Erroneous code example:
14951494
14961495
```
1497-
type NineString = [char, ..9] // This isn't a named type (struct, enum or trait)
1498-
impl NineString {
1499-
// Some code here
1496+
impl (u8, u8) { // error: no base type found for inherent implementation
1497+
fn get_state(&self) -> String {
1498+
// ...
1499+
}
1500+
}
1501+
```
1502+
1503+
To fix this error, please implement a trait on the type or wrap it in a struct.
1504+
Example:
1505+
1506+
```
1507+
// we create a trait here
1508+
trait LiveLongAndProsper {
1509+
fn get_state(&self) -> String;
1510+
}
1511+
1512+
// and now you can implement it on (u8, u8)
1513+
impl LiveLongAndProsper for (u8, u8) {
1514+
fn get_state(&self) -> String {
1515+
"He's dead, Jim!".to_owned()
1516+
}
15001517
}
15011518
```
15021519
1503-
In the other, simpler case, Rust just can't find the type you are providing an
1504-
impelementation for:
1520+
Alternatively, you can create a newtype. A newtype is a wrapping tuple-struct.
1521+
For example, `NewType` is a newtype over `Foo` in `struct NewType(Foo)`.
1522+
Example:
15051523
15061524
```
1507-
impl SomeTypeThatDoesntExist { }
1525+
struct TypeWrapper((u8, u8));
1526+
1527+
impl TypeWrapper {
1528+
fn get_state(&self) -> String {
1529+
"Fascinating!".to_owned()
1530+
}
1531+
}
15081532
```
15091533
"##,
15101534

0 commit comments

Comments
 (0)