Skip to content

Commit

Permalink
X-Links: Boolean Edition
Browse files Browse the repository at this point in the history
This commit mostly adds links across the reference where things talked
about the boolean type. I also normalized the language so that it always
uses "boolean type". Other links were added as appropriate.

This commit also changes a few "x expressions" to "x operands" for if
and while expressions. The whole books needs this treatment, but while
I was here, I did it for these sections.

I also changed "`#[repr(C)]`" to "the C representation" in unions so
that I could actually link to them without breaking linkcheck.
  • Loading branch information
Havvy committed Jan 23, 2021
1 parent a1a711e commit 7c6e0c0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/behavior-considered-undefined.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ code.
a function/primitive operation or returned from a function/primitive
operation.
The following values are invalid (at their respective type):
* A value other than `false` (`0`) or `true` (`1`) in a `bool`.
* A value other than `false` (`0`) or `true` (`1`) in a [`bool`].
* A discriminant in an `enum` not included in the type definition.
* A null `fn` pointer.
* A value in a `char` which is a surrogate or above `char::MAX`.
Expand Down Expand Up @@ -77,7 +77,8 @@ cannot be bigger than `isize::MAX` bytes.
> vice versa, undefined behavior in Rust can cause adverse affects on code
> executed by any FFI calls to other languages.
[`const`]: items/constant-items.html
[`bool`]: types/boolean.md
[`const`]: items/constant-items.md
[noalias]: http://llvm.org/docs/LangRef.html#noalias
[pointer aliasing rules]: http://llvm.org/docs/LangRef.html#pointer-aliasing-rules
[undef]: http://llvm.org/docs/LangRef.html#undefined-values
Expand Down
15 changes: 8 additions & 7 deletions src/expressions/if-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
> | _IfExpression_
> | _IfLetExpression_ ) )<sup>\?</sup>
An `if` expression is a conditional branch in program control. The form of an
`if` expression is a condition expression, followed by a consequent block, any
An `if` expression is a conditional branch in program control. The syntax of an
`if` expression is a condition operand, followed by a consequent block, any
number of `else if` conditions and blocks, and an optional trailing `else`
block. The condition expressions must have type `bool`. If a condition
expression evaluates to `true`, the consequent block is executed and any
subsequent `else if` or `else` block is skipped. If a condition expression
block. The condition operands must have the [boolean type]. If a condition
operand evaluates to `true`, the consequent block is executed and any
subsequent `else if` or `else` block is skipped. If a condition operand
evaluates to `false`, the consequent block is skipped and any subsequent `else
if` condition is evaluated. If all `if` and `else if` conditions evaluate to
`false` then any `else` block is executed. An if expression evaluates to the
Expand Down Expand Up @@ -52,8 +52,8 @@ assert_eq!(y, "Bigger");
> | _IfLetExpression_ ) )<sup>\?</sup>
An `if let` expression is semantically similar to an `if` expression but in
place of a condition expression it expects the keyword `let` followed by a
pattern, an `=` and a [scrutinee] expression. If the value of the scrutinee
place of a condition operand it expects the keyword `let` followed by a
pattern, an `=` and a [scrutinee] operand. If the value of the scrutinee
matches the pattern, the corresponding block will execute. Otherwise, flow
proceeds to the following `else` block if it exists. Like `if` expressions,
`if let` expressions have a value determined by the block that is evaluated.
Expand Down Expand Up @@ -158,4 +158,5 @@ if let PAT = ( EXPR || EXPR ) { .. }
[_MatchArmPatterns_]: match-expr.md
[_eRFCIfLetChain_]: https://github.com/rust-lang/rfcs/blob/master/text/2497-if-let-chains.md#rollout-plan-and-transitioning-to-rust-2018
[`match` expression]: match-expr.md
[boolean type]: ../types/boolean.md
[scrutinee]: ../glossary.md#scrutinee
7 changes: 4 additions & 3 deletions src/expressions/loop-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ have type compatible with the value of the `break` expression(s).
> _PredicateLoopExpression_ :\
> &nbsp;&nbsp; `while` [_Expression_]<sub>_except struct expression_</sub> [_BlockExpression_]
A `while` loop begins by evaluating the boolean loop conditional expression. If
the loop conditional expression evaluates to `true`, the loop body block
executes, then control returns to the loop conditional expression. If the loop
A `while` loop begins by evaluating the [boolean] loop conditional operand. If
the loop conditional operand evaluates to `true`, the loop body block
executes, then control returns to the loop conditional operand. If the loop
conditional expression evaluates to `false`, the `while` expression completes.

An example:
Expand Down Expand Up @@ -294,6 +294,7 @@ expression `()`.
[_MatchArmPatterns_]: match-expr.md
[_Pattern_]: ../patterns.md
[`match` expression]: match-expr.md
[boolean]: ../types/boolean.md
[scrutinee]: ../glossary.md#scrutinee
[temporary values]: ../expressions.md#temporaries
[_LazyBooleanOperatorExpression_]: operator-expr.md#lazy-boolean-operators
Expand Down
16 changes: 10 additions & 6 deletions src/items/unions.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ let f = unsafe { u.f1 };
Unions have no notion of an "active field". Instead, every union access just
interprets the storage at the type of the field used for the access. Reading a
union field reads the bits of the union at the field's type. Fields might have a
non-zero offset (except when `#[repr(C)]` is used); in that case the bits
starting at the offset of the fields are read. It is the programmer's
non-zero offset (except when [the C representation] is used); in that case the
bits starting at the offset of the fields are read. It is the programmer's
responsibility to make sure that the data is valid at the field's type. Failing
to do so results in undefined behavior. For example, reading the value `3` at
type `bool` is undefined behavior. Effectively, writing to and then reading from
a `#[repr(C)]` union is analogous to a [`transmute`] from the type used for
writing to the type used for reading.
to do so results in [undefined behavior]. For example, reading the value `3`
through of a field of the [boolean type] is undefined behavior. Effectively,
writing to and then reading from a union with [the C representation] is
analogous to a [`transmute`] from the type used for writing to the type used for
reading.

Consequently, all reads of union fields have to be placed in `unsafe` blocks:

Expand Down Expand Up @@ -182,4 +183,7 @@ checking, etc etc etc).
[_StructFields_]: structs.md
[`transmute`]: ../../std/mem/fn.transmute.html
[`Copy`]: ../../std/marker/trait.Copy.html
[boolean type]: ../types/boolean.md
[ManuallyDrop]: ../../std/mem/struct.ManuallyDrop.html
[the C representation]: ../type-layout.md#reprc-unions
[undefined behavior]: ../behavior-considered-undefined.html

0 comments on commit 7c6e0c0

Please sign in to comment.