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

Clarify "immutable bytes" definition #1616

Open
RalfJung opened this issue Sep 18, 2024 · 4 comments · May be fixed by #1637
Open

Clarify "immutable bytes" definition #1616

RalfJung opened this issue Sep 18, 2024 · 4 comments · May be fixed by #1637

Comments

@RalfJung
Copy link
Member

Currently, we define "immutable bytes" as

All bytes inside a const item or within an implicitly const-promoted expression are immutable. The bytes owned by an immutable binding or immutable static are immutable, unless those bytes are part of an UnsafeCell.

However, there are some issue with this. First of all, a const item can only be used as a value, not as a place, so it doesn't really "contain" bytes that could be (im)mutable -- except if it is subject to lifetime extension, as in

const C: &Vec<i32> = &Vec::new();

Secondly, for static, the actual rule is that for bytes inside lifetime-extended values, they are all immutable. UnsafeCell makes no difference.

@chorman0773
Copy link
Contributor

I have a few ideas about how to write it, for varying levels of rigor. I think that in terms of lifetime extension, const-promotion occurs for both static and const items, correct? I'd expect that the const-promotion rule would apply there as well.

@RalfJung
Copy link
Member Author

RalfJung commented Sep 18, 2024

Const promotion and lifetime extension are largely orthogonal mechanisms. I was talking about lifetime extension above. Promotion does not apply in that example as Vec has a destructor.

@chorman0773
Copy link
Contributor

Ok, then I'd probably say

All bytes within a const-promoted expression, or a lifetime-extended expression in the initializer of a static or const item are immutable. All bytes owned (directly) by an immutable binding or immutable static are immutable, except bytes within an [core::cell::UnsafeCell].

@RalfJung
Copy link
Member Author

Yeah, that sounds good.

@RalfJung RalfJung linked a pull request Sep 29, 2024 that will close this issue
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 a pull request may close this issue.

2 participants