Skip to content

Commit f3da828

Browse files
committed
Refactor type_map::stub parameters
Push span lookup into `type_map::stub` and pass the `DefId` instead of doing the lookup outside and passing in the location metadata.
1 parent 94669d9 commit f3da828

File tree

6 files changed

+65
-62
lines changed

6 files changed

+65
-62
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
204204
Stub::Struct,
205205
unique_type_id,
206206
&ptr_type_debuginfo_name,
207-
unknown_file_metadata(cx),
208-
UNKNOWN_LINE_NUMBER,
207+
None,
209208
cx.size_and_align_of(ptr_type),
210209
NO_SCOPE_METADATA,
211210
DIFlags::FlagZero,
@@ -373,8 +372,7 @@ fn build_dyn_type_di_node<'ll, 'tcx>(
373372
Stub::Struct,
374373
unique_type_id,
375374
&type_name,
376-
unknown_file_metadata(cx),
377-
UNKNOWN_LINE_NUMBER,
375+
None,
378376
cx.size_and_align_of(dyn_type),
379377
NO_SCOPE_METADATA,
380378
DIFlags::FlagZero,
@@ -845,8 +843,7 @@ fn build_foreign_type_di_node<'ll, 'tcx>(
845843
Stub::Struct,
846844
unique_type_id,
847845
&compute_debuginfo_type_name(cx.tcx, t, false),
848-
unknown_file_metadata(cx),
849-
UNKNOWN_LINE_NUMBER,
846+
None,
850847
cx.size_and_align_of(t),
851848
Some(get_namespace_for_item(cx, def_id)),
852849
DIFlags::FlagZero,
@@ -1050,24 +1047,14 @@ fn build_struct_type_di_node<'ll, 'tcx>(
10501047
let struct_type_and_layout = cx.layout_of(struct_type);
10511048
let variant_def = adt_def.non_enum_variant();
10521049

1053-
let tcx = cx.tcx;
1054-
let struct_span = tcx.def_span(adt_def.did());
1055-
let (file_metadata, line_number) = if !struct_span.is_dummy() {
1056-
let loc = cx.lookup_debug_loc(struct_span.lo());
1057-
(file_metadata(cx, &loc.file), loc.line)
1058-
} else {
1059-
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1060-
};
1061-
10621050
type_map::build_type_with_children(
10631051
cx,
10641052
type_map::stub(
10651053
cx,
10661054
Stub::Struct,
10671055
unique_type_id,
10681056
&compute_debuginfo_type_name(cx.tcx, struct_type, false),
1069-
file_metadata,
1070-
line_number,
1057+
Some(adt_def.did()),
10711058
size_and_align_of(struct_type_and_layout),
10721059
Some(containing_scope),
10731060
visibility_di_flags(cx, adt_def.did(), adt_def.did()),
@@ -1171,8 +1158,7 @@ fn build_tuple_type_di_node<'ll, 'tcx>(
11711158
Stub::Struct,
11721159
unique_type_id,
11731160
&type_name,
1174-
unknown_file_metadata(cx),
1175-
UNKNOWN_LINE_NUMBER,
1161+
None,
11761162
size_and_align_of(tuple_type_and_layout),
11771163
NO_SCOPE_METADATA,
11781164
DIFlags::FlagZero,
@@ -1212,23 +1198,14 @@ fn build_closure_env_di_node<'ll, 'tcx>(
12121198
let containing_scope = get_namespace_for_item(cx, def_id);
12131199
let type_name = compute_debuginfo_type_name(cx.tcx, closure_env_type, false);
12141200

1215-
let closure_span = cx.tcx.def_span(def_id);
1216-
let (file_metadata, line_number) = if !closure_span.is_dummy() {
1217-
let loc = cx.lookup_debug_loc(closure_span.lo());
1218-
(file_metadata(cx, &loc.file), loc.line)
1219-
} else {
1220-
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1221-
};
1222-
12231201
type_map::build_type_with_children(
12241202
cx,
12251203
type_map::stub(
12261204
cx,
12271205
Stub::Struct,
12281206
unique_type_id,
12291207
&type_name,
1230-
file_metadata,
1231-
line_number,
1208+
Some(def_id),
12321209
cx.size_and_align_of(closure_env_type),
12331210
Some(containing_scope),
12341211
DIFlags::FlagZero,
@@ -1260,8 +1237,7 @@ fn build_union_type_di_node<'ll, 'tcx>(
12601237
Stub::Union,
12611238
unique_type_id,
12621239
&type_name,
1263-
unknown_file_metadata(cx),
1264-
UNKNOWN_LINE_NUMBER,
1240+
Some(union_def_id),
12651241
size_and_align_of(union_ty_and_layout),
12661242
Some(containing_scope),
12671243
DIFlags::FlagZero,
@@ -1454,8 +1430,7 @@ fn build_vtable_type_di_node<'ll, 'tcx>(
14541430
Stub::VTableTy { vtable_holder },
14551431
unique_type_id,
14561432
&vtable_type_name,
1457-
unknown_file_metadata(cx),
1458-
UNKNOWN_LINE_NUMBER,
1433+
None,
14591434
(size, pointer_align),
14601435
NO_SCOPE_METADATA,
14611436
DIFlags::FlagArtificial,

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>(
199199
type_map::Stub::Union,
200200
unique_type_id,
201201
&enum_type_name,
202-
unknown_file_metadata(cx),
203-
UNKNOWN_LINE_NUMBER,
202+
Some(enum_adt_def.did()),
204203
cx.size_and_align_of(enum_type),
205204
NO_SCOPE_METADATA,
206205
visibility_di_flags(cx, enum_adt_def.did(), enum_adt_def.did()),
@@ -276,8 +275,7 @@ pub(super) fn build_coroutine_di_node<'ll, 'tcx>(
276275
type_map::Stub::Union,
277276
unique_type_id,
278277
&coroutine_type_name,
279-
unknown_file_metadata(cx),
280-
UNKNOWN_LINE_NUMBER,
278+
None,
281279
size_and_align_of(coroutine_type_and_layout),
282280
NO_SCOPE_METADATA,
283281
DIFlags::FlagZero,
@@ -332,6 +330,7 @@ fn build_single_variant_union_fields<'ll, 'tcx>(
332330
variant_index,
333331
Cow::from(enum_adt_def.variant(variant_index).name.as_str()),
334332
)),
333+
enum_adt_def.did(),
335334
);
336335

337336
let variant_struct_type_wrapper_di_node = build_variant_struct_wrapper_type_di_node(
@@ -394,6 +393,7 @@ fn build_union_fields_for_enum<'ll, 'tcx>(
394393
let variant_name = Cow::from(enum_adt_def.variant(variant_index).name.as_str());
395394
(variant_index, variant_name)
396395
}),
396+
enum_adt_def.did(),
397397
);
398398
let visibility_flags = visibility_di_flags(cx, enum_adt_def.did(), enum_adt_def.did());
399399

@@ -451,13 +451,15 @@ fn build_variant_names_type_di_node<'ll, 'tcx>(
451451
cx: &CodegenCx<'ll, 'tcx>,
452452
containing_scope: &'ll DIType,
453453
variants: impl Iterator<Item = (VariantIdx, Cow<'tcx, str>)>,
454+
enum_def_id: rustc_span::def_id::DefId,
454455
) -> &'ll DIType {
455456
// Create an enumerator for each variant.
456457
super::build_enumeration_type_di_node(
457458
cx,
458459
"VariantNames",
459460
variant_names_enum_base_type(cx),
460461
variants.map(|(variant_index, variant_name)| (variant_name, variant_index.as_u32().into())),
462+
enum_def_id,
461463
containing_scope,
462464
)
463465
}
@@ -485,8 +487,7 @@ fn build_variant_struct_wrapper_type_di_node<'ll, 'tcx>(
485487
variant_index,
486488
),
487489
&variant_struct_wrapper_type_name(variant_index),
488-
unknown_file_metadata(cx),
489-
UNKNOWN_LINE_NUMBER,
490+
None,
490491
// NOTE: We use size and align of enum_type, not from variant_layout:
491492
size_and_align_of(enum_or_coroutine_type_and_layout),
492493
Some(enum_or_coroutine_type_di_node),
@@ -690,6 +691,7 @@ fn build_union_fields_for_direct_tag_coroutine<'ll, 'tcx>(
690691
variant_range
691692
.clone()
692693
.map(|variant_index| (variant_index, CoroutineArgs::variant_name(variant_index))),
694+
coroutine_def_id,
693695
);
694696

695697
let discriminants: IndexVec<VariantIdx, DiscrResult> = {

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use super::{SmallVec, size_and_align_of};
1616
use crate::common::{AsCCharPtr, CodegenCx};
1717
use crate::debuginfo::metadata::type_map::{self, Stub};
1818
use crate::debuginfo::metadata::{
19-
UNKNOWN_LINE_NUMBER, build_field_di_node, build_generic_type_param_di_nodes, type_di_node,
20-
unknown_file_metadata,
19+
UNKNOWN_LINE_NUMBER, build_field_di_node, build_generic_type_param_di_nodes,
20+
file_metadata_from_def_id, type_di_node, unknown_file_metadata,
2121
};
2222
use crate::debuginfo::utils::{DIB, create_DIArray, get_namespace_for_item};
2323
use crate::llvm::debuginfo::{DIFlags, DIType};
@@ -77,6 +77,7 @@ fn build_c_style_enum_di_node<'ll, 'tcx>(
7777
let name = Cow::from(enum_adt_def.variant(variant_index).name.as_str());
7878
(name, discr.val)
7979
}),
80+
enum_adt_def.did(),
8081
containing_scope,
8182
),
8283
already_stored_in_typemap: false,
@@ -92,6 +93,7 @@ fn build_enumeration_type_di_node<'ll, 'tcx>(
9293
type_name: &str,
9394
base_type: Ty<'tcx>,
9495
enumerators: impl Iterator<Item = (Cow<'tcx, str>, u128)>,
96+
def_id: rustc_span::def_id::DefId,
9597
containing_scope: &'ll DIType,
9698
) -> &'ll DIType {
9799
let is_unsigned = match base_type.kind() {
@@ -115,14 +117,16 @@ fn build_enumeration_type_di_node<'ll, 'tcx>(
115117
})
116118
.collect();
117119

120+
let (file_metadata, line_number) = file_metadata_from_def_id(cx, Some(def_id));
121+
118122
unsafe {
119123
llvm::LLVMRustDIBuilderCreateEnumerationType(
120124
DIB(cx),
121125
containing_scope,
122126
type_name.as_c_char_ptr(),
123127
type_name.len(),
124-
unknown_file_metadata(cx),
125-
UNKNOWN_LINE_NUMBER,
128+
file_metadata,
129+
line_number,
126130
size.bits(),
127131
align.bits() as u32,
128132
create_DIArray(DIB(cx), &enumerator_di_nodes[..]),
@@ -204,8 +208,7 @@ fn build_enum_variant_struct_type_di_node<'ll, 'tcx>(
204208
variant_index,
205209
),
206210
variant_def.name.as_str(),
207-
unknown_file_metadata(cx),
208-
UNKNOWN_LINE_NUMBER,
211+
None,
209212
// NOTE: We use size and align of enum_type, not from variant_layout:
210213
size_and_align_of(enum_type_and_layout),
211214
Some(enum_type_di_node),
@@ -288,8 +291,7 @@ fn build_coroutine_variant_struct_type_di_node<'ll, 'tcx>(
288291
Stub::Struct,
289292
unique_type_id,
290293
&variant_name,
291-
unknown_file_metadata(cx),
292-
UNKNOWN_LINE_NUMBER,
294+
None,
293295
size_and_align_of(coroutine_type_and_layout),
294296
Some(coroutine_type_di_node),
295297
DIFlags::FlagZero,
@@ -355,11 +357,7 @@ enum DiscrResult {
355357

356358
impl DiscrResult {
357359
fn opt_single_val(&self) -> Option<u128> {
358-
if let Self::Value(d) = *self {
359-
Some(d)
360-
} else {
361-
None
362-
}
360+
if let Self::Value(d) = *self { Some(d) } else { None }
363361
}
364362
}
365363

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/native.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>(
6262
Stub::Struct,
6363
unique_type_id,
6464
&enum_type_name,
65-
unknown_file_metadata(cx),
66-
UNKNOWN_LINE_NUMBER,
65+
Some(enum_adt_def.did()),
6766
size_and_align_of(enum_type_and_layout),
6867
Some(containing_scope),
6968
visibility_flags,
@@ -143,8 +142,7 @@ pub(super) fn build_coroutine_di_node<'ll, 'tcx>(
143142
Stub::Struct,
144143
unique_type_id,
145144
&coroutine_type_name,
146-
unknown_file_metadata(cx),
147-
UNKNOWN_LINE_NUMBER,
145+
None,
148146
size_and_align_of(coroutine_type_and_layout),
149147
Some(containing_scope),
150148
DIFlags::FlagZero,

compiler/rustc_codegen_llvm/src/debuginfo/metadata/type_map.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use rustc_macros::HashStable;
88
use rustc_middle::bug;
99
use rustc_middle::ty::{ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt};
1010

11-
use super::{SmallVec, UNKNOWN_LINE_NUMBER, unknown_file_metadata};
11+
use super::{SmallVec, UNKNOWN_LINE_NUMBER, file_metadata, unknown_file_metadata};
1212
use crate::common::{AsCCharPtr, CodegenCx};
1313
use crate::debuginfo::utils::{DIB, create_DIArray, debug_context};
14-
use crate::llvm::debuginfo::{DIFile, DIFlags, DIScope, DIType};
14+
use crate::llvm::debuginfo::{DIFlags, DIScope, DIType};
1515
use crate::llvm::{self};
1616

1717
mod private {
@@ -174,15 +174,26 @@ pub(super) fn stub<'ll, 'tcx>(
174174
kind: Stub<'ll>,
175175
unique_type_id: UniqueTypeId<'tcx>,
176176
name: &str,
177-
file_metadata: &'ll DIFile,
178-
line_number: u32,
177+
def_id: Option<rustc_span::def_id::DefId>,
179178
(size, align): (Size, Align),
180179
containing_scope: Option<&'ll DIScope>,
181180
flags: DIFlags,
182181
) -> StubInfo<'ll, 'tcx> {
183182
let empty_array = create_DIArray(DIB(cx), &[]);
184183
let unique_type_id_str = unique_type_id.generate_unique_id_string(cx.tcx);
185184

185+
let (file_metadata, line_number) = if let Some(def_id) = def_id {
186+
let span = cx.tcx.def_span(def_id);
187+
if !span.is_dummy() {
188+
let loc = cx.lookup_debug_loc(span.lo());
189+
(file_metadata(cx, &loc.file), loc.line)
190+
} else {
191+
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
192+
}
193+
} else {
194+
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
195+
};
196+
186197
let metadata = match kind {
187198
Stub::Struct | Stub::VTableTy { .. } => {
188199
let vtable_holder = match kind {

tests/codegen/issue-98678.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,31 @@
33
// compile-flags: -C debuginfo=2
44
#![crate_type = "lib"]
55

6-
// CHECK: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}src/test/codegen/issue-98678.rs{{".*}})
6+
// The use of CHECK-DAG here is because the C++-like enum is emitted before the `DIFile` node
7+
8+
// CHECK-DAG: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}src/test/codegen/issue-98678.rs{{".*}})
9+
10+
// CHECK-DAG: !DICompositeType({{.*"}}MyCppLikeEnum{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]],
11+
#[repr(C)]
12+
pub enum MyCppLikeEnum {
13+
One,
14+
}
715

816
// CHECK: !DICompositeType({{.*"}}MyType{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
917
pub struct MyType;
1018

11-
pub fn foo(_: MyType) {
19+
// CHECK: !DICompositeType({{.*"}}MyUnion{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
20+
pub union MyUnion {
21+
i: i32, // TODO fields are still wrong
22+
f: f32,
23+
}
24+
25+
// CHECK: !DICompositeType({{.*"}}MyNativeEnum{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
26+
pub enum MyNativeEnum {
27+
One,
28+
}
29+
30+
pub fn foo(_: MyType, _: MyUnion, _: MyNativeEnum, _: MyCppLikeEnum) {
1231
// CHECK: !DICompositeType({{.*"[{]}}closure_env#0{{[}]".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
1332
let closure = |x| x;
1433
closure(0);

0 commit comments

Comments
 (0)