Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/accepted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ declare_features! (
(accepted, non_modrs_mods, "1.30.0", Some(44660)),
/// Allows using multiple nested field accesses in offset_of!
(accepted, offset_of_nested, "1.82.0", Some(120140)),
/// Allows using fields with slice type in offset_of!
(accepted, offset_of_slice, "CURRENT_RUSTC_VERSION", Some(126151)),
/// Allows the use of or-patterns (e.g., `0 | 1`).
(accepted, or_patterns, "1.53.0", Some(54883)),
/// Allows using `+bundle,+whole-archive` link modifiers with native libs.
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,6 @@ declare_features! (
(incomplete, non_lifetime_binders, "1.69.0", Some(108185)),
/// Allows using enums in offset_of!
(unstable, offset_of_enum, "1.75.0", Some(120141)),
/// Allows using fields with slice type in offset_of!
(unstable, offset_of_slice, "1.81.0", Some(126151)),
/// Allows using `#[optimize(X)]`.
(unstable, optimize_attribute, "1.34.0", Some(54882)),
/// Allows specifying nop padding on functions for dynamic patching.
Expand Down
20 changes: 2 additions & 18 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

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

Could you please add a comment onto require_type_has_static_alignment that notes that changing the behavior will extend the functionality of offset_of and is insta-stable?


field_indices.push((FIRST_VARIANT, index.into()));
current_container = field_ty;
Expand Down
15 changes: 7 additions & 8 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1263,10 +1263,10 @@ impl<T> SizedTypeProperties for T {}
///
Copy link
Member

Choose a reason for hiding this comment

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

Duplicate this item, mark one as #[cfg(bootstrap)] and one as #[cfg(not(bootstrap))]. Give the #[cfg(bootstrap)] one dummy documentation like /// This item will not be documented so it doesn't get doc-tested in stage 0.

/// # 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;
Expand All @@ -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
Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -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)+ $(,)?) {
Expand Down
30 changes: 0 additions & 30 deletions src/doc/unstable-book/src/language-features/offset-of-slice.md

This file was deleted.

2 changes: 0 additions & 2 deletions tests/ui/offset-of/offset-of-slice-normalized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//@[next] compile-flags: -Znext-solver
//@ run-pass

#![feature(offset_of_slice)]

use std::mem::offset_of;

trait Mirror {
Expand Down
1 change: 0 additions & 1 deletion tests/ui/offset-of/offset-of-slice.rs
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;

Expand Down
Loading