Skip to content

Commit 2476b8a

Browse files
committed
Auto merge of #159966 - JonathanBrouwer:rollup-efqUEIj, r=JonathanBrouwer
Rollup of 28 pull requests Successful merges: - rust-lang/rust#159638 (bootstrap: Split the `Step` trait into multiple traits) - rust-lang/rust#159774 (rustc_trait_selection: fix trait solver hang caused by degenerate obligations) - rust-lang/rust#159837 (line-tables-only test: check that the line number matches the function name) - rust-lang/rust#159946 (Update Enzyme submodule to imporve llvm-cov) - rust-lang/rust#159962 (miri subtree update) - rust-lang/rust#156570 (tests: extend remap-path-prefix-std to all stdlib rlibs) - rust-lang/rust#159617 (Fix up `#[linkage]` target checking) - rust-lang/rust#159633 (Improve workings of attribute suggestions) - rust-lang/rust#159733 (std: Switch implementations of `thread_local!` for WASI) - rust-lang/rust#159783 (Check unsafe impls on safe EIIs) - rust-lang/rust#159810 (Add tuple never coercion collection regression test) - rust-lang/rust#159826 (Remove redundant `#[rustc_paren_sugar]` feature gate) - rust-lang/rust#159846 (Implement `str::copy_from_str`) - rust-lang/rust#159849 (rustc_parse: Stop returning `Option` from statement parsing) - rust-lang/rust#159853 (Updated expect messages for `CString` struct and method documentation) - rust-lang/rust#159875 (More cleanup in `rustc_attr_parsing`) - rust-lang/rust#159882 (Update expect messages in library/alloc/boxed.rs and library/alloc/string.rs to follow the style guide) - rust-lang/rust#159891 (Split multiline derives into std/rustc macros) - rust-lang/rust#159893 (Fix `find_attr` hygiene and `rustc_hir` cleanups) - rust-lang/rust#159895 (rustc-dev-guide subtree update) - rust-lang/rust#159902 (Clarify that the expected runtime symbols signature is for the current target only) - rust-lang/rust#159914 (Fix error in diagnostic on_unmatched_args) - rust-lang/rust#159917 (spare capacity mut constification) - rust-lang/rust#159918 (rename abort_unwind → abort_on_unwind) - rust-lang/rust#159927 (Remove sve2 from the ImpliedFeatures of AArch64 v9a.) - rust-lang/rust#159936 (Minor `rustc_ast::ast` doc cleanups) - rust-lang/rust#159945 (Update expect messages in library/core/src/ptr/non_null.rs) - rust-lang/rust#159950 (Add CFI tests for return types and never type)
2 parents 120a76f + 92a71d6 commit 2476b8a

14 files changed

Lines changed: 350 additions & 328 deletions

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
390279b302ca98ae270f434100ae3730531d1246
1+
da86f4d0726be475afbbffe40cb2f65741c51ad3

src/ast-validation.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
# AST validation
22

33
_AST validation_ is a separate AST pass that visits each
4-
item in the tree and performs simple checks. This pass
5-
doesn't perform any complex analysis, type checking or
4+
item in the tree and performs simple checks.
5+
This pass doesn't perform any complex analysis, type checking or
66
name resolution.
77

8-
Before performing any validation, the compiler first expands
9-
the macros. Then this pass performs validations to check
10-
that each AST item is in the correct state. And when this pass
11-
is done, the compiler runs the crate resolution pass.
8+
Before performing any validation, the compiler first expands the macros.
9+
Then this pass performs validations to check that each AST item is in the correct state.
10+
And when this pass is done, the compiler runs the crate resolution pass.
1211

1312
## Validations
1413

1514
Validations are defined in `AstValidator` type, which
16-
itself is located in `rustc_ast_passes` crate. This
17-
type implements various simple checks which emit errors
15+
itself is located in `rustc_ast_passes` crate.
16+
This type implements various simple checks which emit errors
1817
when certain language rules are broken.
1918

2019
In addition, `AstValidator` implements `Visitor` trait
2120
that defines how to visit AST items (which can be functions,
2221
traits, enums, etc).
2322

