Skip to content

Commit 09a0e24

Browse files
committed
Avoid non-local definitions in functions
1 parent 3fe3c65 commit 09a0e24

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
- Feature Name: N/A
2+
- Start Date: 2022-01-19
3+
- RFC PR: [rust-lang/rfcs#0000](https://github.com/rust-lang/rfcs/pull/0000)
4+
- Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/0000)
5+
6+
# Summary
7+
[summary]: #summary
8+
9+
Starting in Rust 2024, stop allowing items inside functions to implement
10+
methods or traits that are visible outside the function.
11+
12+
# Motivation
13+
[motivation]: #motivation
14+
15+
Currently, tools cross-referencing uses and definitions (such as IDEs) must
16+
search inside all function bodies to find potential definitions corresponding
17+
to uses within a function.
18+
19+
Humans cross-referencing such uses and definitions may find themselves
20+
similarly baffled.
21+
22+
With this change, both humans and tools can limit the scope of their search and
23+
avoid looking for definitions inside other functions.
24+
25+
# Explanation
26+
[explanation]: #explanation
27+
28+
Starting in the Rust 2024 edition:
29+
- An item nested inside a `fn` (through any level of nesting) may not define an
30+
`impl Type` block unless the `Type` is also nested inside the same `fn`.
31+
- An item nested inside a `fn` (through any level of nesting) may not define an
32+
`impl Trait for Type` unless either the `Trait` or the `Type` is also nested
33+
inside the same `fn`.
34+
35+
Rust 2015, 2018, and 2021 continue to permit this, but will produce a
36+
warn-by-default lint.
37+
38+
# Drawbacks
39+
[drawbacks]: #drawbacks
40+
41+
Some existing code makes use of this pattern, and would need to migrate to a
42+
different pattern. In particular, this pattern may occur in macro-generated
43+
code, or in code generated by tools like rustdoc. Making this change would
44+
require such code and tools to restructure to meet this requirement.
45+
46+
# Unresolved questions
47+
[unresolved-questions]: #unresolved-questions
48+
49+
We'll need a crater run to look at how widespread this pattern is in existing
50+
code.
51+
52+
# Future possibilities
53+
[future-possibilities]: #future-possibilities
54+
55+
If in the future Rust provides a "standalone `derive`" mechanism (e.g. `derive
56+
Trait for Type` as a standalone definition separate from `Type`), the `impl`
57+
produced by that mechanism would be subject to the same requirements.

0 commit comments

Comments
 (0)