Skip to content

Commit 6adc2c1

Browse files
committed
Deduplicate template parameter creation
1 parent b4acf7a commit 6adc2c1

File tree

3 files changed

+26
-45
lines changed

3 files changed

+26
-45
lines changed

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+23-13
Original file line numberDiff line numberDiff line change
@@ -1290,21 +1290,31 @@ fn build_generic_type_param_di_nodes<'ll, 'tcx>(
12901290
ty: Ty<'tcx>,
12911291
) -> SmallVec<Option<&'ll DIType>> {
12921292
if let ty::Adt(def, args) = *ty.kind() {
1293-
if args.types().next().is_some() {
1294-
let generics = cx.tcx.generics_of(def.did());
1295-
let names = get_parameter_names(cx, generics);
1296-
let template_params: SmallVec<_> = iter::zip(args, names)
1297-
.filter_map(|(kind, name)| {
1298-
kind.as_type().map(|ty| {
1299-
let actual_type = cx.tcx.normalize_erasing_regions(cx.typing_env(), ty);
1300-
let actual_type_di_node = type_di_node(cx, actual_type);
1301-
Some(cx.create_template_type_parameter(name.as_str(), actual_type_di_node))
1302-
})
1293+
let generics = cx.tcx.generics_of(def.did());
1294+
return get_template_parameters(cx, generics, args);
1295+
}
1296+
1297+
return smallvec![];
1298+
}
1299+
1300+
pub(super) fn get_template_parameters<'ll, 'tcx>(
1301+
cx: &CodegenCx<'ll, 'tcx>,
1302+
generics: &ty::Generics,
1303+
args: ty::GenericArgsRef<'tcx>,
1304+
) -> SmallVec<Option<&'ll DIType>> {
1305+
if args.types().next().is_some() {
1306+
let names = get_parameter_names(cx, generics);
1307+
let template_params: SmallVec<_> = iter::zip(args, names)
1308+
.filter_map(|(kind, name)| {
1309+
kind.as_type().map(|ty| {
1310+
let actual_type = cx.tcx.normalize_erasing_regions(cx.typing_env(), ty);
1311+
let actual_type_di_node = type_di_node(cx, actual_type);
1312+
Some(cx.create_template_type_parameter(name.as_str(), actual_type_di_node))
13031313
})
1304-
.collect();
1314+
})
1315+
.collect();
13051316

1306-
return template_params;
1307-
}
1317+
return template_params;
13081318
}
13091319

13101320
return smallvec![];

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ fn build_coroutine_variant_struct_type_di_node<'ll, 'tcx>(
363363

364364
state_specific_fields.into_iter().chain(common_fields).collect()
365365
},
366+
// FIXME: this is a no-op. `build_generic_type_param_di_nodes` only works for Adts.
366367
|cx| build_generic_type_param_di_nodes(cx, coroutine_type_and_layout.ty),
367368
)
368369
.di_node

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+2-32
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use std::cell::{OnceCell, RefCell};
44
use std::ops::Range;
5+
use std::ptr;
56
use std::sync::Arc;
6-
use std::{iter, ptr};
77

88
use libc::c_uint;
99
use rustc_abi::Size;
@@ -487,40 +487,10 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
487487
generics: &ty::Generics,
488488
args: GenericArgsRef<'tcx>,
489489
) -> &'ll DIArray {
490-
if args.types().next().is_none() {
491-
return create_DIArray(DIB(cx), &[]);
492-
}
493-
494-
// Again, only create type information if full debuginfo is enabled
495-
let template_params: Vec<_> = if cx.sess().opts.debuginfo == DebugInfo::Full {
496-
let names = get_parameter_names(cx, generics);
497-
iter::zip(args, names)
498-
.filter_map(|(kind, name)| {
499-
kind.as_type().map(|ty| {
500-
let actual_type = cx.tcx.normalize_erasing_regions(cx.typing_env(), ty);
501-
let actual_type_metadata = type_di_node(cx, actual_type);
502-
Some(cx.create_template_type_parameter(
503-
name.as_str(),
504-
actual_type_metadata,
505-
))
506-
})
507-
})
508-
.collect()
509-
} else {
510-
vec![]
511-
};
512-
490+
let template_params = metadata::get_template_parameters(cx, generics, args);
513491
create_DIArray(DIB(cx), &template_params)
514492
}
515493

516-
fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
517-
let mut names = generics.parent.map_or_else(Vec::new, |def_id| {
518-
get_parameter_names(cx, cx.tcx.generics_of(def_id))
519-
});
520-
names.extend(generics.own_params.iter().map(|param| param.name));
521-
names
522-
}
523-
524494
/// Returns a scope, plus `true` if that's a type scope for "class" methods,
525495
/// otherwise `false` for plain namespace scopes.
526496
fn get_containing_scope<'ll, 'tcx>(

0 commit comments

Comments
 (0)