Skip to content

Commit 731601c

Browse files
committed
Check tuple elements are Sized in offset_of
1 parent d59363a commit 731601c

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3117,16 +3117,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
31173117
}
31183118
}
31193119
ty::Tuple(tys) => {
3120-
let fstr = field.as_str();
3121-
3122-
if let Ok(index) = fstr.parse::<usize>() {
3123-
if fstr == index.to_string() {
3124-
if let Some(&field_ty) = tys.get(index) {
3125-
field_indices.push(index.into());
3126-
current_container = field_ty;
3120+
if let Ok(index) = field.as_str().parse::<usize>()
3121+
&& field.name == sym::integer(index)
3122+
{
3123+
for ty in tys.iter().take(index + 1) {
3124+
self.require_type_is_sized(ty, expr.span, traits::MiscObligation);
3125+
}
3126+
if let Some(&field_ty) = tys.get(index) {
3127+
field_indices.push(index.into());
3128+
current_container = field_ty;
31273129

3128-
continue;
3129-
}
3130+
continue;
31303131
}
31313132
}
31323133
}

tests/ui/offset-of/offset-of-dst-field.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ fn main() {
3636
offset_of!(Alpha, z); //~ ERROR the size for values of type
3737
offset_of!(Beta, z); //~ ERROR the size for values of type
3838
offset_of!(Gamma, z); //~ ERROR the size for values of type
39+
offset_of!((u8, dyn Trait), 0); // ok
40+
offset_of!((u8, dyn Trait), 1); //~ ERROR the size for values of type
3941
}
4042

4143
fn delta() {

tests/ui/offset-of/offset-of-dst-field.stderr

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@ LL | offset_of!(Gamma, z);
2525
= help: the trait `Sized` is not implemented for `Extern`
2626
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
2727

28+
error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
29+
--> $DIR/offset-of-dst-field.rs:40:5
30+
|
31+
LL | offset_of!((u8, dyn Trait), 1);
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
33+
|
34+
= help: the trait `Sized` is not implemented for `dyn Trait`
35+
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
36+
2837
error[E0277]: the size for values of type `Extern` cannot be known at compilation time
29-
--> $DIR/offset-of-dst-field.rs:43:5
38+
--> $DIR/offset-of-dst-field.rs:45:5
3039
|
3140
LL | offset_of!(Delta<Extern>, z);
3241
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
@@ -35,7 +44,7 @@ LL | offset_of!(Delta<Extern>, z);
3544
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
3645

3746
error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
38-
--> $DIR/offset-of-dst-field.rs:44:5
47+
--> $DIR/offset-of-dst-field.rs:46:5
3948
|
4049
LL | offset_of!(Delta<dyn Trait>, z);
4150
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
@@ -44,7 +53,7 @@ LL | offset_of!(Delta<dyn Trait>, z);
4453
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
4554

4655
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
47-
--> $DIR/offset-of-dst-field.rs:42:5
56+
--> $DIR/offset-of-dst-field.rs:44:5
4857
|
4958
LL | offset_of!(Delta<Alpha>, z);
5059
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
@@ -58,7 +67,7 @@ LL | struct Alpha {
5867
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)
5968

6069
error[E0277]: the size for values of type `T` cannot be known at compilation time
61-
--> $DIR/offset-of-dst-field.rs:48:5
70+
--> $DIR/offset-of-dst-field.rs:50:5
6271
|
6372
LL | fn generic_with_maybe_sized<T: ?Sized>() -> usize {
6473
| - this type parameter needs to be `std::marker::Sized`
@@ -72,6 +81,6 @@ LL - fn generic_with_maybe_sized<T: ?Sized>() -> usize {
7281
LL + fn generic_with_maybe_sized<T>() -> usize {
7382
|
7483

75-
error: aborting due to 7 previous errors
84+
error: aborting due to 8 previous errors
7685

7786
For more information about this error, try `rustc --explain E0277`.

tests/ui/offset-of/offset-of-unsized.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// build-pass
2-
// regression test for #112051
2+
// regression test for #112051, not in `offset-of-dst` as the issue is in codegen,
3+
// and isn't triggered in the presence of typeck errors
34

45
#![feature(offset_of)]
56

0 commit comments

Comments
 (0)