Skip to content

Commit 18d9c03

Browse files
authored
Rollup merge of rust-lang#124997 - gurry:124848-ice-should-be-sized, r=Nadrieril
Fix ICE while casting a type with error Fixes rust-lang#124848 The ICE originates here: https://github.com/rust-lang/rust/blob/f9a3fd966162b3c7386d90fe4626471f66ba3b8f/compiler/rustc_hir_typeck/src/cast.rs#L143 The underlying cause is that a type with error, `MyType` was involved in a cast. During cast checks the below method `pointer_kind` was called: https://github.com/rust-lang/rust/blob/f9a3fd966162b3c7386d90fe4626471f66ba3b8f/compiler/rustc_hir_typeck/src/cast.rs#L87-L91 Thanks to the changes in PR rust-lang#123491, `type_is_sized_modulo_regions` in `pointer_kind` returned `false` which caused control to reach the `span_bug` here: https://github.com/rust-lang/rust/blob/f9a3fd966162b3c7386d90fe4626471f66ba3b8f/compiler/rustc_hir_typeck/src/cast.rs#L143 resulting in an ICE. This PR fixes the issue by changing the `span_bug` to a `span_delayed_bug`.
2 parents 74a78af + fb619ec commit 18d9c03

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

compiler/rustc_hir_typeck/src/cast.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
141141
| ty::Never
142142
| ty::Dynamic(_, _, ty::DynStar)
143143
| ty::Error(_) => {
144-
self.dcx().span_bug(span, format!("`{t:?}` should be sized but is not?"));
144+
let guar = self
145+
.dcx()
146+
.span_delayed_bug(span, format!("`{t:?}` should be sized but is not?"));
147+
return Err(guar);
145148
}
146149
})
147150
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Regression test for ICE #124848
2+
// Tests that there is no ICE when a cast
3+
// involves a type with error
4+
5+
use std::cell::Cell;
6+
7+
struct MyType<'a>(Cell<Option<&'unpinned mut MyType<'a>>>, Pin);
8+
//~^ ERROR use of undeclared lifetime name `'unpinned`
9+
//~| ERROR cannot find type `Pin` in this scope
10+
11+
fn main() {
12+
let mut unpinned = MyType(Cell::new(None));
13+
//~^ ERROR his struct takes 2 arguments but 1 argument was supplied
14+
let bad_addr = &unpinned as *const Cell<Option<&'a mut MyType<'a>>> as usize;
15+
//~^ ERROR use of undeclared lifetime name `'a`
16+
//~| ERROR use of undeclared lifetime name `'a`
17+
//~| ERROR casting `&MyType<'_>` as `*const Cell<Option<&mut MyType<'_>>>` is invalid
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
error[E0261]: use of undeclared lifetime name `'unpinned`
2+
--> $DIR/ice-cast-type-with-error-124848.rs:7:32
3+
|
4+
LL | struct MyType<'a>(Cell<Option<&'unpinned mut MyType<'a>>>, Pin);
5+
| - ^^^^^^^^^ undeclared lifetime
6+
| |
7+
| help: consider introducing lifetime `'unpinned` here: `'unpinned,`
8+
9+
error[E0261]: use of undeclared lifetime name `'a`
10+
--> $DIR/ice-cast-type-with-error-124848.rs:14:53
11+
|
12+
LL | fn main() {
13+
| - help: consider introducing lifetime `'a` here: `<'a>`
14+
...
15+
LL | let bad_addr = &unpinned as *const Cell<Option<&'a mut MyType<'a>>> as usize;
16+
| ^^ undeclared lifetime
17+
18+
error[E0261]: use of undeclared lifetime name `'a`
19+
--> $DIR/ice-cast-type-with-error-124848.rs:14:67
20+
|
21+
LL | fn main() {
22+
| - help: consider introducing lifetime `'a` here: `<'a>`
23+
...
24+
LL | let bad_addr = &unpinned as *const Cell<Option<&'a mut MyType<'a>>> as usize;
25+
| ^^ undeclared lifetime
26+
27+
error[E0412]: cannot find type `Pin` in this scope
28+
--> $DIR/ice-cast-type-with-error-124848.rs:7:60
29+
|
30+
LL | struct MyType<'a>(Cell<Option<&'unpinned mut MyType<'a>>>, Pin);
31+
| ^^^ not found in this scope
32+
|
33+
help: consider importing this struct
34+
|
35+
LL + use std::pin::Pin;
36+
|
37+
38+
error[E0061]: this struct takes 2 arguments but 1 argument was supplied
39+
--> $DIR/ice-cast-type-with-error-124848.rs:12:24
40+
|
41+
LL | let mut unpinned = MyType(Cell::new(None));
42+
| ^^^^^^----------------- an argument is missing
43+
|
44+
note: tuple struct defined here
45+
--> $DIR/ice-cast-type-with-error-124848.rs:7:8
46+
|
47+
LL | struct MyType<'a>(Cell<Option<&'unpinned mut MyType<'a>>>, Pin);
48+
| ^^^^^^
49+
help: provide the argument
50+
|
51+
LL | let mut unpinned = MyType(Cell::new(None), /* value */);
52+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53+
54+
error[E0606]: casting `&MyType<'_>` as `*const Cell<Option<&mut MyType<'_>>>` is invalid
55+
--> $DIR/ice-cast-type-with-error-124848.rs:14:20
56+
|
57+
LL | let bad_addr = &unpinned as *const Cell<Option<&'a mut MyType<'a>>> as usize;
58+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
59+
60+
error: aborting due to 6 previous errors
61+
62+
Some errors have detailed explanations: E0061, E0261, E0412, E0606.
63+
For more information about an error, try `rustc --explain E0061`.

0 commit comments

Comments
 (0)