1
1
//! This pass statically detects code which has undefined behaviour or is likely to be erroneous.
2
2
//! It can be used to locate problems in MIR building or optimizations. It assumes that all code
3
3
//! can be executed, so it has false positives.
4
+
5
+ use rustc_hir:: { HirId , CRATE_HIR_ID } ;
4
6
use rustc_index:: bit_set:: BitSet ;
5
7
use rustc_middle:: mir:: visit:: { PlaceContext , Visitor } ;
6
8
use rustc_middle:: mir:: * ;
7
9
use rustc_middle:: ty:: TyCtxt ;
8
10
use rustc_mir_dataflow:: impls:: { MaybeStorageDead , MaybeStorageLive } ;
9
11
use rustc_mir_dataflow:: storage:: always_storage_live_locals;
10
12
use rustc_mir_dataflow:: { Analysis , ResultsCursor } ;
13
+ use rustc_session:: lint:: builtin:: BROKEN_MIR ;
11
14
use std:: borrow:: Cow ;
12
15
13
16
pub 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) {
28
31
tcx,
29
32
when,
30
33
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) ) ,
31
39
is_fn_like : tcx. def_kind ( body. source . def_id ( ) ) . is_fn_like ( ) ,
32
40
always_live_locals,
33
41
reachable_blocks,
@@ -41,6 +49,7 @@ struct Lint<'a, 'tcx> {
41
49
tcx : TyCtxt < ' tcx > ,
42
50
when : String ,
43
51
body : & ' a Body < ' tcx > ,
52
+ lint_id : HirId ,
44
53
is_fn_like : bool ,
45
54
always_live_locals : & ' a BitSet < Local > ,
46
55
reachable_blocks : BitSet < BasicBlock > ,
@@ -49,10 +58,13 @@ struct Lint<'a, 'tcx> {
49
58
}
50
59
51
60
impl < ' a , ' tcx > Lint < ' a , ' tcx > {
52
- #[ track_caller]
61
+ #[ allow( rustc:: untranslatable_diagnostic) ]
62
+ #[ allow( rustc:: diagnostic_outside_of_impl) ]
53
63
fn fail ( & self , location : Location , msg : impl AsRef < str > ) {
54
64
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 ,
56
68
span,
57
69
format ! (
58
70
"broken MIR in {:?} ({}) at {:?}:\n {}" ,
@@ -61,6 +73,7 @@ impl<'a, 'tcx> Lint<'a, 'tcx> {
61
73
location,
62
74
msg. as_ref( )
63
75
) ,
76
+ |_| { } ,
64
77
) ;
65
78
}
66
79
}
0 commit comments