-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Modify RFC #803 (type ascription) to make type ascription expressions lvalues #987
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -172,24 +172,20 @@ 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 | ||
| expression): | ||
| The proposed solution is that type ascription expressions are lvalues. 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 | ||
|
Contributor
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 |
||
| 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. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 find this phrasing kind of odd, though I think I agree with the intention.
It seems odd to say that
<expr>: Tis always an lvalue. For example, isfoo(): Tan lvalue? The only way I can make sense of that is if<expr>: Tputs<expr>into a temporary (when it is an rvalue) and then is equivalent to referencing that temporary.My original thought was that
<expr>: Twould be an lvalue if<expr>is an lvalue, and an rvalue if<expr>is an rvalue. Moreover, there would be an additional rule that when<expr>: Tappears in a ref context, the type of<expr>andTmust match exactly (though, strictly speaking, I think it is ok to upcast if<expr>is an rvalue).That said, I think that the "temporary interpretation" I gave above and my interpretation behave the same way from an end-user point of view. For example, if you write
&(foo(): T), and you considerfoo(): Tto be an rvalue, then this will create a temporary with the enclosing temporary scope and storefoo()in it. But if you take the first interpretation, it will also create a temporary and storefoo()in it, and presumably the lifetime of that temporary would be the same (though we would probably want to adjust the temporary lifetime rules to "see through"<expr>: T).Anyway, just for reference, let me define my terms a bit:
x,x.fx()<expr>in&<expr>or in a match, etc.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.
Update: I realized that since lvalue controls what you can assign to, it definitely seems wrong to say that type ascription is always an lvalue, since that would permit
foo(): T = 5.I prefer just saying that it is an lvalue/rvalue based on the underlying expression, and that
x: T = 5is a reference context (and hence this annotation is basically an assertion that the type ofxisT).