Skip to content

Commit a85c9f7

Browse files
authored
Merge branch 'rust-lang:master' into again-again-again
2 parents a26329f + e356977 commit a85c9f7

14 files changed

+236
-34
lines changed

src/attributes.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ struct S {
196196
pub fn f() {}
197197
```
198198

199-
> Note: `rustc` currently recognizes the tools "clippy" and "rustfmt".
199+
> Note: `rustc` currently recognizes the tools "clippy", "rustfmt" and "diagnostic".
200200
201201
## Built-in attributes index
202202

@@ -224,6 +224,8 @@ The following is an index of all built-in attributes.
224224
- [`allow`], [`warn`], [`deny`], [`forbid`] — Alters the default lint level.
225225
- [`deprecated`] — Generates deprecation notices.
226226
- [`must_use`] — Generates a lint for unused values.
227+
- [`diagnostic::on_unimplemented`] — Hints the compiler to emit a certain error
228+
message if a trait is not implemented.
227229
- ABI, linking, symbols, and FFI
228230
- [`link`] — Specifies a native library to link with an `extern` block.
229231
- [`link_name`] — Specifies the name of the symbol for functions or statics
@@ -273,6 +275,7 @@ The following is an index of all built-in attributes.
273275
added in future.
274276
- Debugger
275277
- [`debugger_visualizer`] — Embeds a file that specifies debugger output for a type.
278+
- [`collapse_debuginfo`] — Controls how macro invocations are encoded in debuginfo.
276279

277280
[Doc comments]: comments.md#doc-comments
278281
[ECMA-334]: https://www.ecma-international.org/publications-and-standards/standards/ecma-334/
@@ -291,6 +294,7 @@ The following is an index of all built-in attributes.
291294
[`cfg_attr`]: conditional-compilation.md#the-cfg_attr-attribute
292295
[`cfg`]: conditional-compilation.md#the-cfg-attribute
293296
[`cold`]: attributes/codegen.md#the-cold-attribute
297+
[`collapse_debuginfo`]: attributes/debugger.md#the-collapse_debuginfo-attribute
294298
[`crate_name`]: crates-and-source-files.md#the-crate_name-attribute
295299
[`crate_type`]: linkage.md
296300
[`debugger_visualizer`]: attributes/debugger.md#the-debugger_visualizer-attribute
@@ -352,3 +356,4 @@ The following is an index of all built-in attributes.
352356
[closure]: expressions/closure-expr.md
353357
[function pointer]: types/function-pointer.md
354358
[variadic functions]: items/external-blocks.html#variadic-functions
359+
[`diagnostic::on_unimplemented`]: attributes/diagnostics.md#the-diagnosticon_unimplemented-attribute

src/attributes/codegen.md

+15-5
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ Feature | Implicitly Enables | Description
262262
[rv-zks]: https://github.com/riscv/riscv-crypto/blob/e2dd7d98b7f34d477e38cb5fd7a3af4379525189/doc/scalar/riscv-crypto-scalar-zks.adoc
263263
[rv-zksed]: https://github.com/riscv/riscv-crypto/blob/e2dd7d98b7f34d477e38cb5fd7a3af4379525189/doc/scalar/riscv-crypto-scalar-zksed.adoc
264264
[rv-zksh]: https://github.com/riscv/riscv-crypto/blob/e2dd7d98b7f34d477e38cb5fd7a3af4379525189/doc/scalar/riscv-crypto-scalar-zksh.adoc
265-
[rv-zkt]: https://github.com/riscv/riscv-crypto/blob/e2dd7d98b7f34d477e38cb5fd7a3af4379525189/doc/scalar/riscv-crypto-scalar-zkr.adoc
265+
[rv-zkt]: https://github.com/riscv/riscv-crypto/blob/e2dd7d98b7f34d477e38cb5fd7a3af4379525189/doc/scalar/riscv-crypto-scalar-zkt.adoc
266266

267267
#### `wasm32` or `wasm64`
268268

@@ -273,10 +273,20 @@ attempting to use instructions unsupported by the Wasm engine will fail at load
273273
time without the risk of being interpreted in a way different from what the
274274
compiler expected.
275275

276-
Feature | Description
277-
------------|-------------------
278-
`simd128` | [WebAssembly simd proposal][simd128]
279-
276+
Feature | Description
277+
----------------------|-------------------
278+
`bulk-memory` | [WebAssembly bulk memory operations proposal][bulk-memory]
279+
`extended-const` | [WebAssembly extended const expressions proposal][extended-const]
280+
`mutable-globals` | [WebAssembly mutable global proposal][mutable-globals]
281+
`nontrapping-fptoint` | [WebAssembly non-trapping float-to-int conversion proposal][nontrapping-fptoint]
282+
`sign-ext` | [WebAssembly sign extension operators Proposal][sign-ext]
283+
`simd128` | [WebAssembly simd proposal][simd128]
284+
285+
[bulk-memory]: https://github.com/WebAssembly/bulk-memory-operations
286+
[extended-const]: https://github.com/WebAssembly/extended-const
287+
[mutable-globals]: https://github.com/WebAssembly/mutable-global
288+
[nontrapping-fptoint]: https://github.com/WebAssembly/nontrapping-float-to-int-conversions
289+
[sign-ext]: https://github.com/WebAssembly/sign-extension-ops
280290
[simd128]: https://github.com/webassembly/simd
281291

282292
### Additional information

src/attributes/debugger.md

+29
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,32 @@ When the crate's debug executable is passed into GDB[^rust-gdb], `print bob` wil
139139
[Natvis documentation]: https://docs.microsoft.com/en-us/visualstudio/debugger/create-custom-views-of-native-objects
140140
[pretty printing documentation]: https://sourceware.org/gdb/onlinedocs/gdb/Pretty-Printing.html
141141
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
142+
143+
## The `collapse_debuginfo` attribute
144+
145+
The *`collapse_debuginfo` [attribute]* controls whether code locations from a macro definition are collapsed into a single location associated with the macro's call site,
146+
when generating debuginfo for code calling this macro.
147+
148+
The attribute uses the [_MetaListIdents_] syntax to specify its inputs, and can only be applied to macro definitions.
149+
150+
Accepted options:
151+
- `#[collapse_debuginfo(yes)]` — code locations in debuginfo are collapsed.
152+
- `#[collapse_debuginfo(no)]` — code locations in debuginfo are not collapsed.
153+
- `#[collapse_debuginfo(external)]` — code locations in debuginfo are collapsed only if the macro comes from a different crate.
154+
155+
The `external` behavior is the default for macros that don't have this attribute, unless they are built-in macros.
156+
For built-in macros the default is `yes`.
157+
158+
> **Note**: `rustc` has a `-C collapse-macro-debuginfo` CLI option to override both the default collapsing behavior and `#[collapse_debuginfo]` attributes.
159+
160+
```rust
161+
#[collapse_debuginfo(yes)]
162+
macro_rules! example {
163+
() => {
164+
println!("hello!");
165+
};
166+
}
167+
```
168+
169+
[attribute]: ../attributes.md
170+
[_MetaListIdents_]: ../attributes.md#meta-item-attribute-syntax

src/attributes/diagnostics.md

+70
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,76 @@ When used on a function in a trait implementation, the attribute does nothing.
301301
> let _ = five();
302302
> ```
303303
304+
## The `diagnostic` tool attribute namespace
305+
306+
The `#[diagnostic]` attribute namespace is a home for attributes to influence compile-time error messages.
307+
The hints provided by these attributes are not guaranteed to be used.
308+
Unknown attributes in this namespace are accepted, though they may emit warnings for unused attributes.
309+
Additionally, invalid inputs to known attributes will typically be a warning (see the attribute definitions for details).
310+
This is meant to allow adding or discarding attributes and changing inputs in the future to allow changes without the need to keep the non-meaningful attributes or options working.
311+
312+
### The `diagnostic::on_unimplemented` attribute
313+
314+
The `#[diagnostic::on_unimplemented]` attribute is a hint to the compiler to supplement the error message that would normally be generated in scenarios where a trait is required but not implemented on a type.
315+
The attribute should be placed on a [trait declaration], though it is not an error to be located in other positions.
316+
The attribute uses the [_MetaListNameValueStr_] syntax to specify its inputs, though any malformed input to the attribute is not considered as an error to provide both forwards and backwards compatibility.
317+
The following keys have the given meaning:
318+
319+
* `message` — The text for the top level error message.
320+
* `label` — The text for the label shown inline in the broken code in the error message.
321+
* `note` — Provides additional notes.
322+
323+
The `note` option can appear several times, which results in several note messages being emitted.
324+
If any of the other options appears several times the first occurrence of the relevant option specifies the actually used value.
325+
Any other occurrence generates an lint warning.
326+
For any other non-existing option a lint-warning is generated.
327+
328+
All three options accept a string as an argument, interpreted using the same formatting as a [`std::fmt`] string.
329+
Format parameters with the given named parameter will be replaced with the following text:
330+
331+
* `{Self}` — The name of the type implementing the trait.
332+
* `{` *GenericParameterName* `}` — The name of the generic argument's type for the given generic parameter.
333+
334+
Any other format parameter will generate a warning, but will otherwise be included in the string as-is.
335+
336+
Invalid format strings may generate a warning, but are otherwise allowed, but may not display as intended.
337+
Format specifiers may generate a warning, but are otherwise ignored.
338+
339+
In this example:
340+
341+
```rust,compile_fail,E0277
342+
#[diagnostic::on_unimplemented(
343+
message = "My Message for `ImportantTrait<{A}>` implemented for `{Self}`",
344+
label = "My Label",
345+
note = "Note 1",
346+
note = "Note 2"
347+
)]
348+
trait ImportantTrait<A> {}
349+
350+
fn use_my_trait(_: impl ImportantTrait<i32>) {}
351+
352+
fn main() {
353+
use_my_trait(String::new());
354+
}
355+
```
356+
357+
the compiler may generate an error message which looks like this:
358+
359+
```text
360+
error[E0277]: My Message for `ImportantTrait<i32>` implemented for `String`
361+
--> src/main.rs:14:18
362+
|
363+
14 | use_my_trait(String::new());
364+
| ------------ ^^^^^^^^^^^^^ My Label
365+
| |
366+
| required by a bound introduced by this call
367+
|
368+
= help: the trait `ImportantTrait<i32>` is not implemented for `String`
369+
= note: Note 1
370+
= note: Note 2
371+
```
372+
373+
[`std::fmt`]: ../../std/fmt/index.html
304374
[Clippy]: https://github.com/rust-lang/rust-clippy
305375
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
306376
[_MetaListPaths_]: ../attributes.md#meta-item-attribute-syntax

src/attributes/type_system.md

+52-15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ pub struct Config {
2020
pub window_height: u16,
2121
}
2222

23+
#[non_exhaustive]
24+
pub struct Token;
25+
26+
#[non_exhaustive]
27+
pub struct Id(pub u64);
28+
2329
#[non_exhaustive]
2430
pub enum Error {
2531
Message(String),
@@ -34,11 +40,13 @@ pub enum Message {
3440

3541
// Non-exhaustive structs can be constructed as normal within the defining crate.
3642
let config = Config { window_width: 640, window_height: 480 };
43+
let token = Token;
44+
let id = Id(4);
3745

3846
// Non-exhaustive structs can be matched on exhaustively within the defining crate.
39-
if let Config { window_width, window_height } = config {
40-
// ...
41-
}
47+
let Config { window_width, window_height } = config;
48+
let Token = token;
49+
let Id(id_number) = id;
4250

4351
let error = Error::Other;
4452
let message = Message::Reaction(3);
@@ -64,47 +72,68 @@ Non-exhaustive types cannot be constructed outside of the defining crate:
6472

6573
- Non-exhaustive variants ([`struct`][struct] or [`enum` variant][enum]) cannot be constructed
6674
with a [_StructExpression_] \(including with [functional update syntax]).
75+
- The implicitly defined same-named constant of a [unit-like struct][struct],
76+
or the same-named constructor function of a [tuple struct][struct],
77+
has a [visibility] no greater than `pub(crate)`.
78+
That is, if the struct’s visibility is `pub`, then the constant or constructor’s visibility
79+
is `pub(crate)`, and otherwise the visibility of the two items is the same
80+
(as is the case without `#[non_exhaustive]`).
6781
- [`enum`][enum] instances can be constructed.
6882

83+
The following examples of construction do not compile when outside the defining crate:
84+
6985
<!-- ignore: requires external crates -->
7086
```rust,ignore
71-
// `Config`, `Error`, and `Message` are types defined in an upstream crate that have been
72-
// annotated as `#[non_exhaustive]`.
73-
use upstream::{Config, Error, Message};
87+
// These are types defined in an upstream crate that have been annotated as
88+
// `#[non_exhaustive]`.
89+
use upstream::{Config, Token, Id, Error, Message};
7490
75-
// Cannot construct an instance of `Config`, if new fields were added in
91+
// Cannot construct an instance of `Config`; if new fields were added in
7692
// a new version of `upstream` then this would fail to compile, so it is
7793
// disallowed.
7894
let config = Config { window_width: 640, window_height: 480 };
7995
80-
// Can construct an instance of `Error`, new variants being introduced would
96+
// Cannot construct an instance of `Token`; if new fields were added, then
97+
// it would not be a unit-like struct any more, so the same-named constant
98+
// created by it being a unit-like struct is not public outside the crate;
99+
// this code fails to compile.
100+
let token = Token;
101+
102+
// Cannot construct an instance of `Id`; if new fields were added, then
103+
// its constructor function signature would change, so its constructor
104+
// function is not public outside the crate; this code fails to compile.
105+
let id = Id(5);
106+
107+
// Can construct an instance of `Error`; new variants being introduced would
81108
// not result in this failing to compile.
82109
let error = Error::Message("foo".to_string());
83110
84-
// Cannot construct an instance of `Message::Send` or `Message::Reaction`,
111+
// Cannot construct an instance of `Message::Send` or `Message::Reaction`;
85112
// if new fields were added in a new version of `upstream` then this would
86113
// fail to compile, so it is disallowed.
87114
let message = Message::Send { from: 0, to: 1, contents: "foo".to_string(), };
88115
let message = Message::Reaction(0);
89116
90-
// Cannot construct an instance of `Message::Quit`, if this were converted to
117+
// Cannot construct an instance of `Message::Quit`; if this were converted to
91118
// a tuple-variant `upstream` then this would fail to compile.
92119
let message = Message::Quit;
93120
```
94121

95122
There are limitations when matching on non-exhaustive types outside of the defining crate:
96123

97124
- When pattern matching on a non-exhaustive variant ([`struct`][struct] or [`enum` variant][enum]),
98-
a [_StructPattern_] must be used which must include a `..`. Tuple variant constructor visibility
99-
is lowered to `min($vis, pub(crate))`.
125+
a [_StructPattern_] must be used which must include a `..`. A tuple variant's constructor's
126+
[visibility] is reduced to be no greater than `pub(crate)`.
100127
- When pattern matching on a non-exhaustive [`enum`][enum], matching on a variant does not
101128
contribute towards the exhaustiveness of the arms.
102129

130+
The following examples of matching do not compile when outside the defining crate:
131+
103132
<!-- ignore: requires external crates -->
104133
```rust, ignore
105-
// `Config`, `Error`, and `Message` are types defined in an upstream crate that have been
106-
// annotated as `#[non_exhaustive]`.
107-
use upstream::{Config, Error, Message};
134+
// These are types defined in an upstream crate that have been annotated as
135+
// `#[non_exhaustive]`.
136+
use upstream::{Config, Token, Id, Error, Message};
108137
109138
// Cannot match on a non-exhaustive enum without including a wildcard arm.
110139
match error {
@@ -118,6 +147,13 @@ if let Ok(Config { window_width, window_height }) = config {
118147
// would compile with: `..`
119148
}
120149
150+
// Cannot match a non-exhaustive unit-like or tuple struct except by using
151+
// braced struct syntax with a wildcard.
152+
// This would compile as `let Token { .. } = token;`
153+
let Token = token;
154+
// This would compile as `let Id { 0: id_number, .. } = id;`
155+
let Id(id_number) = id;
156+
121157
match message {
122158
// Cannot match on a non-exhaustive struct enum variant without including a wildcard.
123159
Message::Send { from, to, contents } => { },
@@ -147,3 +183,4 @@ Non-exhaustive types are always considered inhabited in downstream crates.
147183
[enum]: ../items/enumerations.md
148184
[functional update syntax]: ../expressions/struct-expr.md#functional-update-syntax
149185
[struct]: ../items/structs.md
186+
[visibility]: ../visibility-and-privacy.md

src/const_eval.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ to be run.
2727
* [Tuple expressions].
2828
* [Array expressions].
2929
* [Struct] expressions.
30-
* [Block expressions], including `unsafe` blocks.
30+
* [Block expressions], including `unsafe` and `const` blocks.
3131
* [let statements] and thus irrefutable [patterns], including mutable bindings
3232
* [assignment expressions]
3333
* [compound assignment expressions]
@@ -59,6 +59,7 @@ A _const context_ is one of the following:
5959
* [statics]
6060
* [enum discriminants]
6161
* A [const generic argument]
62+
* A [const block]
6263

6364
## Const Functions
6465

@@ -106,6 +107,7 @@ Conversely, the following are possible in a const function, but not in a const c
106107
[cast]: expressions/operator-expr.md#type-cast-expressions
107108
[closure expressions]: expressions/closure-expr.md
108109
[comparison]: expressions/operator-expr.md#comparison-operators
110+
[const block]: expressions/block-expr.md#const-blocks
109111
[const functions]: items/functions.md#const-functions
110112
[const generic argument]: items/generics.md#const-generics
111113
[const generic parameters]: items/generics.md#const-generics

src/expressions.md

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup>[](#expression-attributes)\
3636
> &nbsp;&nbsp; (\
3737
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; [_BlockExpression_]\
38+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_ConstBlockExpression_]\
3839
> &nbsp;&nbsp; &nbsp;&nbsp; | [_UnsafeBlockExpression_]\
3940
> &nbsp;&nbsp; &nbsp;&nbsp; | [_LoopExpression_]\
4041
> &nbsp;&nbsp; &nbsp;&nbsp; | [_IfExpression_]\
@@ -311,6 +312,7 @@ They are never allowed before:
311312
[_ClosureExpression_]: expressions/closure-expr.md
312313
[_ComparisonExpression_]: expressions/operator-expr.md#comparison-operators
313314
[_CompoundAssignmentExpression_]: expressions/operator-expr.md#compound-assignment-expressions
315+
[_ConstBlockExpression_]: expressions/block-expr.md#const-blocks
314316
[_ContinueExpression_]: expressions/loop-expr.md#continue-expressions
315317
[_FieldExpression_]: expressions/field-expr.md
316318
[_GroupedExpression_]: expressions/grouped-expr.md

0 commit comments

Comments
 (0)