-
Notifications
You must be signed in to change notification settings - Fork 530
Expression editing #1003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Expression editing #1003
Changes from all commits
9aa7919
0c6d2df
4e42e53
74bf944
1980677
5832c52
3304bd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -41,28 +41,33 @@ | |||||
> | [_MatchExpression_]\ | ||||||
> ) | ||||||
|
||||||
An expression may have two roles: it always produces a *value*, and it may have | ||||||
*effects* (otherwise known as "side effects"). An expression *evaluates to* a | ||||||
value, and has effects during *evaluation*. Many expressions contain | ||||||
sub-expressions, called the *operands* of the expression. The meaning of each | ||||||
kind of expression dictates several things: | ||||||
Expressions are the fundamental unit of computation. | ||||||
When evaluated, they may have *effects* and evaluates to either a value or place. | ||||||
|
||||||
Where expressions are allowed are *expression contexts*. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence sounds rather awkward. What is it you are trying to say? E.g. |
||||||
Expressions commonly contain sub-expressions, called the *operands* of the expression. | ||||||
|
||||||
The meaning of each kind of expression dictates several things: | ||||||
|
||||||
* Whether or not to evaluate the operands when evaluating the expression | ||||||
* The order in which to evaluate the operands | ||||||
* How to combine the operands' values to obtain the value of the expression | ||||||
* How to combine the operands' values to obtain the value or place of the expression | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Expressions like Or we could find some term for "the result of evalauting an expression", i.e., a term for "value or place". Usually that term is "value" but we already burned that. In the Stacked Borrows paper formalization, we hence used the term "result". |
||||||
|
||||||
In this way, the structure of expressions dictates the structure of execution. | ||||||
Blocks are just another kind of expression, so blocks, statements, expressions, | ||||||
and blocks again can recursively nest inside each other to an arbitrary depth. | ||||||
Blocks are just another kind of expression, so blocks, statements, expressions, and blocks again can recursively nest inside each other to an arbitrary depth. | ||||||
|
||||||
> **Note**: We give names to the operands of expressions so that we may discuss | ||||||
> them, but these names are not stable and may be changed. | ||||||
> **Note**: We give names to the operands of expressions so that we may discuss them, but these names are not stable and may be changed. | ||||||
|
||||||
## Expression precedence | ||||||
|
||||||
The precedence of Rust operators and expressions is ordered as follows, going | ||||||
from strong to weak. Binary Operators at the same precedence level are grouped | ||||||
in the order given by their associativity. | ||||||
The grammar for expressions as presented in this book is wrong. | ||||||
It purposefully ignores that certain operands cannot be certain expressions due to precedence. | ||||||
For example, `a * b + c` is actually parsed as an addition operator expression with the multiplication operator expression as its left operand. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
The grammar, taking precedence into account, is too unwieldy to easily read and understand. | ||||||
|
||||||
The precedence of operators and expressions is ordered as follows, going from strongest to weakest. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Binary Operators at the same precedence level are grouped in the order given by their associativity. | ||||||
Expressions in this table cannot have operands that are expressions lower in the table. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe say something here about how you can have operands that are lower precedence if wrapped in parenthesis or similar -- we don't want to imply it's impossible to write There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have that as a note after the table. I could put it before the table. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could also put |
||||||
|
||||||
| Operator/Expression | Associativity | | ||||||
|-----------------------------|---------------------| | ||||||
|
@@ -86,11 +91,12 @@ in the order given by their associativity. | |||||
| `=` `+=` `-=` `*=` `/=` `%=` <br> `&=` <code>|=</code> `^=` `<<=` `>>=` | right to left | | ||||||
| `return` `break` closures | | | ||||||
|
||||||
> **Note**: Wrapping an expression with a [parenthetical expression][paren] increases its precedence. | ||||||
|
||||||
## Evaluation order of operands | ||||||
|
||||||
The following list of expressions all evaluate their operands the same way, as | ||||||
described after the list. Other expressions either don't take operands or | ||||||
evaluate them conditionally as described on their respective pages. | ||||||
The following list of expressions all evaluate their operands the same way, as described after the list. | ||||||
Other expressions either don't take operands or evaluate them conditionally as described on their respective pages. | ||||||
|
||||||
* Dereference expression | ||||||
* Error propagation expression | ||||||
|
@@ -112,15 +118,12 @@ evaluate them conditionally as described on their respective pages. | |||||
* Range expression | ||||||
* Return expression | ||||||
|
||||||
The operands of these expressions are evaluated prior to applying the effects of | ||||||
the expression. Expressions taking multiple operands are evaluated left to right | ||||||
as written in the source code. | ||||||
The operands of these expressions are evaluated prior to applying the effects of the expression. | ||||||
Expressions taking multiple operands are evaluated left to right as written in the source code. | ||||||
|
||||||
> **Note**: Which subexpressions are the operands of an expression is | ||||||
> determined by expression precedence as per the previous section. | ||||||
> **Note**: Which subexpressions are the operands of an expression is determined by expression precedence as per the previous section. | ||||||
|
||||||
For example, the two `next` method calls will always be called in the same | ||||||
order: | ||||||
For example, the two `next` method calls will always be called in the same order: | ||||||
|
||||||
```rust | ||||||
# // Using vec instead of array to avoid references | ||||||
|
@@ -133,96 +136,88 @@ assert_eq!( | |||||
); | ||||||
``` | ||||||
|
||||||
> **Note**: Since this is applied recursively, these expressions are also | ||||||
> evaluated from innermost to outermost, ignoring siblings until there are no | ||||||
> inner subexpressions. | ||||||
> **Note**: Since this is applied recursively, these expressions are also evaluated from innermost to outermost, ignoring siblings until there are no inner subexpressions. | ||||||
|
||||||
## Place Expressions and Value Expressions | ||||||
## Expression categories | ||||||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Expressions are divided into two main categories: place expressions and | ||||||
value expressions. Likewise within each expression, operands may occur | ||||||
in either place context or value context. The evaluation of an expression | ||||||
depends both on its own category and the context it occurs within. | ||||||
<!-- This section was renamed, but heavily linked to. So adding this span to keep the links working. --> | ||||||
<span id="place-expressions-and-value-expressions"></span> | ||||||
|
||||||
A *place expression* is an expression that represents a memory location. These | ||||||
expressions are [paths] which refer to local variables, [static variables], | ||||||
[dereferences][deref] (`*expr`), [array indexing] expressions (`expr[expr]`), | ||||||
[field] references (`expr.f`) and parenthesized place expressions. All other | ||||||
expressions are value expressions. | ||||||
Expressions and expression contexts are divided into two *expression categories*: *place* and *value*. | ||||||
For expressions, what they evaluate into determines their categories. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels awkward. We already established the "place or value" dichotomy earlier, so IMO it'd be better to say upfront (L45) that there are "place expressions" and "value expressions" -- or we find some way to not make the place/value distinction at all until here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My thought was that L45 gives a summary, and then this section expands upon the idea. It would feel weird to not include the dichotomy in the main definition but including the terms in the main definition would be more detail then necessary. Also, the fact that the category exists for both the expression and the expression context makes this harder to describe. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
These are not two independent facts, so maybe there is a way to present them as one fact? There are two kinds of expressions, basically two kinds of "non-terminals" in the grammar ( |
||||||
Expressions that evaluate to a value are *value expressions* while those that evaluate to a place are *place expressions*. | ||||||
For expression contexts, whether they operate on a place or value determines their context. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no idea what this last sentence is supposed to tell me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I'll think of a way to phrase it better. |
||||||
|
||||||
A *value expression* is an expression that represents an actual value. | ||||||
Evaluating an expression in the opposite expression context has effects after evaluation of the expression to produce the expected place or value of the context. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I only understood this sentence after reading the rest of this paragraph. What about something more like "When a place expression is evaluated in value context or vice versa, extra effects apply to perform the required conversion"? |
||||||
When evaluating a value expression in place expression context, a temporary place is initialized to that value. | ||||||
When evaluating a place expression in value expression context, the value at that place is read, moving or copying it. | ||||||
|
||||||
The following contexts are *place expression* contexts: | ||||||
The following lists all place expressions. | ||||||
Expressions not listed here are value expressions. | ||||||
|
||||||
* The left operand of an [assignment][assign] or [compound assignment] | ||||||
expression. | ||||||
* [Paths] which refer to local variables or [static variables]. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't all paths place expressions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually don't know. This was here beforehand. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this was meant as "Paths, which refer to ...". IOW, this is a definition of "Paths". |
||||||
* [The dereference operator][deref] | ||||||
* [Field access expressions][field] | ||||||
* [Tuple indexing expressions] | ||||||
* [Array indexing expressions] | ||||||
* [Parenthetical expressions][paren] when its enclosed operand is a place expression | ||||||
|
||||||
The following list all place expression contexts. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
All other contexts are value expression contexts. | ||||||
|
||||||
* The initializer of a [let statement]. | ||||||
* The assigned place operand of an [assignment][assign] or [compound assignment] expression. | ||||||
* The operand of a unary [borrow] or [dereference][deref] operator. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should "address-of operator" be listed here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's in your PR which I need to get back to. But my work schedule this week is night/morning/night/morning and I had a migraine over my weekend. I'll rebase against master once that one is merged. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No worries, get a good cup of sleep first. :) |
||||||
* The operand of a field expression. | ||||||
* The container operand of a field expression. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's a "container operand"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's the operand of the field expression, given a name. See the field expression page, which I should add a link to on this list. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That name strikes me as rather counterintuitive. Maybe "base" would be better? Since there is only one operand, just saying "the operand" would also work IMO. |
||||||
* The indexed operand of an array indexing expression. | ||||||
* The operand of any [implicit borrow]. | ||||||
* The initializer of a [let statement]. | ||||||
* The [scrutinee] of an [`if let`], [`match`][match], or [`while let`] | ||||||
expression. | ||||||
* The base of a [functional update] struct expression. | ||||||
* The [scrutinee] operand of an [`if let`], [`match`][match], or [`while let`] expression. | ||||||
* The base operand of a [functional update] struct expression. | ||||||
|
||||||
> Note: Historically, place expressions were called *lvalues* and value | ||||||
> expressions were called *rvalues*. | ||||||
> Note: Historically, place expressions were called *lvalues* and value expressions were called *rvalues*. | ||||||
|
||||||
### Moved and copied types | ||||||
|
||||||
When a place expression is evaluated in a value expression context, or is bound | ||||||
by value in a pattern, it denotes the value held _in_ that memory location. If | ||||||
the type of that value implements [`Copy`], then the value will be copied. In | ||||||
the remaining situations if that type is [`Sized`], then it may be possible to | ||||||
move the value. Only the following place expressions may be moved out of: | ||||||
When a place expression is evaluated in a value expression context, or is bound by value in a pattern, it denotes the value held _in_ that memory location. | ||||||
If the type of that value implements [`Copy`], then the value will be copied. | ||||||
In the remaining situations if that type is [`Sized`], then it may be possible to move the value. | ||||||
Only the following place expressions may be moved out of: | ||||||
|
||||||
* [Variables] which are not currently borrowed. | ||||||
* [Temporary values](#temporaries). | ||||||
* [Fields][field] of a place expression which can be moved out of and | ||||||
doesn't implement [`Drop`]. | ||||||
* The result of [dereferencing][deref] an expression with type [`Box<T>`] and | ||||||
that can also be moved out of. | ||||||
* [Fields][field] of a place expression which can be moved out of and doesn't implement [`Drop`]. | ||||||
* The result of [dereferencing][deref] an expression with type [`Box<T>`] and that can also be moved out of. | ||||||
|
||||||
Moving out of a place expression that evaluates to a local variable, the | ||||||
location is deinitialized and cannot be read from again until it is | ||||||
reinitialized. In all other cases, trying to use a place expression in a value | ||||||
expression context is an error. | ||||||
Moving out of a place expression that evaluates to a local variable, the location is deinitialized and cannot be read from again until it is reinitialized. | ||||||
In all other cases, trying to use a place expression in a value expression context is an error. | ||||||
|
||||||
### Mutability | ||||||
|
||||||
For a place expression to be [assigned][assign] to, mutably [borrowed][borrow], | ||||||
[implicitly mutably borrowed], or bound to a pattern containing `ref mut` it | ||||||
must be _mutable_. We call these *mutable place expressions*. In contrast, | ||||||
other place expressions are called *immutable place expressions*. | ||||||
For a place expression to be [assigned][assign] to, mutably [borrowed][borrow], [implicitly mutably borrowed], or bound to a pattern containing `ref mut` it must be _mutable_. | ||||||
We call these *mutable place expressions*. | ||||||
In contrast, other place expressions are called *immutable place expressions*. | ||||||
|
||||||
The following expressions can be mutable place expression contexts: | ||||||
|
||||||
* Mutable [variables], which are not currently borrowed. | ||||||
* [Mutable `static` items]. | ||||||
* [Temporary values]. | ||||||
* [Fields][field], this evaluates the subexpression in a mutable place | ||||||
expression context. | ||||||
* [Fields][field], this evaluates the subexpression in a mutable place expression context. | ||||||
* [Dereferences][deref] of a `*mut T` pointer. | ||||||
* Dereference of a variable, or field of a variable, with type `&mut T`. Note: | ||||||
This is an exception to the requirement of the next rule. | ||||||
* Dereferences of a type that implements `DerefMut`, this then requires that | ||||||
the value being dereferenced is evaluated is a mutable place expression context. | ||||||
* [Array indexing] of a type that implements `IndexMut`, this | ||||||
then evaluates the value being indexed, but not the index, in mutable place | ||||||
expression context. | ||||||
* Dereference of a variable, or field of a variable, with type `&mut T`. | ||||||
Note: This is an exception to the requirement of the next rule. | ||||||
* Dereferences of a type that implements `DerefMut`, this then requires that the value being dereferenced is evaluated is a mutable place expression context. | ||||||
* [Array indexing expressions] of a type that implements `IndexMut`, this then evaluates the value being indexed, but not the index, in mutable place expression context. | ||||||
|
||||||
### Temporaries | ||||||
|
||||||
When using a value expression in most place expression contexts, a temporary | ||||||
unnamed memory location is created initialized to that value and the expression | ||||||
evaluates to that location instead, except if [promoted] to a `static`. The | ||||||
[drop scope] of the temporary is usually the end of the enclosing statement. | ||||||
When using a value expression in most place expression contexts, a temporary unnamed memory location is created initialized to that value and the expression evaluates to that location instead, except if [promoted] to a `static`. | ||||||
The [drop scope] of the temporary is usually the end of the enclosing statement. | ||||||
|
||||||
### Implicit Borrows | ||||||
|
||||||
Certain expressions will treat an expression as a place expression by implicitly | ||||||
borrowing it. For example, it is possible to compare two unsized [slices][slice] for | ||||||
equality directly, because the `==` operator implicitly borrows it's operands: | ||||||
Certain expressions will treat an expression as a place expression by implicitly borrowing it. | ||||||
For example, it is possible to compare two unsized [slices][slice] for equality directly, because the `==` operator implicitly borrows it's operands: | ||||||
|
||||||
```rust | ||||||
# let c = [1, 2, 3]; | ||||||
|
@@ -242,21 +237,19 @@ Implicit borrows may be taken in the following expressions: | |||||
* Left operand in [method-call] expressions. | ||||||
* Left operand in [field] expressions. | ||||||
* Left operand in [call expressions]. | ||||||
* Left operand in [array indexing] expressions. | ||||||
* Left operand in [array indexing expressions]. | ||||||
* Operand of the [dereference operator][deref] (`*`). | ||||||
* Operands of [comparison]. | ||||||
* Left operands of the [compound assignment]. | ||||||
|
||||||
## Overloading Traits | ||||||
|
||||||
Many of the following operators and expressions can also be overloaded for | ||||||
other types using traits in `std::ops` or `std::cmp`. These traits also | ||||||
exist in `core::ops` and `core::cmp` with the same names. | ||||||
Many of the following operators and expressions can also be overloaded for other types using traits in `std::ops` or `std::cmp`. | ||||||
These traits also exist in `core::ops` and `core::cmp` with the same names. | ||||||
|
||||||
## Expression Attributes | ||||||
|
||||||
[Outer attributes][_OuterAttribute_] before an expression are allowed only in | ||||||
a few specific cases: | ||||||
[Outer attributes][_OuterAttribute_] before an expression are allowed only in a few specific cases: | ||||||
|
||||||
* Before an expression used as a [statement]. | ||||||
* Elements of [array expressions], [tuple expressions], [call expressions], | ||||||
|
@@ -271,9 +264,7 @@ a few specific cases: | |||||
|
||||||
They are never allowed before: | ||||||
* [Range][_RangeExpression_] expressions. | ||||||
* Binary operator expressions ([_ArithmeticOrLogicalExpression_], | ||||||
[_ComparisonExpression_], [_LazyBooleanExpression_], [_TypeCastExpression_], | ||||||
[_AssignmentExpression_], [_CompoundAssignmentExpression_]). | ||||||
* Binary operator expressions ([_ArithmeticOrLogicalExpression_], [_ComparisonExpression_], [_LazyBooleanExpression_], [_TypeCastExpression_], [_AssignmentExpression_], [_CompoundAssignmentExpression_]). | ||||||
|
||||||
|
||||||
[block expressions]: expressions/block-expr.md | ||||||
|
@@ -283,13 +274,15 @@ They are never allowed before: | |||||
[`if let`]: expressions/if-expr.md#if-let-expressions | ||||||
[match]: expressions/match-expr.md | ||||||
[method-call]: expressions/method-call-expr.md | ||||||
[paren]: expressions/grouped-expr.md | ||||||
[paths]: expressions/path-expr.md | ||||||
[struct]: expressions/struct-expr.md | ||||||
[tuple expressions]: expressions/tuple-expr.md | ||||||
[tuple indexing expressions]: expressions/tuple-expr.md#tuple-indexing-expressions | ||||||
[`while let`]: expressions/loop-expr.md#predicate-pattern-loops | ||||||
|
||||||
[array expressions]: expressions/array-expr.md | ||||||
[array indexing]: expressions/array-expr.md#array-and-slice-indexing-expressions | ||||||
[array expressions]: expressions/array-expr.md | ||||||
[array indexing expressions]: expressions/array-expr.md#array-and-slice-indexing-expressions | ||||||
|
||||||
[assign]: expressions/operator-expr.md#assignment-expressions | ||||||
[borrow]: expressions/operator-expr.md#borrow-operators | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should probably keep the
(otherwise known as "side effects")
part