Skip to content

Commit

Permalink
Auto merge of #116195 - fmease:rustdoc-investigate-perf-regression, r…
Browse files Browse the repository at this point in the history
…=<try>

[perf] rustdoc: investigate recent perf regression

Investigate perf regression caused by #116084.

r? `@ghost`
  • Loading branch information
bors committed Sep 27, 2023
2 parents 085acd0 + 2b7f06f commit 935f41f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 72 deletions.
21 changes: 8 additions & 13 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,10 @@ fn clean_function<'tcx>(
clean_args_from_types_and_names(cx, sig.decl.inputs, names)
}
};
let decl = clean_fn_decl_with_args(cx, sig.decl, Some(&sig.header), 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 })
Expand Down Expand Up @@ -1159,16 +1162,12 @@ fn clean_args_from_types_and_body_id<'tcx>(
fn clean_fn_decl_with_args<'tcx>(
cx: &mut DocContext<'tcx>,
decl: &hir::FnDecl<'tcx>,
header: Option<&hir::FnHeader>,
args: Arguments,
) -> FnDecl {
let mut output = match decl.output {
let output = match decl.output {
hir::FnRetTy::Return(typ) => clean_ty(typ, cx),
hir::FnRetTy::DefaultReturn(..) => Type::Tuple(Vec::new()),
};
if let Some(header) = header && header.is_async() {
output = output.sugared_async_return_type();
}
FnDecl { inputs: args, output, c_variadic: decl.c_variadic }
}

Expand All @@ -1181,11 +1180,7 @@ 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 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();
}
let output = clean_middle_ty(sig.output(), cx, None, None);

FnDecl {
output,
Expand Down Expand Up @@ -2571,7 +2566,7 @@ fn clean_bare_fn_ty<'tcx>(
.map(|x| clean_generic_param(cx, None, x))
.collect();
let args = clean_args_from_types_and_names(cx, bare_fn.decl.inputs, bare_fn.param_names);
let decl = clean_fn_decl_with_args(cx, bare_fn.decl, None, args);
let decl = clean_fn_decl_with_args(cx, bare_fn.decl, args);
(generic_params, decl)
});
BareFunctionDecl { unsafety: bare_fn.unsafety, abi: bare_fn.abi, decl, generic_params }
Expand Down Expand Up @@ -3082,7 +3077,7 @@ fn clean_maybe_renamed_foreign_item<'tcx>(
// NOTE: generics must be cleaned before args
let generics = clean_generics(generics, cx);
let args = clean_args_from_types_and_names(cx, decl.inputs, names);
let decl = clean_fn_decl_with_args(cx, decl, None, args);
let decl = clean_fn_decl_with_args(cx, decl, args);
(generics, decl)
});
ForeignFunctionItem(Box::new(Function { decl, generics }))
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 @@ -1384,6 +1384,28 @@ 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 @@ -1595,28 +1617,6 @@ 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: 0 additions & 19 deletions tests/rustdoc/inline_cross/async-fn.rs

This file was deleted.

18 changes: 0 additions & 18 deletions tests/rustdoc/inline_cross/auxiliary/async-fn.rs

This file was deleted.

6 changes: 6 additions & 0 deletions tests/rustdoc/inline_cross/auxiliary/impl_trait_aux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ 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: 8 additions & 0 deletions tests/rustdoc/inline_cross/impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ 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 935f41f

Please sign in to comment.