diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index ccae524352a59..023321a8453c4 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -1191,7 +1191,24 @@ pub(crate) fn check_static_item<'tcx>( let is_foreign_item = tcx.is_foreign_item(item_id); let forbid_unsized = !is_foreign_item || { - let tail = tcx.struct_tail_for_codegen(item_ty, wfcx.infcx.typing_env(wfcx.param_env)); + let tail = tcx.struct_tail_raw( + item_ty, + &ObligationCause::dummy(), + |ty| match tcx + .try_normalize_erasing_regions(wfcx.infcx.typing_env(wfcx.param_env), ty) + { + Ok(ty) => ty, + Err(e) => { + tcx.dcx().span_delayed_bug( + span, + format!("could not normalize field type: {e:?}"), + ); + ty + } + }, + || {}, + ); + !matches!(tail.kind(), ty::Foreign(_)) }; diff --git a/tests/ui/static/extern-static-normalization_failure-issue-148161.rs b/tests/ui/static/extern-static-normalization_failure-issue-148161.rs new file mode 100644 index 0000000000000..b2e1f6f8f466e --- /dev/null +++ b/tests/ui/static/extern-static-normalization_failure-issue-148161.rs @@ -0,0 +1,18 @@ +trait Trait { + type Assoc; +} + +impl Trait for u8 { + type Assoc = i8; +} + +struct Struct { + member: T::Assoc, +} + +unsafe extern "C" { + static VAR: Struct; + //~^ ERROR: the trait bound `i8: Trait` is not satisfied +} + +fn main() {} diff --git a/tests/ui/static/extern-static-normalization_failure-issue-148161.stderr b/tests/ui/static/extern-static-normalization_failure-issue-148161.stderr new file mode 100644 index 0000000000000..0678f3c71797b --- /dev/null +++ b/tests/ui/static/extern-static-normalization_failure-issue-148161.stderr @@ -0,0 +1,20 @@ +error[E0277]: the trait bound `i8: Trait` is not satisfied + --> $DIR/extern-static-normalization_failure-issue-148161.rs:14:17 + | +LL | static VAR: Struct; + | ^^^^^^^^^^ the trait `Trait` is not implemented for `i8` + | +help: the trait `Trait` is implemented for `u8` + --> $DIR/extern-static-normalization_failure-issue-148161.rs:5:1 + | +LL | impl Trait for u8 { + | ^^^^^^^^^^^^^^^^^ +note: required by a bound in `Struct` + --> $DIR/extern-static-normalization_failure-issue-148161.rs:9:18 + | +LL | struct Struct { + | ^^^^^ required by this bound in `Struct` + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0277`.