Skip to content

Commit f13e094

Browse files
authored
Merge branch 'master' into spec-add-identifiers-exprs
2 parents 3055d0d + 9c21bee commit f13e094

32 files changed

+317
-202
lines changed

docs/authoring.md

Lines changed: 3 additions & 2 deletions
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
@@ -154,4 +155,4 @@ The reference does not document which targets exist, or the properties of specif
154155

155156
### Editions
156157

157-
The main text and flow should document only the current edition. Whenever there is a difference between editions, the differences should be called out with an "Edition Differences" block.
158+
The main text and flow should document only the current edition. Whenever there is a difference between editions, the differences should be called out with an "Edition differences" block.

mdbook-spec/src/lib.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(rust_2018_idioms, unused_lifetimes)]
2+
13
use mdbook::book::{Book, Chapter};
24
use mdbook::errors::Error;
35
use mdbook::preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext};
@@ -17,7 +19,7 @@ static RULE_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^r\[([^]]+)]$").unwr
1719
/// The Regex for the syntax for blockquotes that have a specific CSS class,
1820
/// like `> [!WARNING]`.
1921
static ADMONITION_RE: Lazy<Regex> = Lazy::new(|| {
20-
Regex::new(r"(?m)^ *> \[!(?<admon>[^]]+)\]\n(?<blockquote>(?: *> .*\n)+)").unwrap()
22+
Regex::new(r"(?m)^ *> \[!(?<admon>[^]]+)\]\n(?<blockquote>(?: *>.*\n)+)").unwrap()
2123
});
2224

2325
pub fn handle_preprocessing(pre: &dyn Preprocessor) -> Result<(), Error> {
@@ -82,8 +84,8 @@ impl Spec {
8284
}
8385
}
8486
format!(
85-
"<div class=\"rule\" id=\"{rule_id}\">\
86-
<a class=\"rule-link\" href=\"#{rule_id}\">[{rule_id}]</a>\
87+
"<div class=\"rule\" id=\"r-{rule_id}\">\
88+
<a class=\"rule-link\" href=\"#r-{rule_id}\">[{rule_id}]</a>\
8789
</div>\n"
8890
)
8991
})
@@ -102,7 +104,7 @@ impl Spec {
102104
.iter()
103105
.map(|(rule_id, (_, path))| {
104106
let relative = pathdiff::diff_paths(path, current_path).unwrap();
105-
format!("[{rule_id}]: {}#{rule_id}\n", relative.display())
107+
format!("[{rule_id}]: {}#r-{rule_id}\n", relative.display())
106108
})
107109
.collect();
108110
format!(
@@ -129,15 +131,30 @@ impl Spec {
129131
ADMONITION_RE
130132
.replace_all(&chapter.content, |caps: &Captures<'_>| {
131133
let lower = caps["admon"].to_lowercase();
134+
let term = to_initial_case(&caps["admon"]);
135+
let blockquote = &caps["blockquote"];
136+
let initial_spaces = blockquote.chars().position(|ch| ch != ' ').unwrap_or(0);
137+
let space = &blockquote[..initial_spaces];
132138
format!(
133-
"<div class=\"{lower}\">\n\n{}\n\n</div>\n",
134-
&caps["blockquote"]
139+
"{space}<div class=\"{lower}\">\n\
140+
\n\
141+
{space}> ***{term}:***\n\
142+
{blockquote}\n\
143+
\n\
144+
{space}</div>\n",
135145
)
136146
})
137147
.to_string()
138148
}
139149
}
140150

