Skip to content

Commit

Permalink
rustdoc: correctly render ret ty of cross-crate async fns
Browse files Browse the repository at this point in the history
  • Loading branch information
fmease committed Sep 23, 2023
1 parent e4a361a commit 386f401
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 38 deletions.
8 changes: 6 additions & 2 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ fn clean_function<'tcx>(
};
let mut decl = clean_fn_decl_with_args(cx, sig.decl, args);
if sig.header.is_async() {
decl.output = decl.sugared_async_return_type();
decl.output = decl.output.sugared_async_return_type();
}
(generics, decl)
});
Expand Down Expand Up @@ -1179,7 +1179,11 @@ fn clean_fn_decl_from_did_and_sig<'tcx>(

// We assume all empty tuples are default return type. This theoretically can discard `-> ()`,
// but shouldn't change any code meaning.
let output = clean_middle_ty(sig.output(), cx, None, None);
let mut output = clean_middle_ty(sig.output(), cx, None, None);

if let Some(did) = did && cx.tcx.asyncness(did).is_async() {
output = output.sugared_async_return_type();
}

FnDecl {
output,
Expand Down
44 changes: 22 additions & 22 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1380,28 +1380,6 @@ impl FnDecl {
pub(crate) fn self_type(&self) -> Option<SelfTy> {
self.inputs.values.get(0).and_then(|v| v.to_self())
}

/// Returns the sugared return type for an async function.
///
/// For example, if the return type is `impl std::future::Future<Output = i32>`, this function
/// will return `i32`.
///
/// # Panics
///
/// This function will panic if the return type does not match the expected sugaring for async
/// functions.
pub(crate) fn sugared_async_return_type(&self) -> Type {
if let Type::ImplTrait(v) = &self.output &&
let [GenericBound::TraitBound(PolyTrait { trait_, .. }, _ )] = &v[..]
{
let bindings = trait_.bindings().unwrap();
let ret_ty = bindings[0].term();
let ty = ret_ty.ty().expect("Unexpected constant return term");
ty.clone()
} else {
panic!("unexpected desugaring of async function")
}
}
}

#[derive(Clone, PartialEq, Eq, Debug, Hash)]
Expand Down Expand Up @@ -1613,6 +1591,28 @@ impl Type {
}
}

/// Returns the sugared return type for an async function.
///
/// For example, if the return type is `impl std::future::Future<Output = i32>`, this function
/// will return `i32`.
///
/// # Panics
///
/// This function will panic if the return type does not match the expected sugaring for async
/// functions.
pub(crate) fn sugared_async_return_type(&self) -> Type {
if let Type::ImplTrait(v) = self &&
let [GenericBound::TraitBound(PolyTrait { trait_, .. }, _ )] = &v[..]
{
let bindings = trait_.bindings().unwrap();
let ret_ty = bindings[0].term();
let ty = ret_ty.ty().expect("unexpected constant in async fn return term");
ty.clone()
} else {
panic!("unexpected async fn return type")
}
}

/// Checks if this is a `T::Name` path for an associated type.
pub(crate) fn is_assoc_ty(&self) -> bool {
match self {
Expand Down
19 changes: 19 additions & 0 deletions tests/rustdoc/inline_cross/async-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Regression test for issue #115760.
// Check that we render the correct return type of free and
// associated async functions reexported from external crates.

// aux-crate:async_fn=async-fn.rs
// edition: 2021
#![crate_name = "user"]

// @has user/fn.load.html
// @has - '//pre[@class="rust item-decl"]' "pub async fn load() -> i32"
pub use async_fn::load;

// @has user/trait.Load.html
// @has - '//*[@id="tymethod.run"]' 'async fn run(&self) -> i32'
pub use async_fn::Load;

// @has user/struct.Loader.html
// @has - '//*[@id="method.run"]' 'async fn run(&self) -> i32'
pub use async_fn::Loader;
18 changes: 18 additions & 0 deletions tests/rustdoc/inline_cross/auxiliary/async-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![feature(async_fn_in_trait)]
// edition: 2021

pub async fn load() -> i32 {
0
}

pub trait Load {
async fn run(&self) -> i32;
}

pub struct Loader;

impl Load for Loader {
async fn run(&self) -> i32 {
1
}
}
6 changes: 0 additions & 6 deletions tests/rustdoc/inline_cross/auxiliary/impl_trait_aux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,3 @@ pub struct Foo;
impl Foo {
pub fn method<'a>(_x: impl Clone + Into<Vec<u8>> + 'a) {}
}

pub struct Bar;

impl Bar {
pub async fn async_foo(&self) {}
}
8 changes: 0 additions & 8 deletions tests/rustdoc/inline_cross/impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,7 @@ pub use impl_trait_aux::func4;
// @!has - '//pre[@class="rust item-decl"]' 'where'
pub use impl_trait_aux::func5;

// @has impl_trait/fn.async_fn.html
// @has - '//pre[@class="rust item-decl"]' "pub async fn async_fn()"
pub use impl_trait_aux::async_fn;

// @has impl_trait/struct.Foo.html
// @has - '//*[@id="method.method"]//h4[@class="code-header"]' "pub fn method<'a>(_x: impl Clone + Into<Vec<u8, Global>> + 'a)"
// @!has - '//*[@id="method.method"]//h4[@class="code-header"]' 'where'
pub use impl_trait_aux::Foo;

// @has impl_trait/struct.Bar.html
// @has - '//*[@id="method.async_foo"]' "pub async fn async_foo("
pub use impl_trait_aux::Bar;

0 comments on commit 386f401

Please sign in to comment.