Skip to content

Commit d424dc3

Browse files
authored
Unrolled build for #145623
Rollup merge of #145623 - compiler-errors:pretty-async-name, r=wesleywiser Pretty print the name of an future from calling async closure Fixes #145606 by introducing a way to customize the path rendering of async closures' futures in the pretty printer API.
2 parents f605b57 + ab6f4d6 commit d424dc3

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

compiler/rustc_const_eval/src/util/type_name.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt::Write;
22

33
use rustc_data_structures::intern::Interned;
4-
use rustc_hir::def_id::CrateNum;
4+
use rustc_hir::def_id::{CrateNum, DefId};
55
use rustc_hir::definitions::DisambiguatedDefPathData;
66
use rustc_middle::bug;
77
use rustc_middle::ty::print::{PrettyPrinter, PrintError, Printer};
@@ -132,6 +132,35 @@ impl<'tcx> Printer<'tcx> for TypeNamePrinter<'tcx> {
132132
Ok(())
133133
}
134134
}
135+
136+
fn print_coroutine_with_kind(
137+
&mut self,
138+
def_id: DefId,
139+
parent_args: &'tcx [GenericArg<'tcx>],
140+
kind: Ty<'tcx>,
141+
) -> Result<(), PrintError> {
142+
self.print_def_path(def_id, parent_args)?;
143+
144+
let ty::Coroutine(_, args) = self.tcx.type_of(def_id).instantiate_identity().kind() else {
145+
// Could be `ty::Error`.
146+
return Ok(());
147+
};
148+
149+
let default_kind = args.as_coroutine().kind_ty();
150+
151+
match kind.to_opt_closure_kind() {
152+
_ if kind == default_kind => {
153+
// No need to mark the closure if it's the deduced coroutine kind.
154+
}
155+
Some(ty::ClosureKind::Fn) | None => {
156+
// Should never happen. Just don't mark anything rather than panicking.
157+
}
158+
Some(ty::ClosureKind::FnMut) => self.path.push_str("::{{call_mut}}"),
159+
Some(ty::ClosureKind::FnOnce) => self.path.push_str("::{{call_once}}"),
160+
}
161+
162+
Ok(())
163+
}
135164
}
136165

137166
impl<'tcx> PrettyPrinter<'tcx> for TypeNamePrinter<'tcx> {

compiler/rustc_middle/src/ty/print/mod.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ pub trait Printer<'tcx>: Sized {
124124
trait_ref: Option<ty::TraitRef<'tcx>>,
125125
) -> Result<(), PrintError>;
126126

127+
fn print_coroutine_with_kind(
128+
&mut self,
129+
def_id: DefId,
130+
parent_args: &'tcx [GenericArg<'tcx>],
131+
kind: Ty<'tcx>,
132+
) -> Result<(), PrintError> {
133+
self.print_path_with_generic_args(|p| p.print_def_path(def_id, parent_args), &[kind.into()])
134+
}
135+
127136
// Defaults (should not be overridden):
128137

129138
#[instrument(skip(self), level = "debug")]
@@ -162,9 +171,10 @@ pub trait Printer<'tcx>: Sized {
162171
)) = self.tcx().coroutine_kind(def_id)
163172
&& args.len() > parent_args.len()
164173
{
165-
return self.print_path_with_generic_args(
166-
|p| p.print_def_path(def_id, parent_args),
167-
&args[..parent_args.len() + 1][..1],
174+
return self.print_coroutine_with_kind(
175+
def_id,
176+
parent_args,
177+
args[parent_args.len()].expect_ty(),
168178
);
169179
} else {
170180
// Closures' own generics are only captures, don't print them.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ run-pass
2+
//@ edition: 2024
3+
4+
fn once<F: FnOnce() -> T, T>(f: F) -> T {
5+
f()
6+
}
7+
8+
fn main() {
9+
let closure = async || {};
10+
11+
// Name of future when called normally.
12+
let name = std::any::type_name_of_val(&closure());
13+
assert_eq!(name, "type_name::main::{{closure}}::{{closure}}");
14+
15+
// Name of future when closure is called via its FnOnce shim.
16+
let name = std::any::type_name_of_val(&once(closure));
17+
assert_eq!(name, "type_name::main::{{closure}}::{{closure}}::{{call_once}}");
18+
}

0 commit comments

Comments
 (0)