Skip to content

Commit 812cc68

Browse files
committed
Merge branch 'master' of https://github.com/rust-lang/reference into c-unwind-documentation
2 parents 1658632 + 3c36417 commit 812cc68

30 files changed

+468
-76
lines changed

.github/workflows/main.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
name: CI
2-
on: [push, pull_request]
2+
on:
3+
pull_request:
4+
merge_group:
35

46
jobs:
57
test:

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
- [Code generation](attributes/codegen.md)
4545
- [Limits](attributes/limits.md)
4646
- [Type System](attributes/type_system.md)
47+
- [Debugger](attributes/debugger.md)
4748

4849
- [Statements and expressions](statements-and-expressions.md)
4950
- [Statements](statements.md)

src/attributes.md

+3
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ The following is an index of all built-in attributes.
271271
- Type System
272272
- [`non_exhaustive`] — Indicate that a type will have more fields/variants
273273
added in future.
274+
- Debugger
275+
- [`debugger_visualizer`] — Embeds a file that specifies debugger output for a type.
274276

275277
[Doc comments]: comments.md#doc-comments
276278
[ECMA-334]: https://www.ecma-international.org/publications/standards/Ecma-334.htm
@@ -291,6 +293,7 @@ The following is an index of all built-in attributes.
291293
[`cold`]: attributes/codegen.md#the-cold-attribute
292294
[`crate_name`]: crates-and-source-files.md#the-crate_name-attribute
293295
[`crate_type`]: linkage.md
296+
[`debugger_visualizer`]: attributes/debugger.md#the-debugger_visualizer-attribute
294297
[`deny`]: attributes/diagnostics.md#lint-check-attributes
295298
[`deprecated`]: attributes/diagnostics.md#the-deprecated-attribute
296299
[`derive`]: attributes/derive.md

src/attributes/codegen.md

+20-8
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ Feature | Implicitly Enables | Description
8989
`bmi1` | | [BMI1] — Bit Manipulation Instruction Sets
9090
`bmi2` | | [BMI2] — Bit Manipulation Instruction Sets 2
9191
`cmpxchg16b`| | [`cmpxchg16b`] - Compares and exchange 16 bytes (128 bits) of data atomically
92+
`f16c` | `avx` | [F16C] — 16-bit floating point conversion instructions
9293
`fma` | `avx` | [FMA3] — Three-operand fused multiply-add
9394
`fxsr` | | [`fxsave`] and [`fxrstor`] — Save and restore x87 FPU, MMX Technology, and SSE State
9495
`lzcnt` | | [`lzcnt`] — Leading zeros count
96+
`movbe` | | [`movbe`] - Move data after swapping bytes
9597
`pclmulqdq` | `sse2` | [`pclmulqdq`] — Packed carry-less multiplication quadword
9698
`popcnt` | | [`popcnt`] — Count of bits set to 1
9799
`rdrand` | | [`rdrand`] — Read random number
@@ -117,10 +119,12 @@ Feature | Implicitly Enables | Description
117119
[BMI1]: https://en.wikipedia.org/wiki/Bit_Manipulation_Instruction_Sets
118120
[BMI2]: https://en.wikipedia.org/wiki/Bit_Manipulation_Instruction_Sets#BMI2
119121
[`cmpxchg16b`]: https://www.felixcloutier.com/x86/cmpxchg8b:cmpxchg16b
122+
[F16C]: https://en.wikipedia.org/wiki/F16C
120123
[FMA3]: https://en.wikipedia.org/wiki/FMA_instruction_set
121124
[`fxsave`]: https://www.felixcloutier.com/x86/fxsave
122125
[`fxrstor`]: https://www.felixcloutier.com/x86/fxrstor
123126
[`lzcnt`]: https://www.felixcloutier.com/x86/lzcnt
127+
[`movbe`]: https://www.felixcloutier.com/x86/movbe
124128
[`pclmulqdq`]: https://www.felixcloutier.com/x86/pclmulqdq
125129
[`popcnt`]: https://www.felixcloutier.com/x86/popcnt
126130
[`rdrand`]: https://en.wikipedia.org/wiki/RdRand
@@ -357,15 +361,20 @@ trait object whose methods are attributed.
357361

