@@ -14,7 +14,7 @@ use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKi
1414use rustc_infer:: infer:: { RegionVariableOrigin , TyCtxtInferExt } ;
1515use rustc_middle:: hir:: nested_filter;
1616use rustc_middle:: ty:: fold:: TypeFoldable ;
17- use rustc_middle:: ty:: layout:: MAX_SIMD_LANES ;
17+ use rustc_middle:: ty:: layout:: { LayoutError , MAX_SIMD_LANES } ;
1818use rustc_middle:: ty:: subst:: GenericArgKind ;
1919use rustc_middle:: ty:: util:: { Discr , IntTypeExt } ;
2020use rustc_middle:: ty:: { self , OpaqueTypeKey , ParamEnv , Ty , TyCtxt } ;
@@ -415,10 +415,31 @@ fn check_static_inhabited<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, span: Spa
415415 // have UB during initialization if they are uninhabited, but there also seems to be no good
416416 // reason to allow any statics to be uninhabited.
417417 let ty = tcx. type_of ( def_id) ;
418- let Ok ( layout) = tcx. layout_of ( ParamEnv :: reveal_all ( ) . and ( ty) ) else {
418+ let layout = match tcx. layout_of ( ParamEnv :: reveal_all ( ) . and ( ty) ) {
419+ Ok ( l) => l,
420+ // Foreign statics that overflow their allowed size should emit an error
421+ Err ( LayoutError :: SizeOverflow ( _) )
422+ if {
423+ let node = tcx. hir ( ) . get_by_def_id ( def_id) ;
424+ matches ! (
425+ node,
426+ hir:: Node :: ForeignItem ( hir:: ForeignItem {
427+ kind: hir:: ForeignItemKind :: Static ( ..) ,
428+ ..
429+ } )
430+ )
431+ } =>
432+ {
433+ tcx. sess
434+ . struct_span_err ( span, "extern static is too large for the current architecture" )
435+ . emit ( ) ;
436+ return ;
437+ }
419438 // Generic statics are rejected, but we still reach this case.
420- tcx. sess . delay_span_bug ( span, "generic static must be rejected" ) ;
421- return ;
439+ Err ( e) => {
440+ tcx. sess . delay_span_bug ( span, & e. to_string ( ) ) ;
441+ return ;
442+ }
422443 } ;
423444 if layout. abi . is_uninhabited ( ) {
424445 tcx. struct_span_lint_hir (
0 commit comments