diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index c425de081a6a6..41f9eb3cdf6bd 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -886,7 +886,10 @@ fn clean_function<'tcx>( // NOTE: generics must be cleaned before args let generics = clean_generics(generics, cx); let args = clean_args_from_types_and_body_id(cx, sig.decl.inputs, body_id); - let decl = clean_fn_decl_with_args(cx, sig.decl, args); + let mut decl = clean_fn_decl_with_args(cx, sig.decl, args); + if sig.header.is_async() { + decl.output = decl.sugared_async_return_type(); + } (generics, decl) }); Box::new(Function { decl, generics }) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index b023792e95a58..6f49f00f93e5e 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1310,22 +1310,19 @@ impl clean::FnDecl { ///
Used to determine line-wrapping. /// * `indent`: The number of spaces to indent each successive line with, if line-wrapping is /// necessary. - /// * `asyncness`: Whether the function is async or not. pub(crate) fn full_print<'a, 'tcx: 'a>( &'a self, header_len: usize, indent: usize, - asyncness: hir::IsAsync, cx: &'a Context<'tcx>, ) -> impl fmt::Display + 'a + Captures<'tcx> { - display_fn(move |f| self.inner_full_print(header_len, indent, asyncness, f, cx)) + display_fn(move |f| self.inner_full_print(header_len, indent, f, cx)) } fn inner_full_print( &self, header_len: usize, indent: usize, - asyncness: hir::IsAsync, f: &mut fmt::Formatter<'_>, cx: &Context<'_>, ) -> fmt::Result { @@ -1390,15 +1387,9 @@ impl clean::FnDecl { args_plain.push_str(", ..."); } - let arrow_plain; - let arrow = if let hir::IsAsync::Async = asyncness { - let output = self.sugared_async_return_type(); - arrow_plain = format!("{:#}", output.print(cx)); - if f.alternate() { arrow_plain.clone() } else { format!("{}", output.print(cx)) } - } else { - arrow_plain = format!("{:#}", self.output.print(cx)); - if f.alternate() { arrow_plain.clone() } else { format!("{}", self.output.print(cx)) } - }; + let arrow_plain = format!("{:#}", self.output.print(cx)); + let arrow = + if f.alternate() { arrow_plain.clone() } else { format!("{}", self.output.print(cx)) }; let declaration_len = header_len + args_plain.len() + arrow_plain.len(); let output = if declaration_len > 80 { diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 6272f47f460ca..bc3d2d93da9bf 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -821,7 +821,7 @@ fn assoc_method( href = href, name = name, generics = g.print(cx), - decl = d.full_print(header_len, indent, header.asyncness, cx), + decl = d.full_print(header_len, indent, cx), notable_traits = notable_traits_decl(d, cx), where_clause = print_where_clause(g, cx, indent, end_newline), ) diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 710064a37d041..af8e27ce1a10f 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -530,7 +530,7 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle name = name, generics = f.generics.print(cx), where_clause = print_where_clause(&f.generics, cx, 0, Ending::Newline), - decl = f.decl.full_print(header_len, 0, header.asyncness, cx), + decl = f.decl.full_print(header_len, 0, cx), notable_traits = notable_traits_decl(&f.decl, cx), ); }); diff --git a/src/test/rustdoc-json/fns/async_return.rs b/src/test/rustdoc-json/fns/async_return.rs new file mode 100644 index 0000000000000..b89781ca92da6 --- /dev/null +++ b/src/test/rustdoc-json/fns/async_return.rs @@ -0,0 +1,36 @@ +// edition:2021 +// ignore-tidy-linelength + +// Regression test for + +use std::future::Future; + +// @is "$.index[*][?(@.name=='get_int')].inner.decl.output" '{"inner": "i32", "kind": "primitive"}' +// @is "$.index[*][?(@.name=='get_int')].inner.header.async" false +pub fn get_int() -> i32 { + 42 +} + +// @is "$.index[*][?(@.name=='get_int_async')].inner.decl.output" '{"inner": "i32", "kind": "primitive"}' +// @is "$.index[*][?(@.name=='get_int_async')].inner.header.async" true +pub async fn get_int_async() -> i32 { + 42 +} + +// @is "$.index[*][?(@.name=='get_int_future')].inner.decl.output.kind" '"impl_trait"' +// @is "$.index[*][?(@.name=='get_int_future')].inner.decl.output.inner[0].trait_bound.trait.name" '"Future"' +// @is "$.index[*][?(@.name=='get_int_future')].inner.decl.output.inner[0].trait_bound.trait.args.angle_bracketed.bindings[0].name" '"Output"' +// @is "$.index[*][?(@.name=='get_int_future')].inner.decl.output.inner[0].trait_bound.trait.args.angle_bracketed.bindings[0].binding.equality.type" '{"inner": "i32", "kind": "primitive"}' +// @is "$.index[*][?(@.name=='get_int_future')].inner.header.async" false +pub fn get_int_future() -> impl Future { + async { 42 } +} + +// @is "$.index[*][?(@.name=='get_int_future_async')].inner.decl.output.kind" '"impl_trait"' +// @is "$.index[*][?(@.name=='get_int_future_async')].inner.decl.output.inner[0].trait_bound.trait.name" '"Future"' +// @is "$.index[*][?(@.name=='get_int_future_async')].inner.decl.output.inner[0].trait_bound.trait.args.angle_bracketed.bindings[0].name" '"Output"' +// @is "$.index[*][?(@.name=='get_int_future_async')].inner.decl.output.inner[0].trait_bound.trait.args.angle_bracketed.bindings[0].binding.equality.type" '{"inner": "i32", "kind": "primitive"}' +// @is "$.index[*][?(@.name=='get_int_future_async')].inner.header.async" true +pub async fn get_int_future_async() -> impl Future { + async { 42 } +}