Skip to content
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

Modify RFC #803 (type ascription) to make type ascription expressions lvalues #987

Merged
merged 3 commits into from Jun 10, 2015

Conversation

Projects
None yet
5 participants
@nrc
Copy link
Member

nrc commented Mar 18, 2015

No description provided.

@nrc

This comment has been minimized.

Copy link
Member Author

nrc commented Mar 18, 2015

@huonw

This comment has been minimized.

Copy link
Member

huonw commented Mar 18, 2015

The history seems strange?

@nrc

This comment has been minimized.

Copy link
Member Author

nrc commented Mar 18, 2015

Yeah, I screwed the history up some how, I'll try to fix it at some point

@nrc nrc self-assigned this Mar 18, 2015

@nrc nrc force-pushed the nrc:ascription-2 branch from 9004bcd to 18fe78e Mar 23, 2015

@nrc

This comment has been minimized.

Copy link
Member Author

nrc commented Mar 23, 2015

Fixed the history and addressed @pnkfelix's comment

@nrc nrc referenced this pull request Mar 27, 2015

Closed

Implement type ascription. #23773

@Ericson2314

This comment has been minimized.

Copy link
Contributor

Ericson2314 commented Apr 6, 2015

+1 definitely an improvement. Would be nice if for the assignment case (<expr> = ...;), casting to a subtype was eventually supported.

@nrc nrc added the T-lang label May 15, 2015

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Jun 3, 2015

Hear ye, hear ye. This RFC is entering the final comment period.

@huonw

This comment has been minimized.

Copy link
Member

huonw commented Jun 3, 2015

Just to clarify, this would allow writing the following?

let x;
x: i32 = 10;
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

This comment has been minimized.

@nikomatsakis

nikomatsakis Jun 9, 2015

Contributor

This <expr> renders funny. You should add verbatim quotes.

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

This comment has been minimized.

@nikomatsakis

nikomatsakis Jun 9, 2015

Contributor

I find this phrasing kind of odd, though I think I agree with the intention.

It seems odd to say that <expr>: T is always an lvalue. For example, is foo(): T an lvalue? The only way I can make sense of that is if <expr>: T puts <expr> into a temporary (when it is an rvalue) and then is equivalent to referencing that temporary.

My original thought was that <expr>: T would 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>: T appears in a ref context, the type of <expr> and T must 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 consider foo(): T to be an rvalue, then this will create a temporary with the enclosing temporary scope and store foo() in it. But if you take the first interpretation, it will also create a temporary and store foo() 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:

  • lvalue: something that can be assigned to, e.g. x, x.f
  • rvalue: something that cannot be assigned to, e.g. x()
  • lvalue/ref context: something where you are taking the address, e.g. <expr> in &<expr> or in a match, etc.

This comment has been minimized.

@nikomatsakis

nikomatsakis Jun 9, 2015

Contributor

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 = 5 is a reference context (and hence this annotation is basically an assertion that the type of x is T).

@nrc

This comment has been minimized.

Copy link
Member Author

nrc commented Jun 10, 2015

@huonw yes.

@nrc

This comment has been minimized.

Copy link
Member Author

nrc commented Jun 10, 2015

@nikomatsakis updated the text

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Jun 10, 2015

It's official... the language subteam has decided to accept this RFC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.