1use std::collections::hash_map::Entry;
2use std::marker::PhantomData;
3use std::ops::Range;
4
5use rustc_abi::{BackendRepr, FieldIdx, FieldsShape, Size, VariantIdx};
6use rustc_data_structures::fx::FxHashMap;
7use rustc_index::IndexVec;
8use rustc_index::bit_set::DenseBitSet;
9use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
10use rustc_middle::ty::Ty;
11use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
12use rustc_middle::{mir, ty};
13use rustc_session::config::DebugInfo;
14use rustc_span::{BytePos, DUMMY_SP, Span, Symbol, bug, hygiene, sym};
15
16use super::operand::{OperandRef, OperandValue};
17use super::place::{PlaceRef, PlaceValue};
18use super::{FunctionCx, LocalRef, PerLocalVarDebugInfoIndexVec};
19use crate::traits::*;
20
21pub struct FunctionDebugContext<S, L> {
22 pub scopes: IndexVec<mir::SourceScope, DebugScope<S, L>>,
24}
25
26#[derive(#[automatically_derived]impl ::core::marker::Copy for VariableKind { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
for VariableKind {
}
#[automatically_derived]impl ::core::clone::Clone for VariableKind {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<usize>;
*self
}
}Clone)]
27pub enum VariableKind {
28 ArgumentVariable(usize ),
29 LocalVariable,
30}
31
32#[derive(#[automatically_derived]impl<'tcx, D: ::core::clone::Clone>
::core::clone::Clone for PerLocalVarDebugInfo<'tcx, D> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
PerLocalVarDebugInfo {
name: ::core::clone::Clone::clone(&self.name),
source_info: ::core::clone::Clone::clone(&self.source_info),
dbg_var: ::core::clone::Clone::clone(&self.dbg_var),
fragment: ::core::clone::Clone::clone(&self.fragment),
projection: ::core::clone::Clone::clone(&self.projection),
}
}
}Clone)]
34pub struct PerLocalVarDebugInfo<'tcx, D> {
35 pub name: Symbol,
36 pub source_info: mir::SourceInfo,
37
38 pub dbg_var: Option<D>,
40
41 pub fragment: Option<Range<Size>>,
44
45 pub projection: &'tcx ty::List<mir::PlaceElem<'tcx>>,
47}
48
49pub struct ConstDebugInfo<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
51 pub name: String,
52 pub source_info: mir::SourceInfo,
53 pub operand: OperandRef<'tcx, Bx::Value>,
54 pub dbg_var: Bx::DIVariable,
55 pub dbg_loc: Bx::DILocation,
56 pub fragment: Option<Range<Size>>,
57 pub _phantom: PhantomData<&'a ()>,
58}
59
60#[derive(#[automatically_derived]impl<S: ::core::clone::Clone, L: ::core::clone::Clone>
::core::clone::Clone for DebugScope<S, L> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
DebugScope {
dbg_scope: ::core::clone::Clone::clone(&self.dbg_scope),
inlined_at: ::core::clone::Clone::clone(&self.inlined_at),
file_start_pos: ::core::clone::Clone::clone(&self.file_start_pos),
file_end_pos: ::core::clone::Clone::clone(&self.file_end_pos),
}
}
}Clone, #[automatically_derived]impl<S: ::core::marker::Copy, L: ::core::marker::Copy>
::core::marker::Copy for DebugScope<S, L> {
}Copy, #[automatically_derived]impl<S: ::core::fmt::Debug, L: ::core::fmt::Debug>
::core::fmt::Debug for DebugScope<S, L> {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field4_finish(f,
"DebugScope dbg_scope inlined_at file_start_pos file_end_pos",
&self.dbg_scope, &self.inlined_at, &self.file_start_pos,
&&self.file_end_pos)
}
}Debug)]
61pub struct DebugScope<S, L> {
62 pub dbg_scope: S,
63
64 pub inlined_at: Option<L>,
66
67 pub file_start_pos: BytePos,
70 pub file_end_pos: BytePos,
71}
72
73impl<'tcx, S: Copy, L: Copy> DebugScope<S, L> {
74 pub fn adjust_dbg_scope_for_span<
79 'a,
80 Bx: BuilderMethods<'a, 'tcx, DIScope = S, DILocation = L>,
81 >(
82 &self,
83 bx: &mut Bx,
84 span: Span,
85 ) -> S {
86 let pos = span.lo();
87 if pos < self.file_start_pos || pos >= self.file_end_pos {
88 let sm = bx.sess().source_map();
89 bx.extend_scope_to_file(self.dbg_scope, &sm.lookup_char_pos(pos).file)
90 } else {
91 self.dbg_scope
92 }
93 }
94}
95
96trait DebugInfoOffsetLocation<'tcx, Bx> {
97 fn deref(&self, bx: &mut Bx) -> Self;
98 fn layout(&self) -> TyAndLayout<'tcx>;
99 fn project_field(&self, bx: &mut Bx, field: FieldIdx) -> Self;
100 fn project_constant_index(&self, bx: &mut Bx, offset: u64) -> Self;
101 fn downcast(&self, bx: &mut Bx, variant: VariantIdx) -> Self;
102}
103
104impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> DebugInfoOffsetLocation<'tcx, Bx>
105 for PlaceRef<'tcx, Bx::Value>
106{
107 fn deref(&self, bx: &mut Bx) -> Self {
108 bx.load_operand(*self).deref(bx.cx())
109 }
110
111 fn layout(&self) -> TyAndLayout<'tcx> {
112 self.layout
113 }
114
115 fn project_field(&self, bx: &mut Bx, field: FieldIdx) -> Self {
116 PlaceRef::project_field(*self, bx, field.index())
117 }
118
119 fn project_constant_index(&self, bx: &mut Bx, offset: u64) -> Self {
120 let lloffset = bx.cx().const_usize(offset);
121 self.project_index(bx, lloffset)
122 }
123
124 fn downcast(&self, bx: &mut Bx, variant: VariantIdx) -> Self {
125 self.project_downcast(bx, variant)
126 }
127}
128
129impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> DebugInfoOffsetLocation<'tcx, Bx>
130 for TyAndLayout<'tcx>
131{
132 fn deref(&self, bx: &mut Bx) -> Self {
133 bx.cx().layout_of(
134 self.ty.builtin_deref(true).unwrap_or_else(|| ::rustc_span::macros::bug_impl(None,
format_args!("cannot deref `{0}`", self.ty), Location::caller())bug!("cannot deref `{}`", self.ty)),
135 )
136 }
137
138 fn layout(&self) -> TyAndLayout<'tcx> {
139 *self
140 }
141
142 fn project_field(&self, bx: &mut Bx, field: FieldIdx) -> Self {
143 self.field(bx.cx(), field.index())
144 }
145
146 fn project_constant_index(&self, bx: &mut Bx, index: u64) -> Self {
147 self.field(bx.cx(), index as usize)
148 }
149
150 fn downcast(&self, bx: &mut Bx, variant: VariantIdx) -> Self {
151 self.for_variant(bx.cx(), variant)
152 }
153}
154
155struct DebugInfoOffset<T> {
156 direct_offset: Size,
158 indirect_offsets: Vec<Size>,
161 result: T,
163}
164
165fn calculate_debuginfo_offset<
166 'a,
167 'tcx,
168 Bx: BuilderMethods<'a, 'tcx>,
169 L: DebugInfoOffsetLocation<'tcx, Bx>,
170>(
171 bx: &mut Bx,
172 projection: &[mir::PlaceElem<'tcx>],
173 base: L,
174) -> DebugInfoOffset<L> {
175 let mut direct_offset = Size::ZERO;
176 let mut indirect_offsets = ::alloc::vec::Vec::new()vec![];
178 let mut place = base;
179
180 for elem in projection {
181 match *elem {
182 mir::ProjectionElem::Deref => {
183 indirect_offsets.push(Size::ZERO);
184 place = place.deref(bx);
185 }
186 mir::ProjectionElem::Field(field, _) => {
187 let offset = indirect_offsets.last_mut().unwrap_or(&mut direct_offset);
188 *offset += place.layout().fields.offset(field.index());
189 place = place.project_field(bx, field);
190 }
191 mir::ProjectionElem::Downcast(_, variant) => {
192 place = place.downcast(bx, variant);
193 }
194 mir::ProjectionElem::ConstantIndex {
195 offset: index,
196 min_length: _,
197 from_end: false,
198 } => {
199 let offset = indirect_offsets.last_mut().unwrap_or(&mut direct_offset);
200 let FieldsShape::Array { stride, count: _ } = place.layout().fields else {
201 ::rustc_span::macros::bug_impl(None,
format_args!("ConstantIndex on non-array type {0:?}", place.layout()),
Location::caller())bug!("ConstantIndex on non-array type {:?}", place.layout())
202 };
203 *offset += stride * index;
204 place = place.project_constant_index(bx, index);
205 }
206 _ => {
207 if !!elem.can_use_in_debuginfo() {
::core::panicking::panic("assertion failed: !elem.can_use_in_debuginfo()")
};assert!(!elem.can_use_in_debuginfo());
209 ::rustc_span::macros::bug_impl(None,
format_args!("unsupported var debuginfo projection `{0:?}`", projection),
Location::caller())bug!("unsupported var debuginfo projection `{:?}`", projection)
210 }
211 }
212 }
213
214 DebugInfoOffset { direct_offset, indirect_offsets, result: place }
215}
216
217impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
218 pub fn set_debug_loc(&self, bx: &mut Bx, source_info: mir::SourceInfo) {
219 bx.set_span(source_info.span);
220 if let Some(dbg_loc) = self.dbg_loc(bx, source_info) {
221 bx.set_dbg_loc(dbg_loc);
222 }
223 }
224
225 fn dbg_loc(&self, bx: &mut Bx, source_info: mir::SourceInfo) -> Option<Bx::DILocation> {
226 let (dbg_scope, inlined_at, span) = self.adjusted_span_and_dbg_scope(bx, source_info)?;
227 Some(bx.dbg_loc(dbg_scope, inlined_at, span))
228 }
229
230 fn adjusted_span_and_dbg_scope(
231 &self,
232 bx: &mut Bx,
233 source_info: mir::SourceInfo,
234 ) -> Option<(Bx::DIScope, Option<Bx::DILocation>, Span)> {
235 let scope = &self.debug_context.as_ref()?.scopes[source_info.scope];
236 let span = hygiene::walk_chain_collapsed(source_info.span, self.mir.span);
237 Some((scope.adjust_dbg_scope_for_span(bx, span), scope.inlined_at, span))
238 }
239
240 fn spill_operand_to_stack(
241 operand: OperandRef<'tcx, Bx::Value>,
242 name: Option<String>,
243 bx: &mut Bx,
244 ) -> PlaceRef<'tcx, Bx::Value> {
245 let spill_slot = PlaceRef::alloca(bx, operand.layout);
251 if let Some(name) = name {
252 bx.set_var_name(spill_slot.val.llval, &(name + ".dbg.spill"));
253 }
254 operand.val.store(bx, spill_slot);
255 spill_slot
256 }
257
258 fn debug_new_val_to_local(
261 &self,
262 bx: &mut Bx,
263 local: mir::Local,
264 base: PlaceRef<'tcx, Bx::Value>,
265 projection: &[mir::PlaceElem<'tcx>],
266 ) {
267 let full_debug_info = bx.sess().opts.debuginfo == DebugInfo::Full;
268 if !full_debug_info {
269 return;
270 }
271
272 let vars = match &self.per_local_var_debug_info {
273 Some(per_local) => &per_local[local],
274 None => return,
275 };
276
277 let DebugInfoOffset { direct_offset, indirect_offsets, result: _ } =
278 calculate_debuginfo_offset(bx, projection, base.layout);
279 for var in vars.iter() {
280 let Some(dbg_var) = var.dbg_var else {
281 continue;
282 };
283 let Some(dbg_loc) = self.dbg_loc(bx, var.source_info) else {
284 continue;
285 };
286 bx.dbg_var_value(
287 dbg_var,
288 dbg_loc,
289 base.val.llval,
290 direct_offset,
291 &indirect_offsets,
292 &var.fragment,
293 );
294 }
295 }
296
297 fn debug_poison_to_local(&self, bx: &mut Bx, local: mir::Local) {
298 let ty = self.monomorphize(self.mir.local_decls[local].ty);
299 let layout = bx.cx().layout_of(ty);
300 let place_ref = PlaceRef::new_sized(bx.cx().const_poison(bx.cx().type_ptr()), layout);
301 self.debug_new_val_to_local(bx, local, place_ref, &[]);
302 }
303
304 pub(crate) fn debug_introduce_local(&self, bx: &mut Bx, local: mir::Local) {
307 let full_debug_info = bx.sess().opts.debuginfo == DebugInfo::Full;
308
309 let vars = match &self.per_local_var_debug_info {
310 Some(per_local) => &per_local[local],
311 None => return,
312 };
313 let whole_local_var = vars.iter().find(|var| var.projection.is_empty()).cloned();
314 let has_proj = || vars.iter().any(|var| !var.projection.is_empty());
315
316 let fallback_var = if self.mir.local_kind(local) == mir::LocalKind::Arg {
317 let arg_index = local.index() - 1;
318
319 if arg_index == 0 && has_proj() {
322 None
326 } else if whole_local_var.is_some() {
327 None
333 } else {
334 let name = sym::empty;
335 let decl = &self.mir.local_decls[local];
336 let dbg_var = if full_debug_info {
337 self.adjusted_span_and_dbg_scope(bx, decl.source_info).map(
338 |(dbg_scope, _, span)| {
339 let kind = VariableKind::ArgumentVariable(arg_index + 1);
341
342 let arg_ty = self.monomorphize(decl.ty);
343
344 bx.create_dbg_var(name, arg_ty, dbg_scope, kind, span)
345 },
346 )
347 } else {
348 None
349 };
350
351 Some(PerLocalVarDebugInfo {
352 name,
353 source_info: decl.source_info,
354 dbg_var,
355 fragment: None,
356 projection: ty::List::empty(),
357 })
358 }
359 } else {
360 None
361 };
362
363 let local_ref = &self.locals[local];
364
365 let name = if bx.sess().fewer_names() {
366 None
367 } else {
368 Some(match whole_local_var.or_else(|| fallback_var.clone()) {
369 Some(var) if var.name != sym::empty => var.name.to_string(),
370 _ => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", local))
})format!("{local:?}"),
371 })
372 };
373
374 if let Some(name) = &name {
375 match local_ref {
376 LocalRef::Place(place) | LocalRef::UnsizedPlace(place) => {
377 bx.set_var_name(place.val.llval, name);
378 }
379 LocalRef::Operand(operand) => match operand.val {
380 OperandValue::Ref(PlaceValue { llval: x, .. }) | OperandValue::Immediate(x) => {
381 bx.set_var_name(x, name);
382 }
383 OperandValue::Pair(a, b) => {
384 bx.set_var_name(a, &(name.clone() + ".0"));
387 bx.set_var_name(b, &(name.clone() + ".1"));
388 }
389 OperandValue::ZeroSized => {
390 }
392 },
393 LocalRef::PendingOperand => {}
394 }
395 }
396
397 if !full_debug_info || vars.is_empty() && fallback_var.is_none() {
398 return;
399 }
400
401 let base = match local_ref {
402 LocalRef::PendingOperand => return,
403
404 LocalRef::Operand(operand) => {
405 let attrs = bx.tcx().codegen_instance_attrs(self.instance.def);
408 if attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
409 return;
410 }
411
412 if operand.layout.ty.is_scalable_vector()
440 && bx.sess().target.arch == rustc_target::spec::Arch::AArch64
441 {
442 let (count, element_ty, _) =
443 operand.layout.ty.scalable_vector_parts(bx.tcx()).unwrap();
444 if element_ty.is_bool() && count != 16 {
446 return;
447 }
448 }
449
450 Self::spill_operand_to_stack(*operand, name, bx)
451 }
452
453 LocalRef::Place(place) => *place,
454
455 LocalRef::UnsizedPlace(_) => return,
457 };
458
459 let vars = vars.iter().cloned().chain(fallback_var);
460
461 for var in vars {
462 self.debug_introduce_local_as_var(bx, local, base, var);
463 }
464 }
465
466 fn debug_introduce_local_as_var(
467 &self,
468 bx: &mut Bx,
469 local: mir::Local,
470 base: PlaceRef<'tcx, Bx::Value>,
471 var: PerLocalVarDebugInfo<'tcx, Bx::DIVariable>,
472 ) {
473 let Some(dbg_var) = var.dbg_var else { return };
474 let Some(dbg_loc) = self.dbg_loc(bx, var.source_info) else { return };
475
476 let DebugInfoOffset { direct_offset, indirect_offsets, result: _ } =
477 calculate_debuginfo_offset(bx, var.projection, base.layout);
478
479 let should_create_individual_allocas = bx.cx().sess().target.is_like_msvc
487 && self.mir.local_kind(local) == mir::LocalKind::Arg
488 && (direct_offset != Size::ZERO || !#[allow(non_exhaustive_omitted_patterns)] match &indirect_offsets[..] {
[Size::ZERO] | [] => true,
_ => false,
}matches!(&indirect_offsets[..], [Size::ZERO] | []));
492
493 if should_create_individual_allocas {
494 let DebugInfoOffset { direct_offset: _, indirect_offsets: _, result: place } =
495 calculate_debuginfo_offset(bx, var.projection, base);
496
497 let ptr_ty = Ty::new_mut_ptr(bx.tcx(), place.layout.ty);
499 let ptr_layout = bx.layout_of(ptr_ty);
500 let alloca = PlaceRef::alloca(bx, ptr_layout);
501 bx.set_var_name(alloca.val.llval, &(var.name.to_string() + ".dbg.spill"));
502
503 bx.store_to_place(place.val.llval, alloca.val);
505
506 bx.dbg_var_addr(
508 dbg_var,
509 dbg_loc,
510 alloca.val.llval,
511 Size::ZERO,
512 &[Size::ZERO],
513 &var.fragment,
514 );
515 } else {
516 bx.dbg_var_addr(
517 dbg_var,
518 dbg_loc,
519 base.val.llval,
520 direct_offset,
521 &indirect_offsets,
522 &var.fragment,
523 );
524 }
525 }
526
527 pub(crate) fn debug_introduce_locals(
528 &self,
529 bx: &mut Bx,
530 consts: Vec<ConstDebugInfo<'a, 'tcx, Bx>>,
531 ) {
532 if bx.sess().opts.debuginfo == DebugInfo::Full || !bx.sess().fewer_names() {
533 for local in self.locals.indices() {
534 self.debug_introduce_local(bx, local);
535 }
536
537 for ConstDebugInfo { name, source_info, operand, dbg_var, dbg_loc, fragment, .. } in
538 consts.into_iter()
539 {
540 self.set_debug_loc(bx, source_info);
541 let base = FunctionCx::spill_operand_to_stack(operand, Some(name), bx);
542 bx.clear_dbg_loc();
543
544 bx.dbg_var_addr(dbg_var, dbg_loc, base.val.llval, Size::ZERO, &[], &fragment);
545 }
546 }
547 }
548
549 pub(crate) fn compute_per_local_var_debug_info(
551 &self,
552 bx: &mut Bx,
553 ) -> Option<(
554 PerLocalVarDebugInfoIndexVec<'tcx, Bx::DIVariable>,
555 Vec<ConstDebugInfo<'a, 'tcx, Bx>>,
556 )> {
557 let full_debug_info = self.cx.sess().opts.debuginfo == DebugInfo::Full;
558
559 let target_is_msvc = self.cx.sess().target.is_like_msvc;
560
561 if !full_debug_info && self.cx.sess().fewer_names() {
562 return None;
563 }
564
565 let mut per_local = IndexVec::from_elem(::alloc::vec::Vec::new()vec![], &self.mir.local_decls);
566 let mut constants = ::alloc::vec::Vec::new()vec![];
567 for var in &self.mir.var_debug_info {
568 let dbg_scope_and_span = if full_debug_info {
569 self.adjusted_span_and_dbg_scope(bx, var.source_info)
570 } else {
571 None
572 };
573
574 let var_ty = if let Some(ref fragment) = var.composite {
575 self.monomorphize(fragment.ty)
576 } else {
577 match var.value {
578 mir::VarDebugInfoContents::Place(place) => {
579 self.monomorphized_place_ty(place.as_ref())
580 }
581 mir::VarDebugInfoContents::Const(c) => self.monomorphize(c.ty()),
582 }
583 };
584
585 let dbg_var = dbg_scope_and_span.map(|(dbg_scope, _, span)| {
586 let var_kind = if let Some(arg_index) = var.argument_index
587 && var.composite.is_none()
588 && let mir::VarDebugInfoContents::Place(place) = var.value
589 && place.projection.is_empty()
590 {
591 let arg_index = arg_index as usize;
592 if target_is_msvc {
593 let var_ty_layout = self.cx.layout_of(var_ty);
597 if let BackendRepr::ScalarPair { a: _, b: _, b_offset: _ } =
598 var_ty_layout.backend_repr
599 {
600 VariableKind::LocalVariable
601 } else {
602 VariableKind::ArgumentVariable(arg_index)
603 }
604 } else {
605 VariableKind::ArgumentVariable(arg_index)
608 }
609 } else {
610 VariableKind::LocalVariable
611 };
612
613 bx.create_dbg_var(var.name, var_ty, dbg_scope, var_kind, span)
614 });
615
616 let fragment = if let Some(ref fragment) = var.composite {
617 let var_layout = self.cx.layout_of(var_ty);
618
619 let DebugInfoOffset { direct_offset, indirect_offsets, result: fragment_layout } =
620 calculate_debuginfo_offset(bx, &fragment.projection, var_layout);
621 if !indirect_offsets.is_empty() {
::core::panicking::panic("assertion failed: indirect_offsets.is_empty()")
};assert!(indirect_offsets.is_empty());
622
623 if fragment_layout.size == Size::ZERO {
624 continue;
627 } else if fragment_layout.size == var_layout.size {
628 None
631 } else {
632 Some(direct_offset..direct_offset + fragment_layout.size)
633 }
634 } else {
635 None
636 };
637
638 match var.value {
639 mir::VarDebugInfoContents::Place(place) => {
640 per_local[place.local].push(PerLocalVarDebugInfo {
641 name: var.name,
642 source_info: var.source_info,
643 dbg_var,
644 fragment,
645 projection: place.projection,
646 });
647 }
648 mir::VarDebugInfoContents::Const(c) => {
649 if let Some(dbg_var) = dbg_var {
650 let Some(dbg_loc) = self.dbg_loc(bx, var.source_info) else { continue };
651
652 let operand = self.eval_mir_constant_to_operand(bx, &c);
653 constants.push(ConstDebugInfo {
654 name: var.name.to_string(),
655 source_info: var.source_info,
656 operand,
657 dbg_var,
658 dbg_loc,
659 fragment,
660 _phantom: PhantomData,
661 });
662 }
663 }
664 }
665 }
666 Some((per_local, constants))
667 }
668
669 pub(crate) fn codegen_stmt_debuginfo(
670 &mut self,
671 bx: &mut Bx,
672 debuginfo: &mir::StmtDebugInfo<'tcx>,
673 ) {
674 match debuginfo {
675 mir::StmtDebugInfo::AssignRef(dest, place) => {
676 let local_ref = match self.locals[place.local] {
677 LocalRef::Place(place_ref) => Some((place_ref, place.projection.as_slice())),
680 LocalRef::Operand(operand_ref) if place.is_indirect_first_projection() => {
683 Some((operand_ref.deref(bx.cx()), &place.projection[1..]))
684 }
685 LocalRef::Operand(_) => None,
689 LocalRef::UnsizedPlace(_) | LocalRef::PendingOperand => None,
690 }
691 .filter(|(_, projection)| {
692 projection.iter().all(|p| p.can_use_in_debuginfo())
694 });
695 if let Some((base, projection)) = local_ref {
696 self.debug_new_val_to_local(bx, *dest, base, projection);
697 } else {
698 self.debug_poison_to_local(bx, *dest);
700 }
701 }
702 mir::StmtDebugInfo::InvalidAssign(local) => {
703 self.debug_poison_to_local(bx, *local);
704 }
705 }
706 }
707
708 pub(crate) fn codegen_stmt_debuginfos(
709 &mut self,
710 bx: &mut Bx,
711 debuginfos: &[mir::StmtDebugInfo<'tcx>],
712 ) {
713 for debuginfo in debuginfos {
714 self.codegen_stmt_debuginfo(bx, debuginfo);
715 }
716 }
717
718 pub(super) fn fill_function_debug_context(&mut self, bx: &mut Bx) {
723 if self.cx.sess().opts.debuginfo == DebugInfo::None {
724 return;
725 }
726
727 self.debug_context = Some(FunctionDebugContext {
729 scopes: IndexVec::with_capacity(self.mir.source_scopes.len()),
730 });
731
732 let variables = if self.cx.sess().opts.debuginfo == DebugInfo::Full {
734 let mut vars = DenseBitSet::new_empty(self.mir.source_scopes.len());
735 for var_debug_info in &self.mir.var_debug_info {
740 vars.insert(var_debug_info.source_info.scope);
741 }
742 Some(vars)
743 } else {
744 None
746 };
747
748 let mut discriminators = FxHashMap::default();
750 for scope in self.mir.source_scopes.indices() {
751 let scope_data = self.make_mir_scope(bx, &variables, &mut discriminators, scope);
752 let _s = self.debug_context.as_mut().unwrap().scopes.push(scope_data);
753 if true {
{
match (&_s, &scope) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};debug_assert_eq!(_s, scope);
754 }
755 }
756
757 fn make_mir_scope(
758 &mut self,
759 bx: &mut Bx,
760 variables: &Option<DenseBitSet<mir::SourceScope>>,
761 discriminators: &mut FxHashMap<BytePos, u32>,
762 scope: mir::SourceScope,
763 ) -> DebugScope<Bx::DIScope, Bx::DILocation> {
764 let scope_data = &self.mir.source_scopes[scope];
765 let parent_scope = if let Some(parent) = scope_data.parent_scope {
766 if true {
if !(parent.as_u32() < scope.as_u32()) {
::core::panicking::panic("assertion failed: parent.as_u32() < scope.as_u32()")
};
};debug_assert!(parent.as_u32() < scope.as_u32());
767 self.debug_context.as_ref().unwrap().scopes[parent]
768 } else {
769 let file = self.cx.sess().source_map().lookup_source_file(self.mir.span.lo());
771 let dbg_scope = bx.dbg_scope_fn(self.instance, self.fn_abi, Some(self.llfn));
772 return DebugScope {
773 dbg_scope,
774 inlined_at: None,
775 file_start_pos: file.start_pos,
776 file_end_pos: file.end_position(),
777 };
778 };
779
780 if let Some(vars) = variables
781 && !vars.contains(scope)
782 && scope_data.inlined.is_none()
783 {
784 return parent_scope;
787 }
788
789 let dbg_scope = match scope_data.inlined {
790 Some((callee, _)) => {
791 let callee = self.monomorphize(callee);
792 let callee_fn_abi = self.cx.fn_abi_of_instance(callee, ty::List::empty());
793 bx.dbg_scope_fn(callee, callee_fn_abi, None)
794 }
795 None => bx.dbg_create_lexical_block(scope_data.span.lo(), parent_scope.dbg_scope),
796 };
797
798 let inlined_at = scope_data.inlined.map(|(_, callsite_span)| {
799 let callsite_span = hygiene::walk_chain_collapsed(callsite_span, self.mir.span);
800 let callsite_scope = parent_scope.adjust_dbg_scope_for_span(bx, callsite_span);
801 let loc = bx.dbg_loc(callsite_scope, parent_scope.inlined_at, callsite_span);
802
803 match discriminators.entry(callsite_span.lo()) {
822 Entry::Occupied(mut o) => {
823 *o.get_mut() += 1;
824 bx.dbg_location_clone_with_discriminator(loc, *o.get()).unwrap_or_else(|| {
828 bx.dbg_loc(callsite_scope, parent_scope.inlined_at, DUMMY_SP)
829 })
830 }
831 Entry::Vacant(v) => {
832 v.insert(0);
833 loc
834 }
835 }
836 });
837
838 let file = self.cx.sess().source_map().lookup_source_file(scope_data.span.lo());
839 DebugScope {
840 dbg_scope,
841 inlined_at: inlined_at.or(parent_scope.inlined_at),
842 file_start_pos: file.start_pos,
843 file_end_pos: file.end_position(),
844 }
845 }
846}