Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion compiler/rustc_const_eval/src/util/type_name.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Write;

use rustc_data_structures::intern::Interned;
use rustc_hir::def_id::CrateNum;
use rustc_hir::def_id::{CrateNum, DefId};
use rustc_hir::definitions::DisambiguatedDefPathData;
use rustc_middle::bug;
use rustc_middle::ty::print::{PrettyPrinter, PrintError, Printer};
Expand Down Expand Up @@ -132,6 +132,35 @@ impl<'tcx> Printer<'tcx> for TypeNamePrinter<'tcx> {
Ok(())
}
}

fn print_coroutine_with_kind(
&mut self,
def_id: DefId,
parent_args: &'tcx [GenericArg<'tcx>],
kind: Ty<'tcx>,
) -> Result<(), PrintError> {
self.print_def_path(def_id, parent_args)?;

let ty::Coroutine(_, args) = self.tcx.type_of(def_id).instantiate_identity().kind() else {
// Could be `ty::Error`.
return Ok(());
};

let default_kind = args.as_coroutine().kind_ty();

match kind.to_opt_closure_kind() {
_ if kind == default_kind => {
// No need to mark the closure if it's the deduced coroutine kind.
}
Some(ty::ClosureKind::Fn) | None => {
// Should never happen. Just don't mark anything rather than panicking.
}
Some(ty::ClosureKind::FnMut) => self.path.push_str("::{{call_mut}}"),
Some(ty::ClosureKind::FnOnce) => self.path.push_str("::{{call_once}}"),
}

Ok(())
}
}

impl<'tcx> PrettyPrinter<'tcx> for TypeNamePrinter<'tcx> {
Expand Down
16 changes: 13 additions & 3 deletions compiler/rustc_middle/src/ty/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ pub trait Printer<'tcx>: Sized {
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<(), PrintError>;

fn print_coroutine_with_kind(
&mut self,
def_id: DefId,
parent_args: &'tcx [GenericArg<'tcx>],
kind: Ty<'tcx>,
) -> Result<(), PrintError> {
self.print_path_with_generic_args(|p| p.print_def_path(def_id, parent_args), &[kind.into()])
}

// Defaults (should not be overridden):

#[instrument(skip(self), level = "debug")]
Expand Down Expand Up @@ -162,9 +171,10 @@ pub trait Printer<'tcx>: Sized {
)) = self.tcx().coroutine_kind(def_id)
&& args.len() > parent_args.len()
{
return self.print_path_with_generic_args(
|p| p.print_def_path(def_id, parent_args),
&args[..parent_args.len() + 1][..1],
return self.print_coroutine_with_kind(
def_id,
parent_args,
args[parent_args.len()].expect_ty(),
);
} else {
// Closures' own generics are only captures, don't print them.
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/async-await/async-closures/type-name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ run-pass
//@ edition: 2024

fn once<F: FnOnce() -> T, T>(f: F) -> T {
f()
}

fn main() {
let closure = async || {};

// Name of future when called normally.
let name = std::any::type_name_of_val(&closure());
assert_eq!(name, "type_name::main::{{closure}}::{{closure}}");

// Name of future when closure is called via its FnOnce shim.
let name = std::any::type_name_of_val(&once(closure));
assert_eq!(name, "type_name::main::{{closure}}::{{closure}}::{{call_once}}");
}
Loading