Skip to content

Commit 2f566a8

Browse files
authored
Rollup merge of #149210 - ShoyuVanilla:issue-148161, r=jdonszelmann
fix: Do not ICE on normalization failure of an extern static item Fixes #148161
2 parents 53276ad + cf46682 commit 2f566a8

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,14 +1186,21 @@ pub(crate) fn check_static_item<'tcx>(
11861186
) -> Result<(), ErrorGuaranteed> {
11871187
enter_wf_checking_ctxt(tcx, item_id, |wfcx| {
11881188
let span = tcx.ty_span(item_id);
1189-
let item_ty = wfcx.deeply_normalize(span, Some(WellFormedLoc::Ty(item_id)), ty);
1189+
let loc = Some(WellFormedLoc::Ty(item_id));
1190+
let item_ty = wfcx.deeply_normalize(span, loc, ty);
11901191

11911192
let is_foreign_item = tcx.is_foreign_item(item_id);
1193+
let is_structurally_foreign_item = || {
1194+
let tail = tcx.struct_tail_raw(
1195+
item_ty,
1196+
&ObligationCause::dummy(),
1197+
|ty| wfcx.deeply_normalize(span, loc, ty),
1198+
|| {},
1199+
);
11921200

1193-
let forbid_unsized = !is_foreign_item || {
1194-
let tail = tcx.struct_tail_for_codegen(item_ty, wfcx.infcx.typing_env(wfcx.param_env));
1195-
!matches!(tail.kind(), ty::Foreign(_))
1201+
matches!(tail.kind(), ty::Foreign(_))
11961202
};
1203+
let forbid_unsized = !(is_foreign_item && is_structurally_foreign_item());
11971204

11981205
wfcx.register_wf_obligation(span, Some(WellFormedLoc::Ty(item_id)), item_ty.into());
11991206
if forbid_unsized {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Regression test for https://github.com/rust-lang/rust/issues/148161
2+
trait Trait {
3+
type Assoc;
4+
}
5+
6+
impl Trait for u8 {
7+
type Assoc = i8;
8+
}
9+
10+
struct Struct<T: Trait> {
11+
member: T::Assoc,
12+
}
13+
14+
unsafe extern "C" {
15+
// This used to be an ICE due to normalization failure on `<Struct<i8> as Trait>::Assoc`
16+
// while wf checking this static item.
17+
static VAR: Struct<i8>;
18+
//~^ ERROR: the trait bound `i8: Trait` is not satisfied
19+
}
20+
21+
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:17: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:6: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:10: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)