151+
fn to_initial_case(s: &str) -> String {
152+
let mut chars = s.chars();
153+
let first = chars.next().expect("not empty").to_uppercase();
154+
let rest = chars.as_str().to_lowercase();
155+
format!("{first}{rest}")
156+
}
157+
141158
impl Preprocessor for Spec {
142159
fn name(&self) -> &str {
143160
"spec"

mdbook-spec/src/std_links.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn collect_markdown_links(chapter: &Chapter) -> Vec<Link<'_>> {
145145
// Broken links are collected so that you can write something like
146146
// `[std::option::Option]` which in pulldown_cmark's eyes is a broken
147147
// link. However, that is the normal syntax for rustdoc.
148-
let broken_link = |broken_link: BrokenLink| {
148+
let broken_link = |broken_link: BrokenLink<'_>| {
149149
broken_links.push(Link {
150150
link_type: broken_link.link_type,
151151
// Necessary due to lifetime issues.
@@ -205,7 +205,7 @@ fn collect_markdown_links(chapter: &Chapter) -> Vec<Link<'_>> {
205205
/// generate intra-doc links on them.
206206
///
207207
/// The output will be in the given `tmp` directory.
208-
fn run_rustdoc(tmp: &TempDir, chapter_links: &HashMap<&PathBuf, Vec<Link>>) {
208+
fn run_rustdoc(tmp: &TempDir, chapter_links: &HashMap<&PathBuf, Vec<Link<'_>>>) {
209209
let src_path = tmp.path().join("a.rs");
210210
// Allow redundant since there could some in-scope things that are
211211
// technically not necessary, but we don't care about (like

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: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,10 @@ behaviors. `unsafe` code that satisfies this property for any safe client is
1212
called *sound*; if `unsafe` code can be misused by safe code to exhibit
1313
undefined behavior, it is *unsound*.
1414

15-
<div class="warning">
16-
17-
***Warning:*** The following list is not exhaustive; it may grow or shrink.
18-
There is no formal model of Rust's semantics for what is and is not allowed in
19-
unsafe code, so there may be more behavior considered unsafe. We also reserve
20-
the right to make some of the behavior in that list defined in the future. In
21-
other words, this list does not say that anything will *definitely* always be
22-
undefined in all future Rust version (but we might make such commitments for
23-
some list items in the future).
24-
25-
Please read the [Rustonomicon] before writing unsafe code.
26-
27-
</div>
15+
> [!WARNING]
16+
> The following list is not exhaustive; it may grow or shrink. There is no formal model of Rust's semantics for what is and is not allowed in unsafe code, so there may be more behavior considered unsafe. We also reserve the right to make some of the behavior in that list defined in the future. In other words, this list does not say that anything will *definitely* always be undefined in all future Rust version (but we might make such commitments for some list items in the future).
17+
>
18+
> Please read the [Rustonomicon] before writing unsafe code.
2819
2920
* Data races.
3021
* Accessing (loading from or storing to) a place that is [dangling] or [based on
@@ -87,7 +78,7 @@ The span of bytes a pointer or reference "points to" is determined by the pointe
8778
A place is said to be "based on a misaligned pointer" if the last `*` projection
8879
during place computation was performed on a pointer that was not aligned for its
8980
type. (If there is no `*` projection in the place expression, then this is
90-
accessing the field of a local and rustc will guarantee proper alignment. If
81+
accessing the field of a local or `static` and rustc will guarantee proper alignment. If
9182
there are multiple `*` projection, then each of them incurs a load of the
9283
pointer-to-be-dereferenced itself from memory, and each of these loads is
9384
subject to the alignment constraint. Note that some `*` projections can be

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/conditional-compilation.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,8 @@ configuration option from within the source code of the crate being compiled.
5555
> by [Cargo][cargo-feature] for specifying compile-time options and optional
5656
> dependencies.
5757
58-
<div class="warning">
59-
60-
Warning: Arbitrarily-set configuration options can clash with compiler-set configuration options. For example, it is possible to do `rustc --cfg "unix" program.rs` while compiling to a Windows target, and have both `unix` and `windows` configuration options set at the same time. Doing this would be unwise.
61-
62-
</div>
58+
> [!WARNING]
59+
> Arbitrarily-set configuration options can clash with compiler-set configuration options. For example, it is possible to do `rustc --cfg "unix" program.rs` while compiling to a Windows target, and have both `unix` and `windows` configuration options set at the same time. Doing this would be unwise.
6360
6461
### `target_arch`
6562

src/const_eval.md

Lines changed: 13 additions & 25 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

@@ -61,40 +65,24 @@ A _const context_ is one of the following:
6165
* A [const generic argument]
6266
* A [const block]
6367

68+
Const contexts that are used as parts of types (array type and repeat length
69+
expressions as well as const generic arguments) can only make restricted use of
70+
surrounding generic parameters: such an expression must either be a single bare
71+
const generic parameter, or an arbitrary expression not making use of any
72+
generics.
73+
6474
## Const Functions
6575

6676
A _const fn_ is a function that one is permitted to call from a const context. Declaring a function
6777
`const` has no effect on any existing uses, it only restricts the types that arguments and the
68-
return type may use, as well as prevent various expressions from being used within it. You can freely
69-
do anything with a const function that you can do with a regular function.
78+
return type may use, and restricts the function body to constant expressions.
7079

7180
When called from a const context, the function is interpreted by the
7281
compiler at compile time. The interpretation happens in the
7382
environment of the compilation target and not the host. So `usize` is
7483
`32` bits if you are compiling against a `32` bit system, irrelevant
7584
of whether you are building on a `64` bit or a `32` bit system.
7685

77-
Const functions have various restrictions to make sure that they can be
78-
evaluated at compile-time. It is, for example, not possible to write a random
79-
number generator as a const function. Calling a const function at compile-time
80-
will always yield the same result as calling it at runtime, even when called
81-
multiple times. There's one exception to this rule: if you are doing complex
82-
floating point operations in extreme situations, then you might get (very
83-
slightly) different results. It is advisable to not make array lengths and enum
84-
discriminants depend on floating point computations.
85-
86-
87-
Notable features that are allowed in const contexts but not in const functions include:
88-
89-
* floating point operations
90-
* floating point values are treated just like generic parameters without trait bounds beyond
91-
`Copy`. So you cannot do anything with them but copy/move them around.
92-
93-
Conversely, the following are possible in a const function, but not in a const context:
94-
95-
* Use of generic type and lifetime parameters.
96-
* Const contexts do allow limited use of [const generic parameters].
97-
9886
[arithmetic]: expressions/operator-expr.md#arithmetic-and-logical-binary-operators
9987
[array expressions]: expressions/array-expr.md
10088
[array indexing]: expressions/array-expr.md#array-and-slice-indexing-expressions

src/expressions/method-call-expr.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,18 @@ r[expr.method.ambiguious-search]
8080
If a step is reached where there is more than one possible method, such as where generic methods or traits are considered the same, then it is a compiler error.
8181
These cases require a [disambiguating function call syntax] for method and function invocation.
8282
83-
> **Edition Differences**: Before the 2021 edition, during the search for visible methods, if the candidate receiver type is an [array type], methods provided by the standard library [`IntoIterator`] trait are ignored.
83+
> **Edition differences**: Before the 2021 edition, during the search for visible methods, if the candidate receiver type is an [array type], methods provided by the standard library [`IntoIterator`] trait are ignored.
8484
>
8585
> The edition used for this purpose is determined by the token representing the method name.
8686
>
8787
> This special case may be removed in the future.
8888
89-
<div class="warning">
89+
> [!WARNING]
90+
> For [trait objects], if there is an inherent method of the same name as a trait method, it will give a compiler error when trying to call the method in a method call expression.
91+
> Instead, you can call the method using [disambiguating function call syntax], in which case it calls the trait method, not the inherent method.
92+
> There is no way to call the inherent method.
93+
> Just don't define inherent methods on trait objects with the same name as a trait method and you'll be fine.
9094
91-
***Warning:*** For [trait objects], if there is an inherent method of the same name as a trait method, it will give a compiler error when trying to call the method in a method call expression.
92-
Instead, you can call the method using [disambiguating function call syntax], in which case it calls the trait method, not the inherent method.
93-
There is no way to call the inherent method.
94-
Just don't define inherent methods on trait objects with the same name as a trait method and you'll be fine.
95-
96-
</div>
9795
9896
[_CallParams_]: call-expr.md
9997
[_Expression_]: ../expressions.md

src/expressions/operator-expr.md

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -577,14 +577,10 @@ r[expr.as.int-as-pointer]
577577

578578
Casting from an integer to a raw pointer interprets the integer as a memory address and produces a pointer referencing that memory.
579579

580-
<div class="warning">
581-
582-
Warning:
583-
This interacts with the Rust memory model, which is still under development.
584-
A pointer obtained from this cast may suffer additional restrictions even if it is bitwise equal to a valid pointer.
585-
Dereferencing such a pointer may be [undefined behavior] if aliasing rules are not followed.
586-
587-
</div>
580+
> [!WARNING]
581+
> This interacts with the Rust memory model, which is still under development.
582+
> A pointer obtained from this cast may suffer additional restrictions even if it is bitwise equal to a valid pointer.
583+
> Dereferencing such a pointer may be [undefined behavior] if aliasing rules are not followed.
588584
589585
A trivial example of sound address arithmetic:
590586

@@ -810,14 +806,11 @@ fn example() {
810806
r[expr.compound-assign.result]
811807
Like assignment expressions, compound assignment expressions always produce [the unit value][unit].
812808

813-
<div class="warning">
814-
815-
Warning: The evaluation order of operands swaps depending on the types of the operands:
816-
with primitive types the right-hand side will get evaluated first, while with non-primitive types the left-hand side will get evaluated first.
817-
Try not to write code that depends on the evaluation order of operands in compound assignment expressions.
818-
See [this test] for an example of using this dependency.
819-
820-
</div>
809+
> [!WARNING]
810+
> The evaluation order of operands swaps depending on the types of the operands:
811+
> with primitive types the right-hand side will get evaluated first, while with non-primitive types the left-hand side will get evaluated first.
812+
> Try not to write code that depends on the evaluation order of operands in compound assignment expressions.
813+
> See [this test] for an example of using this dependency.
821814
822815
[copies or moves]: ../expressions.md#moved-and-copied-types
823816
[dropping]: ../destructors.md

0 commit comments

Comments
 (0)