Skip to content

Commit 05e1f0d

Browse files
committed
Generate more precise generator names
Currently all generators are named with a `generator$N` suffix, regardless of where they come from. This means an `async fn` shows up as a generator in stack traces, which can be surprising to async programmers since they should not need to know that async functions are implementated using generators. This change generators a different name depending on the generator kind, allowing us to tell whether the generator is the result of an async block, an async closure, an async fn, or a plain generator.
1 parent 22e491a commit 05e1f0d

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -519,12 +519,18 @@ fn push_unqualified_item_name(
519519
output.push_str(tcx.crate_name(def_id.krate).as_str());
520520
}
521521
DefPathData::ClosureExpr if tcx.generator_kind(def_id).is_some() => {
522+
let key = match tcx.generator_kind(def_id).unwrap() {
523+
hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Block) => "async_block",
524+
hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Closure) => "async_closure",
525+
hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Fn) => "async_fn",
526+
hir::GeneratorKind::Gen => "generator",
527+
};
522528
// Generators look like closures, but we want to treat them differently
523529
// in the debug info.
524530
if cpp_like_debuginfo(tcx) {
525-
write!(output, "generator${}", disambiguated_data.disambiguator).unwrap();
531+
write!(output, "{}${}", key, disambiguated_data.disambiguator).unwrap();
526532
} else {
527-
write!(output, "{{generator#{}}}", disambiguated_data.disambiguator).unwrap();
533+
write!(output, "{{{}#{}}}", key, disambiguated_data.disambiguator).unwrap();
528534
}
529535
}
530536
_ => match disambiguated_data.data.name() {

src/test/codegen/async-fn-debug-msvc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async fn async_fn_test() {
1717
// FIXME: No way to reliably check the filename.
1818

1919
// CHECK-DAG: [[ASYNC_FN:!.*]] = !DINamespace(name: "async_fn_test"
20-
// CHECK-DAG: [[GEN:!.*]] = !DICompositeType(tag: DW_TAG_union_type, name: "generator$0"
20+
// CHECK-DAG: [[GEN:!.*]] = !DICompositeType(tag: DW_TAG_union_type, name: "async_fn$0"
2121
// CHECK: {{!.*}} = !DIDerivedType(tag: DW_TAG_member, name: "variant0", scope: [[GEN]],
2222
// For brevity, we only check the struct name and members of the last variant.
2323
// CHECK-SAME: file: [[FILE:![0-9]*]], line: 11,

0 commit comments

Comments
 (0)