Skip to content

Commit 6099d17

Browse files
committedAug 30, 2022
rustdoc: Resugar async fn return type in clean, not html
This way it also happens for json output. Fixes #101199
1 parent 0631ea5 commit 6099d17

File tree

5 files changed

+46
-16
lines changed

5 files changed

+46
-16
lines changed
 

‎src/librustdoc/clean/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,10 @@ fn clean_function<'tcx>(
886886
// NOTE: generics must be cleaned before args
887887
let generics = clean_generics(generics, cx);
888888
let args = clean_args_from_types_and_body_id(cx, sig.decl.inputs, body_id);
889-
let decl = clean_fn_decl_with_args(cx, sig.decl, args);
889+
let mut decl = clean_fn_decl_with_args(cx, sig.decl, args);
890+
if sig.header.is_async() {
891+
decl.output = decl.sugared_async_return_type();
892+
}
890893
(generics, decl)
891894
});
892895
Box::new(Function { decl, generics })

‎src/librustdoc/html/format.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -1310,22 +1310,19 @@ impl clean::FnDecl {
13101310
/// <br>Used to determine line-wrapping.
13111311
/// * `indent`: The number of spaces to indent each successive line with, if line-wrapping is
13121312
/// necessary.
1313-
/// * `asyncness`: Whether the function is async or not.
13141313
pub(crate) fn full_print<'a, 'tcx: 'a>(
13151314
&'a self,
13161315
header_len: usize,
13171316
indent: usize,
1318-
asyncness: hir::IsAsync,
13191317
cx: &'a Context<'tcx>,
13201318
) -> impl fmt::Display + 'a + Captures<'tcx> {
1321-
display_fn(move |f| self.inner_full_print(header_len, indent, asyncness, f, cx))
1319+
display_fn(move |f| self.inner_full_print(header_len, indent, f, cx))
13221320
}
13231321

13241322
fn inner_full_print(
13251323
&self,
13261324
header_len: usize,
13271325
indent: usize,
1328-
asyncness: hir::IsAsync,
13291326
f: &mut fmt::Formatter<'_>,
13301327
cx: &Context<'_>,
13311328
) -> fmt::Result {
@@ -1390,15 +1387,9 @@ impl clean::FnDecl {
13901387
args_plain.push_str(", ...");
13911388
}
13921389

1393-
let arrow_plain;
1394-
let arrow = if let hir::IsAsync::Async = asyncness {
1395-
let output = self.sugared_async_return_type();
1396-
arrow_plain = format!("{:#}", output.print(cx));
1397-
if f.alternate() { arrow_plain.clone() } else { format!("{}", output.print(cx)) }
1398-
} else {
1399-
arrow_plain = format!("{:#}", self.output.print(cx));
1400-
if f.alternate() { arrow_plain.clone() } else { format!("{}", self.output.print(cx)) }
1401-
};
1390+
let arrow_plain = format!("{:#}", self.output.print(cx));
1391+
let arrow =
1392+
if f.alternate() { arrow_plain.clone() } else { format!("{}", self.output.print(cx)) };
14021393

14031394
let declaration_len = header_len + args_plain.len() + arrow_plain.len();
14041395
let output = if declaration_len > 80 {

‎src/librustdoc/html/render/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ fn assoc_method(
821821
href = href,
822822
name = name,
823823
generics = g.print(cx),
824-
decl = d.full_print(header_len, indent, header.asyncness, cx),
824+
decl = d.full_print(header_len, indent, cx),
825825
notable_traits = notable_traits_decl(d, cx),
826826
where_clause = print_where_clause(g, cx, indent, end_newline),
827827
)

‎src/librustdoc/html/render/print_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
530530
name = name,
531531
generics = f.generics.print(cx),
532532
where_clause = print_where_clause(&f.generics, cx, 0, Ending::Newline),
533-
decl = f.decl.full_print(header_len, 0, header.asyncness, cx),
533+
decl = f.decl.full_print(header_len, 0, cx),
534534
notable_traits = notable_traits_decl(&f.decl, cx),
535535
);
536536
});
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// edition:2021
2+
// ignore-tidy-linelength
3+
4+
// Regression test for <https://github.com/rust-lang/rust/issues/101199>
5+
6+
use std::future::Future;
7+
8+
// @is "$.index[*][?(@.name=='get_int')].inner.decl.output" '{"inner": "i32", "kind": "primitive"}'
9+
// @is "$.index[*][?(@.name=='get_int')].inner.header.async" false
10+
pub fn get_int() -> i32 {
11+
42
12+
}
13+
14+
// @is "$.index[*][?(@.name=='get_int_async')].inner.decl.output" '{"inner": "i32", "kind": "primitive"}'
15+
// @is "$.index[*][?(@.name=='get_int_async')].inner.header.async" true
16+
pub async fn get_int_async() -> i32 {
17+
42
18+
}
19+
20+
// @is "$.index[*][?(@.name=='get_int_future')].inner.decl.output.kind" '"impl_trait"'
21+
// @is "$.index[*][?(@.name=='get_int_future')].inner.decl.output.inner[0].trait_bound.trait.name" '"Future"'
22+
// @is "$.index[*][?(@.name=='get_int_future')].inner.decl.output.inner[0].trait_bound.trait.args.angle_bracketed.bindings[0].name" '"Output"'
23+
// @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"}'
24+
// @is "$.index[*][?(@.name=='get_int_future')].inner.header.async" false
25+
pub fn get_int_future() -> impl Future<Output = i32> {
26+
async { 42 }
27+
}
28+
29+
// @is "$.index[*][?(@.name=='get_int_future_async')].inner.decl.output.kind" '"impl_trait"'
30+
// @is "$.index[*][?(@.name=='get_int_future_async')].inner.decl.output.inner[0].trait_bound.trait.name" '"Future"'
31+
// @is "$.index[*][?(@.name=='get_int_future_async')].inner.decl.output.inner[0].trait_bound.trait.args.angle_bracketed.bindings[0].name" '"Output"'
32+
// @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"}'
33+
// @is "$.index[*][?(@.name=='get_int_future_async')].inner.header.async" true
34+
pub async fn get_int_future_async() -> impl Future<Output = i32> {
35+
async { 42 }
36+
}

0 commit comments

Comments
 (0)
Please sign in to comment.