Skip to content

Commit 025a2cd

Browse files
committed
rustdoc: correctly render ret ty of cross-crate async fns
1 parent e4a361a commit 025a2cd

File tree

6 files changed

+72
-44
lines changed

6 files changed

+72
-44
lines changed

src/librustdoc/clean/mod.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -1107,10 +1107,7 @@ fn clean_function<'tcx>(
11071107
clean_args_from_types_and_names(cx, sig.decl.inputs, names)
11081108
}
11091109
};
1110-
let mut decl = clean_fn_decl_with_args(cx, sig.decl, args);
1111-
if sig.header.is_async() {
1112-
decl.output = decl.sugared_async_return_type();
1113-
}
1110+
let decl = clean_fn_decl_with_args(cx, sig.decl, Some(&sig.header), args);
11141111
(generics, decl)
11151112
});
11161113
Box::new(Function { decl, generics })
@@ -1161,12 +1158,16 @@ fn clean_args_from_types_and_body_id<'tcx>(
11611158
fn clean_fn_decl_with_args<'tcx>(
11621159
cx: &mut DocContext<'tcx>,
11631160
decl: &hir::FnDecl<'tcx>,
1161+
header: Option<&hir::FnHeader>,
11641162
args: Arguments,
11651163
) -> FnDecl {
1166-
let output = match decl.output {
1164+
let mut output = match decl.output {
11671165
hir::FnRetTy::Return(typ) => clean_ty(typ, cx),
11681166
hir::FnRetTy::DefaultReturn(..) => Type::Tuple(Vec::new()),
11691167
};
1168+
if let Some(header) = header && header.is_async() {
1169+
output = output.sugared_async_return_type();
1170+
}
11701171
FnDecl { inputs: args, output, c_variadic: decl.c_variadic }
11711172
}
11721173

@@ -1179,7 +1180,11 @@ fn clean_fn_decl_from_did_and_sig<'tcx>(
11791180

11801181
// We assume all empty tuples are default return type. This theoretically can discard `-> ()`,
11811182
// but shouldn't change any code meaning.
1182-
let output = clean_middle_ty(sig.output(), cx, None, None);
1183+
let mut output = clean_middle_ty(sig.output(), cx, None, None);
1184+
1185+
if let Some(did) = did && cx.tcx.asyncness(did).is_async() {
1186+
output = output.sugared_async_return_type();
1187+
}
11831188

11841189
FnDecl {
11851190
output,
@@ -2566,7 +2571,7 @@ fn clean_bare_fn_ty<'tcx>(
25662571
.map(|x| clean_generic_param(cx, None, x))
25672572
.collect();
25682573
let args = clean_args_from_types_and_names(cx, bare_fn.decl.inputs, bare_fn.param_names);
2569-
let decl = clean_fn_decl_with_args(cx, bare_fn.decl, args);
2574+
let decl = clean_fn_decl_with_args(cx, bare_fn.decl, None, args);
25702575
(generic_params, decl)
25712576
});
25722577
BareFunctionDecl { unsafety: bare_fn.unsafety, abi: bare_fn.abi, decl, generic_params }
@@ -3077,7 +3082,7 @@ fn clean_maybe_renamed_foreign_item<'tcx>(
30773082
// NOTE: generics must be cleaned before args
30783083
let generics = clean_generics(generics, cx);
30793084
let args = clean_args_from_types_and_names(cx, decl.inputs, names);
3080-
let decl = clean_fn_decl_with_args(cx, decl, args);
3085+
let decl = clean_fn_decl_with_args(cx, decl, None, args);
30813086
(generics, decl)
30823087
});
30833088
ForeignFunctionItem(Box::new(Function { decl, generics }))

src/librustdoc/clean/types.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -1380,28 +1380,6 @@ impl FnDecl {
13801380
pub(crate) fn self_type(&self) -> Option<SelfTy> {
13811381
self.inputs.values.get(0).and_then(|v| v.to_self())
13821382
}
1383-
1384-
/// Returns the sugared return type for an async function.
1385-
///
1386-
/// For example, if the return type is `impl std::future::Future<Output = i32>`, this function
1387-
/// will return `i32`.
1388-
///
1389-
/// # Panics
1390-
///
1391-
/// This function will panic if the return type does not match the expected sugaring for async
1392-
/// functions.
1393-
pub(crate) fn sugared_async_return_type(&self) -> Type {
1394-
if let Type::ImplTrait(v) = &self.output &&
1395-
let [GenericBound::TraitBound(PolyTrait { trait_, .. }, _ )] = &v[..]
1396-
{
1397-
let bindings = trait_.bindings().unwrap();
1398-
let ret_ty = bindings[0].term();
1399-
let ty = ret_ty.ty().expect("Unexpected constant return term");
1400-
ty.clone()
1401-
} else {
1402-
panic!("unexpected desugaring of async function")
1403-
}
1404-
}
14051383
}
14061384

