Skip to content

Commit

Permalink
Merge remote-tracking branch 'nrc/ascription-2'
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Jun 10, 2015
2 parents 642bde9 + d69cf92 commit 3dc86bb
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions text/0803-type-ascription.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ context of cross-platform programming).
### Type ascription and temporaries

There is an implementation choice between treating `x: T` as an lvalue or
rvalue. Note that when a rvalue is used in lvalue context (e.g., the subject of
a reference operation), then the compiler introduces a temporary variable.
Neither option is satisfactory, if we treat an ascription expression as an
lvalue (i.e., no new temporary), then there is potential for unsoundness:
rvalue. Note that when an rvalue is used in 'reference context' (e.g., the
subject of a reference operation), then the compiler introduces a temporary
variable. Neither option is satisfactory, if we treat an ascription expression
as an lvalue (i.e., no new temporary), then there is potential for unsoundness:

```
let mut foo: S = ...;
Expand All @@ -172,24 +172,22 @@ lvalue position), then we don't have the soundness problem, but we do get the
unexpected result that `&(x: T)` is not in fact a reference to `x`, but a
reference to a temporary copy of `x`.

The proposed solution is that type ascription expressions are rvalues, but
taking a reference of such an expression is forbidden. I.e., type asciption is
forbidden in the following contexts (where `<expr>` is a type ascription
The proposed solution is that type ascription expressions inherit their
'lvalue-ness' from their underlying expressions. I.e., `e: T` is an lvalue if
`e` is an lvalue, and an rvalue otherwise. If the type ascription expression is
in reference context, then we require the ascribed type to exactly match the
type of the expression, i.e., neither subtyping nor coercion is allowed. These
reference contexts are as follows (where `<expr>` is a type ascription
expression):

```
&[mut] <expr>
let ref [mut] x = <expr>
match <expr> { .. ref [mut] x .. => { .. } .. }
<expr>.foo() // due to autoref
<expr> = ...;
```

Like other rvalues, type ascription would not be allowed as the lhs of assignment.

Note that, if type asciption is required in such a context, an lvalue can be
forced by using `{}`, e.g., write `&mut { foo: T }`, rather than `&mut (foo: T)`.


# Drawbacks

More syntax, another feature in the language.
Expand Down

0 comments on commit 3dc86bb

Please sign in to comment.