Auto merge of #142882 - kornelski:var-debug-info-lazy, r= · rust-lang/rust@920fe63 (original) (raw)

`@@ -7,17 +7,17 @@ use rustc_data_structures::fx::FxIndexMap;

`

7

7

`use rustc_errors::{Applicability, Diag, EmissionGuarantee, MultiSpan, listify};

`

8

8

`use rustc_hir::def::{CtorKind, Namespace};

`

9

9

`use rustc_hir::{self as hir, CoroutineKind, LangItem};

`

10

``

`-

use rustc_index::IndexSlice;

`

``

10

`+

use rustc_index::{IndexSlice, IndexVec};

`

11

11

`use rustc_infer::infer::{BoundRegionConversionTime, NllRegionVariableOrigin};

`

12

12

`use rustc_infer::traits::SelectionError;

`

13

``

`-

use rustc_middle::bug;

`

14

13

`use rustc_middle::mir::{

`

15

14

`AggregateKind, CallSource, ConstOperand, ConstraintCategory, FakeReadCause, Local, LocalInfo,

`

16

15

`LocalKind, Location, Operand, Place, PlaceRef, PlaceTy, ProjectionElem, Rvalue, Statement,

`

17

``

`-

StatementKind, Terminator, TerminatorKind, find_self_call,

`

``

16

`+

StatementKind, Terminator, TerminatorKind, VarDebugInfoContents, find_self_call,

`

18

17

`};

`

19

18

`use rustc_middle::ty::print::Print;

`

20

19

`use rustc_middle::ty::{self, Ty, TyCtxt};

`

``

20

`+

use rustc_middle::{bug, span_bug};

`

21

21

`use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult, MoveOutIndex};

`

22

22

`use rustc_span::def_id::LocalDefId;

`

23

23

`use rustc_span::source_map::Spanned;

`

`@@ -190,6 +190,36 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {

`

190

190

`) -> Option<&(PlaceRef<'tcx>, Diag<'infcx>)> {

`

191

191

`self.diags_buffer.buffered_move_errors.get(move_out_indices)

`

192

192

`}

`

``

193

+

``

194

`` +

/// Uses body.var_debug_info to find the symbol

``

``

195

`+

fn local_name(&self, index: Local) -> Option {

`

``

196

`+

*self.local_names().get(index)?

`

``

197

`+

}

`

``

198

+

``

199

`+

fn local_names(&self) -> &IndexSlice<Local, Option> {

`

``

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

`+

}

`

193

223

`}

`

194

224

``

195

225

`impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {

`

`@@ -430,7 +460,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {

`

430

460

`` /// a name, or its name was generated by the compiler, then Err is returned

``

431

461

`fn append_local_to_string(&self, local: Local, buf: &mut String) -> Result<(), ()> {

`

432

462

`let decl = &self.body.local_decls[local];

`

433

``

`-

match self.local_names[local] {

`

``

463

`+

match self.local_name(local) {

`

434

464

`Some(name) if !decl.from_compiler_desugaring() => {

`

435

465

` buf.push_str(name.as_str());

`

436

466

`Ok(())

`

`@@ -1500,4 +1530,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {

`

1500

1530

`}

`

1501

1531

`}

`

1502

1532

`}

`

``

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

`+

}

`

1503

1538

`}

`