Skip to content

Commit

Permalink
Add explanation for E0116 and update the error message.
Browse files Browse the repository at this point in the history
Also updates the reference on this point.
  • Loading branch information
Nick Hamann committed Jun 10, 2015
1 parent 172cd83 commit c8b088e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/doc/reference.md
Expand Up @@ -1550,7 +1550,7 @@ methods in such an implementation can only be used as direct calls on the
values of the type that the implementation targets. In such an implementation,
the trait type and `for` after `impl` are omitted. Such implementations are
limited to nominal types (enums, structs), and the implementation must appear
in the same module or a sub-module as the `self` type:
in the same crate as the `self` type:

```
struct Point {x: i32, y: i32}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/coherence/orphan.rs
Expand Up @@ -33,8 +33,8 @@ impl<'cx, 'tcx> OrphanChecker<'cx, 'tcx> {
fn check_def_id(&self, item: &ast::Item, def_id: ast::DefId) {
if def_id.krate != ast::LOCAL_CRATE {
span_err!(self.tcx.sess, item.span, E0116,
"cannot associate methods with a type outside the \
crate the type is defined in; define and implement \
"cannot define inherent `impl` for a type outside of the \
crate where the type is defined; define and implement \
a trait or new type instead");
}
}
Expand Down
27 changes: 26 additions & 1 deletion src/librustc_typeck/diagnostics.rs
Expand Up @@ -730,6 +730,32 @@ RFC. It is, however, [currently unimplemented][iss15872].
[iss15872]: https://github.com/rust-lang/rust/issues/15872
"##,

E0116: r##"
You can only define an inherent implementation for a type in the same crate
where the type was defined. For example, an `impl` block as below is not allowed
since `Vec` is defined in the standard library:
```
impl Vec<u8> { ... } // error
```
To fix this problem, you can do either of these things:
- define a trait that has the desired associated functions/types/constants and
implement the trait for the type in question
- define a new type wrapping the type and define an implementation on the new
type
Note that using the `type` keyword does not work here because `type` only
introduces a type alias:
```
type Bytes = Vec<u8>;
impl Bytes { ... } // error, same as above
```
"##,

E0121: r##"
In order to be consistent with Rust's lack of global type inference, type
placeholders are disallowed by design in item signatures.
Expand Down Expand Up @@ -1232,7 +1258,6 @@ register_diagnostics! {
E0102,
E0103,
E0104,
E0116,
E0117,
E0118,
E0119,
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/trait-or-new-type-instead.rs
@@ -1,4 +1,4 @@
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -9,7 +9,7 @@
// except according to those terms.

impl<T> Option<T> {
//~^ ERROR cannot associate methods with a type outside the crate the type is defined in
//~^ ERROR cannot define inherent `impl` for a type outside of the crate where the type is defined
pub fn foo(&self) { }
}

Expand Down

0 comments on commit c8b088e

Please sign in to comment.