-
Notifications
You must be signed in to change notification settings - Fork 605
explicitly keep the door open for some but not all subobject provenance #2338
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,18 @@ r[undefined.alias] | |
|
|
||
| All this also applies when values of these types are passed in a (nested) field of a compound type, but not behind pointer indirections. | ||
|
|
||
| r[undefined.subobject] | ||
| * Using a pointer or reference outside the subrange of memory it is allowed to access. | ||
|
|
||
| Generally, a reference may only access the memory it [points to]. | ||
| This restriction also applies to all raw pointers derived from this reference. | ||
| The one exception is that a reference to an element of an array or slice may be used to access other elements of the same array or slice without immediately causing undefined behavior. | ||
| This exception also applies for nested arrays, but not for fields of values inside arrays. | ||
|
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. Can we clarify what "this exception also applies for nested arrays" means? I presume it means that you can go from e.g. |
||
|
|
||
| Furthermore, a reference to a field of an enum may not be used to change the discriminant of said enum. | ||
| If the tag lies inside the range of memory accessible by the reference (as in the previous paragraph), then the reference may be used to *temporarily* change the discriminant of the enum without immediately causing undefined behavior, but the original discriminant must be restored before the lifetime of the reference ends. | ||
|
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. What does "as in the previous paragraph" refer to?
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. What if changing the discriminant has the effect of just doing a transmute? E.g.: #[repr(u8)]
enum Zero { Zero = 0 }
#[repr(u8)]
enum One { One = 1 }
enum Bit {
Zero(Zero),
One(One),
}If Rust chooses to niche-optimize
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. We often say that lifetimes are not relevant to opsem. What does the term "lifetime" mean in this context? Is this about crossing an API boundary (i.e., this is really a safety thing about what you're allowed to assume when an enum reference/pointer crosses an API boundary), or is this about SB/TB, or something else? |
||
| This restriction also applies to all raw pointers derived from this reference. | ||
|
|
||
| r[undefined.immutable] | ||
| * Mutating immutable bytes. All bytes reachable through a [const-promoted] expression are immutable, as well as bytes reachable through borrows in `static` and `const` initializers that have been [lifetime-extended] to `'static`. The bytes owned by an immutable binding or immutable `static` are immutable, unless those bytes are part of an [`UnsafeCell<U>`]. | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
How does this play with transmutes? Consider:
This goes from:
&Pairto&[usize; 8](a sound transmute)Presumably if we skipped the
&Pair -> &[usize; 8]step and instead constructedi0from&pair.first[0], this would be unsound because it would entail jumping betweenfirstandsecond.Two questions:
&Pair -> &[usize; 8], it would not be sound?View changes since the review