Skip to content

Commit ec630b3

Browse files
committed
fix: Do not ICE on normalization failure of an extern static item
1 parent 5934b06 commit ec630b3

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,24 @@ pub(crate) fn check_static_item<'tcx>(
11911191
let is_foreign_item = tcx.is_foreign_item(item_id);
11921192

11931193
let forbid_unsized = !is_foreign_item || {
1194-
let tail = tcx.struct_tail_for_codegen(item_ty, wfcx.infcx.typing_env(wfcx.param_env));
1194+
let tail = tcx.struct_tail_raw(
1195+
item_ty,
1196+
&ObligationCause::dummy(),
1197+
|ty| match tcx
1198+
.try_normalize_erasing_regions(wfcx.infcx.typing_env(wfcx.param_env), ty)
1199+
{
1200+
Ok(ty) => ty,
1201+
Err(e) => {
1202+
tcx.dcx().span_delayed_bug(
1203+
span,
1204+
format!("could not normalize field type: {e:?}"),
1205+
);
1206+
ty
1207+
}
1208+
},
1209+
|| {},
1210+
);
1211+
11951212
!matches!(tail.kind(), ty::Foreign(_))
11961213
};
11971214

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
trait Trait {
2+
type Assoc;
3+
}
4+
5+
impl Trait for u8 {
6+
type Assoc = i8;
7+
}
8+
9+
struct Struct<T: Trait> {
10+
member: T::Assoc,
11+
}
12+
13+
unsafe extern "C" {
14+
static VAR: Struct<i8>;
15+
//~^ ERROR: the trait bound `i8: Trait` is not satisfied
16+
}
17+
18+
fn main() {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0277]: the trait bound `i8: Trait` is not satisfied
2+
--> $DIR/extern-static-normalization_failure-issue-148161.rs:14:17
3+
|
4+
LL | static VAR: Struct<i8>;
5+
| ^^^^^^^^^^ the trait `Trait` is not implemented for `i8`
6+
|
7+
help: the trait `Trait` is implemented for `u8`
8+
--> $DIR/extern-static-normalization_failure-issue-148161.rs:5:1
9+
|
10+
LL | impl Trait for u8 {
11+
| ^^^^^^^^^^^^^^^^^
12+
note: required by a bound in `Struct`
13+
--> $DIR/extern-static-normalization_failure-issue-148161.rs:9:18
14+
|
15+
LL | struct Struct<T: Trait> {
16+
| ^^^^^ required by this bound in `Struct`
17+
18+
error: aborting due to 1 previous error
19+
20+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)