Skip to content

Commit e3faeb4

Browse files
committed
mir: Improve size_of handling when arg is unsized
1 parent a243ad2 commit e3faeb4

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

Diff for: compiler/rustc_middle/src/mir/interpret/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ pub enum InvalidProgramInfo<'tcx> {
127127
Layout(layout::LayoutError<'tcx>),
128128
/// An invalid transmute happened.
129129
TransmuteSizeDiff(Ty<'tcx>, Ty<'tcx>),
130+
/// SizeOf of unsized type was requested.
131+
SizeOfUnsizedType(Ty<'tcx>),
130132
}
131133

132134
impl fmt::Display for InvalidProgramInfo<'_> {
@@ -144,6 +146,7 @@ impl fmt::Display for InvalidProgramInfo<'_> {
144146
"transmuting `{}` to `{}` is not possible, because these types do not have the same size",
145147
from_ty, to_ty
146148
),
149+
SizeOfUnsizedType(ty) => write!(f, "size_of called on unsized type `{}`", ty),
147150
}
148151
}
149152
}

Diff for: compiler/rustc_mir/src/interpret/step.rs

+1
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
270270
self.frame().current_span(),
271271
&format!("SizeOf nullary MIR operator called for unsized type {}", ty),
272272
);
273+
throw_inval!(SizeOfUnsizedType(ty));
273274
}
274275
self.write_scalar(Scalar::from_machine_usize(layout.size.bytes(), self), dest)?;
275276
}

Diff for: src/test/ui/mir/issue-80742.stderr

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
3+
|
4+
LL | intrinsics::size_of::<T>()
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| size_of called on unsized type `dyn Debug`
8+
| inside `std::mem::size_of::<dyn Debug>` at $SRC_DIR/core/src/mem/mod.rs:LL:COL
9+
|
10+
::: $DIR/issue-80742.rs:23:10
11+
|
12+
LL | [u8; size_of::<T>() + 1]: ,
13+
| -------------- inside `Inline::<dyn Debug>::{constant#0}` at $DIR/issue-80742.rs:23:10
14+
115
error[E0599]: no function or associated item named `new` found for struct `Inline<dyn Debug>` in the current scope
216
--> $DIR/issue-80742.rs:31:36
317
|
@@ -21,6 +35,20 @@ LL | pub trait Debug {
2135
= note: the method `new` exists but the following trait bounds were not satisfied:
2236
`dyn Debug: Sized`
2337

38+
error[E0080]: evaluation of constant value failed
39+
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
40+
|
41+
LL | intrinsics::size_of::<T>()
42+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
43+
| |
44+
| size_of called on unsized type `dyn Debug`
45+
| inside `std::mem::size_of::<dyn Debug>` at $SRC_DIR/core/src/mem/mod.rs:LL:COL
46+
|
47+
::: $DIR/issue-80742.rs:15:10
48+
|
49+
LL | [u8; size_of::<T>() + 1]: ,
50+
| -------------- inside `Inline::<dyn Debug>::{constant#0}` at $DIR/issue-80742.rs:15:10
51+
2452
error[E0277]: the size for values of type `dyn Debug` cannot be known at compilation time
2553
--> $DIR/issue-80742.rs:31:15
2654
|
@@ -36,7 +64,7 @@ help: consider relaxing the implicit `Sized` restriction
3664
LL | struct Inline<T: ?Sized>
3765
| ^^^^^^^^
3866

39-
error: aborting due to 2 previous errors
67+
error: aborting due to 4 previous errors
4068

41-
Some errors have detailed explanations: E0277, E0599.
42-
For more information about an error, try `rustc --explain E0277`.
69+
Some errors have detailed explanations: E0080, E0277, E0599.
70+
For more information about an error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)