-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Stabilize offset_of_slice
#139673
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
Stabilize offset_of_slice
#139673
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 |
---|---|---|
|
@@ -3946,15 +3946,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
{ | ||
let field_ty = self.field_ty(expr.span, field, args); | ||
|
||
if self.tcx.features().offset_of_slice() { | ||
self.require_type_has_static_alignment(field_ty, expr.span); | ||
} else { | ||
self.require_type_is_sized( | ||
field_ty, | ||
expr.span, | ||
ObligationCauseCode::Misc, | ||
); | ||
} | ||
self.require_type_has_static_alignment(field_ty, expr.span); | ||
|
||
if field.vis.is_accessible_from(def_scope, self.tcx) { | ||
self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None); | ||
|
@@ -3975,15 +3967,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
&& field.name == sym::integer(index) | ||
{ | ||
if let Some(&field_ty) = tys.get(index) { | ||
if self.tcx.features().offset_of_slice() { | ||
self.require_type_has_static_alignment(field_ty, expr.span); | ||
} else { | ||
self.require_type_is_sized( | ||
field_ty, | ||
expr.span, | ||
ObligationCauseCode::Misc, | ||
); | ||
} | ||
self.require_type_has_static_alignment(field_ty, expr.span); | ||
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. Could you please add a comment onto |
||
|
||
field_indices.push((FIRST_VARIANT, index.into())); | ||
current_container = field_ty; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1263,10 +1263,10 @@ impl<T> SizedTypeProperties for T {} | |
/// | ||
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. Duplicate this item, mark one as |
||
/// # Offsets of, and in, dynamically sized types | ||
/// | ||
/// The field’s type must be [`Sized`], but it may be located in a [dynamically sized] container. | ||
/// If the field type is dynamically sized, then you cannot use `offset_of!` (since the field's | ||
/// alignment, and therefore its offset, may also be dynamic) and must take the offset from an | ||
/// actual pointer to the container instead. | ||
/// The field’s type must have a statically known alignment. In other words, it is [`Sized`], a slice, | ||
/// or some wrapper around a slice. Notably, this is not the case if the field is a trait object. | ||
/// The alignment of trait objects can be different based on what the underlying type is, which can | ||
/// affect the offset of the field. Therefore you cannot use `offset_of!`. | ||
/// | ||
/// ``` | ||
/// # use core::mem; | ||
|
@@ -1281,8 +1281,9 @@ impl<T> SizedTypeProperties for T {} | |
/// #[repr(C, align(4))] | ||
/// struct Align4(u32); | ||
/// | ||
/// assert_eq!(mem::offset_of!(Struct<dyn Debug>, a), 0); // OK — Sized field | ||
/// assert_eq!(mem::offset_of!(Struct<Align4>, b), 4); // OK — not DST | ||
/// assert_eq!(mem::offset_of!(Struct<Align4>, b), 4); // OK — the last field is Sized | ||
/// assert_eq!(mem::offset_of!(Struct<[u8]>, b), 1); // OK — the last field is a slice | ||
/// assert_eq!(mem::offset_of!(Struct<dyn Debug>, a), 0); // OK — the struct is unsized, but the field `a` is Sized | ||
/// | ||
/// // assert_eq!(mem::offset_of!(Struct<dyn Debug>, b), 1); | ||
/// // ^^^ error[E0277]: ... cannot be known at compilation time | ||
|
@@ -1344,7 +1345,6 @@ impl<T> SizedTypeProperties for T {} | |
/// The following unstable features expand the functionality of `offset_of!`: | ||
/// | ||
/// * [`offset_of_enum`] — allows `enum` variants to be traversed as if they were fields. | ||
/// * [`offset_of_slice`] — allows getting the offset of a field of type `[T]`. | ||
/// | ||
/// # Examples | ||
/// | ||
|
@@ -1374,7 +1374,6 @@ impl<T> SizedTypeProperties for T {} | |
/// | ||
/// [dynamically sized]: https://doc.rust-lang.org/reference/dynamically-sized-types.html | ||
/// [`offset_of_enum`]: https://doc.rust-lang.org/nightly/unstable-book/language-features/offset-of-enum.html | ||
/// [`offset_of_slice`]: https://doc.rust-lang.org/nightly/unstable-book/language-features/offset-of-slice.html | ||
#[stable(feature = "offset_of", since = "1.77.0")] | ||
#[allow_internal_unstable(builtin_syntax)] | ||
pub macro offset_of($Container:ty, $($fields:expr)+ $(,)?) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
//@run-pass | ||
#![feature(offset_of_slice)] | ||
|
||
use std::mem::offset_of; | ||
|
||
|