Skip to content

Commit a4833a8

Browse files
committed
Move additional source location info behind -Z option
1 parent b6659b0 commit a4833a8

File tree

10 files changed

+123
-34
lines changed

10 files changed

+123
-34
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,12 @@ fn build_field_di_node<'ll, 'tcx>(
998998
type_di_node: &'ll DIType,
999999
def_id: Option<DefId>,
10001000
) -> &'ll DIType {
1001-
let (file_metadata, line_number) = file_metadata_from_def_id(cx, def_id);
1001+
let (file_metadata, line_number) =
1002+
if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo {
1003+
file_metadata_from_def_id(cx, def_id)
1004+
} else {
1005+
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1006+
};
10021007
unsafe {
10031008
llvm::LLVMRustDIBuilderCreateMemberType(
10041009
DIB(cx),
@@ -1050,6 +1055,11 @@ fn build_struct_type_di_node<'ll, 'tcx>(
10501055
let containing_scope = get_namespace_for_item(cx, adt_def.did());
10511056
let struct_type_and_layout = cx.layout_of(struct_type);
10521057
let variant_def = adt_def.non_enum_variant();
1058+
let def_location = if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo {
1059+
Some(file_metadata_from_def_id(cx, Some(adt_def.did())))
1060+
} else {
1061+
None
1062+
};
10531063

10541064
type_map::build_type_with_children(
10551065
cx,
@@ -1058,7 +1068,7 @@ fn build_struct_type_di_node<'ll, 'tcx>(
10581068
Stub::Struct,
10591069
unique_type_id,
10601070
&compute_debuginfo_type_name(cx.tcx, struct_type, false),
1061-
Some(file_metadata_from_def_id(cx, Some(adt_def.did()))),
1071+
def_location,
10621072
size_and_align_of(struct_type_and_layout),
10631073
Some(containing_scope),
10641074
visibility_di_flags(cx, adt_def.did(), adt_def.did()),
@@ -1078,6 +1088,12 @@ fn build_struct_type_di_node<'ll, 'tcx>(
10781088
Cow::Borrowed(f.name.as_str())
10791089
};
10801090
let field_layout = struct_type_and_layout.field(cx, i);
1091+
let def_id = if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo
1092+
{
1093+
Some(f.did)
1094+
} else {
1095+
None
1096+
};
10811097
build_field_di_node(
10821098
cx,
10831099
owner,
@@ -1086,7 +1102,7 @@ fn build_struct_type_di_node<'ll, 'tcx>(
10861102
struct_type_and_layout.fields.offset(i),
10871103
visibility_di_flags(cx, f.did, adt_def.did()),
10881104
type_di_node(cx, field_layout.ty),
1089-
Some(f.did),
1105+
def_id,
10901106
)
10911107
})
10921108
.collect()
@@ -1236,6 +1252,11 @@ fn build_union_type_di_node<'ll, 'tcx>(
12361252
let containing_scope = get_namespace_for_item(cx, union_def_id);
12371253
let union_ty_and_layout = cx.layout_of(union_type);
12381254
let type_name = compute_debuginfo_type_name(cx.tcx, union_type, false);
1255+
let def_location = if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo {
1256+
Some(file_metadata_from_def_id(cx, Some(union_def_id)))
1257+
} else {
1258+
None
1259+
};
12391260

12401261
type_map::build_type_with_children(
12411262
cx,
@@ -1244,7 +1265,7 @@ fn build_union_type_di_node<'ll, 'tcx>(
12441265
Stub::Union,
12451266
unique_type_id,
12461267
&type_name,
1247-
Some(file_metadata_from_def_id(cx, Some(union_def_id))),
1268+
def_location,
12481269
size_and_align_of(union_ty_and_layout),
12491270
Some(containing_scope),
12501271
DIFlags::FlagZero,
@@ -1257,6 +1278,12 @@ fn build_union_type_di_node<'ll, 'tcx>(
12571278
.enumerate()
12581279
.map(|(i, f)| {
12591280
let field_layout = union_ty_and_layout.field(cx, i);
1281+
let def_id = if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo
1282+
{
1283+
Some(f.did)
1284+
} else {
1285+
None
1286+
};
12601287
build_field_di_node(
12611288
cx,
12621289
owner,
@@ -1265,7 +1292,7 @@ fn build_union_type_di_node<'ll, 'tcx>(
12651292
Size::ZERO,
12661293
DIFlags::FlagZero,
12671294
type_di_node(cx, field_layout.ty),
1268-
Some(f.did),
1295+
def_id,
12691296
)
12701297
})
12711298
.collect()

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

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,20 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>(
192192

193193
assert!(!wants_c_like_enum_debuginfo(cx.tcx, enum_type_and_layout));
194194

195+
let def_location = if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo {
196+
Some(file_metadata_from_def_id(cx, Some(enum_adt_def.did())))
197+
} else {
198+
None
199+
};
200+
195201
type_map::build_type_with_children(
196202
cx,
197203
type_map::stub(
198204
cx,
199205
type_map::Stub::Union,
200206
unique_type_id,
201207
&enum_type_name,
202-
Some(file_metadata_from_def_id(cx, Some(enum_adt_def.did()))),
208+
def_location,
203209
cx.size_and_align_of(enum_type),
204210
NO_SCOPE_METADATA,
205211
visibility_di_flags(cx, enum_adt_def.did(), enum_adt_def.did()),
@@ -263,8 +269,13 @@ pub(super) fn build_coroutine_di_node<'ll, 'tcx>(
263269
unique_type_id: UniqueTypeId<'tcx>,
264270
) -> DINodeCreationResult<'ll> {
265271
let coroutine_type = unique_type_id.expect_ty();
266-
let &ty::Coroutine(coroutine_def_id, _, _) = coroutine_type.kind() else {
267-
bug!("build_coroutine_di_node() called with non-coroutine type: `{:?}`", coroutine_type)
272+
let def_location = if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo {
273+
let &ty::Coroutine(coroutine_def_id, _) = coroutine_type.kind() else {
274+
bug!("build_coroutine_di_node() called with non-coroutine type: `{:?}`", coroutine_type)
275+
};
276+
Some(file_metadata_from_def_id(cx, Some(coroutine_def_id)))
277+
} else {
278+
None
268279
};
269280
let coroutine_type_and_layout = cx.layout_of(coroutine_type);
270281
let coroutine_type_name = compute_debuginfo_type_name(cx.tcx, coroutine_type, false);
@@ -278,7 +289,7 @@ pub(super) fn build_coroutine_di_node<'ll, 'tcx>(
278289
type_map::Stub::Union,
279290
unique_type_id,
280291
&coroutine_type_name,
281-
Some(file_metadata_from_def_id(cx, Some(coroutine_def_id))),
292+
def_location,
282293
size_and_align_of(coroutine_type_and_layout),
283294
NO_SCOPE_METADATA,
284295
DIFlags::FlagZero,
@@ -326,14 +337,20 @@ fn build_single_variant_union_fields<'ll, 'tcx>(
326337
let tag_base_type_di_node = type_di_node(cx, tag_base_type);
327338
let tag_base_type_align = cx.align_of(tag_base_type);
328339

340+
let enum_adt_def_id = if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo {
341+
Some(enum_adt_def.did())
342+
} else {
343+
None
344+
};
345+
329346
let variant_names_type_di_node = build_variant_names_type_di_node(
330347
cx,
331348
enum_type_di_node,
332349
std::iter::once((
333350
variant_index,
334351
Cow::from(enum_adt_def.variant(variant_index).name.as_str()),
335352
)),
336-
enum_adt_def.did(),
353+
enum_adt_def_id,
337354
);
338355

339356
let variant_struct_type_wrapper_di_node = build_variant_struct_wrapper_type_di_node(
@@ -391,14 +408,20 @@ fn build_union_fields_for_enum<'ll, 'tcx>(
391408
) -> SmallVec<&'ll DIType> {
392409
let tag_base_type = tag_base_type(cx.tcx, enum_type_and_layout);
393410

411+
let enum_adt_def_id = if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo {
412+
Some(enum_adt_def.did())
413+
} else {
414+
None
415+
};
416+
394417
let variant_names_type_di_node = build_variant_names_type_di_node(
395418
cx,
396419
enum_type_di_node,
397420
variant_indices.clone().map(|variant_index| {
398421
let variant_name = Cow::from(enum_adt_def.variant(variant_index).name.as_str());
399422
(variant_index, variant_name)
400423
}),
401-
enum_adt_def.did(),
424+
enum_adt_def_id,
402425
);
403426
let visibility_flags = visibility_di_flags(cx, enum_adt_def.did(), enum_adt_def.did());
404427

@@ -456,7 +479,7 @@ fn build_variant_names_type_di_node<'ll, 'tcx>(
456479
cx: &CodegenCx<'ll, 'tcx>,
457480
containing_scope: &'ll DIType,
458481
variants: impl Iterator<Item = (VariantIdx, Cow<'tcx, str>)>,
459-
enum_def_id: rustc_span::def_id::DefId,
482+
enum_def_id: Option<rustc_span::def_id::DefId>,
460483
) -> &'ll DIType {
461484
// Create an enumerator for each variant.
462485
super::build_enumeration_type_di_node(
@@ -698,7 +721,11 @@ fn build_union_fields_for_direct_tag_coroutine<'ll, 'tcx>(
698721
variant_range
699722
.clone()
700723
.map(|variant_index| (variant_index, CoroutineArgs::variant_name(variant_index))),
701-
coroutine_def_id,
724+
if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo {
725+
Some(coroutine_def_id)
726+
} else {
727+
None
728+
},
702729
);
703730

704731
let discriminants: IndexVec<VariantIdx, DiscrResult> = {
@@ -791,7 +818,11 @@ fn build_union_fields_for_direct_tag_enum_or_coroutine<'ll, 'tcx>(
791818
tag_base_type_di_node,
792819
tag_base_type,
793820
variant_member_info.discr,
794-
variant_member_info.source_info,
821+
if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo {
822+
variant_member_info.source_info
823+
} else {
824+
None
825+
},
795826
);
796827

797828
// We use LLVMRustDIBuilderCreateMemberType() member type directly because

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ fn build_c_style_enum_di_node<'ll, 'tcx>(
6868
enum_type_and_layout: TyAndLayout<'tcx>,
6969
) -> DINodeCreationResult<'ll> {
7070
let containing_scope = get_namespace_for_item(cx, enum_adt_def.did());
71+
let enum_adt_def_id = if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo {
72+
Some(enum_adt_def.did())
73+
} else {
74+
None
75+
};
7176
DINodeCreationResult {
7277
di_node: build_enumeration_type_di_node(
7378
cx,
@@ -77,7 +82,7 @@ fn build_c_style_enum_di_node<'ll, 'tcx>(
7782
let name = Cow::from(enum_adt_def.variant(variant_index).name.as_str());
7883
(name, discr.val)
7984
}),
80-
enum_adt_def.did(),
85+
enum_adt_def_id,
8186
containing_scope,
8287
),
8388
already_stored_in_typemap: false,
@@ -93,7 +98,7 @@ fn build_enumeration_type_di_node<'ll, 'tcx>(
9398
type_name: &str,
9499
base_type: Ty<'tcx>,
95100
enumerators: impl Iterator<Item = (Cow<'tcx, str>, u128)>,
96-
def_id: rustc_span::def_id::DefId,
101+
def_id: Option<rustc_span::def_id::DefId>,
97102
containing_scope: &'ll DIType,
98103
) -> &'ll DIType {
99104
let is_unsigned = match base_type.kind() {
@@ -117,7 +122,12 @@ fn build_enumeration_type_di_node<'ll, 'tcx>(
117122
})
118123
.collect();
119124

120-
let (file_metadata, line_number) = file_metadata_from_def_id(cx, Some(def_id));
125+
let (file_metadata, line_number) =
126+
if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo {
127+
file_metadata_from_def_id(cx, def_id)
128+
} else {
129+
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
130+
};
121131

122132
unsafe {
123133
llvm::LLVMRustDIBuilderCreateEnumerationType(
@@ -197,6 +207,12 @@ fn build_enum_variant_struct_type_di_node<'ll, 'tcx>(
197207
) -> &'ll DIType {
198208
assert_eq!(variant_layout.ty, enum_type_and_layout.ty);
199209

210+
let def_location = if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo {
211+
Some(file_metadata_from_def_id(cx, Some(variant_def.def_id)))
212+
} else {
213+
None
214+
};
215+
200216
type_map::build_type_with_children(
201217
cx,
202218
type_map::stub(
@@ -208,7 +224,7 @@ fn build_enum_variant_struct_type_di_node<'ll, 'tcx>(
208224
variant_index,
209225
),
210226
variant_def.name.as_str(),
211-
Some(file_metadata_from_def_id(cx, Some(variant_def.def_id))),
227+
def_location,
212228
// NOTE: We use size and align of enum_type, not from variant_layout:
213229
size_and_align_of(enum_type_and_layout),
214230
Some(enum_type_di_node),

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,20 @@ pub(super) fn build_coroutine_di_node<'ll, 'tcx>(
140140

141141
let coroutine_type_name = compute_debuginfo_type_name(cx.tcx, coroutine_type, false);
142142

143+
let def_location = if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo {
144+
Some(file_metadata_from_def_id(cx, Some(coroutine_def_id)))
145+
} else {
146+
None
147+
};
148+
143149
type_map::build_type_with_children(
144150
cx,
145151
type_map::stub(
146152
cx,
147153
Stub::Struct,
148154
unique_type_id,
149155
&coroutine_type_name,
150-
Some(file_metadata_from_def_id(cx, Some(coroutine_def_id))),
156+
def_location,
151157
size_and_align_of(coroutine_type_and_layout),
152158
Some(containing_scope),
153159
DIFlags::FlagZero,
@@ -245,7 +251,12 @@ fn build_enum_variant_part_di_node<'ll, 'tcx>(
245251
let variant_part_unique_type_id =
246252
UniqueTypeId::for_enum_variant_part(cx.tcx, enum_type_and_layout.ty);
247253

248-
let (file_metadata, line_number) = file_metadata_from_def_id(cx, Some(enum_type_def_id));
254+
let (file_metadata, line_number) =
255+
if cx.sess().opts.unstable_opts.more_source_locations_in_debuginfo {
256+
file_metadata_from_def_id(cx, Some(enum_type_def_id))
257+
} else {
258+
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
259+
};
249260

250261
let stub = StubInfo::new(
251262
cx,

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ fn test_unstable_options_tracking_hash() {
709709
untracked!(macro_backtrace, true);
710710
untracked!(meta_stats, true);
711711
untracked!(mir_include_spans, MirIncludeSpans::On);
712+
untracked!(more_source_locations_in_debuginfo, true);
712713
untracked!(nll_facts, true);
713714
untracked!(no_analysis, true);
714715
untracked!(no_leak_check, true);

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,6 +1907,8 @@ options! {
19071907
#[rustc_lint_opt_deny_field_access("use `Session::mir_opt_level` instead of this field")]
19081908
mir_opt_level: Option<usize> = (None, parse_opt_number, [TRACKED],
19091909
"MIR optimization level (0-4; default: 1 in non optimized builds and 2 in optimized builds)"),
1910+
more_source_locations_in_debuginfo: bool = (false, parse_bool, [UNTRACKED],
1911+
"include additional source file and line number information in debuginfo (default: no)"),
19101912
move_size_limit: Option<usize> = (None, parse_opt_number, [TRACKED],
19111913
"the size at which the `large_assignments` lint starts to be emitted"),
19121914
mutable_noalias: bool = (true, parse_bool, [TRACKED],

tests/codegen/issues/issue-98678-async.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
// async functions.
33
//
44
// edition: 2021
5-
// compile-flags: -C debuginfo=2
5+
// compile-flags: -C debuginfo=2 -Z more-source-locations-in-debuginfo=true
66
#![crate_type = "lib"]
77

88
// ignore-tidy-linelength
99

10-
// NONMSVC-DAG: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}/codegen/issue-98678-async.rs{{".*}})
11-
// MSVC: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}\\codegen\\issue-98678-async.rs{{".*}})
10+
// NONMSVC-DAG: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}/issue-98678-async.rs{{".*}})
11+
// MSVC: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}\\issue-98678-async.rs{{".*}})
1212

1313
// NONMSVC-DAG: !DISubprogram(name: "foo",{{.*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]],
1414
// MSVC-DAG: !DISubprogram(name: "foo",{{.*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
15-
pub async fn foo() -> u8 { 5 }
15+
pub async fn foo() -> u8 {
16+
5
17+
}
1618

1719
pub fn bar() -> impl std::future::Future<Output = u8> {
1820
// NONMSVC: !DICompositeType({{.*"}}{async_block_env#0}{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]],
@@ -21,5 +23,4 @@ pub fn bar() -> impl std::future::Future<Output = u8> {
2123
let x: u8 = foo().await;
2224
x + 5
2325
}
24-
2526
}

tests/codegen/issues/issue-98678-closure-generator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// This test verifies the accuracy of emitted file and line debuginfo metadata for closures and
22
// generators.
33
//
4-
// compile-flags: -C debuginfo=2
4+
// compile-flags: -C debuginfo=2 -Z more-source-locations-in-debuginfo
55
#![crate_type = "lib"]
66
#![feature(generators, stmt_expr_attributes)]
77

88
// ignore-tidy-linelength
99

10-
// NONMSVC: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}/codegen/issue-98678-closure-generator.rs{{".*}})
11-
// MSVC: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}\\codegen\\issue-98678-closure-generator.rs{{".*}})
10+
// NONMSVC: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}/issue-98678-closure-generator.rs{{".*}})
11+
// MSVC: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}\\issue-98678-closure-generator.rs{{".*}})
1212

1313
pub fn foo() {
1414
// NONMSVC: !DICompositeType({{.*"}}{closure_env#0}{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]],

0 commit comments

Comments
 (0)