11//! This pass statically detects code which has undefined behaviour or is likely to be erroneous.
22//! It can be used to locate problems in MIR building or optimizations. It assumes that all code
33//! can be executed, so it has false positives.
4+
5+ use rustc_hir:: { HirId , CRATE_HIR_ID } ;
46use rustc_index:: bit_set:: BitSet ;
57use rustc_middle:: mir:: visit:: { PlaceContext , Visitor } ;
68use rustc_middle:: mir:: * ;
79use rustc_middle:: ty:: TyCtxt ;
810use rustc_mir_dataflow:: impls:: { MaybeStorageDead , MaybeStorageLive } ;
911use rustc_mir_dataflow:: storage:: always_storage_live_locals;
1012use rustc_mir_dataflow:: { Analysis , ResultsCursor } ;
13+ use rustc_session:: lint:: builtin:: BROKEN_MIR ;
1114use std:: borrow:: Cow ;
1215
1316pub fn lint_body < ' tcx > ( tcx : TyCtxt < ' tcx > , body : & Body < ' tcx > , when : String ) {
@@ -28,6 +31,11 @@ pub fn lint_body<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, when: String) {
2831 tcx,
2932 when,
3033 body,
34+ lint_id : body
35+ . source
36+ . def_id ( )
37+ . as_local ( )
38+ . map_or ( CRATE_HIR_ID , |def_id| tcx. local_def_id_to_hir_id ( def_id) ) ,
3139 is_fn_like : tcx. def_kind ( body. source . def_id ( ) ) . is_fn_like ( ) ,
3240 always_live_locals,
3341 reachable_blocks,
@@ -41,6 +49,7 @@ struct Lint<'a, 'tcx> {
4149 tcx : TyCtxt < ' tcx > ,
4250 when : String ,
4351 body : & ' a Body < ' tcx > ,
52+ lint_id : HirId ,
4453 is_fn_like : bool ,
4554 always_live_locals : & ' a BitSet < Local > ,
4655 reachable_blocks : BitSet < BasicBlock > ,
@@ -49,10 +58,13 @@ struct Lint<'a, 'tcx> {
4958}
5059
5160impl < ' a , ' tcx > Lint < ' a , ' tcx > {
52- #[ track_caller]
61+ #[ allow( rustc:: untranslatable_diagnostic) ]
62+ #[ allow( rustc:: diagnostic_outside_of_impl) ]
5363 fn fail ( & self , location : Location , msg : impl AsRef < str > ) {
5464 let span = self . body . source_info ( location) . span ;
55- self . tcx . sess . dcx ( ) . span_delayed_bug (
65+ self . tcx . struct_span_lint_hir (
66+ BROKEN_MIR ,
67+ self . lint_id ,
5668 span,
5769 format ! (
5870 "broken MIR in {:?} ({}) at {:?}:\n {}" ,
@@ -61,6 +73,7 @@ impl<'a, 'tcx> Lint<'a, 'tcx> {
6173 location,
6274 msg. as_ref( )
6375 ) ,
76+ |_| { } ,
6477 ) ;
6578 }
6679}
0 commit comments