@@ -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,6 +17,7 @@ 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:: is_call_from_compiler_builtins_to_upstream_monomorphization;
1921use rustc_session:: config:: OptLevel ;
2022use rustc_span:: { source_map:: Spanned , sym, Span } ;
2123use rustc_target:: abi:: call:: { ArgAbi , FnAbi , PassMode , Reg } ;
@@ -157,8 +159,27 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
157159 destination : Option < ( ReturnDest < ' tcx , Bx :: Value > , mir:: BasicBlock ) > ,
158160 mut unwind : mir:: UnwindAction ,
159161 copied_constant_arguments : & [ PlaceRef < ' tcx , <Bx as BackendTypes >:: Value > ] ,
162+ instance : Option < Instance < ' tcx > > ,
160163 mergeable_succ : bool ,
161164 ) -> MergingSucc {
165+ let tcx = bx. tcx ( ) ;
166+ if let Some ( instance) = instance {
167+ if is_call_from_compiler_builtins_to_upstream_monomorphization ( tcx, instance) {
168+ if destination. is_some ( ) {
169+ let callee = with_no_trimmed_paths ! ( tcx. def_path_str( instance. def_id( ) ) ) ;
170+ tcx. dcx ( ) . emit_err ( CompilerBuiltinsCannotCall { callee } ) ;
171+ } else {
172+ info ! (
173+ "compiler_builtins call to diverging function {:?} replaced with abort" ,
174+ instance. def_id( )
175+ ) ;
176+ bx. abort ( ) ;
177+ bx. unreachable ( ) ;
178+ return MergingSucc :: False ;
179+ }
180+ }
181+ }
182+
162183 // If there is a cleanup block and the function we're calling can unwind, then
163184 // do an invoke, otherwise do a call.
164185 let fn_ty = bx. fn_decl_backend_type ( fn_abi) ;
@@ -480,6 +501,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
480501 let ty = location. ty ( self . mir , bx. tcx ( ) ) . ty ;
481502 let ty = self . monomorphize ( ty) ;
482503 let drop_fn = Instance :: resolve_drop_in_place ( bx. tcx ( ) , ty) ;
504+ let instance = drop_fn. clone ( ) ;
483505
484506 if let ty:: InstanceDef :: DropGlue ( _, None ) = drop_fn. def {
485507 // we don't actually need to drop anything.
@@ -582,6 +604,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
582604 Some ( ( ReturnDest :: Nothing , target) ) ,
583605 unwind,
584606 & [ ] ,
607+ Some ( instance) ,
585608 mergeable_succ,
586609 )
587610 }
@@ -658,10 +681,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
658681 }
659682 } ;
660683
661- let ( fn_abi, llfn) = common:: build_langcall ( bx, Some ( span) , lang_item) ;
684+ let ( fn_abi, llfn, instance ) = common:: build_langcall ( bx, Some ( span) , lang_item) ;
662685
663686 // Codegen the actual panic invoke/call.
664- let merging_succ = helper. do_call ( self , bx, fn_abi, llfn, & args, None , unwind, & [ ] , false ) ;
687+ let merging_succ =
688+ helper. do_call ( self , bx, fn_abi, llfn, & args, None , unwind, & [ ] , Some ( instance) , false ) ;
665689 assert_eq ! ( merging_succ, MergingSucc :: False ) ;
666690 MergingSucc :: False
667691 }
@@ -677,7 +701,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
677701 self . set_debug_loc ( bx, terminator. source_info ) ;
678702
679703 // Obtain the panic entry point.
680- let ( fn_abi, llfn) = common:: build_langcall ( bx, Some ( span) , reason. lang_item ( ) ) ;
704+ let ( fn_abi, llfn, instance ) = common:: build_langcall ( bx, Some ( span) , reason. lang_item ( ) ) ;
681705
682706 // Codegen the actual panic invoke/call.
683707 let merging_succ = helper. do_call (
@@ -689,6 +713,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
689713 None ,
690714 mir:: UnwindAction :: Unreachable ,
691715 & [ ] ,
716+ Some ( instance) ,
692717 false ,
693718 ) ;
694719 assert_eq ! ( merging_succ, MergingSucc :: False ) ;
@@ -738,7 +763,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
738763 let msg = bx. const_str ( & msg_str) ;
739764
740765 // Obtain the panic entry point.
741- let ( fn_abi, llfn) =
766+ let ( fn_abi, llfn, instance ) =
742767 common:: build_langcall ( bx, Some ( source_info. span ) , LangItem :: PanicNounwind ) ;
743768
744769 // Codegen the actual panic invoke/call.
@@ -751,6 +776,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
751776 target. as_ref ( ) . map ( |bb| ( ReturnDest :: Nothing , * bb) ) ,
752777 unwind,
753778 & [ ] ,
779+ Some ( instance) ,
754780 mergeable_succ,
755781 )
756782 } else {
@@ -798,6 +824,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
798824 ty:: FnPtr ( _) => ( None , Some ( callee. immediate ( ) ) ) ,
799825 _ => bug ! ( "{} is not callable" , callee. layout. ty) ,
800826 } ;
827+
801828 let def = instance. map ( |i| i. def ) ;
802829
803830 if let Some ( ty:: InstanceDef :: DropGlue ( _, None ) ) = def {
@@ -1106,6 +1133,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
11061133 destination,
11071134 unwind,
11081135 & copied_constant_arguments,
1136+ instance,
11091137 mergeable_succ,
11101138 )
11111139 }
@@ -1664,7 +1692,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
16641692
16651693 self . set_debug_loc ( & mut bx, mir:: SourceInfo :: outermost ( self . mir . span ) ) ;
16661694
1667- let ( fn_abi, fn_ptr) = common:: build_langcall ( & bx, None , reason. lang_item ( ) ) ;
1695+ let ( fn_abi, fn_ptr, _instance ) = common:: build_langcall ( & bx, None , reason. lang_item ( ) ) ;
16681696 let fn_ty = bx. fn_decl_backend_type ( fn_abi) ;
16691697
16701698 let llret = bx. call ( fn_ty, None , Some ( fn_abi) , fn_ptr, & [ ] , funclet. as_ref ( ) ) ;
0 commit comments