Improve E0401 diagnostics to mention other items.
This commit is contained in:
parent
8f031bf962
commit
4423463465
@ -274,7 +274,7 @@ pub mod foo {
|
||||
"##,
|
||||
|
||||
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 bar(y: T) { // T is defined in the "outer" function
|
||||
}
|
||||
```
|
||||
|
||||
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 bar<T>(y: 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 bar<T: Copy>(y: 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> {
|
||||
|
Loading…
Reference in New Issue
Block a user