@@ -14,7 +14,7 @@ use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKi
14
14
use rustc_infer:: infer:: { RegionVariableOrigin , TyCtxtInferExt } ;
15
15
use rustc_middle:: hir:: nested_filter;
16
16
use rustc_middle:: ty:: fold:: TypeFoldable ;
17
- use rustc_middle:: ty:: layout:: MAX_SIMD_LANES ;
17
+ use rustc_middle:: ty:: layout:: { LayoutError , MAX_SIMD_LANES } ;
18
18
use rustc_middle:: ty:: subst:: GenericArgKind ;
19
19
use rustc_middle:: ty:: util:: { Discr , IntTypeExt } ;
20
20
use 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
415
415
// have UB during initialization if they are uninhabited, but there also seems to be no good
416
416
// reason to allow any statics to be uninhabited.
417
417
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
+ }
419
438
// 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
+ }
422
443
} ;
423
444
if layout. abi . is_uninhabited ( ) {
424
445
tcx. struct_span_lint_hir (
0 commit comments