Skip to content

Commit 31dc83f

Browse files
committedMar 4, 2021
More editorial changes to expressions
* Go back to calling the syntactic elements "expressions", but name them operands afterwards. * Use "called XYZ" to name something in syntax. * Trade out a few uses of expressions using letters for operand names for the actual operand names. * Pick arbitrary names for the operands. I don't like some of them, but they're not set in stone. * Say they're not set in stone in the main expressions chapter.
1 parent 3da65a1 commit 31dc83f

File tree

8 files changed

+39
-30
lines changed

8 files changed

+39
-30
lines changed
 

‎src/expressions.md

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ In this way, the structure of expressions dictates the structure of execution.
5555
Blocks are just another kind of expression, so blocks, statements, expressions,
5656
and blocks again can recursively nest inside each other to an arbitrary depth.
5757

58+
> **Note**: We give names to the operands of expressions so that we may discuss
59+
> them, but these names are not stable and may be changed.
60+
5861
## Expression precedence
5962

6063
The precedence of Rust operators and expressions is ordered as follows, going

‎src/expressions/array-expr.md

+13-9
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@
1414
Array expressions come in two forms.
1515

1616
The first form lists out every value in the array.
17-
The syntax for this form is a comma-separated list of operands of uniform type enclosed in square brackets.
17+
The syntax for this form is a comma-separated list of expressions of uniform type enclosed in square brackets.
1818
This produces an array containing each of these values in the order they are written.
1919

20-
The syntax for the second form is two operands separated by a semicolon (`;`) enclosed in square brackets.
21-
The operand after the `;` must have type `usize` and be a [constant expression], such as a [literal] or a [constant item].
22-
`[a; b]` creates an array containing `b` copies of the value of `a`.
23-
If the operand after the semicolon has a value greater than 1 then this requires that the type of `a` is [`Copy`], or `a` must be a path to a constant item.
20+
The syntax for the second form is two expressions separated by a semicolon (`;`) enclosed in square brackets.
21+
The expression before the `;` is called the *repeat operand*.
22+
The expression after the `;` is called the *length operand*.
23+
It must have type `usize` and be a [constant expression], such as a [literal] or a [constant item].
24+
An array expression of this form creates an array with the length of the value of the legnth operand with each element a copy of the repeat operand.
25+
That is, `[a; b]` creates an array containing `b` copies of the value of `a`.
26+
If the length operand has a value greater than 1 then this requires that the type of the repeat operand is [`Copy`] or that it must be a [path] to a constant item.
2427

25-
When the repeat expression, `a`, is a constant item, it is evaluated `b` times.
26-
If `b` is 0, the constant item is not evaluated at all.
27-
For expressions that are not a constant item, it is evaluated exactly once, and then the result is copied `b` times.
28+
When the repeat operand is a constant item, it is evaluated the length operand's value times.
29+
If that value is `0`, then the constant item is not evaluated at all.
30+
For expressions that are not a constant item, it is evaluated exactly once, and then the result is copied the length operand's value times.
2831

2932
<div class="warning">
3033

