about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-11-21 00:54:16 +0000
committerbors <bors@rust-lang.org>2015-11-21 00:54:16 +0000
commit79b7a9efa9ab8601a2609d621de45bf0e4b30ce1 (patch)
treebe51b399e85290fa75fedbbe9ce93297b0a04be1
parent731c0ce4c9fce4b689841be477841595be686ce9 (diff)
parentc23cbae0cbd2618b56477425343d87f93cbd9194 (diff)
downloadrust-79b7a9efa9ab8601a2609d621de45bf0e4b30ce1.tar.gz
rust-79b7a9efa9ab8601a2609d621de45bf0e4b30ce1.zip
Auto merge of #29916 - Manishearth:diag-401, r=steveklabnik
None
-rw-r--r--src/librustc_resolve/diagnostics.rs72
1 files changed, 71 insertions, 1 deletions
diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs
index f35b554d6cf..85fb5d9ccf9 100644
--- a/src/librustc_resolve/diagnostics.rs
+++ b/src/librustc_resolve/diagnostics.rs
@@ -273,6 +273,77 @@ on this topic:
 https://doc.rust-lang.org/reference.html#use-declarations
 "##,
 
+E0401: r##"
+Inner functions do not inherit type parameters from the functions they are
+embedded in. For example, this will not compile:
+
+```
+fn foo<T>(x: T) {
+    fn bar(y: T) { // T is defined in the "outer" function
+        // ..
+    }
+    bar(x);
+}
+```
+
+Functions inside functions are basically just like top-level functions, except
+that they can only be called from the function they are in.
+
+There are a couple of solutions for this.
+
+You can use a closure:
+
+```
+fn foo<T>(x: T) {
+    let bar = |y: T| { // explicit type annotation may not be necessary
+        // ..
+    }
+    bar(x);
+}
+```
+
+or copy over the parameters:
+
+```
+fn foo<T>(x: T) {
+    fn bar<T>(y: T) {
+        // ..
+    }
+    bar(x);
+}
+```
+
+Be sure to copy over any bounds as well:
+
+```
+fn foo<T: Copy>(x: T) {
+    fn bar<T: Copy>(y: T) {
+        // ..
+    }
+    bar(x);
+}
+```
+
+This may require additional type hints in the function body.
+
+In case the function is in an `impl`, defining a private helper function might
+be easier:
+
+```
+impl<T> Foo<T> {
+    pub fn foo(&self, x: T) {
+        self.bar(x);
+    }
+    fn bar(&self, y: T) {
+        // ..
+    }
+}
+```
+
+For default impls in traits, the private helper solution won't work, however
+closures or copying the parameters should still work.
+"##,
+
 E0403: r##"
 Some type parameters have the same name. Example of erroneous code:
 
@@ -909,7 +980,6 @@ register_diagnostics! {
     E0254, // import conflicts with imported crate in this module
     E0257,
     E0258,
-    E0401, // can't use type parameters from outer function
     E0402, // cannot use an outer type parameter in this context
     E0406, // undeclared associated type
     E0408, // variable from pattern #1 is not bound in pattern #