diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2015-12-07 14:58:26 +0530 |
|---|---|---|
| committer | Manish Goregaokar <manishsmail@gmail.com> | 2015-12-15 10:25:29 +0530 |
| commit | 4423463465827f8ad48437b9438e3bc4757f2834 (patch) | |
| tree | a5b400a47240fc5447ff12e4c7e02538aeed73c4 /src | |
| parent | 8f031bf96205ed4cb990c2c7aded84d5ac079254 (diff) | |
Improve E0401 diagnostics to mention other items.
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc_resolve/diagnostics.rs | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 85fb5d9ccf9..04ab3fe70e9 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -274,7 +274,7 @@ https://doc.rust-lang.org/reference.html#use-declarations "##, E0401: r##" -Inner functions do not inherit type parameters from the functions they are +Inner items do not inherit type parameters from the functions they are embedded in. For example, this will not compile: ``` @@ -286,12 +286,32 @@ fn foo<T>(x: T) { } ``` -Functions inside functions are basically just like top-level functions, except -that they can only be called from the function they are in. +nor will this: + +``` +fn foo<T>(x: T) { + type MaybeT = Option<T>; + // ... +} +``` + +or this: + +``` +fn foo<T>(x: T) { + struct Foo { + x: T, + } + // ... +} +``` + +Items inside functions are basically just like top-level items, except +that they can only be used from the function they are in. There are a couple of solutions for this. -You can use a closure: +If the item is a function, you may use a closure: ``` fn foo<T>(x: T) { @@ -302,7 +322,7 @@ fn foo<T>(x: T) { } ``` -or copy over the parameters: +For a generic item, you can copy over the parameters: ``` fn foo<T>(x: T) { @@ -313,6 +333,12 @@ fn foo<T>(x: T) { } ``` +``` +fn foo<T>(x: T) { + type MaybeT<T> = Option<T>; +} +``` + Be sure to copy over any bounds as well: ``` @@ -324,10 +350,18 @@ fn foo<T: Copy>(x: T) { } ``` +``` +fn foo<T: Copy>(x: T) { + struct Foo<T: Copy> { + x: T, + } +} +``` + 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: +In case the item is a function inside an `impl`, defining a private helper +function might be easier: ``` impl<T> Foo<T> { |