31-
Warning: In the case where `b` is 0, and `a` is a non-constant item, there is currently a bug in `rustc` where the value `a` is evaluated but not dropped, thus causing a leak.
34+
Warning: In the case where the length operand is 0, and the repeat operand is a non-constant item, there is currently a bug in `rustc` where the value `a` is evaluated but not dropped, thus causing a leak.
3235
See [issue #74836](https://github.com/rust-lang/rust/issues/74836).
3336

3437
</div>
@@ -95,4 +98,5 @@ The array index expression can be implemented for types other than arrays and sl
9598
[constant item]: ../items/constant-items.md
9699
[literal]: ../tokens.md#literals
97100
[memory location]: ../expressions.md#place-expressions-and-value-expressions
101+
[path]: path-expr.md
98102
[slice]: ../types/slice.md

‎src/expressions/await-expr.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
> &nbsp;&nbsp; [_Expression_] `.` `await`
66
77
*Await expressions* suspend the current computation until the given future is ready to produce a value.
8-
The syntax for an await expression is an operand with a type that implements the Future trait, `.`, and then the `await` keyword.
8+
The syntax for an await expression is an expression with a type that implements the Future trait, called the *future operand*, then the token `.`, and then the `await` keyword.
99
Await expressions are legal only within an [async context], like an [`async fn`] or an [`async` block].
1010

11-
More specifically, an `<expr>.await` expression has the following effect.
11+
More specifically, an await expression has the following effect.
1212

13-
1. Evaluate `<expr>` to a [future] `tmp`;
13+
1. Evaluate the future operand to a [future] `tmp`;
1414
2. Pin `tmp` using [`Pin::new_unchecked`];
1515
3. This pinned future is then polled by calling the [`Future::poll`] method and passing it the current [task context](#task-context);
1616
3. If the call to `poll` returns [`Poll::Pending`], then the future returns `Poll::Pending`, suspending its state so that, when the surrounding async context is re-polled,execution returns to step 2;
@@ -25,11 +25,11 @@ Because `await` expressions are only legal in an async context, there must be so
2525

2626
## Approximate desugaring
2727

28-
Effectively, an `<expr>.await` expression is roughly equivalent to the following non-normative desugaring:
28+
Effectively, an await expression is roughly equivalent to the following non-normative desugaring:
2929

3030
<!-- ignore: example expansion -->
3131
```rust,ignore
32-
match /* <expr> */ {
32+
match future_operand {
3333
mut pinned => loop {
3434
let mut pin = unsafe { Pin::new_unchecked(&mut pinned) };
3535
match Pin::future::poll(Pin::borrow(&mut pin), &mut current_context) {

‎src/expressions/block-expr.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ A *block expression*, or *block*, is a control flow expression and anonymous nam
1616
As a control flow expression, a block sequentially executes its component non-item declaration statements and then its final optional expression.
1717
As an anonymous namespace scope, item declarations are only in scope inside the block itself and variables declared by `let` statements are in scope from the next statement until the end of the block.
1818

19-
The syntax for a block is `{`, then any [inner attributes], then [statements], then an optional operand, and finally a `}`.
19+
The syntax for a block is `{`, then any [inner attributes], then any number of [statements], then an optional expression, called the final operand, and finally a `}`.
2020

2121
Statements are usually required to be followed by a semicolon, with two exceptions:
2222

@@ -25,9 +25,9 @@ Statements are usually required to be followed by a semicolon, with two exceptio
2525
Furthermore, extra semicolons between statements are allowed, but these semicolons do not affect semantics.
2626

2727
When evaluating a block expression, each statement, except for item declaration statements, is executed sequentially.
28-
Then the final expression is executed, if given.
28+
Then the final operand is executed, if given.
2929

30-
The type of a block is the type of the final expression, or `()` if the final expression is omitted.
30+
The type of a block is the type of the final operand, or `()` if the final operand is omitted.
3131

3232
```rust
3333
# fn fn_call() {}
@@ -47,7 +47,6 @@ assert_eq!(5, five);
4747
4848
Blocks are always [value expressions] and evaluate the last operand in value expression context.
4949

50-
5150
> **Note**: This can be used to force moving a value if really needed.
5251
> For example, the following example fails on the call to `consume_self` because the struct was moved out of `s` in the block expression.
5352
>

‎src/expressions/call-expr.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
> &nbsp;&nbsp; [_Expression_]&nbsp;( `,` [_Expression_] )<sup>\*</sup> `,`<sup>?</sup>
99
1010
A *call expression* calls a function.
11-
The syntax of a call expression is an operand, the *function operand*, followed by a parenthesized comma-separated list of operands, the *argument operands*.
11+
The syntax of a call expression is an expression, called the *function operand*, followed by a parenthesized comma-separated list of expression, called the *argument operands*.
1212
If the function eventually returns, then the expression completes.
13-
For [non-function types](../types/function-item.md), the expression f(...) uses the method on one of the [`std::ops::Fn`], [`std::ops::FnMut`] or [`std::ops::FnOnce`] traits, which differ in whether they take the type by reference, mutable reference, or take ownership respectively.
13+
For [non-function types], the expression `f(...)` uses the method on one of the [`std::ops::Fn`], [`std::ops::FnMut`] or [`std::ops::FnOnce`] traits, which differ in whether they take the type by reference, mutable reference, or take ownership respectively.
1414
An automatic borrow will be taken if needed.
15-
`f` will also be automatically dereferences as required.
15+
The function operand will also be [automatically dereferenced] as required.
1616

1717
Some examples of call expressions:
1818

@@ -92,4 +92,6 @@ Refer to [RFC 132] for further details and motivations.
9292
[`std::ops::FnMut`]: ../../std/ops/trait.FnMut.html
9393
[`std::ops::FnOnce`]: ../../std/ops/trait.FnOnce.html
9494
[`std::ops::Fn`]: ../../std/ops/trait.Fn.html
95+
[automatically dereferenced]: field-expr.md#automatic-dereferencing
9596
[fully-qualified syntax]: ../paths.md#qualified-paths
97+
[non-function types]: ../types/function-item.md

‎src/expressions/closure-expr.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> [_Pattern_]&nbsp;( `:` [_Type_] )<sup>?</sup>
1414
1515
A *closure expression*, also know as a lambda expression or a lambda, defines a [closure type] and evaluates to a value of that type.
16-
The syntax for a closure expression is an optional `move` keyword, then a pipe-symbol-delimited (`|`) comma-separated list of [patterns], the *closure parameters* each optionally followed by a `:` and a type, then an optional `->` and type, the *return type*, and then an operand, the *closure body*.
16+
The syntax for a closure expression is an optional `move` keyword, then a pipe-symbol-delimited (`|`) comma-separated list of [patterns], called the *closure parameters* each optionally followed by a `:` and a type, then an optional `->` and type, called the *return type*, and then an expression, called the *closure body operand*.
1717
The optional type after each pattern is a type annotation for the pattern.
1818
If there is a return type, the closure body must be a [block].
1919

‎src/expressions/field-expr.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
A *field expression* is a [place expression] that evaluates to the location of a field of a [struct] or [union].
88
When the operand is [mutable], the field expression is also mutable.
99

10-
The syntax for a field expression is an operand, then a `.`, and finally an [identifier].
11-
Field expressions cannot be followed by a parenthetical comma-separated list of expressions, as that is parsed as a [method call expression].
10+
The syntax for a field expression is an expression, called the container operand*, then a `.`, and finally an [identifier].
11+
Field expressions cannot be followed by a parenthetical comma-separated list of expressions, as that is instead parsed as a [method call expression].
1212
That is, they cannot be the function operand of a [call expression].
1313

1414
> **Note**: Wrap the field expression in a [parenthesized expression] to use it in a call expression.
@@ -24,9 +24,7 @@ That is, they cannot be the function operand of a [call expression].
2424
> (holds_callable.callable)();
2525
> ```
2626
27-
A field expression denotes a field of a [struct] or [union].
28-
29-
To call a function stored in a struct, parentheses are needed around the field expression.
27+
Examples:
3028
3129
<!-- ignore: needs lots of support code -->
3230
```rust,ignore
@@ -38,7 +36,7 @@ foo().x;
3836
3937
## Automatic dereferencing
4038

41-
If the type of the operand implements [`Deref`] or [`DerefMut`][`Deref`] depending on whether the operand is [mutable], it is *automatically dereferenced* as many times as necessary to make the field access possible.
39+
If the type of the container operand implements [`Deref`] or [`DerefMut`][`Deref`] depending on whether the operand is [mutable], it is *automatically dereferenced* as many times as necessary to make the field access possible.
4240
This processes is also called *autoderef* for short.
4341

4442
## Borrowing

‎src/expressions/grouped-expr.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
> &nbsp;&nbsp; `(` [_InnerAttribute_]<sup>\*</sup> [_Expression_] `)`
66
77
A *parenthesized expression* wraps a single expression, evaluating to that expression.
8-
The syntax for a parenthesized expression is a `(`, then an operand, the *enclosed operand*, and then a `)`.
8+
The syntax for a parenthesized expression is a `(`, then an expression, called the *enclosed operand*, and then a `)`.
99

10-
An expression enclosed in parentheses evaluates to the value of the enclosed operand.
10+
Parenthesized expressions evaluate to the value of the enclosed operand.
11+
Unlike other expressions, parenthesized expressions are both [place expressions and value expressions][place].
12+
When the enclosed operand is a place expression, it is a place expression and when the enclosed operand is a value expression, it is a value expression.
1113

1214
Parentheses can be used to explicitly modify the precedence order of subexpressions within an expression.
1315

@@ -45,3 +47,4 @@ assert_eq!((a.f)(), "The field f");
4547
[_Expression_]: ../expressions.md
4648
[_InnerAttribute_]: ../attributes.md
4749
[attributes on block expressions]: block-expr.md#attributes-on-block-expressions
50+
[place]: ../expressions.md#place-expressions-and-value-expressions

0 commit comments

Comments
 (0)
Please sign in to comment.