|
6 | 6 | # Summary
|
7 | 7 | [summary]: #summary
|
8 | 8 |
|
9 |
| -Starting in Rust 2024, stop allowing items inside functions to implement |
10 |
| -methods or traits that are visible outside the function. |
| 9 | +Starting in Rust 2024, stop allowing items inside functions or expressions to |
| 10 | +implement methods or traits that are visible outside the function or |
| 11 | +expression. |
11 | 12 |
|
12 | 13 | # Motivation
|
13 | 14 | [motivation]: #motivation
|
14 | 15 |
|
15 | 16 | Currently, tools cross-referencing uses and definitions (such as IDEs) must
|
16 |
| -either search inside all function bodies to find potential definitions |
17 |
| -corresponding to uses within a function, or not cross-reference those |
18 |
| -definitions at all. |
| 17 | +either search inside all function bodies and other expression-containing items |
| 18 | +to find potential definitions corresponding to uses within another function, or |
| 19 | +not cross-reference those definitions at all. |
19 | 20 |
|
20 | 21 | Humans cross-referencing such uses and definitions may find themselves
|
21 | 22 | similarly baffled.
|
22 | 23 |
|
23 | 24 | With this change, both humans and tools can limit the scope of their search and
|
24 |
| -avoid looking for definitions inside other functions, without missing any |
25 |
| -relevant definitions. |
| 25 | +avoid looking for definitions inside other functions or items, without missing |
| 26 | +any relevant definitions. |
26 | 27 |
|
27 | 28 | # Explanation
|
28 | 29 | [explanation]: #explanation
|
29 | 30 |
|
| 31 | +The following types of items, "expression-containing items", can contain |
| 32 | +expressions, including the definitions of other items: |
| 33 | +- Functions |
| 34 | +- Closures |
| 35 | +- The values assigned to `static`/`const` items |
| 36 | +- The discriminant values assigned to `enum` variants |
| 37 | + |
30 | 38 | Starting in the Rust 2024 edition:
|
31 |
| -- An item nested inside a function or closure (through any level of nesting) |
32 |
| - may not define an `impl Type` block unless the `Type` is also nested inside |
33 |
| - the same function or closure. |
34 |
| -- An item nested inside a function or closure (through any level of nesting) |
35 |
| - may not define an `impl Trait for Type` unless either the `Trait` or the |
36 |
| - `Type` is also nested inside the same function or closure. |
37 |
| -- An item nested inside a function or closure (through any level of nesting) |
38 |
| - may not define an exported macro visible outside the function or closure |
39 |
| - (e.g. using `#[macro_export]`). |
| 39 | +- An item nested inside an expression-containing item (through any level of |
| 40 | + nesting) may not define an `impl Type` block unless the `Type` is also nested |
| 41 | + inside the same expression-containing item. |
| 42 | +- An item nested inside an expression-containing item (through any level of |
| 43 | + nesting) may not define an `impl Trait for Type` unless either the `Trait` or |
| 44 | + the `Type` is also nested inside the same expression-containing item. |
| 45 | +- An item nested inside an expression-containing item (through any level of |
| 46 | + nesting) may not define an exported macro visible outside the |
| 47 | + expression-containing item (e.g. using `#[macro_export]`). |
40 | 48 |
|
41 | 49 | Rust 2015, 2018, and 2021 continue to permit this, but will produce a
|
42 | 50 | warn-by-default lint.
|
43 | 51 |
|
44 |
| -No other language features provide a means of defining a name inside a function |
45 |
| -and referencing that name outside the function. |
46 |
| - |
47 | 52 | # Drawbacks
|
48 | 53 | [drawbacks]: #drawbacks
|
49 | 54 |
|
|
0 commit comments