14071385
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
@@ -1613,6 +1591,28 @@ impl Type {
16131591
}
16141592
}
16151593

1594+
/// Returns the sugared return type for an async function.
1595+
///
1596+
/// For example, if the return type is `impl std::future::Future<Output = i32>`, this function
1597+
/// will return `i32`.
1598+
///
1599+
/// # Panics
1600+
///
1601+
/// This function will panic if the return type does not match the expected sugaring for async
1602+
/// functions.
1603+
pub(crate) fn sugared_async_return_type(&self) -> Type {
1604+
if let Type::ImplTrait(v) = self &&
1605+
let [GenericBound::TraitBound(PolyTrait { trait_, .. }, _ )] = &v[..]
1606+
{
1607+
let bindings = trait_.bindings().unwrap();
1608+
let ret_ty = bindings[0].term();
1609+
let ty = ret_ty.ty().expect("unexpected constant in async fn return term");
1610+
ty.clone()
1611+
} else {
1612+
panic!("unexpected async fn return type")
1613+
}
1614+
}
1615+
16161616
/// Checks if this is a `T::Name` path for an associated type.
16171617
pub(crate) fn is_assoc_ty(&self) -> bool {
16181618
match self {
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Regression test for issue #115760.
2+
// Check that we render the correct return type of free and
3+
// associated async functions reexported from external crates.
4+
5+
// aux-crate:async_fn=async-fn.rs
6+
// edition: 2021
7+
#![crate_name = "user"]
8+
9+
// @has user/fn.load.html
10+
// @has - '//pre[@class="rust item-decl"]' "pub async fn load() -> i32"
11+
pub use async_fn::load;
12+
13+
// @has user/trait.Load.html
14+
// @has - '//*[@id="tymethod.run"]' 'async fn run(&self) -> i32'
15+
pub use async_fn::Load;
16+
17+
// @has user/struct.Loader.html
18+
// @has - '//*[@id="method.run"]' 'async fn run(&self) -> i32'
19+
pub use async_fn::Loader;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(async_fn_in_trait)]
2+
// edition: 2021
3+
4+
pub async fn load() -> i32 {
5+
0
6+
}
7+
8+
pub trait Load {
9+
async fn run(&self) -> i32;
10+
}
11+
12+
pub struct Loader;
13+
14+
impl Load for Loader {
15+
async fn run(&self) -> i32 {
16+
1
17+
}
18+
}

tests/rustdoc/inline_cross/auxiliary/impl_trait_aux.rs

-6
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,3 @@ pub struct Foo;
3333
impl Foo {
3434
pub fn method<'a>(_x: impl Clone + Into<Vec<u8>> + 'a) {}
3535
}
36-
37-
pub struct Bar;
38-
39-
impl Bar {
40-
pub async fn async_foo(&self) {}
41-
}

tests/rustdoc/inline_cross/impl_trait.rs

-8
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,7 @@ pub use impl_trait_aux::func4;
3333
// @!has - '//pre[@class="rust item-decl"]' 'where'
3434
pub use impl_trait_aux::func5;
3535

36-
// @has impl_trait/fn.async_fn.html
37-
// @has - '//pre[@class="rust item-decl"]' "pub async fn async_fn()"
38-
pub use impl_trait_aux::async_fn;
39-
4036
// @has impl_trait/struct.Foo.html
4137
// @has - '//*[@id="method.method"]//h4[@class="code-header"]' "pub fn method<'a>(_x: impl Clone + Into<Vec<u8, Global>> + 'a)"
4238
// @!has - '//*[@id="method.method"]//h4[@class="code-header"]' 'where'
4339
pub use impl_trait_aux::Foo;
44-
45-
// @has impl_trait/struct.Bar.html
46-
// @has - '//*[@id="method.async_foo"]' "pub async fn async_foo("
47-
pub use impl_trait_aux::Bar;

0 commit comments

Comments
 (0)