diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 85fb5d9ccf9e5..04ab3fe70e9fa 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(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(x: T) { + type MaybeT = Option; + // ... +} +``` + +or this: + +``` +fn foo(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(x: T) { @@ -302,7 +322,7 @@ fn foo(x: T) { } ``` -or copy over the parameters: +For a generic item, you can copy over the parameters: ``` fn foo(x: T) { @@ -313,6 +333,12 @@ fn foo(x: T) { } ``` +``` +fn foo(x: T) { + type MaybeT = Option; +} +``` + Be sure to copy over any bounds as well: ``` @@ -324,10 +350,18 @@ fn foo(x: T) { } ``` +``` +fn foo(x: T) { + struct Foo { + 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 Foo {