Skip to content

Commit 0060118

Browse files
authored
Merge branch 'rust-lang:master' into spec-type-layout-improvements
2 parents cd8bf7b + bff8721 commit 0060118

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+984
-77
lines changed

.github/workflows/main.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,34 @@ jobs:
9898
working-directory: ./mdbook-spec
9999
run: cargo fmt --check
100100

101+
preview:
102+
if: github.event_name == 'pull_request'
103+
runs-on: ubuntu-latest
104+
steps:
105+
- uses: actions/checkout@master
106+
- name: Update rustup
107+
run: rustup self update
108+
- name: Install Rust
109+
run: |
110+
rustup set profile minimal
111+
rustup toolchain install nightly
112+
rustup default nightly
113+
- name: Install mdbook
114+
run: |
115+
mkdir bin
116+
curl -sSL https://github.com/rust-lang/mdBook/releases/download/v${MDBOOK_VERSION}/mdbook-v${MDBOOK_VERSION}-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=bin
117+
echo "$(pwd)/bin" >> $GITHUB_PATH
118+
- name: Build the book
119+
env:
120+
SPEC_RELATIVE: 0
121+
run: mdbook build --dest-dir dist/preview-${{ github.event.pull_request.number }}
122+
- name: Upload artifact
123+
uses: actions/upload-artifact@v4
124+
with:
125+
name: preview-${{ github.event.pull_request.number }}
126+
overwrite: true
127+
path: dist
128+
101129
# The success job is here to consolidate the total success/failure state of
102130
# all other jobs. This job is then included in the GitHub branch protection
103131
# rule which prevents merges unless all other jobs are passing. This makes
@@ -110,6 +138,7 @@ jobs:
110138
- code-tests
111139
- style-tests
112140
- mdbook-spec
141+
# preview is explicitly excluded here since it doesn't run on merge
113142
runs-on: ubuntu-latest
114143
steps:
115144
- run: jq --exit-status 'all(.result == "success")' <<< '${{ toJson(needs) }}'

