Skip to content

promotion tweaks #31

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 9 commits into from
Nov 27, 2019
Merged

promotion tweaks #31

merged 9 commits into from
Nov 27, 2019

Conversation

RalfJung
Copy link
Member

@ecstatic-morse
Copy link
Contributor

This looks good to me apart from the mention about promotion of references to types with interior mutability. Perhaps we could include the following as an example?

const X: Cell<i32> = Cell::new(5); // ok
fn main() {
    let x: &'static _ = &X; // not ok
}

@RalfJung
Copy link
Member Author

This looks good to me apart from the mention about promotion of references to types with interior mutability. Perhaps we could include the following as an example?

Done.

at compile-time, so we do not have to check the initializer. However, the
restrictions described above still apply for the *result* of the promoted
computation: in particular, it must be a valid `const` (i.e., it cannot
introduce interior mutability) and it must not require dropping.
Copy link
Contributor

@ecstatic-morse ecstatic-morse Nov 25, 2019

Choose a reason for hiding this comment

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

It's worth noting that these restrictions apply only in the context lifetime extension. We can initialize an array, which also uses the implicit rules, with a constant containing interior mutability (i.e. [CELL; 4]). My revisions failed to do this.

Copy link
Member Author

Choose a reason for hiding this comment

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

But [&CELL; 4] gets rejected, I hope?

Copy link
Contributor

@ecstatic-morse ecstatic-morse Nov 25, 2019

Choose a reason for hiding this comment

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

That compiles on stable because &T is Copy for all T. Any use of it will cause a borrow check error since the access to CELL has a temporary lifetime. Promotion fails for &CELL even if it's in an array initializer if that's what you're asking.

Copy link
Member Author

Choose a reason for hiding this comment

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

Promotion fails for &CELL even if it's in an array initializer if that's what you're asking.

Good.

But then behavior seems to be the same for arrays and lifetime extension? It's just that lifetime extension always involves a borrow.

Co-Authored-By: ecstatic-morse <ecstaticmorse@gmail.com>
@RalfJung
Copy link
Member Author

@ecstatic-morse any more concerns/comments?

@RalfJung RalfJung merged commit e9c3add into rust-lang:master Nov 27, 2019
@RalfJung RalfJung deleted the promotion branch November 27, 2019 22:55
Copy link
Contributor

@ecstatic-morse ecstatic-morse left a comment

Choose a reason for hiding this comment

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

r=me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants