Skip to content

Commit 8e127ee

Browse files
committed
Rollup merge of rust-lang#30253 - Manishearth:diag-401-improve, r=steveklabnik
r? @steveklabnik
2 parents 27a1834 + 4423463 commit 8e127ee

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

src/librustc_resolve/diagnostics.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ https://doc.rust-lang.org/reference.html#use-declarations
274274
"##,
275275

276276
E0401: r##"
277-
Inner functions do not inherit type parameters from the functions they are
277+
Inner items do not inherit type parameters from the functions they are
278278
embedded in. For example, this will not compile:
279279
280280
```
@@ -286,12 +286,32 @@ fn foo<T>(x: T) {
286286
}
287287
```
288288
289-
Functions inside functions are basically just like top-level functions, except
290-
that they can only be called from the function they are in.
289+
nor will this:
290+
291+
```
292+
fn foo<T>(x: T) {
293+
type MaybeT = Option<T>;
294+
// ...
295+
}
296+
```
297+
298+
or this:
299+
300+
```
301+
fn foo<T>(x: T) {
302+
struct Foo {
303+
x: T,
304+
}
305+
// ...
306+
}
307+
```
308+
309+
Items inside functions are basically just like top-level items, except
310+
that they can only be used from the function they are in.
291311
292312
There are a couple of solutions for this.
293313
294-
You can use a closure:
314+
If the item is a function, you may use a closure:
295315
296316
```
297317
fn foo<T>(x: T) {
@@ -302,7 +322,7 @@ fn foo<T>(x: T) {
302322
}
303323
```
304324
305-
or copy over the parameters:
325+
For a generic item, you can copy over the parameters:
306326
307327
```
308328
fn foo<T>(x: T) {
@@ -313,6 +333,12 @@ fn foo<T>(x: T) {
313333
}
314334
```
315335
336+
```
337+
fn foo<T>(x: T) {
338+
type MaybeT<T> = Option<T>;
339+
}
340+
```
341+
316342
Be sure to copy over any bounds as well:
317343
318344
```
@@ -324,10 +350,18 @@ fn foo<T: Copy>(x: T) {
324350
}
325351
```
326352
353+
```
354+
fn foo<T: Copy>(x: T) {
355+
struct Foo<T: Copy> {
356+
x: T,
357+
}
358+
}
359+
```
360+
327361
This may require additional type hints in the function body.
328362
329-
In case the function is in an `impl`, defining a private helper function might
330-
be easier:
363+
In case the item is a function inside an `impl`, defining a private helper
364+
function might be easier:
331365
332366
```
333367
impl<T> Foo<T> {

0 commit comments

Comments
 (0)