about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNick Hamann <nick@wabbo.org>2015-06-09 03:05:24 -0500
committerNick Hamann <nick@wabbo.org>2015-06-10 02:18:14 -0500
commitc8b088eb38128e8b0e50a550d4b89334574f1216 (patch)
tree90d328da78fde2be113b24ab7d31508da59e605d
parent172cd83490cc66065e72861aed53e3efec29b34f (diff)
downloadrust-c8b088eb38128e8b0e50a550d4b89334574f1216.tar.gz
rust-c8b088eb38128e8b0e50a550d4b89334574f1216.zip
Add explanation for E0116 and update the error message.
Also updates the reference on this point.
-rw-r--r--src/doc/reference.md2
-rw-r--r--src/librustc_typeck/coherence/orphan.rs4
-rw-r--r--src/librustc_typeck/diagnostics.rs27
-rw-r--r--src/test/compile-fail/trait-or-new-type-instead.rs4
4 files changed, 31 insertions, 6 deletions
diff --git a/src/doc/reference.md b/src/doc/reference.md
index 8d57238dc17..2a0f7b659d1 100644
--- a/src/doc/reference.md
+++ b/src/doc/reference.md
@@ -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}
diff --git a/src/librustc_typeck/coherence/orphan.rs b/src/librustc_typeck/coherence/orphan.rs
index 7dc865ef885..cc03dffe3a0 100644
--- a/src/librustc_typeck/coherence/orphan.rs
+++ b/src/librustc_typeck/coherence/orphan.rs
@@ -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");
         }
     }
diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs
index 10bae6f63d0..c2cb55bb1f5 100644
--- a/src/librustc_typeck/diagnostics.rs
+++ b/src/librustc_typeck/diagnostics.rs
@@ -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.
@@ -1232,7 +1258,6 @@ register_diagnostics! {
     E0102,
     E0103,
     E0104,
-    E0116,
     E0117,
     E0118,
     E0119,
diff --git a/src/test/compile-fail/trait-or-new-type-instead.rs b/src/test/compile-fail/trait-or-new-type-instead.rs
index 3aec23a55b8..b1e4d06affe 100644
--- a/src/test/compile-fail/trait-or-new-type-instead.rs
+++ b/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.
 //
@@ -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) { }
 }