Skip to content

Commit 66ef463

Browse files
committed
Handle other expression-containing items (static, const, enum discriminants)
1 parent 22ac798 commit 66ef463

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

text/3373-avoid-nonlocal-definitions-in-fns.md

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,49 @@
66
# Summary
77
[summary]: #summary
88

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.
1112

1213
# Motivation
1314
[motivation]: #motivation
1415

1516
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.
1920

2021
Humans cross-referencing such uses and definitions may find themselves
2122
similarly baffled.
2223

2324
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.
2627

2728
# Explanation
2829
[explanation]: #explanation
2930

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+
3038
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]`).
4048

4149
Rust 2015, 2018, and 2021 continue to permit this, but will produce a
4250
warn-by-default lint.
4351

44-
No other language features provide a means of defining a name inside a function
45-
and referencing that name outside the function.
46-
4752
# Drawbacks
4853
[drawbacks]: #drawbacks
4954

0 commit comments

Comments
 (0)