diff options
| author | Steve Klabnik <steve@steveklabnik.com> | 2015-12-29 15:55:08 -0500 |
|---|---|---|
| committer | Steve Klabnik <steve@steveklabnik.com> | 2015-12-29 15:55:08 -0500 |
| commit | 8e127ee626fb7b68749559c9030baf3db0096df9 (patch) | |
| tree | 66f69a5fd41ece5de6bd74a32ae0565f54c16d84 /src | |
| parent | 27a1834ce522e3ec7fe4726b1661de16ee30c503 (diff) | |
| parent | 4423463465827f8ad48437b9438e3bc4757f2834 (diff) | |
Rollup merge of #30253 - Manishearth:diag-401-improve, r=steveklabnik
r? @steveklabnik
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> { |