358362
## The `instruction_set` attribute
359363

360-
The *`instruction_set` attribute* may be applied to a function to enable code generation for a specific
361-
instruction set supported by the target architecture. It uses the [_MetaListPath_] syntax and a path
362-
comprised of the architecture and instruction set to specify how to generate the code for
363-
architectures where a single program may utilize multiple instruction sets.
364+
The *`instruction_set` [attribute]* may be applied to a function to control which instruction set the function will be generated for.
365+
This allows mixing more than one instruction set in a single program on CPU architectures that support it.
366+
It uses the [_MetaListPath_] syntax, and a path comprised of the architecture family name and instruction set name.
364367

365-
The following values are available on targets for the `ARMv4` and `ARMv5te` architectures:
368+
[_MetaListPath_]: ../attributes.md#meta-item-attribute-syntax
369+
370+
It is a compilation error to use the `instruction_set` attribute on a target that does not support it.
371+
372+
### On ARM
366373

367-
* `arm::a32` - Uses ARM code.
368-
* `arm::t32` - Uses Thumb code.
374+
For the `ARMv4T` and `ARMv5te` architectures, the following are supported:
375+
376+
* `arm::a32` - Generate the function as A32 "ARM" code.
377+
* `arm::t32` - Generate the function as T32 "Thumb" code.
369378

370379
<!-- ignore: arm-only -->
371380
```rust,ignore
@@ -376,4 +385,7 @@ fn foo_arm_code() {}
376385
fn bar_thumb_code() {}
377386
```
378387

379-
[_MetaListPath_]: ../attributes.md#meta-item-attribute-syntax
388+
Using the `instruction_set` attribute has the following effects:
389+
390+
* If the address of the function is taken as a function pointer, the low bit of the address will be set to 0 (arm) or 1 (thumb) depending on the instruction set.
391+
* Any inline assembly in the function must use the specified instruction set instead of the target default.

