@@ -5,6 +5,7 @@ use super::{CachedLlbb, FunctionCx, LocalRef};
55
66use crate :: base;
77use crate :: common:: { self , IntPredicate } ;
8+ use crate :: errors:: CompilerBuiltinsCannotCall ;
89use crate :: meth;
910use crate :: traits:: * ;
1011use crate :: MemFlags ;
@@ -16,7 +17,9 @@ use rustc_middle::mir::{self, AssertKind, BasicBlock, SwitchTargets, UnwindTermi
1617use rustc_middle:: ty:: layout:: { HasTyCtxt , LayoutOf , ValidityRequirement } ;
1718use rustc_middle:: ty:: print:: { with_no_trimmed_paths, with_no_visible_paths} ;
1819use rustc_middle:: ty:: { self , Instance , Ty } ;
20+ use rustc_monomorphize:: should_codegen_locally;
1921use rustc_session:: config:: OptLevel ;
22+ use rustc_span:: def_id:: LOCAL_CRATE ;
2023use rustc_span:: { source_map:: Spanned , sym, Span } ;
2124use rustc_target:: abi:: call:: { ArgAbi , FnAbi , PassMode , Reg } ;
2225use rustc_target:: abi:: { self , HasDataLayout , WrappingRange } ;
@@ -157,8 +160,31 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
157160 destination : Option < ( ReturnDest < ' tcx , Bx :: Value > , mir:: BasicBlock ) > ,
158161 mut unwind : mir:: UnwindAction ,
159162 copied_constant_arguments : & [ PlaceRef < ' tcx , <Bx as BackendTypes >:: Value > ] ,
163+ instance : Option < Instance < ' tcx > > ,
160164 mergeable_succ : bool ,
161165 ) -> MergingSucc {
166+ let tcx = bx. tcx ( ) ;
167+ if let Some ( instance) = instance {
168+ let def_id = instance. def_id ( ) ;
169+ if !def_id. is_local ( )
170+ && tcx. is_compiler_builtins ( LOCAL_CRATE )
171+ && !should_codegen_locally ( tcx, & instance)
172+ {
173+ if destination. is_some ( ) {
174+ let callee = with_no_trimmed_paths ! ( tcx. def_path_str( def_id) ) ;
175+ tcx. dcx ( ) . emit_err ( CompilerBuiltinsCannotCall { callee } ) ;
176+ } else {
177+ info ! (
178+ "compiler_builtins call to diverging function {:?} replaced with abort" ,
179+ instance. def_id( )
180+ ) ;
181+ bx. abort ( ) ;
182+ bx. unreachable ( ) ;
183+ return MergingSucc :: False ;
184+ }
185+ }
186+ }
187+
162188 // If there is a cleanup block and the function we're calling can unwind, then
163189 // do an invoke, otherwise do a call.
164190 let fn_ty = bx. fn_decl_backend_type ( fn_abi) ;
@@ -480,6 +506,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
480506 let ty = location. ty ( self . mir , bx. tcx ( ) ) . ty ;
481507 let ty = self . monomorphize ( ty) ;
482508 let drop_fn = Instance :: resolve_drop_in_place ( bx. tcx ( ) , ty) ;
509+ let instance = drop_fn. clone ( ) ;
483510
484511 if let ty:: InstanceDef :: DropGlue ( _, None ) = drop_fn. def {
485512 // we don't actually need to drop anything.
@@ -582,6 +609,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
582609 Some ( ( ReturnDest :: Nothing , target) ) ,
583610 unwind,
584611 & [ ] ,
612+ Some ( instance) ,
585613 mergeable_succ,
586614 )
587615 }
@@ -658,10 +686,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
658686 }
659687 } ;
660688
661- let ( fn_abi, llfn) = common:: build_langcall ( bx, Some ( span) , lang_item) ;
689+ let ( fn_abi, llfn, instance ) = common:: build_langcall ( bx, Some ( span) , lang_item) ;
662690
663691 // Codegen the actual panic invoke/call.
664- let merging_succ = helper. do_call ( self , bx, fn_abi, llfn, & args, None , unwind, & [ ] , false ) ;
692+ let merging_succ =
693+ helper. do_call ( self , bx, fn_abi, llfn, & args, None , unwind, & [ ] , Some ( instance) , false ) ;
665694 assert_eq ! ( merging_succ, MergingSucc :: False ) ;
666695 MergingSucc :: False
667696 }
@@ -677,7 +706,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
677706 self . set_debug_loc ( bx, terminator. source_info ) ;
678707
679708 // Obtain the panic entry point.
680- let ( fn_abi, llfn) = common:: build_langcall ( bx, Some ( span) , reason. lang_item ( ) ) ;
709+ let ( fn_abi, llfn, instance ) = common:: build_langcall ( bx, Some ( span) , reason. lang_item ( ) ) ;
681710
682711 // Codegen the actual panic invoke/call.
683712 let merging_succ = helper. do_call (
@@ -689,6 +718,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
689718 None ,
690719 mir:: UnwindAction :: Unreachable ,
691720 & [ ] ,
721+ Some ( instance) ,
692722 false ,
693723 ) ;
694724 assert_eq ! ( merging_succ, MergingSucc :: False ) ;
@@ -738,7 +768,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
738768 let msg = bx. const_str ( & msg_str) ;
739769
740770 // Obtain the panic entry point.
741- let ( fn_abi, llfn) =
771+ let ( fn_abi, llfn, instance ) =
742772 common:: build_langcall ( bx, Some ( source_info. span ) , LangItem :: PanicNounwind ) ;
743773
744774 // Codegen the actual panic invoke/call.
@@ -751,6 +781,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
751781 target. as_ref ( ) . map ( |bb| ( ReturnDest :: Nothing , * bb) ) ,
752782 unwind,
753783 & [ ] ,
784+ Some ( instance) ,
754785 mergeable_succ,
755786 )
756787 } else {
@@ -798,6 +829,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
798829 ty:: FnPtr ( _) => ( None , Some ( callee. immediate ( ) ) ) ,
799830 _ => bug ! ( "{} is not callable" , callee. layout. ty) ,
800831 } ;
832+
801833 let def = instance. map ( |i| i. def ) ;
802834
803835 if let Some ( ty:: InstanceDef :: DropGlue ( _, None ) ) = def {
@@ -1106,6 +1138,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
11061138 destination,
11071139 unwind,
11081140 & copied_constant_arguments,
1141+ instance,
11091142 mergeable_succ,
11101143 )
11111144 }
@@ -1664,7 +1697,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
16641697
16651698 self . set_debug_loc ( & mut bx, mir:: SourceInfo :: outermost ( self . mir . span ) ) ;
16661699
1667- let ( fn_abi, fn_ptr) = common:: build_langcall ( & bx, None , reason. lang_item ( ) ) ;
1700+ let ( fn_abi, fn_ptr, _instance ) = common:: build_langcall ( & bx, None , reason. lang_item ( ) ) ;
16681701 let fn_ty = bx. fn_decl_backend_type ( fn_abi) ;
16691702
16701703 let llret = bx. call ( fn_ty, None , Some ( fn_abi) , fn_ptr, & [ ] , funclet. as_ref ( ) ) ;
0 commit comments