Skip to content

Commit 471152c

Browse files
committed
Add information about breaking changes when adding types to the prelude
1 parent 0943ca7 commit 471152c

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/breaking-changes/prelude.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Breaking changes to the prelude
22

33
Making changes to the prelude can easily cause breakage because it impacts all Rust code.
4-
In most cases the impact is limited since prelude items have the lowest priority in name lookup (lower than glob imports), but there are two cases where this doesn't work.
4+
In most cases the impact is limited since prelude items have the lowest priority in name lookup (lower than glob imports), but there are three cases where this requires additional care or may not be possible.
55

66
## Traits
77

@@ -16,3 +16,13 @@ Unlike other item types, rustc's name resolution for macros does not support giv
1616
As a general rule, avoid adding macros to the prelude except at edition boundaries.
1717

1818
This issues was encoutered when trying to land the [`assert_matches!` macro](https://github.com/rust-lang/rust/issues/82913).
19+
20+
## Types
21+
22+
Adding a new type usually doesn't create any breakage, because prelude types have a lower priority than names in user code.
23+
24+
However, code that declares an enum, *and* has a derive on that enum, *and* attempts to locally import a variant from that enum (e.g. `use EnumType::Variant;`) will break if the prelude starts exporting a type with the same name as that enum.
25+
26+
This makes adding a new type to the prelude require a crater run to check for breakage in existing Rust code.
27+
28+
If Rust code declares a local variable binding and the prelude adds a type of the same name, this can likewise introduce breakage. This case is much less likely to arise, as it requires declaring a variable name using the `CamelCase` convention typically used for types, and ignoring rustc's warnings about doing so.

src/policy/prelude.md

+21-8
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,33 @@ prelude for all Rust editions.
1818
When to use editions
1919
--------------------
2020

21+
[Some kinds of additions to the prelude can cause breakage in existing
22+
code](../breaking-changes/prelude.md), so we must either avoid making such
23+
changes entirely or check carefully with crater before making them.
24+
2125
Adding a trait to the prelude can break existing Rust code, by creating name
2226
resolution ambiguity where none previously existed. While introducing
2327
resolution ambiguities may be "permitted breakage", doing so is quite
2428
disruptive, and we want to avoid doing so. Thus, we generally never add a trait
25-
to the common prelude; we only add traits to the prelude for a new edition of
29+
to the common prelude; we only add traits to the preludes for new editions of
2630
Rust.
2731

28-
Adding items *other* than traits to the prelude will never produce conflicts or
29-
other compatibility issues with existing code, since we allow shadowing and
30-
give other sources of names priority over names from the prelude. Thus, if we
31-
choose to add a non-trait item to the prelude, we should typically add it to
32-
the common prelude for all editions of Rust. (Exceptions to this would include
33-
names that form part of an edition transition, such that the same name resolves
34-
to something different in different editions.)
32+
Likewise, adding a macro to the prelude can break existing code, so we
33+
generally can't add macros to the common prelude, and can only add macros to
34+
the preludes for new editions of Rust.
35+
36+
Finally, in some cases adding a type to the prelude can break existing code.
37+
The cases in which this arises are narrower, but still require at a minimum
38+
checking a crater run.
39+
40+
Adding items other than traits, macros, or types to the prelude will never
41+
produce conflicts or other compatibility issues with existing code, since we
42+
allow shadowing and give other sources of names priority over names from the
43+
prelude. Thus, if we choose to add another kind of item to the prelude, we
44+
should typically add it to the common prelude for all editions of Rust.
45+
(Exceptions to this would include names that form part of an edition
46+
transition, such that the same name resolves to something different in
47+
different editions.)
3548

3649
Criteria for including an item
3750
------------------------------

0 commit comments

Comments
 (0)