@@ -5,6 +5,7 @@ use super::{CachedLlbb, FunctionCx, LocalRef};
5
5
6
6
use crate :: base;
7
7
use crate :: common:: { self , IntPredicate } ;
8
+ use crate :: errors:: CompilerBuiltinsCannotCall ;
8
9
use crate :: meth;
9
10
use crate :: traits:: * ;
10
11
use crate :: MemFlags ;
@@ -16,6 +17,7 @@ use rustc_middle::mir::{self, AssertKind, BasicBlock, SwitchTargets, UnwindTermi
16
17
use rustc_middle:: ty:: layout:: { HasTyCtxt , LayoutOf , ValidityRequirement } ;
17
18
use rustc_middle:: ty:: print:: { with_no_trimmed_paths, with_no_visible_paths} ;
18
19
use rustc_middle:: ty:: { self , Instance , Ty } ;
20
+ use rustc_monomorphize:: is_call_from_compiler_builtins_to_upstream_monomorphization;
19
21
use rustc_session:: config:: OptLevel ;
20
22
use rustc_span:: { source_map:: Spanned , sym, Span } ;
21
23
use rustc_target:: abi:: call:: { ArgAbi , FnAbi , PassMode , Reg } ;
@@ -157,8 +159,28 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
157
159
destination : Option < ( ReturnDest < ' tcx , Bx :: Value > , mir:: BasicBlock ) > ,
158
160
mut unwind : mir:: UnwindAction ,
159
161
copied_constant_arguments : & [ PlaceRef < ' tcx , <Bx as BackendTypes >:: Value > ] ,
162
+ instance : Option < Instance < ' tcx > > ,
160
163
mergeable_succ : bool ,
161
164
) -> 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 caller = with_no_trimmed_paths ! ( tcx. def_path_str( fx. instance. def_id( ) ) ) ;
170
+ let callee = with_no_trimmed_paths ! ( tcx. def_path_str( instance. def_id( ) ) ) ;
171
+ tcx. dcx ( ) . emit_err ( CompilerBuiltinsCannotCall { caller, callee } ) ;
172
+ } else {
173
+ info ! (
174
+ "compiler_builtins call to diverging function {:?} replaced with abort" ,
175
+ instance. def_id( )
176
+ ) ;
177
+ bx. abort ( ) ;
178
+ bx. unreachable ( ) ;
179
+ return MergingSucc :: False ;
180
+ }
181
+ }
182
+ }
183
+
162
184
// If there is a cleanup block and the function we're calling can unwind, then
163
185
// do an invoke, otherwise do a call.
164
186
let fn_ty = bx. fn_decl_backend_type ( fn_abi) ;
@@ -480,6 +502,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
480
502
let ty = location. ty ( self . mir , bx. tcx ( ) ) . ty ;
481
503
let ty = self . monomorphize ( ty) ;
482
504
let drop_fn = Instance :: resolve_drop_in_place ( bx. tcx ( ) , ty) ;
505
+ let instance = drop_fn. clone ( ) ;
483
506
484
507
if let ty:: InstanceDef :: DropGlue ( _, None ) = drop_fn. def {
485
508
// we don't actually need to drop anything.
@@ -582,6 +605,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
582
605
Some ( ( ReturnDest :: Nothing , target) ) ,
583
606
unwind,
584
607
& [ ] ,
608
+ Some ( instance) ,
585
609
mergeable_succ,
586
610
)
587
611
}
@@ -658,10 +682,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
658
682
}
659
683
} ;
660
684
661
- let ( fn_abi, llfn) = common:: build_langcall ( bx, Some ( span) , lang_item) ;
685
+ let ( fn_abi, llfn, instance ) = common:: build_langcall ( bx, Some ( span) , lang_item) ;
662
686
663
687
// Codegen the actual panic invoke/call.
664
- let merging_succ = helper. do_call ( self , bx, fn_abi, llfn, & args, None , unwind, & [ ] , false ) ;
688
+ let merging_succ =
689
+ helper. do_call ( self , bx, fn_abi, llfn, & args, None , unwind, & [ ] , Some ( instance) , false ) ;
665
690
assert_eq ! ( merging_succ, MergingSucc :: False ) ;
666
691
MergingSucc :: False
667
692
}
@@ -677,7 +702,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
677
702
self . set_debug_loc ( bx, terminator. source_info ) ;
678
703
679
704
// Obtain the panic entry point.
680
- let ( fn_abi, llfn) = common:: build_langcall ( bx, Some ( span) , reason. lang_item ( ) ) ;
705
+ let ( fn_abi, llfn, instance ) = common:: build_langcall ( bx, Some ( span) , reason. lang_item ( ) ) ;
681
706
682
707
// Codegen the actual panic invoke/call.
683
708
let merging_succ = helper. do_call (
@@ -689,6 +714,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
689
714
None ,
690
715
mir:: UnwindAction :: Unreachable ,
691
716
& [ ] ,
717
+ Some ( instance) ,
692
718
false ,
693
719
) ;
694
720
assert_eq ! ( merging_succ, MergingSucc :: False ) ;
@@ -738,7 +764,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
738
764
let msg = bx. const_str ( & msg_str) ;
739
765
740
766
// Obtain the panic entry point.
741
- let ( fn_abi, llfn) =
767
+ let ( fn_abi, llfn, instance ) =
742
768
common:: build_langcall ( bx, Some ( source_info. span ) , LangItem :: PanicNounwind ) ;
743
769
744
770
// Codegen the actual panic invoke/call.
@@ -751,6 +777,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
751
777
target. as_ref ( ) . map ( |bb| ( ReturnDest :: Nothing , * bb) ) ,
752
778
unwind,
753
779
& [ ] ,
780
+ Some ( instance) ,
754
781
mergeable_succ,
755
782
)
756
783
} else {
@@ -798,6 +825,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
798
825
ty:: FnPtr ( _) => ( None , Some ( callee. immediate ( ) ) ) ,
799
826
_ => bug ! ( "{} is not callable" , callee. layout. ty) ,
800
827
} ;
828
+
801
829
let def = instance. map ( |i| i. def ) ;
802
830
803
831
if let Some ( ty:: InstanceDef :: DropGlue ( _, None ) ) = def {
@@ -1106,6 +1134,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1106
1134
destination,
1107
1135
unwind,
1108
1136
& copied_constant_arguments,
1137
+ instance,
1109
1138
mergeable_succ,
1110
1139
)
1111
1140
}
@@ -1664,7 +1693,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1664
1693
1665
1694
self . set_debug_loc ( & mut bx, mir:: SourceInfo :: outermost ( self . mir . span ) ) ;
1666
1695
1667
- let ( fn_abi, fn_ptr) = common:: build_langcall ( & bx, None , reason. lang_item ( ) ) ;
1696
+ let ( fn_abi, fn_ptr, _instance ) = common:: build_langcall ( & bx, None , reason. lang_item ( ) ) ;
1668
1697
let fn_ty = bx. fn_decl_backend_type ( fn_abi) ;
1669
1698
1670
1699
let llret = bx. call ( fn_ty, None , Some ( fn_abi) , fn_ptr, & [ ] , funclet. as_ref ( ) ) ;
0 commit comments