docs/authoring.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ This document serves as a guide for editors and reviewers. Some conventions and
1515
* Code blocks should have an explicit language tag.
1616
* Do not wrap long lines. This helps with reviewing diffs of the source.
1717
* Use [smart punctuation] instead of Unicode characters. For example, use `---` for em-dash instead of the Unicode character. Characters like em-dash can be difficult to see in a fixed-width editor, and some editors may not have easy methods to enter such characters.
18-
* Links should be relative with the `.md` extension. Links to other rust-lang books that are published with the reference or the standard library API should also be relative so that the linkchecker can validate them.
18+
* Links should be relative with the `.md` extension. Links to other rust-lang books that are published with the reference should also be relative so that the linkchecker can validate them.
19+
* Links to the standard library should use rustdoc-style links described in [Standard library links](#standard-library-links).
1920
* The use of reference links is preferred, with shortcuts if appropriate. Place the sorted link reference definitions at the bottom of the file, or at the bottom of a section if there are an unusually large number of links that are specific to the section.
2021

2122
```markdown
@@ -75,6 +76,29 @@ Rules can be linked to by their ID using markdown such as `[foo.bar]`. There are
7576

7677
In the HTML, the rules are clickable just like headers.
7778

79+
When assigning rules to new paragraphs, or when modifying rule names, use the following guidelines:
80+
81+
1. A rule applies to one core idea, which should be easily determined when reading the paragraph it is applied to.
82+
2. Other than the "intro" paragraph, purely explanatory, expository, or exemplary content does not need a rule. If the expository paragraph isn't directly related to the previous, separate it with a hard (rendered) line break.
83+
* This content will be moved to `[!NOTE]` or more specific admonitions in the future.
84+
3. Rust code examples and tests do not need their own rules.
85+
4. Use the following guidelines for admonitions:
86+
* Notes: Do not include a rule.
87+
* Warning: Omit the rule if the warning follows from the previous paragraph or if the warning is explanatory and doesn't introduce any new rules.
88+
* Target specific behavior: Always include the rule.
89+
* Edition differences: Always include the rule.
90+
5. The following keywords should be used to identify paragraphs when unambiguous:
91+
* `intro`: The beginning paragraph of each section - should explain the construct being defined overall.
92+
* `syntax`: Syntax definitions or explanations when BNF syntax definitions are not used.
93+
* `namespace`: For items only, specifies the namespace(s) the item introduces a name in. May also be used elsewhere when defining a namespace (e.g. `r[attribute.diagnostic.namespace]`).
94+
6. When a rule doesn't fall under the above keywords, or for section rule ids, name the subrule as follows:
95+
* If the rule is naming a specific Rust language construct (e.g. an attribute, standard library type/function, or keyword-introduced concept), use the construct as named in the language, appropriately case-adjusted (but do not replace `_`s with `-`s).
96+
* Other than Rust language concepts with `_`s in the name, use `-` characters to separate words within a "subrule".
97+
* Whenever possible, do not repeat previous components of the rule.
98+
* Edition differences admonitions should typically be named by the edition referenced directly by the rule. If multiple editions are named, use the one for which the behavior is defined by the admonition, and not by a previous paragraph.
99+
* Target specific admonitions should typically be named by the least specific target property to which they apply (e.g. if a rule affects all x86 CPUs, the rule name should include `x86` rather than separately listing `i586`, `i686` and `x86_64`, and if a rule applies to all ELF platforms, it should be named `elf` rather than listing every ELF OS).
100+
* Use an appropriately descriptive, but short, name if the language does not provide one.
101+
78102
### Standard library links
79103

80104
You should link to the standard library without specifying a URL in a fashion similar to [rustdoc intra-doc links][intra]. Some examples:

src/attributes/type_system.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,35 @@ match message {
163163
}
164164
```
165165

166-
It's also not allowed to cast non-exhaustive types from foreign crates.
167-
```rust, ignore
168-
use othercrate::NonExhaustiveEnum;
166+
It's also not allowed to use numeric casts (`as`) on enums that contain any non-exhaustive variants.
167+
168+
For example, the following enum can be cast because it doesn't contain any non-exhaustive variants:
169+
170+
```rust
171+
#[non_exhaustive]
172+
pub enum Example {
173+
First,
174+
Second
175+
}
176+
```
177+
178+
However, if the enum contains even a single non-exhaustive variant, casting will result in an error. Consider this modified version of the same enum:
179+
180+
```rust
181+
#[non_exhaustive]
182+
pub enum EnumWithNonExhaustiveVariants {
183+
First,
184+
#[non_exhaustive]
185+
Second
186+
}
187+
```
188+
189+
<!-- ignore: needs multiple crates -->
190+
```rust,ignore
191+
use othercrate::EnumWithNonExhaustiveVariants;
169192
170-
// Cannot cast a non-exhaustive enum outside of its defining crate.
171-
let _ = NonExhaustiveEnum::default() as u8;
193+
// Error: cannot cast an enum with a non-exhaustive variant when it's defined in another crate
194+
let _ = EnumWithNonExhaustiveVariants::First as u8;
172195
```
173196

174197
Non-exhaustive types are always considered inhabited in downstream crates.

src/behavior-considered-undefined.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ undefined behavior, it is *unsound*.
4040
All this also applies when values of these
4141
types are passed in a (nested) field of a compound type, but not behind
4242
pointer indirections.
43-
* Mutating immutable bytes. All bytes inside a [`const`] item or within an implicitly [const-promoted] expression are immutable.
43+
* Mutating immutable bytes.
44+
All bytes reachable through a [const-promoted] expression are immutable, as well as bytes reachable through borrows in `static` and `const` initializers that have been [lifetime-extended] to `'static`.
4445
The bytes owned by an immutable binding or immutable `static` are immutable, unless those bytes are part of an [`UnsafeCell<U>`].
4546

4647
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.
@@ -78,7 +79,7 @@ The span of bytes a pointer or reference "points to" is determined by the pointe
7879
A place is said to be "based on a misaligned pointer" if the last `*` projection
7980
during place computation was performed on a pointer that was not aligned for its
8081
type. (If there is no `*` projection in the place expression, then this is
81-
accessing the field of a local and rustc will guarantee proper alignment. If
82+
accessing the field of a local or `static` and rustc will guarantee proper alignment. If
8283
there are multiple `*` projection, then each of them incurs a load of the
8384
pointer-to-be-dereferenced itself from memory, and each of these loads is
8485
subject to the alignment constraint. Note that some `*` projections can be
@@ -179,3 +180,4 @@ reading uninitialized memory is permitted are inside `union`s and in "padding"
179180
[project-tuple]: expressions/tuple-expr.md#tuple-indexing-expressions
180181
[project-slice]: expressions/array-expr.md#array-and-slice-indexing-expressions
181182
[const-promoted]: destructors.md#constant-promotion
183+
[lifetime-extended]: destructors.md#temporary-lifetime-extension

src/comments.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Comments
22

3+
r[comments.syntax]
4+
35
> **<sup>Lexer</sup>**\
46
> LINE_COMMENT :\
57
> &nbsp;&nbsp; &nbsp;&nbsp; `//` (~\[`/` `!` `\n`] | `//`) ~`\n`<sup>\*</sup>\
@@ -34,27 +36,48 @@
3436
3537
## Non-doc comments
3638

39+
r[comments.normal]
40+
3741
Comments follow the general C++ style of line (`//`) and
3842
block (`/* ... */`) comment forms. Nested block comments are supported.
3943

44+
r[comments.normal.tokenization]
4045
Non-doc comments are interpreted as a form of whitespace.
4146

4247
## Doc comments
4348

49+
r[comments.doc]
50+
51+
r[comments.doc.syntax]
4452
Line doc comments beginning with exactly _three_ slashes (`///`), and block
4553
doc comments (`/** ... */`), both outer doc comments, are interpreted as a
46-
special syntax for [`doc` attributes]. That is, they are equivalent to writing
54+
special syntax for [`doc` attributes].
55+
56+
r[comments.doc.attributes]
57+
That is, they are equivalent to writing
4758
`#[doc="..."]` around the body of the comment, i.e., `/// Foo` turns into
48-
`#[doc="Foo"]` and `/** Bar */` turns into `#[doc="Bar"]`.
59+
`#[doc="Foo"]` and `/** Bar */` turns into `#[doc="Bar"]`. They must therefore
60+
appear before something that accepts an outer attribute.
4961

62+
r[comments.doc.inner-syntax]
5063
Line comments beginning with `//!` and block comments `/*! ... */` are
5164
doc comments that apply to the parent of the comment, rather than the item
52-
that follows. That is, they are equivalent to writing `#![doc="..."]` around
65+
that follows.
66+
67+
r[comments.doc.inner-attributes]
68+
That is, they are equivalent to writing `#![doc="..."]` around
5369
the body of the comment. `//!` comments are usually used to document
5470
modules that occupy a source file.
5571

72+
r[comments.doc.bare-crs]
5673
The character `U+000D` (CR) is not allowed in doc comments.
5774

75+
> **Note**: It is conventional for doc comments to contain Markdown, as expected by
76+
> `rustdoc`. However, the comment syntax does not respect any internal Markdown.
77+
> ``/** `glob = "*/*.rs";` */`` terminates the comment at the first `*/`, and the
78+
> remaining code would cause a syntax error. This slightly limits the content of
79+
> block doc comments compared to line doc comments.
80+
5881
> **Note**: The sequence `U+000D` (CR) immediately followed by `U+000A` (LF) would have been previously transformed into a single `U+000A` (LF).
5982
6083
## Examples

src/const_eval.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ to be run.
3838
* [Closure expressions] which don't capture variables from the environment.
3939
* Built-in [negation], [arithmetic], [logical], [comparison] or [lazy boolean]
4040
operators used on integer and floating point types, `bool`, and `char`.
41-
* Shared [borrow]s, except if applied to a type with [interior mutability].
42-
* The [dereference operator] except for raw pointers.
41+
* All forms of [borrow]s, including raw borrows, with one limitation:
42+
mutable borrows and shared borrows to values with interior mutability
43+
are only allowed to refer to *transient* places. A place is *transient*
44+
if its lifetime is strictly contained inside the current [const context].
45+
* The [dereference operator].
4346
* [Grouped] expressions.
4447
* [Cast] expressions, except
4548
* pointer to address casts and
@@ -49,6 +52,7 @@ to be run.
4952
* [if], [`if let`] and [match] expressions.
5053

5154
## Const context
55+
[const context]: #const-context
5256

5357
A _const context_ is one of the following:
5458

src/destructors.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,18 @@ let x = &mut 0;
325325
println!("{}", x);
326326
```
327327

328+
r[destructors.scope.lifetime-extension.static]
329+
Lifetime extension also applies to `static` and `const` items, where it
330+
makes temporaries live until the end of the program. For example:
331+
332+
```rust
333+
const C: &Vec<i32> = &Vec::new();
334+
// Usually this would be a dangling reference as the `Vec` would only
335+
// exist inside the initializer expression of `C`, but instead the
336+
// borrow gets lifetime-extended so it effectively has `'static` lifetime.
337+
println!("{:?}", C);
338+
```
339+
328340
r[destructors.scope.lifetime-extension.sub-expressions]
329341
If a [borrow][borrow expression], [dereference][dereference expression],
330342
[field][field expression], or [tuple indexing expression] has an extended

src/inline-assembly.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ The following ABIs can be used with `clobber_abi`:
484484
| AArch64 | `"C"`, `"system"`, `"efiapi"` | `x[0-17]`, `x18`\*, `x30`, `v[0-31]`, `p[0-15]`, `ffr` |
485485
| ARM | `"C"`, `"system"`, `"efiapi"`, `"aapcs"` | `r[0-3]`, `r12`, `r14`, `s[0-15]`, `d[0-7]`, `d[16-31]` |
486486
| RISC-V | `"C"`, `"system"`, `"efiapi"` | `x1`, `x[5-7]`, `x[10-17]`, `x[28-31]`, `f[0-7]`, `f[10-17]`, `f[28-31]`, `v[0-31]` |
487-
| LoongArch | `"C"`, `"system"`, `"efiapi"` | `$r1`, `$r[4-20]`, `$f[0-23]` |
487+
| LoongArch | `"C"`, `"system"` | `$r1`, `$r[4-20]`, `$f[0-23]` |
488488

489489
> Notes:
490490
> - On AArch64 `x18` only included in the clobber list if it is not considered as a reserved register on the target.

src/input-format.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
11
# Input format
22

3+
r[input]
4+
5+
r[input.intro]
36
This chapter describes how a source file is interpreted as a sequence of tokens.
47

58
See [Crates and source files] for a description of how programs are organised into files.
69

710
## Source encoding
811

12+
r[input.encoding]
13+
14+
r[input.encoding.utf8]
915
Each source file is interpreted as a sequence of Unicode characters encoded in UTF-8.
16+
17+
r[input.encoding.invalid]
1018
It is an error if the file is not valid UTF-8.
1119

1220
## Byte order mark removal
1321

22+
r[input.byte-order-mark]
23+
1424
If the first character in the sequence is `U+FEFF` ([BYTE ORDER MARK]), it is removed.
1525

1626
## CRLF normalization
1727

28+
r[input.crlf]
29+
1830
Each pair of characters `U+000D` (CR) immediately followed by `U+000A` (LF) is replaced by a single `U+000A` (LF).
1931

2032
Other occurrences of the character `U+000D` (CR) are left in place (they are treated as [whitespace]).
2133

2234
## Shebang removal
2335

36+
r[input.shebang]
37+
38+
r[input.shebang.intro]
2439
If the remaining sequence begins with the characters `#!`, the characters up to and including the first `U+000A` (LF) are removed from the sequence.
2540

2641
For example, the first line of the following file would be ignored:
@@ -34,15 +49,17 @@ fn main() {
3449
}
3550
```
3651

52+
r[input.shebang.inner-attribute]
3753
As an exception, if the `#!` characters are followed (ignoring intervening [comments] or [whitespace]) by a `[` token, nothing is removed.
3854
This prevents an [inner attribute] at the start of a source file being removed.
3955

4056
> **Note**: The standard library [`include!`] macro applies byte order mark removal, CRLF normalization, and shebang removal to the file it reads. The [`include_str!`] and [`include_bytes!`] macros do not.
4157
4258
## Tokenization
4359

44-
The resulting sequence of characters is then converted into tokens as described in the remainder of this chapter.
60+
r[input.tokenization]
4561

62+
The resulting sequence of characters is then converted into tokens as described in the remainder of this chapter.
4663

4764
[inner attribute]: attributes.md
4865
[BYTE ORDER MARK]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8

src/interior-mutability.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
# Interior Mutability
22

3+
r[interior-mut]
4+
5+
r[interior-mut.intro]
36
Sometimes a type needs to be mutated while having multiple aliases. In Rust this
4-
is achieved using a pattern called _interior mutability_. A type has interior
5-
mutability if its internal state can be changed through a [shared reference] to
6-
it. This goes against the usual [requirement][ub] that the value pointed to by a
7+
is achieved using a pattern called _interior mutability_.
8+
9+
r[interior-mut.shared-ref]
10+
A type has interior mutability if its internal state can be changed through a [shared reference] to
11+
it.
12+
13+
r[interior-mut.no-constraint]
14+
This goes against the usual [requirement][ub] that the value pointed to by a
715
shared reference is not mutated.
816

17+
r[interior-mut.unsafe-cell]
918
[`std::cell::UnsafeCell<T>`] type is the only allowed way to disable
1019
this requirement. When `UnsafeCell<T>` is immutably aliased, it is still safe to
11-
mutate, or obtain a mutable reference to, the `T` it contains. As with all
12-
other types, it is undefined behavior to have multiple `&mut UnsafeCell<T>`
20+
mutate, or obtain a mutable reference to, the `T` it contains.
21+
22+
r[interior-mut.mut-unsafe-cell]
23+
As with all other types, it is undefined behavior to have multiple `&mut UnsafeCell<T>`
1324
aliases.
1425

26+
r[interior-mut.abstraction]
1527
Other types with interior mutability can be created by using `UnsafeCell<T>` as
1628
a field. The standard library provides a variety of types that provide safe
17-
interior mutability APIs. For example, [`std::cell::RefCell<T>`] uses run-time
18-
borrow checks to ensure the usual rules around multiple references. The
19-
[`std::sync::atomic`] module contains types that wrap a value that is only
29+
interior mutability APIs.
30+
31+
r[interior-mut.ref-cell]
32+
For example, [`std::cell::RefCell<T>`] uses run-time borrow checks to ensure the usual rules around multiple references.
33+
34+
r[interior-mut.atomic]
35+
The [`std::sync::atomic`] module contains types that wrap a value that is only
2036
accessed with atomic operations, allowing the value to be shared and mutated
2137
across threads.
2238

src/items/constant-items.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ guaranteed to refer to the same memory address.
1414
The constant declaration defines the constant value in the [value namespace] of the module or block where it is located.
1515

1616
Constants must be explicitly typed. The type must have a `'static` lifetime: any
17-
references in the initializer must have `'static` lifetimes.
17+
references in the initializer must have `'static` lifetimes. References
18+
in the type of a constant default to `'static` lifetime; see [static lifetime
19+
elision].
1820

19-
Constants may refer to the address of other constants, in which case the
20-
address will have elided lifetimes where applicable, otherwise -- in most cases
21-
-- defaulting to the `static` lifetime. (See [static lifetime
22-
elision].) The compiler is, however, still at liberty to translate the constant
23-
many times, so the address referred to may not be stable.
21+
A reference to a constant will have `'static` lifetime if the constant value is eligible for
22+
[promotion]; otherwise, a temporary will be created.
2423

2524
```rust
2625
const BIT1: u32 = 1 << 0;
@@ -118,3 +117,4 @@ fn unused_generic_function<T>() {
118117
[_Expression_]: ../expressions.md
119118
[`Copy`]: ../special-types-and-traits.md#copy
120119
[value namespace]: ../names/namespaces.md
120+
[promotion]: ../destructors.md#constant-promotion

src/items/functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ Functions qualified with the `const` keyword are [const functions], as are
227227
[tuple struct] and [tuple variant] constructors. _Const functions_ can be
228228
called from within [const contexts].
229229

230-
Const functions may use the [`extern`] function qualifier, but only with the `"Rust"` and `"C"` ABIs.
230+
Const functions may use the [`extern`] function qualifier.
231231

232232
Const functions are not allowed to be [async](#async-functions).
233233

0 commit comments

Comments
 (0)