src/attributes/debugger.md

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Debugger attributes
2+
3+
The following [attributes] are used for enhancing the debugging experience when using third-party debuggers like GDB or WinDbg.
4+
5+
## The `debugger_visualizer` attribute
6+
7+
The *`debugger_visualizer` attribute* can be used to embed a debugger visualizer file into the debug information.
8+
This enables an improved debugger experience for displaying values in the debugger.
9+
It uses the [_MetaListNameValueStr_] syntax to specify its inputs, and must be specified as a crate attribute.
10+
11+
### Using `debugger_visualizer` with Natvis
12+
13+
Natvis is an XML-based framework for Microsoft debuggers (such as Visual Studio and WinDbg) that uses declarative rules to customize the display of types.
14+
For detailed information on the Natvis format, refer to Microsoft's [Natvis documentation].
15+
16+
This attribute only supports embedding Natvis files on `-windows-msvc` targets.
17+
18+
The path to the Natvis file is specified with the `natvis_file` key, which is a path relative to the crate source file:
19+
20+
<!-- ignore: requires external files, and msvc -->
21+
```rust ignore
22+
#![debugger_visualizer(natvis_file = "Rectangle.natvis")]
23+
24+
struct FancyRect {
25+
x: f32,
26+
y: f32,
27+
dx: f32,
28+
dy: f32,
29+
}
30+
31+
fn main() {
32+
let fancy_rect = FancyRect { x: 10.0, y: 10.0, dx: 5.0, dy: 5.0 };
33+
println!("set breakpoint here");
34+
}
35+
```
36+
37+
and `Rectangle.natvis` contains:
38+
39+
```xml
40+
<?xml version="1.0" encoding="utf-8"?>
41+
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
42+
<Type Name="foo::FancyRect">
43+
<DisplayString>({x},{y}) + ({dx}, {dy})</DisplayString>
44+
<Expand>
45+
<Synthetic Name="LowerLeft">
46+
<DisplayString>({x}, {y})</DisplayString>
47+
</Synthetic>
48+
<Synthetic Name="UpperLeft">
49+
<DisplayString>({x}, {y + dy})</DisplayString>
50+
</Synthetic>
51+
<Synthetic Name="UpperRight">
52+
<DisplayString>({x + dx}, {y + dy})</DisplayString>
53+
</Synthetic>
54+
<Synthetic Name="LowerRight">
55+
<DisplayString>({x + dx}, {y})</DisplayString>
56+
</Synthetic>
57+
</Expand>
58+
</Type>
59+
</AutoVisualizer>
60+
```
61+
62+
When viewed under WinDbg, the `fancy_rect` variable would be shown as follows:
63+
64+
```text
65+
> Variables:
66+
> fancy_rect: (10.0, 10.0) + (5.0, 5.0)
67+
> LowerLeft: (10.0, 10.0)
68+
> UpperLeft: (10.0, 15.0)
69+
> UpperRight: (15.0, 15.0)
70+
> LowerRight: (15.0, 10.0)
71+
```
72+
73+
### Using `debugger_visualizer` with GDB
74+
75+
GDB supports the use of a structured Python script, called a *pretty printer*, that describes how a type should be visualized in the debugger view.
76+
For detailed information on pretty printers, refer to GDB's [pretty printing documentation].
77+
78+
Embedded pretty printers are not automatically loaded when debugging a binary under GDB.
79+
There are two ways to enable auto-loading embedded pretty printers:
80+
1. Launch GDB with extra arguments to explicitly add a directory or binary to the auto-load safe path: `gdb -iex "add-auto-load-safe-path safe-path path/to/binary" path/to/binary`
81+
For more information, see GDB's [auto-loading documentation].
82+
1. Create a file named `gdbinit` under `$HOME/.config/gdb` (you may need to create the directory if it doesn't already exist). Add the following line to that file: `add-auto-load-safe-path path/to/binary`.
83+
84+
These scripts are embedded using the `gdb_script_file` key, which is a path relative to the crate source file.
85+
86+
<!-- ignore: requires external files -->
87+
```rust ignore
88+
#![debugger_visualizer(gdb_script_file = "printer.py")]
89+
90+
struct Person {
91+
name: String,
92+
age: i32,
93+
}
94+
95+
fn main() {
96+
let bob = Person { name: String::from("Bob"), age: 10 };
97+
println!("set breakpoint here");
98+
}
99+
```
100+
101+
and `printer.py` contains:
102+
103+
```python
104+
import gdb
105+
106+
class PersonPrinter:
107+
"Print a Person"
108+
109+
def __init__(self, val):
110+
self.val = val
111+
self.name = val["name"]
112+
self.age = int(val["age"])
113+
114+
def to_string(self):
115+
return "{} is {} years old.".format(self.name, self.age)
116+
117+
def lookup(val):
118+
lookup_tag = val.type.tag
119+
if lookup_tag is None:
120+
return None
121+
if "foo::Person" == lookup_tag:
122+
return PersonPrinter(val)
123+
124+
return None
125+
126+
gdb.current_objfile().pretty_printers.append(lookup)
127+
```
128+
129+
When the crate's debug executable is passed into GDB[^rust-gdb], `print bob` will display:
130+
131+
```text
132+
"Bob" is 10 years old.
133+
```
134+
135+
[^rust-gdb]: Note: This assumes you are using the `rust-gdb` script which configures pretty-printers for standard library types like `String`.
136+
137+
[auto-loading documentation]: https://sourceware.org/gdb/onlinedocs/gdb/Auto_002dloading-safe-path.html
138+
[attributes]: ../attributes.md
139+
[Natvis documentation]: https://docs.microsoft.com/en-us/visualstudio/debugger/create-custom-views-of-native-objects
140+
[pretty printing documentation]: https://sourceware.org/gdb/onlinedocs/gdb/Pretty-Printing.html
141+
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax

src/attributes/diagnostics.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ check on and off:
4949

5050
```rust
5151
#[warn(missing_docs)]
52-
pub mod m2{
52+
pub mod m2 {
5353
#[allow(missing_docs)]
5454
pub mod nested {
5555
// Missing documentation is ignored here

src/behavior-considered-undefined.md

+19-6
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ code.
4242
All this also applies when values of these
4343
types are passed in a (nested) field of a compound type, but not behind
4444
pointer indirections.
45-
* Mutating immutable data. All data inside a [`const`] item is immutable. Moreover, all
46-
data reached through a shared reference or data owned by an immutable binding
47-
is immutable, unless that data is contained within an [`UnsafeCell<U>`].
45+
* Mutating immutable bytes. All bytes inside a [`const`] item are immutable.
46+
The bytes owned by an immutable binding are immutable, unless those bytes are part of an [`UnsafeCell<U>`].
47+
48+
Moreover, the bytes [pointed to] by a shared reference, including transitively through other references (both shared and mutable) and `Box`es, are immutable; transitivity includes those references stored in fields of compound types.
49+
50+
A mutation is any write of more than 0 bytes which overlaps with any of the relevant bytes (even if that write does not change the memory contents).
4851
* Invoking undefined behavior via compiler intrinsics.
4952
* Executing code compiled with platform features that the current platform
5053
does not support (see [`target_feature`]), *except* if the platform explicitly documents this to be safe.
@@ -79,6 +82,11 @@ code.
7982
> `rustc_layout_scalar_valid_range_*` attributes.
8083
* Incorrect use of inline assembly. For more details, refer to the [rules] to
8184
follow when writing code that uses inline assembly.
85+
* **In [const context](const_eval.md#const-context)**: transmuting or otherwise
86+
reinterpreting a pointer (reference, raw pointer, or function pointer) into
87+
some allocated object as a non-pointer type (such as integers).
88+
'Reinterpreting' refers to loading the pointer value at integer type without a
89+
cast, e.g. by doing raw pointer casts or using a union.
8290

8391
**Note:** Uninitialized memory is also implicitly invalid for any type that has
8492
a restricted set of valid values. In other words, the only cases in which
@@ -91,13 +99,16 @@ reading uninitialized memory is permitted are inside `union`s and in "padding"
9199
> vice versa, undefined behavior in Rust can cause adverse affects on code
92100
> executed by any FFI calls to other languages.
93101
102+
### Pointed-to bytes
103+
104+
The span of bytes a pointer or reference "points to" is determined by the pointer value and the size of the pointee type (using `size_of_val`).
105+
94106
### Dangling pointers
95107
[dangling]: #dangling-pointers
96108

97109
A reference/pointer is "dangling" if it is null or not all of the bytes it
98-
points to are part of the same live allocation (so in particular they all have to be
99-
part of *some* allocation). The span of bytes it points to is determined by the
100-
pointer value and the size of the pointee type (using `size_of_val`).
110+
[points to] are part of the same live allocation (so in particular they all have to be
111+
part of *some* allocation).
101112

102113
If the size is 0, then the pointer must either point inside of a live allocation
103114
(including pointing just after the last byte of the allocation), or it must be
@@ -121,3 +132,5 @@ must never exceed `isize::MAX`.
121132
[dereference expression]: expressions/operator-expr.md#the-dereference-operator
122133
[place expression context]: expressions.md#place-expressions-and-value-expressions
123134
[rules]: inline-assembly.md#rules-for-inline-assembly
135+
[points to]: #pointed-to-bytes
136+
[pointed to]: #pointed-to-bytes

src/comments.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Non-doc comments are interpreted as a form of whitespace.
4242
## Doc comments
4343

4444
Line doc comments beginning with exactly _three_ slashes (`///`), and block
45-
doc comments (`/** ... */`), both inner doc comments, are interpreted as a
45+
doc comments (`/** ... */`), both outer doc comments, are interpreted as a
4646
special syntax for [`doc` attributes]. That is, they are equivalent to writing
4747
`#[doc="..."]` around the body of the comment, i.e., `/// Foo` turns into
4848
`#[doc="Foo"]` and `/** Bar */` turns into `#[doc="Bar"]`.

src/conditional-compilation.md

+7
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ Example values:
129129
* `"dragonfly"`
130130
* `"openbsd"`
131131
* `"netbsd"`
132+
* `"none"` (typical for embedded targets)
132133

133134
### `target_family`
134135

@@ -254,6 +255,12 @@ It is written as `cfg`, `(`, a configuration predicate, and finally `)`.
254255
If the predicate is true, the thing is rewritten to not have the `cfg` attribute
255256
on it. If the predicate is false, the thing is removed from the source code.
256257

258+
When a crate-level `cfg` has a false predicate, the behavior is slightly
259+
different: any crate attributes preceding the `cfg` are kept, and any crate
260+
attributes following the `cfg` are removed. This allows `#![no_std]` and
261+
`#![no_core]` crates to avoid linking `std`/`core` even if a `#![cfg(...)]` has
262+
removed the entire crate.
263+
257264
Some examples on functions:
258265

259266
```rust

src/destructors.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ An *extending pattern* is either
285285
* An [identifier pattern] that binds by reference or mutable reference.
286286
* A [struct][struct pattern], [tuple][tuple pattern], [tuple struct][tuple
287287
struct pattern], or [slice][slice pattern] pattern where at least one of the
288-
direct subpatterns is a extending pattern.
288+
direct subpatterns is an extending pattern.
289289

290290
So `ref x`, `V(ref x)` and `[ref x, y]` are all extending patterns, but `x`,
291291
`&ref x` and `&(ref x,)` are not.

src/expressions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ Explicitly, the assignee expressions are:
162162
- Place expressions.
163163
- [Underscores][_UnderscoreExpression_].
164164
- [Tuples][_TupleExpression_] of assignee expressions.
165-
- [Slices][_ArrayExpression_] of assingee expressions.
165+
- [Slices][_ArrayExpression_] of assignee expressions.
166166
- [Tuple structs][_StructExpression_] of assignee expressions.
167167
- [Structs][_StructExpression_] of assignee expressions (with optionally named
168168
fields).

src/expressions/call-expr.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ trait Pretty {
4848
}
4949

5050
trait Ugly {
51-
fn print(&self);
51+
fn print(&self);
5252
}
5353

5454
struct Foo;

src/expressions/loop-expr.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,27 @@ A `break` expression is only permitted in the body of a loop, and has one of the
249249
> &nbsp;&nbsp; [_BlockExpression_]
250250
251251
Labelled block expressions are exactly like block expressions, except that they allow using `break` expressions within the block.
252-
Unlike other loops, `break` expressions within a label expression *must* have a label (i.e. the label is not optional).
253-
Unlike other loops, labelled block expressions *must* begin with a label.
252+
Unlike loops, `break` expressions within a labelled block expression *must* have a label (i.e. the label is not optional).
253+
Similarly, labelled block expressions *must* begin with a label.
254+
255+
```rust
256+
# fn do_thing() {}
257+
# fn condition_not_met() -> bool { true }
258+
# fn do_next_thing() {}
259+
# fn do_last_thing() {}
260+
let result = 'block: {
261+
do_thing();
262+
if condition_not_met() {
263+
break 'block 1;
264+
}
265+
do_next_thing();
266+
if condition_not_met() {
267+
break 'block 2;
268+
}
269+
do_last_thing();
270+
3
271+
};
272+
```
254273

255274
## `continue` expressions
256275

0 commit comments

Comments
 (0)