-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
offset_of: allow (unstably) taking the offset of slice tail fields #126150
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 |
---|---|---|
|
@@ -3363,7 +3363,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
|
||
let field_ty = self.field_ty(expr.span, field, args); | ||
|
||
// FIXME: DSTs with static alignment should be allowed | ||
// Enums are anyway always sized. But just to safeguard against future | ||
// language extensions, let's double-check. | ||
self.require_type_is_sized(field_ty, expr.span, ObligationCauseCode::Misc); | ||
|
||
if field.vis.is_accessible_from(sub_def_scope, self.tcx) { | ||
|
@@ -3391,8 +3392,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
{ | ||
let field_ty = self.field_ty(expr.span, field, args); | ||
|
||
// FIXME: DSTs with static alignment should be allowed | ||
self.require_type_is_sized(field_ty, expr.span, ObligationCauseCode::Misc); | ||
if self.tcx.features().offset_of_slice { | ||
self.require_type_has_static_alignment( | ||
field_ty, | ||
expr.span, | ||
ObligationCauseCode::Misc, | ||
); | ||
} else { | ||
self.require_type_is_sized( | ||
field_ty, | ||
expr.span, | ||
ObligationCauseCode::Misc, | ||
); | ||
} | ||
|
||
if field.vis.is_accessible_from(def_scope, self.tcx) { | ||
self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None); | ||
|
@@ -3412,10 +3424,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
if let Ok(index) = field.as_str().parse::<usize>() | ||
&& field.name == sym::integer(index) | ||
{ | ||
for ty in tys.iter().take(index + 1) { | ||
self.require_type_is_sized(ty, expr.span, ObligationCauseCode::Misc); | ||
} | ||
Comment on lines
-3415
to
-3417
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's the justification for removing this? Trivial that this is implied by the well-formedness of the type? For annoying level of completetness, is there a test that exercises this? If not, pls add. Maybe something like 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. I made sure all tests still pass and couldn't see any reason why the loop existed, and there was no comment explaining it, so I removed it. We don't have a similar loop for ADTs either. Not sure what is the point of a test for an obviously ill-formed type... and why only for tuples? Such a test, if we want it, is entirely orthogonal for this PR and could cover all sorts of axes of ill-definedness of a type. EDIT: Turns out we had a good place to put such a test. 🤷 |
||
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, | ||
ObligationCauseCode::Misc, | ||
); | ||
} else { | ||
self.require_type_is_sized( | ||
field_ty, | ||
expr.span, | ||
ObligationCauseCode::Misc, | ||
); | ||
} | ||
|
||
field_indices.push((FIRST_VARIANT, index.into())); | ||
current_container = field_ty; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -386,6 +386,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
} | ||
} | ||
|
||
pub fn require_type_has_static_alignment( | ||
&self, | ||
ty: Ty<'tcx>, | ||
span: Span, | ||
code: traits::ObligationCauseCode<'tcx>, | ||
) { | ||
if !ty.references_error() { | ||
let tail = | ||
self.tcx.struct_tail_with_normalize(ty, |ty| self.normalize(span, ty), || {}); | ||
// Sized types have static alignment, and so do slices. | ||
if tail.is_trivially_sized(self.tcx) || matches!(tail.kind(), ty::Slice(..)) { | ||
// Nothing else is required here. | ||
} else { | ||
// We can't be sure, let's required full `Sized`. | ||
let lang_item = self.tcx.require_lang_item(LangItem::Sized, None); | ||
self.require_type_meets(ty, span, code, lang_item); | ||
Comment on lines
+403
to
+404
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.
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. That also duplicates some of the logic, though. |
||
} | ||
} | ||
} | ||
|
||
pub fn register_bound( | ||
&self, | ||
ty: Ty<'tcx>, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use std::mem::offset_of; | ||
|
||
struct S { | ||
a: u8, | ||
b: (u8, u8), | ||
c: [i32], | ||
} | ||
|
||
struct T { | ||
x: i32, | ||
y: S, | ||
} | ||
|
||
type Tup = (i32, [i32]); | ||
|
||
fn main() { | ||
offset_of!(S, c); //~ ERROR size for values of type `[i32]` cannot be known at compilation time | ||
} | ||
|
||
fn other() { | ||
offset_of!(T, y); //~ ERROR size for values of type `[i32]` cannot be known at compilation time | ||
} | ||
|
||
fn tuple() { | ||
offset_of!(Tup, 1); //~ ERROR size for values of type `[i32]` cannot be known at compilation time | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
error[E0277]: the size for values of type `[i32]` cannot be known at compilation time | ||
--> $DIR/feature-gate-offset-of-slice.rs:17:5 | ||
| | ||
LL | offset_of!(S, c); | ||
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `[i32]` | ||
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error[E0277]: the size for values of type `[i32]` cannot be known at compilation time | ||
--> $DIR/feature-gate-offset-of-slice.rs:21:5 | ||
| | ||
LL | offset_of!(T, y); | ||
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: within `S`, the trait `Sized` is not implemented for `[i32]`, which is required by `S: Sized` | ||
note: required because it appears within the type `S` | ||
--> $DIR/feature-gate-offset-of-slice.rs:3:8 | ||
| | ||
LL | struct S { | ||
| ^ | ||
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error[E0277]: the size for values of type `[i32]` cannot be known at compilation time | ||
--> $DIR/feature-gate-offset-of-slice.rs:25:5 | ||
| | ||
LL | offset_of!(Tup, 1); | ||
| ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `[i32]` | ||
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//@run-pass | ||
#![feature(offset_of_slice, offset_of_nested)] | ||
|
||
use std::mem::offset_of; | ||
|
||
#[repr(C)] | ||
struct S { | ||
a: u8, | ||
b: (u8, u8), | ||
c: [i32], | ||
} | ||
|
||
#[repr(C)] | ||
struct T { | ||
x: i8, | ||
y: S, | ||
} | ||
|
||
type Tup = (i16, [i32]); | ||
|
||
fn main() { | ||
assert_eq!(offset_of!(S, c), 4); | ||
assert_eq!(offset_of!(T, y), 4); | ||
assert_eq!(offset_of!(T, y.c), 8); | ||
assert_eq!(offset_of!(Tup, 1), 4); | ||
} |
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.
Can't leave an inline comment bc github is dumb, but there's a:
...above for the enum case.
But we don't allow that for enums, since their tails are required to be sized. Can you tweak that message so it doesn't look like something that someone may be interested in fixing, since it's not really actionable unless we were to tweak enums in general.