Skip to content

lvalues are called places in Rust/MIR #40

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions reference/src/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* *niche*
* *layout*
* *tag*
* *rvalue*
* *lvalue*
* *place* (or *lvalue* in C/C++ speak)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are called "lvalues" in C, and "glvalues" in C++.

* *rvalue* (maybe we can come up with a Rust term for this as well?)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are called "values of the expression" in C, "prvalue" in C++.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and we should just call them "values")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a term to distinguish expressions like "3+2" from expressions like "x", where the latter can be used as lvalues but the former cannot?

Copy link

@strega-nil strega-nil Nov 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the latter is a (Rust) place expression, (C) lvalue expression, or (C++) glvalue expression, whereas the former is a (Rust) value expression, (C++) prvalue expression, and C doesn't have a special name for it.

In Rust, there's an automatic coercion from value expressions to place expressions in some specific places; and in C++, there's an automatic coercion from prvalue expressions to xvalue expressions (a type of glvalue expression).

If you want to convert from a place expression to a value expression it depends on what the kind of the place expression is:

  • in C it doesn't matter, you can always convert an lvalue expression to the value of the expression.
  • in Rust
    • if the type of the place expression is immovable (!Move), you cannot convert, in general, to a value expression.
    • otherwise, if the type of the place expression is !Copy, then the place expression must be owning in order to convert to a value expression
    • otherwise, the type is Copy, and you can always go from the place expression to the value expression.
  • in C++, a glvalue expression is either an lvalue or an xvalue
    • if the type is trivially copyable, then the value is just copied out of the glvalue expression
    • otherwise, if the expression is an lvalue, then the type must be copyable, and the copy constructor will be called; otherwise, compilation will fail
    • otherwise, if the expression is an xvalue, and the move constructor exists, then that is called
    • otherwise, the expression is an xvalue and the move constructor does not exist. If the copy constructor exists and takes a type const&, then that is called; otherwise, compilation fails.


## Unsafe abstraction