Skip to content

Commit ebb7f64

Browse files
Rollup merge of #112193 - clubby789:offset-of-tuple-sized, r=est31
Check tuple elements are `Sized` in `offset_of` Fixes #112186
2 parents 2286046 + d722f27 commit ebb7f64

File tree

6 files changed

+75
-15
lines changed

6 files changed

+75
-15
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+10-9
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

+2
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

+14-5
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-tuple.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(offset_of)]
2+
#![feature(builtin_syntax)]
3+
4+
fn main() {
5+
core::mem::offset_of!((u8, u8), _0); //~ ERROR no field `_0`
6+
core::mem::offset_of!((u8, u8), +1); //~ ERROR no rules expected
7+
core::mem::offset_of!((u8, u8), -1); //~ ERROR no rules expected
8+
builtin # offset_of((u8, u8), _0); //~ ERROR no field `_0`
9+
builtin # offset_of((u8, u8), +1); //~ ERROR expected identifier
10+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error: expected identifier, found `+`
2+
--> $DIR/offset-of-tuple.rs:9:35
3+
|
4+
LL | builtin # offset_of((u8, u8), +1);
5+
| ^ expected identifier
6+
7+
error: no rules expected the token `1`
8+
--> $DIR/offset-of-tuple.rs:6:38
9+
|
10+
LL | core::mem::offset_of!((u8, u8), +1);
11+
| ^ no rules expected this token in macro call
12+
|
13+
= note: while trying to match sequence start
14+
15+
error: no rules expected the token `1`
16+
--> $DIR/offset-of-tuple.rs:7:38
17+
|
18+
LL | core::mem::offset_of!((u8, u8), -1);
19+
| ^ no rules expected this token in macro call
20+
|
21+
= note: while trying to match sequence start
22+
23+
error[E0609]: no field `_0` on type `(u8, u8)`
24+
--> $DIR/offset-of-tuple.rs:5:37
25+
|
26+
LL | core::mem::offset_of!((u8, u8), _0);
27+
| ^^
28+
29+
error[E0609]: no field `_0` on type `(u8, u8)`
30+
--> $DIR/offset-of-tuple.rs:8:35
31+
|
32+
LL | builtin # offset_of((u8, u8), _0);
33+
| ^^
34+
35+
error: aborting due to 5 previous errors
36+
37+
For more information about this error, try `rustc --explain E0609`.

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

+2-1
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)