24-
For each item, visitor performs specific checks. For
25-
example, when visiting a function declaration,
23+
For each item, visitor performs specific checks.
24+
For example, when visiting a function declaration,
2625
`AstValidator` checks that the function has:
2726

2827
* no more than `u16::MAX` parameters;

src/attributes.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,35 @@ For more information on these attributes, see the chapter about [attribute parsi
2525

2626
[attr-parsing-chapter]: ./hir/attribute-parsing.md
2727

28+
### Note on adding new builtin attributes
29+
30+
<div class="warning">
31+
32+
**Warning: Name resolution ambiguity potential when adding new builtin attributes**
33+
34+
Please note that adding **new builtin attributes** (whose name is not reserved, i.e. a new builtin
35+
attribute whose name does not start with `rustc`), even if *unstable*-gated, can introduce breakage
36+
from name resolution ambiguity in stable code if (1) the stable code has a macro of the same name
37+
which gets re-exported, or (2) or a proc-macro derive helper attribute of the same name.
38+
39+
Typically, the builtin attributes probably has to start out as `#[rustc_foo]` instead of `#[foo]` to
40+
avoid colliding with user-defined macros and proc-macro helper attributes.
41+
Then, prior to stabilization,
42+
a rename to `#[foo]` should be done separately with a crater run to assess fallout,
43+
with a deliberate breakage FCP proposal for T-lang to consider.
44+
45+
Remember also that crater is *not* exhaustive and does not contain all existing stable code.
46+
47+
See:
48+
- [Built-in attributes are treated differently vs prelude attributes, unstable built-in attributes
49+
can name-collide with stable macro, and built-in attributes can break back-compat
50+
#134963](https://github.com/rust-lang/rust/issues/134963) and backlinks within this issue,
51+
including design discussions on how to fix this kind of breakage hazard.
52+
- [Broken build after updating: coverage is ambiguous; ambiguous because of a name conflict with a
53+
builtin attribute #121157](https://github.com/rust-lang/rust/issues/121157).
54+
- [Regression: align is ambiguous #143834](https://github.com/rust-lang/rust/issues/143834).
55+
</div>
56+
2857
## 'Non-builtin'/'active' attributes
2958

3059
These attributes are defined by a crate - either the standard library, or a proc-macro crate.

src/const-eval.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Constant Evaluation
22

3-
Constant evaluation is the process of computing values at compile time. For a
4-
specific item (constant/static/array length) this happens after the MIR for the
5-
item is borrow-checked and optimized. In many cases trying to const evaluate an
3+
Constant evaluation is the process of computing values at compile time.
4+
For a specific item (constant/static/array length) this happens after the MIR for the
5+
item is borrow-checked and optimized.
6+
In many cases trying to const evaluate an
67
item will trigger the computation of its MIR for the first time.
78

89
Prominent examples are:
@@ -11,14 +12,12 @@ Prominent examples are:
1112
* Array length
1213
* needs to be known to reserve stack or heap space
1314
* Enum variant discriminants
14-
* needs to be known to prevent two variants from having the same
15-
discriminant
15+
* needs to be known to prevent two variants from having the same discriminant
1616
* Patterns
1717
* need to be known to check for overlapping patterns
1818

1919
Additionally constant evaluation can be used to reduce the workload or binary
20-
size at runtime by precomputing complex operations at compile time and only
21-
storing the result.
20+
size at runtime by precomputing complex operations at compile time and only storing the result.
2221

2322
All uses of constant evaluation can either be categorized as "influencing the type system"
2423
(array lengths, enum variant discriminants, const generic parameters), or as solely being
@@ -37,7 +36,8 @@ They're the wrappers of the `const_eval` query.
3736

3837
The `const_eval_*` functions use a [`ParamEnv`](./typing-parameter-envs.md) of environment
3938
in which the constant is evaluated (e.g. the function within which the constant is used)
40-
and a [`GlobalId`]. The `GlobalId` is made up of an `Instance` referring to a constant
39+
and a [`GlobalId`].
40+
The `GlobalId` is made up of an `Instance` referring to a constant
4141
or static or of an `Instance` of a function and an index into the function's `Promoted` table.
4242

4343
Constant evaluation returns an [`EvalToValTreeResult`] for type system constants

src/notification-groups/gpu-target.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
**Github Label:** None <br>
44
**Ping command:** `@rustbot ping gpu-target`
55

6-
This notification group deals with linker related issues and their integration
7-
within the compiler.
6+
This notification group deals with linker-related issues and their integration within the compiler.
87

98
The group also has an associated Zulip stream ([`#t-compiler/gpgpu-backend`])
109
where people can go to ask questions and discuss GPU-related topics and issues.
1110

12-
if you're interested in participating, feel free to sign up for this group! To
13-
do so, open a PR against the [rust-lang/team] repository and add your GitHub
11+
if you're interested in participating, feel free to sign up for this group!
12+
To do so, open a PR against the [rust-lang/team] repository and add your GitHub
1413
user to [this file][gpu-target-team].
1514

1615
[`#t-compiler/gpgpu-backend`]: https://rust-lang.zulipchat.com/#narrow/channel/422870-t-compiler.2Fgpgpu-backend

src/notification-groups/wasi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ where people can go to pose questions and discuss WASI-specific topics.
1414

1515
So, if you are interested in participating, please sign up for the WASI group!
1616
To do so, open a PR against the [rust-lang/team] repository.
17-
Just [follow this example][eg], but change the username to your own!
17+
Just follow [this example], but change the username to your own!
1818

1919
[`#t-compiler/wasm`]: https://rust-lang.zulipchat.com/#narrow/stream/463513-t-compiler.2Fwasm
2020
[rust-lang/team]: https://github.com/rust-lang/team
21-
[eg]: https://github.com/rust-lang/team/pull/1580
21+
[this example]: https://github.com/rust-lang/team/pull/1580

src/notification-groups/wasm.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ WebAssembly-related issues as well as suggestions on how to resolve
1010
interesting questions regarding our WASM support.
1111

1212
The group also has an associated Zulip channel ([`#t-compiler/wasm`])
13-
where people can go to pose questions and discuss WASM-specific
14-
topics.
13+
where people can go to pose questions and discuss WASM-specific topics.
1514

16-
So, if you are interested in participating, please sign up for the
17-
WASM group! To do so, open a PR against the [rust-lang/team]
18-
repository. Just [follow this example][eg], but change the username to
19-
your own!
15+
So, if you are interested in participating, please sign up for the WASM group!
16+
To do so, open a PR against the [rust-lang/team] repository.
17+
Just follow [this example], but change the username to your own!
2018

2119
[`#t-compiler/wasm`]: https://rust-lang.zulipchat.com/#narrow/stream/463513-t-compiler.2Fwasm
2220
[rust-lang/team]: https://github.com/rust-lang/team
23-
[eg]: https://github.com/rust-lang/team/pull/1581
21+
[this example]: https://github.com/rust-lang/team/pull/1581

src/pat-exhaustive-checking.md

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Pattern and exhaustiveness checking
22

3-
In Rust, pattern matching and bindings have a few very helpful properties. The
4-
compiler will check that bindings are irrefutable when made and that match arms
3+
In Rust, pattern matching and bindings have a few very helpful properties.
4+
The compiler will check that bindings are irrefutable when made and that match arms
55
are exhaustive.
66

77
## Pattern usefulness
@@ -35,8 +35,7 @@ match x {
3535
}
3636
```
3737

38-
Thus usefulness is used for two purposes:
39-
detecting unreachable code (which is useful to the user),
38+
Thus usefulness is used for two purposes: detecting unreachable code (which is useful to the user),
4039
and ensuring that matches are exhaustive (which is important for soundness,
4140
because a match expression can return a value).
4241

@@ -88,22 +87,26 @@ That file contains a detailed description of the algorithm.
8887
### Constructors and fields
8988

9089
In the value `Pair(Some(0), true)`, `Pair` is called the constructor of the value, and `Some(0)` and
91-
`true` are its fields. Every matchable value can be decomposed in this way. Examples of
92-
constructors are: `Some`, `None`, `(,)` (the 2-tuple constructor), `Foo {..}` (the constructor for
93-
a struct `Foo`), and `2` (the constructor for the number `2`).
94-
95-
Each constructor takes a fixed number of fields; this is called its arity. `Pair` and `(,)` have
96-
arity 2, `Some` has arity 1, `None` and `42` have arity 0. Each type has a known set of
97-
constructors. Some types have many constructors (like `u64`) or even an infinitely many (like `&str`
98-
and `&[T]`).
99-
100-
Patterns are similar: `Pair(Some(_), _)` has constructor `Pair` and two fields. The difference is
101-
that we get some extra pattern-only constructors, namely: the wildcard `_`, variable bindings,
102-
integer ranges like `0..=10`, and variable-length slices like `[_, .., _]`. We treat or-patterns
103-
separately.
90+
`true` are its fields.
91+
Every matchable value can be decomposed in this way.
92+
Examples of constructors are:
93+
`Some`, `None`, `(,)` (the 2-tuple constructor), `Foo {..}` (the constructor for a struct `Foo`),
94+
and `2` (the constructor for the number `2`).
95+
96+
Each constructor takes a fixed number of fields; this is called its arity.
97+
`Pair` and `(,)` have arity 2, `Some` has arity 1, `None` and `42` have arity 0.
98+
Each type has a known set of constructors.
99+
Some types have many constructors (like `u64`) or even an infinitely many (like `&str` and `&[T]`).
100+
101+
Patterns are similar: `Pair(Some(_), _)` has constructor `Pair` and two fields.
102+
The difference is that we get some extra pattern-only constructors, namely:
103+
the wildcard `_`, variable bindings,
104+
integer ranges like `0..=10`, and variable-length slices like `[_, .., _]`.
105+
We treat or-patterns separately.
104106

105107
Now to check if a value `v` matches a pattern `p`, we check if `v`'s constructor matches `p`'s
106-
constructor, then recursively compare their fields if necessary. A few representative examples:
108+
constructor, then recursively compare their fields if necessary.
109+
A few representative examples:
107110

108111
- `matches!(v, _) := true`
109112
- `matches!((v0, v1), (p0, p1)) := matches!(v0, p0) && matches!(v1, p1)`
@@ -114,8 +117,9 @@ constructor, then recursively compare their fields if necessary. A few represent
114117
- `matches!([v0], [p0, .., p1]) := false` (incompatible lengths)
115118
- `matches!([v0, v1, v2], [p0, .., p1]) := matches!(v0, p0) && matches!(v2, p1)`
116119

117-
This concept is absolutely central to pattern analysis. The [`constructor`] module provides
118-
functions to extract, list and manipulate constructors. This is a useful enough concept that
120+
This concept is absolutely central to pattern analysis.
121+
The [`constructor`] module provides functions to extract, list, and manipulate constructors.
122+
This is a useful enough concept that
119123
variations of it can be found in other places of the compiler, like in the MIR-lowering of a match
120124
expression and in some clippy lints.
121125

@@ -125,7 +129,8 @@ The pattern-only constructors (`_`, ranges and variable-length slices) each stan
125129
normal constructors, e.g. `_: Option<T>` stands for the set {`None`, `Some`} and `[_, .., _]` stands
126130
for the infinite set {`[,]`, `[,,]`, `[,,,]`, ...} of the slice constructors of arity >= 2.
127131

128-
In order to manage these constructors, we keep them as grouped as possible. For example:
132+
In order to manage these constructors, we keep them as grouped as possible.
133+
For example:
129134

130135
```rust
131136
match (0, false) {
@@ -137,7 +142,8 @@ match (0, false) {
137142

138143
In this example, all of `0`, `1`, .., `49` match the same arms, and thus can be treated as a group.
139144
In fact, in this match, the only ranges we need to consider are: `0..50`, `50..=100`,
140-
`101..=150`,`151..=200` and `201..`. Similarly:
145+
`101..=150`,`151..=200` and `201..`.
146+
Similarly:
141147

142148
```rust
143149
enum Direction { North, South, East, West }
@@ -156,10 +162,11 @@ time.
156162

157163
### Usefulness vs reachability in the presence of empty types
158164

159-
This is likely the subtlest aspect of exhaustiveness. To be fully precise, a match doesn't operate
160-
on a value, it operates on a place. In certain unsafe circumstances, it is possible for a place to
161-
not contain valid data for its type. This has subtle consequences for empty types. Take the
162-
following:
165+
This is likely the subtlest aspect of exhaustiveness.
166+
To be fully precise, a match doesn't operate on a value; it operates on a place.
167+
In certain unsafe circumstances, it is possible for a place to not contain valid data for its type.
168+
This has subtle consequences for empty types.
169+
Take the following:
163170

164171
```rust
165172
enum Void {}
@@ -172,10 +179,11 @@ unsafe {
172179
}
173180
```
174181

175-
In this example, `ptr` is a valid pointer pointing to a place with invalid data. The `_` pattern
176-
does not look at the contents of the place `*ptr`, so this code is ok and the arm is taken. In other
177-
words, despite the place we are inspecting being of type `Void`, there is a reachable arm. If the
178-
arm had a binding however:
182+
In this example, `ptr` is a valid pointer pointing to a place with invalid data.
183+
The `_` pattern does not look at the contents of the place `*ptr`,
184+
so this code is ok and the arm is taken.
185+
In other words, despite the place we are inspecting being of type `Void`, there is a reachable arm.
186+
If the arm had a binding however:
179187

180188
```rust
181189
# #[derive(Copy, Clone)]
@@ -189,25 +197,31 @@ match *ptr {
189197
# }
190198
```
191199

192-
Here the binding loads the value of type `Void` from the `*ptr` place. In this example, this causes
193-
UB since the data is not valid. In the general case, this asserts validity of the data at `*ptr`.
200+
Here the binding loads the value of type `Void` from the `*ptr` place.
201+
In this example, this causes UB since the data is not valid.
202+
In the general case, this asserts validity of the data at `*ptr`.
194203
Either way, this arm will never be taken.
195204

196-
Finally, let's consider the empty match `match *ptr {}`. If we consider this exhaustive, then
197-
having invalid data at `*ptr` is invalid. In other words, the empty match is semantically
198-
equivalent to the `_a => ...` match. In the interest of explicitness, we prefer the case with an
199-
arm, hence we won't tell the user to remove the `_a` arm. In other words, the `_a` arm is
200-
unreachable yet not redundant. This is why we lint on redundant arms rather than unreachable
205+
Finally, let's consider the empty match `match *ptr {}`.
206+
If we consider this exhaustive, then having invalid data at `*ptr` is invalid.
207+
In other words, the empty match is semantically equivalent to the `_a => ...` match.
208+
In the interest of explicitness, we prefer the case with an
209+
arm, hence we won't tell the user to remove the `_a` arm.
210+
In other words, the `_a` arm is unreachable yet not redundant.
211+
This is why we lint on redundant arms rather than unreachable
201212
arms, despite the fact that the lint says "unreachable".
202213

203214
These considerations only affects certain places, namely those that can contain non-valid data
204-
without UB. These are: pointer dereferences, reference dereferences, and union field accesses. We
205-
track during exhaustiveness checking whether a given place is known to contain valid data.
215+
without UB.
216+
These are: pointer dereferences, reference dereferences, and union field accesses.
217+
We track during exhaustiveness checking whether a given place is known to contain valid data.
206218

207219
Having said all that, the current implementation of exhaustiveness checking does not follow the
208-
above considerations. On stable, empty types are for the most part treated as non-empty. The
209-
[`exhaustive_patterns`] feature errs on the other end: it allows omitting arms that could be
210-
reachable in unsafe situations. The [`never_patterns`] experimental feature aims to fix this and
220+
above considerations.
221+
On stable, empty types are for the most part treated as non-empty.
222+
The [`exhaustive_patterns`] feature errs on the other end: it allows omitting arms that could be
223+
reachable in unsafe situations.
224+
The [`never_patterns`] experimental feature aims to fix this and
211225
permit the correct behavior of empty types in patterns.
212226

213227
[`check_match`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir_build/thir/pattern/check_match/index.html

0 commit comments

Comments
 (0)