Skip to content

Commit e77625a

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 29b5c82 commit e77625a

File tree

6 files changed

+64
-57
lines changed

6 files changed

+64
-57
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
213213
Stub::Struct,
214214
unique_type_id,
215215
&ptr_type_debuginfo_name,
216-
unknown_file_metadata(cx),
217-
UNKNOWN_LINE_NUMBER,
216+
None,
218217
cx.size_and_align_of(ptr_type),
219218
NO_SCOPE_METADATA,
220219
DIFlags::FlagZero,
@@ -369,8 +368,7 @@ fn build_dyn_type_di_node<'ll, 'tcx>(
369368
Stub::Struct,
370369
unique_type_id,
371370
&type_name,
372-
unknown_file_metadata(cx),
373-
UNKNOWN_LINE_NUMBER,
371+
None,
374372
cx.size_and_align_of(dyn_type),
375373
NO_SCOPE_METADATA,
376374
DIFlags::FlagZero,
@@ -752,8 +750,7 @@ fn build_foreign_type_di_node<'ll, 'tcx>(
752750
Stub::Struct,
753751
unique_type_id,
754752
&compute_debuginfo_type_name(cx.tcx, t, false),
755-
unknown_file_metadata(cx),
756-
UNKNOWN_LINE_NUMBER,
753+
None,
757754
cx.size_and_align_of(t),
758755
Some(get_namespace_for_item(cx, def_id)),
759756
DIFlags::FlagZero,
@@ -984,24 +981,14 @@ fn build_struct_type_di_node<'ll, 'tcx>(
984981
let struct_type_and_layout = cx.layout_of(struct_type);
985982
let variant_def = adt_def.non_enum_variant();
986983

987-
let tcx = cx.tcx;
988-
let struct_span = tcx.def_span(adt_def.did());
989-
let (file_metadata, line_number) = if !struct_span.is_dummy() {
990-
let loc = cx.lookup_debug_loc(struct_span.lo());
991-
(file_metadata(cx, &loc.file), loc.line)
992-
} else {
993-
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
994-
};
995-
996984
type_map::build_type_with_children(
997985
cx,
998986
type_map::stub(
999987
cx,
1000988
Stub::Struct,
1001989
unique_type_id,
1002990
&compute_debuginfo_type_name(cx.tcx, struct_type, false),
1003-
file_metadata,
1004-
line_number,
991+
Some(adt_def.did()),
1005992
size_and_align_of(struct_type_and_layout),
1006993
Some(containing_scope),
1007994
DIFlags::FlagZero,
@@ -1112,8 +1099,7 @@ fn build_tuple_type_di_node<'ll, 'tcx>(
11121099
Stub::Struct,
11131100
unique_type_id,
11141101
&type_name,
1115-
unknown_file_metadata(cx),
1116-
UNKNOWN_LINE_NUMBER,
1102+
None,
11171103
size_and_align_of(tuple_type_and_layout),
11181104
NO_SCOPE_METADATA,
11191105
DIFlags::FlagZero,
@@ -1152,23 +1138,14 @@ fn build_closure_env_di_node<'ll, 'tcx>(
11521138
let containing_scope = get_namespace_for_item(cx, def_id);
11531139
let type_name = compute_debuginfo_type_name(cx.tcx, closure_env_type, false);
11541140

1155-
let closure_span = cx.tcx.def_span(def_id);
1156-
let (file_metadata, line_number) = if !closure_span.is_dummy() {
1157-
let loc = cx.lookup_debug_loc(closure_span.lo());
1158-
(file_metadata(cx, &loc.file), loc.line)
1159-
} else {
1160-
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1161-
};
1162-
11631141
type_map::build_type_with_children(
11641142
cx,
11651143
type_map::stub(
11661144
cx,
11671145
Stub::Struct,
11681146
unique_type_id,
11691147
&type_name,
1170-
file_metadata,
1171-
line_number,
1148+
Some(def_id),
11721149
cx.size_and_align_of(closure_env_type),
11731150
Some(containing_scope),
11741151
DIFlags::FlagZero,
@@ -1200,8 +1177,7 @@ fn build_union_type_di_node<'ll, 'tcx>(
12001177
Stub::Union,
12011178
unique_type_id,
12021179
&type_name,
1203-
unknown_file_metadata(cx),
1204-
UNKNOWN_LINE_NUMBER,
1180+
Some(union_def_id),
12051181
size_and_align_of(union_ty_and_layout),
12061182
Some(containing_scope),
12071183
DIFlags::FlagZero,
@@ -1387,8 +1363,7 @@ fn build_vtable_type_di_node<'ll, 'tcx>(
13871363
Stub::VTableTy { vtable_holder },
13881364
unique_type_id,
13891365
&vtable_type_name,
1390-
unknown_file_metadata(cx),
1391-
UNKNOWN_LINE_NUMBER,
1366+
None,
13921367
(size, pointer_align),
13931368
NO_SCOPE_METADATA,
13941369
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
@@ -213,8 +213,7 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>(
213213
type_map::Stub::Union,
214214
unique_type_id,
215215
&enum_type_name,
216-
unknown_file_metadata(cx),
217-
UNKNOWN_LINE_NUMBER,
216+
Some(enum_adt_def.did()),
218217
cx.size_and_align_of(enum_type),
219218
NO_SCOPE_METADATA,
220219
DIFlags::FlagZero,
@@ -290,8 +289,7 @@ pub(super) fn build_generator_di_node<'ll, 'tcx>(
290289
type_map::Stub::Union,
291290
unique_type_id,
292291
&generator_type_name,
293-
unknown_file_metadata(cx),
294-
UNKNOWN_LINE_NUMBER,
292+
None,
295293
size_and_align_of(generator_type_and_layout),
296294
NO_SCOPE_METADATA,
297295
DIFlags::FlagZero,
@@ -344,6 +342,7 @@ fn build_single_variant_union_fields<'ll, 'tcx>(
344342
variant_index,
345343
Cow::from(enum_adt_def.variant(variant_index).name.as_str()),
346344
)),
345+
enum_adt_def.did(),
347346
);
348347

349348
let variant_struct_type_wrapper_di_node = build_variant_struct_wrapper_type_di_node(
@@ -406,6 +405,7 @@ fn build_union_fields_for_enum<'ll, 'tcx>(
406405
let variant_name = Cow::from(enum_adt_def.variant(variant_index).name.as_str());
407406
(variant_index, variant_name)
408407
}),
408+
enum_adt_def.did(),
409409
);
410410

411411
let variant_field_infos: SmallVec<VariantFieldInfo<'ll>> = variant_indices
@@ -460,13 +460,15 @@ fn build_variant_names_type_di_node<'ll, 'tcx>(
460460
cx: &CodegenCx<'ll, 'tcx>,
461461
containing_scope: &'ll DIType,
462462
variants: impl Iterator<Item = (VariantIdx, Cow<'tcx, str>)>,
463+
enum_def_id: rustc_span::def_id::DefId,
463464
) -> &'ll DIType {
464465
// Create an enumerator for each variant.
465466
super::build_enumeration_type_di_node(
466467
cx,
467468
"VariantNames",
468469
variant_names_enum_base_type(cx),
469470
variants.map(|(variant_index, variant_name)| (variant_name, variant_index.as_u32().into())),
471+
enum_def_id,
470472
containing_scope,
471473
)
472474
}
@@ -494,8 +496,7 @@ fn build_variant_struct_wrapper_type_di_node<'ll, 'tcx>(
494496
variant_index,
495497
),
496498
&variant_struct_wrapper_type_name(variant_index),
497-
unknown_file_metadata(cx),
498-
UNKNOWN_LINE_NUMBER,
499+
None,
499500
// NOTE: We use size and align of enum_type, not from variant_layout:
500501
size_and_align_of(enum_or_generator_type_and_layout),
501502
Some(enum_or_generator_type_di_node),
@@ -697,6 +698,7 @@ fn build_union_fields_for_direct_tag_generator<'ll, 'tcx>(
697698
variant_range
698699
.clone()
699700
.map(|variant_index| (variant_index, GeneratorSubsts::variant_name(variant_index))),
701+
generator_def_id,
700702
);
701703

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

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ use crate::{
2121
common::CodegenCx,
2222
debuginfo::{
2323
metadata::{
24-
build_field_di_node, build_generic_type_param_di_nodes, type_di_node,
24+
build_field_di_node, build_generic_type_param_di_nodes, file_metadata_from_def_id,
25+
type_di_node,
2526
type_map::{self, Stub},
26-
unknown_file_metadata, UNKNOWN_LINE_NUMBER,
2727
},
2828
utils::{create_DIArray, get_namespace_for_item, DIB},
2929
},
@@ -93,6 +93,7 @@ fn build_c_style_enum_di_node<'ll, 'tcx>(
9393
let name = Cow::from(enum_adt_def.variant(variant_index).name.as_str());
9494
(name, discr.val)
9595
}),
96+
enum_adt_def.did(),
9697
containing_scope,
9798
),
9899
already_stored_in_typemap: false,
@@ -150,6 +151,7 @@ fn build_enumeration_type_di_node<'ll, 'tcx>(
150151
type_name: &str,
151152
base_type: Ty<'tcx>,
152153
enumerators: impl Iterator<Item = (Cow<'tcx, str>, u128)>,
154+
def_id: rustc_span::def_id::DefId,
153155
containing_scope: &'ll DIType,
154156
) -> &'ll DIType {
155157
let is_unsigned = match base_type.kind() {
@@ -173,14 +175,16 @@ fn build_enumeration_type_di_node<'ll, 'tcx>(
173175
})
174176
.collect();
175177

178+
let (file_metadata, line_number) = file_metadata_from_def_id(cx, Some(def_id));
179+
176180
unsafe {
177181
llvm::LLVMRustDIBuilderCreateEnumerationType(
178182
DIB(cx),
179183
containing_scope,
180184
type_name.as_ptr().cast(),
181185
type_name.len(),
182-
unknown_file_metadata(cx),
183-
UNKNOWN_LINE_NUMBER,
186+
file_metadata,
187+
line_number,
184188
size.bits(),
185189
align.bits() as u32,
186190
create_DIArray(DIB(cx), &enumerator_di_nodes[..]),
@@ -261,8 +265,7 @@ fn build_enum_variant_struct_type_di_node<'ll, 'tcx>(
261265
variant_index,
262266
),
263267
variant_def.name.as_str(),
264-
unknown_file_metadata(cx),
265-
UNKNOWN_LINE_NUMBER,
268+
None,
266269
// NOTE: We use size and align of enum_type, not from variant_layout:
267270
size_and_align_of(enum_type_and_layout),
268271
Some(enum_type_di_node),
@@ -345,8 +348,7 @@ pub fn build_generator_variant_struct_type_di_node<'ll, 'tcx>(
345348
Stub::Struct,
346349
unique_type_id,
347350
&variant_name,
348-
unknown_file_metadata(cx),
349-
UNKNOWN_LINE_NUMBER,
351+
None,
350352
size_and_align_of(generator_type_and_layout),
351353
Some(generator_type_di_node),
352354
DIFlags::FlagZero,

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>(
7272
Stub::Struct,
7373
unique_type_id,
7474
&enum_type_name,
75-
unknown_file_metadata(cx),
76-
UNKNOWN_LINE_NUMBER,
75+
Some(enum_adt_def.did()),
7776
size_and_align_of(enum_type_and_layout),
7877
Some(containing_scope),
7978
DIFlags::FlagZero,
@@ -152,8 +151,7 @@ pub(super) fn build_generator_di_node<'ll, 'tcx>(
152151
Stub::Struct,
153152
unique_type_id,
154153
&generator_type_name,
155-
unknown_file_metadata(cx),
156-
UNKNOWN_LINE_NUMBER,
154+
None,
157155
size_and_align_of(generator_type_and_layout),
158156
Some(containing_scope),
159157
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
@@ -16,11 +16,11 @@ use crate::{
1616
debuginfo::utils::{create_DIArray, debug_context, DIB},
1717
llvm::{
1818
self,
19-
debuginfo::{DIFile, DIFlags, DIScope, DIType},
19+
debuginfo::{DIFlags, DIScope, DIType},
2020
},
2121
};
2222

23-
use super::SmallVec;
23+
use super::{file_metadata, unknown_file_metadata, SmallVec, UNKNOWN_LINE_NUMBER};
2424

2525
mod private {
2626
// This type cannot be constructed outside of this module because
@@ -183,15 +183,26 @@ pub(super) fn stub<'ll, 'tcx>(
183183
kind: Stub<'ll>,
184184
unique_type_id: UniqueTypeId<'tcx>,
185185
name: &str,
186-
file_metadata: &'ll DIFile,
187-
line_number: u32,
186+
def_id: Option<rustc_span::def_id::DefId>,
188187
(size, align): (Size, Align),
189188
containing_scope: Option<&'ll DIScope>,
190189
flags: DIFlags,
191190
) -> StubInfo<'ll, 'tcx> {
192191
let empty_array = create_DIArray(DIB(cx), &[]);
193192
let unique_type_id_str = unique_type_id.generate_unique_id_string(cx.tcx);
194193

194+
let (file_metadata, line_number) = if let Some(def_id) = def_id {
195+
let span = cx.tcx.def_span(def_id);
196+
if !span.is_dummy() {
197+
let loc = cx.lookup_debug_loc(span.lo());
198+
(file_metadata(cx, &loc.file), loc.line)
199+
} else {
200+
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
201+
}
202+
} else {
203+
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
204+
};
205+
195206
let metadata = match kind {
196207
Stub::Struct | Stub::VTableTy { .. } => {
197208
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)