Skip to content

Commit bc950c8

Browse files
authored
Rollup merge of rust-lang#81185 - osa1:fix_80742, r=oli-obk
Fix ICE in mir when evaluating SizeOf on unsized type Not quite ready yet. This tries to fix rust-lang#80742 as discussed on [Zulip topic][1], by using `delay_span_bug`. I don't understand what `delay_span_bug` does. It seems like my error message is never used. With this patch, in this program: ```rust #![allow(incomplete_features)] #![feature(const_evaluatable_checked)] #![feature(const_generics)] use std::fmt::Debug; use std::marker::PhantomData; use std::mem::size_of; struct Inline<T> where [u8; size_of::<T>() + 1]: , { _phantom: PhantomData<T>, buf: [u8; size_of::<T>() + 1], } impl<T> Inline<T> where [u8; size_of::<T>() + 1]: , { pub fn new(val: T) -> Inline<T> { todo!() } } fn main() { let dst = Inline::<dyn Debug>::new(0); // line 27 } ``` these errors are printed, both for line 27 (annotated line above): - "no function or associated item named `new` found for struct `Inline<dyn Debug>` in the current scope" - "the size for values of type `dyn Debug` cannot be known at compilation time" Second error makes sense, but I'm not sure about the first one and why it's even printed. Finally, I'm not sure about the span passing in `const_eval`. [1]: https://rust-lang.zulipchat.com/#narrow/stream/269128-miri/topic/Help.20fixing.20.2380742
2 parents 0aeb2fc + 3fb53c2 commit bc950c8

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

compiler/rustc_mir/src/interpret/step.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
264264
NullaryOp(mir::NullOp::SizeOf, ty) => {
265265
let ty = self.subst_from_current_frame_and_normalize_erasing_regions(ty);
266266
let layout = self.layout_of(ty)?;
267-
assert!(
268-
!layout.is_unsized(),
269-
"SizeOf nullary MIR operator called for unsized type"
270-
);
267+
if layout.is_unsized() {
268+
// FIXME: This should be a span_bug (#80742)
269+
self.tcx.sess.delay_span_bug(
270+
self.frame().current_span(),
271+
&format!("SizeOf nullary MIR operator called for unsized type {}", ty),
272+
);
273+
}
271274
self.write_scalar(Scalar::from_machine_usize(layout.size.bytes(), self), dest)?;
272275
}
273276

src/test/ui/mir/issue-80742.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// check-fail
2+
3+
// This test used to cause an ICE in rustc_mir::interpret::step::eval_rvalue_into_place
4+
5+
#![allow(incomplete_features)]
6+
#![feature(const_evaluatable_checked)]
7+
#![feature(const_generics)]
8+
9+
use std::fmt::Debug;
10+
use std::marker::PhantomData;
11+
use std::mem::size_of;
12+
13+
struct Inline<T>
14+
where
15+
[u8; size_of::<T>() + 1]: ,
16+
{
17+
_phantom: PhantomData<T>,
18+
buf: [u8; size_of::<T>() + 1],
19+
}
20+
21+
impl<T> Inline<T>
22+
where
23+
[u8; size_of::<T>() + 1]: ,
24+
{
25+
pub fn new(val: T) -> Inline<T> {
26+
todo!()
27+
}
28+
}
29+
30+
fn main() {
31+
let dst = Inline::<dyn Debug>::new(0); //~ ERROR
32+
//~^ ERROR
33+
}

src/test/ui/mir/issue-80742.stderr

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
error[E0599]: no function or associated item named `new` found for struct `Inline<dyn Debug>` in the current scope
2+
--> $DIR/issue-80742.rs:31:36
3+
|
4+
LL | / struct Inline<T>
5+
LL | | where
6+
LL | | [u8; size_of::<T>() + 1]: ,
7+
LL | | {
8+
LL | | _phantom: PhantomData<T>,
9+
LL | | buf: [u8; size_of::<T>() + 1],
10+
LL | | }
11+
| |_- function or associated item `new` not found for this
12+
...
13+
LL | let dst = Inline::<dyn Debug>::new(0);
14+
| ^^^ function or associated item not found in `Inline<dyn Debug>`
15+
|
16+
::: $SRC_DIR/core/src/fmt/mod.rs:LL:COL
17+
|
18+
LL | pub trait Debug {
19+
| --------------- doesn't satisfy `dyn Debug: Sized`
20+
|
21+
= note: the method `new` exists but the following trait bounds were not satisfied:
22+
`dyn Debug: Sized`
23+
24+
error[E0277]: the size for values of type `dyn Debug` cannot be known at compilation time
25+
--> $DIR/issue-80742.rs:31:15
26+
|
27+
LL | struct Inline<T>
28+
| - required by this bound in `Inline`
29+
...
30+
LL | let dst = Inline::<dyn Debug>::new(0);
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
32+
|
33+
= help: the trait `Sized` is not implemented for `dyn Debug`
34+
help: consider relaxing the implicit `Sized` restriction
35+
|
36+
LL | struct Inline<T: ?Sized>
37+
| ^^^^^^^^
38+
39+
error: aborting due to 2 previous errors
40+
41+
Some errors have detailed explanations: E0277, E0599.
42+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)