@@ -7,17 +7,17 @@ use rustc_data_structures::fx::FxIndexMap;
77use rustc_errors:: { Applicability , Diag , EmissionGuarantee , MultiSpan , listify} ;
88use rustc_hir:: def:: { CtorKind , Namespace } ;
99use rustc_hir:: { self as hir, CoroutineKind , LangItem } ;
10- use rustc_index:: IndexSlice ;
10+ use rustc_index:: { IndexSlice , IndexVec } ;
1111use rustc_infer:: infer:: { BoundRegionConversionTime , NllRegionVariableOrigin } ;
1212use rustc_infer:: traits:: SelectionError ;
13- use rustc_middle:: bug;
1413use rustc_middle:: mir:: {
1514 AggregateKind , CallSource , ConstOperand , ConstraintCategory , FakeReadCause , Local , LocalInfo ,
1615 LocalKind , Location , Operand , Place , PlaceRef , PlaceTy , ProjectionElem , Rvalue , Statement ,
17- StatementKind , Terminator , TerminatorKind , find_self_call,
16+ StatementKind , Terminator , TerminatorKind , VarDebugInfoContents , find_self_call,
1817} ;
1918use rustc_middle:: ty:: print:: Print ;
2019use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
20+ use rustc_middle:: { bug, span_bug} ;
2121use rustc_mir_dataflow:: move_paths:: { InitLocation , LookupResult , MoveOutIndex } ;
2222use rustc_span:: def_id:: LocalDefId ;
2323use rustc_span:: source_map:: Spanned ;
@@ -190,6 +190,36 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
190190 ) -> Option < & ( PlaceRef < ' tcx > , Diag < ' infcx > ) > {
191191 self . diags_buffer . buffered_move_errors . get ( move_out_indices)
192192 }
193+
194+ /// Uses `body.var_debug_info` to find the symbol
195+ fn local_name ( & self , index : Local ) -> Option < Symbol > {
196+ * self . local_names ( ) . get ( index) ?
197+ }
198+
199+ fn local_names ( & self ) -> & IndexSlice < Local , Option < Symbol > > {
200+ self . local_names . get_or_init ( || {
201+ let mut local_names = IndexVec :: from_elem ( None , & self . body . local_decls ) ;
202+ for var_debug_info in & self . body . var_debug_info {
203+ if let VarDebugInfoContents :: Place ( place) = var_debug_info. value {
204+ if let Some ( local) = place. as_local ( ) {
205+ if let Some ( prev_name) = local_names[ local]
206+ && var_debug_info. name != prev_name
207+ {
208+ span_bug ! (
209+ var_debug_info. source_info. span,
210+ "local {:?} has many names (`{}` vs `{}`)" ,
211+ local,
212+ prev_name,
213+ var_debug_info. name
214+ ) ;
215+ }
216+ local_names[ local] = Some ( var_debug_info. name ) ;
217+ }
218+ }
219+ }
220+ local_names
221+ } )
222+ }
193223}
194224
195225impl < ' infcx , ' tcx > MirBorrowckCtxt < ' _ , ' infcx , ' tcx > {
@@ -430,7 +460,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
430460 /// a name, or its name was generated by the compiler, then `Err` is returned
431461 fn append_local_to_string ( & self , local : Local , buf : & mut String ) -> Result < ( ) , ( ) > {
432462 let decl = & self . body . local_decls [ local] ;
433- match self . local_names [ local] {
463+ match self . local_name ( local) {
434464 Some ( name) if !decl. from_compiler_desugaring ( ) => {
435465 buf. push_str ( name. as_str ( ) ) ;
436466 Ok ( ( ) )
@@ -1500,4 +1530,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
15001530 }
15011531 }
15021532 }
1533+
1534+ /// Skip over locals that begin with an underscore or have no name
1535+ pub ( crate ) fn local_excluded_from_unused_mut_lint ( & self , index : Local ) -> bool {
1536+ self . local_name ( index) . is_none_or ( |name| name. as_str ( ) . starts_with ( '_' ) )
1537+ }
15031538}
0 commit comments