mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-14 04:56:49 +00:00
Don't produce debug information for compiler-introduced-vars when desugaring assignments.
An assignment such as (a, b) = (b, c); desugars to the HIR { let (lhs, lhs) = (b, c); a = lhs; b = lhs; }; The repeated `lhs` leads to multiple Locals assigned to the same DILocalVariable. Rather than attempting to fix that, get rid of the debug info for these bindings that don't even exist in the program to begin with. Fixes #138198
This commit is contained in:
parent
01dc45c10e
commit
8cab8e07bc
@ -13,13 +13,13 @@ use std::sync::Arc;
|
||||
use rustc_abi::VariantIdx;
|
||||
use rustc_data_structures::fx::FxIndexMap;
|
||||
use rustc_data_structures::stack::ensure_sufficient_stack;
|
||||
use rustc_hir::{BindingMode, ByRef};
|
||||
use rustc_hir::{BindingMode, ByRef, LetStmt, LocalSource, Node};
|
||||
use rustc_middle::bug;
|
||||
use rustc_middle::middle::region;
|
||||
use rustc_middle::mir::{self, *};
|
||||
use rustc_middle::thir::{self, *};
|
||||
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty};
|
||||
use rustc_span::{BytePos, Pos, Span, Symbol};
|
||||
use rustc_span::{BytePos, Pos, Span, Symbol, sym};
|
||||
use tracing::{debug, instrument};
|
||||
|
||||
use crate::builder::ForGuard::{self, OutsideGuard, RefWithinGuard};
|
||||
@ -2796,13 +2796,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||
)))),
|
||||
};
|
||||
let for_arm_body = self.local_decls.push(local);
|
||||
self.var_debug_info.push(VarDebugInfo {
|
||||
name,
|
||||
source_info: debug_source_info,
|
||||
value: VarDebugInfoContents::Place(for_arm_body.into()),
|
||||
composite: None,
|
||||
argument_index: None,
|
||||
});
|
||||
if self.should_emit_debug_info_for_binding(name, var_id) {
|
||||
self.var_debug_info.push(VarDebugInfo {
|
||||
name,
|
||||
source_info: debug_source_info,
|
||||
value: VarDebugInfoContents::Place(for_arm_body.into()),
|
||||
composite: None,
|
||||
argument_index: None,
|
||||
});
|
||||
}
|
||||
let locals = if has_guard.0 {
|
||||
let ref_for_guard = self.local_decls.push(LocalDecl::<'tcx> {
|
||||
// This variable isn't mutated but has a name, so has to be
|
||||
@ -2815,13 +2817,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||
BindingForm::RefForGuard,
|
||||
))),
|
||||
});
|
||||
self.var_debug_info.push(VarDebugInfo {
|
||||
name,
|
||||
source_info: debug_source_info,
|
||||
value: VarDebugInfoContents::Place(ref_for_guard.into()),
|
||||
composite: None,
|
||||
argument_index: None,
|
||||
});
|
||||
if self.should_emit_debug_info_for_binding(name, var_id) {
|
||||
self.var_debug_info.push(VarDebugInfo {
|
||||
name,
|
||||
source_info: debug_source_info,
|
||||
value: VarDebugInfoContents::Place(ref_for_guard.into()),
|
||||
composite: None,
|
||||
argument_index: None,
|
||||
});
|
||||
}
|
||||
LocalsForNode::ForGuard { ref_for_guard, for_arm_body }
|
||||
} else {
|
||||
LocalsForNode::One(for_arm_body)
|
||||
@ -2829,4 +2833,26 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||
debug!(?locals);
|
||||
self.var_indices.insert(var_id, locals);
|
||||
}
|
||||
|
||||
/// Some bindings are introduced when producing HIR from the AST and don't
|
||||
/// actually exist in the source. Skip producing debug info for those when
|
||||
/// we can recognize them.
|
||||
fn should_emit_debug_info_for_binding(&self, name: Symbol, var_id: LocalVarId) -> bool {
|
||||
// For now we only recognize the output of desugaring assigns.
|
||||
if name != sym::lhs {
|
||||
return true;
|
||||
}
|
||||
|
||||
let tcx = self.tcx;
|
||||
for (_, node) in tcx.hir_parent_iter(var_id.0) {
|
||||
// FIXME(khuey) at what point is it safe to bail on the iterator?
|
||||
// Can we stop at the first non-Pat node?
|
||||
if matches!(node, Node::LetStmt(&LetStmt { source: LocalSource::AssignDesugar(_), .. }))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
18
tests/codegen/assign-desugar-debuginfo.rs
Normal file
18
tests/codegen/assign-desugar-debuginfo.rs
Normal file
@ -0,0 +1,18 @@
|
||||
//@ compile-flags: -g -Zmir-opt-level=0
|
||||
|
||||
#![crate_type = "lib"]
|
||||
|
||||
#[inline(never)]
|
||||
fn swizzle(a: u32, b: u32, c: u32) -> (u32, (u32, u32)) {
|
||||
(b, (c, a))
|
||||
}
|
||||
|
||||
pub fn work() {
|
||||
let mut a = 1;
|
||||
let mut b = 2;
|
||||
let mut c = 3;
|
||||
(a, (b, c)) = swizzle(a, b, c);
|
||||
println!("{a} {b} {c}");
|
||||
}
|
||||
|
||||
// CHECK-NOT: !DILocalVariable(name: "lhs",
|
Loading…
Reference in New Issue
Block a user