Skip to content

Commit 6e582ca

Browse files
authored
Rollup merge of #103892 - compiler-errors:afit-rustdoc, r=GuillaumeGomez
Properly render asyncness for trait fns without default body We weren't properly desugaring async fns in traits unless they had default bodies (in which case rustdoc treats them much like they came from an impl). cc ```@yoshuawuyts``` should help with https://rust-lang.zulipchat.com/#narrow/stream/330606-wg-async.2Fasync-fn-in-trait-impl/topic/type.20inside.20.60async.20fn.60.20body.20must.20be.20known.20in.20this.20context/near/306894869
2 parents 14fdfcb + 59be515 commit 6e582ca

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

Diff for: compiler/rustc_ty_utils/src/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ fn issue33140_self_ty(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Ty<'_>> {
413413
/// Check if a function is async.
414414
fn asyncness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::IsAsync {
415415
let node = tcx.hir().get_by_def_id(def_id.expect_local());
416-
if let Some(fn_kind) = node.fn_kind() { fn_kind.asyncness() } else { hir::IsAsync::NotAsync }
416+
node.fn_sig().map_or(hir::IsAsync::NotAsync, |sig| sig.header.asyncness)
417417
}
418418

419419
/// Don't call this directly: use ``tcx.conservative_is_privately_uninhabited`` instead.

Diff for: src/librustdoc/clean/mod.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ fn clean_fn_or_proc_macro<'tcx>(
880880
ProcMacroItem(ProcMacro { kind, helpers })
881881
}
882882
None => {
883-
let mut func = clean_function(cx, sig, generics, body_id);
883+
let mut func = clean_function(cx, sig, generics, FunctionArgs::Body(body_id));
884884
clean_fn_decl_legacy_const_generics(&mut func, attrs);
885885
FunctionItem(func)
886886
}
@@ -917,16 +917,28 @@ fn clean_fn_decl_legacy_const_generics(func: &mut Function, attrs: &[ast::Attrib
917917
}
918918
}
919919

920+
enum FunctionArgs<'tcx> {
921+
Body(hir::BodyId),
922+
Names(&'tcx [Ident]),
923+
}
924+
920925
fn clean_function<'tcx>(
921926
cx: &mut DocContext<'tcx>,
922927
sig: &hir::FnSig<'tcx>,
923928
generics: &hir::Generics<'tcx>,
924-
body_id: hir::BodyId,
929+
args: FunctionArgs<'tcx>,
925930
) -> Box<Function> {
926931
let (generics, decl) = enter_impl_trait(cx, |cx| {
927932
// NOTE: generics must be cleaned before args
928933
let generics = clean_generics(generics, cx);
929-
let args = clean_args_from_types_and_body_id(cx, sig.decl.inputs, body_id);
934+
let args = match args {
935+
FunctionArgs::Body(body_id) => {
936+
clean_args_from_types_and_body_id(cx, sig.decl.inputs, body_id)
937+
}
938+
FunctionArgs::Names(names) => {
939+
clean_args_from_types_and_names(cx, sig.decl.inputs, names)
940+
}
941+
};
930942
let mut decl = clean_fn_decl_with_args(cx, sig.decl, args);
931943
if sig.header.is_async() {
932944
decl.output = decl.sugared_async_return_type();
@@ -1051,18 +1063,12 @@ fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext
10511063
),
10521064
hir::TraitItemKind::Const(ty, None) => TyAssocConstItem(clean_ty(ty, cx)),
10531065
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
1054-
let m = clean_function(cx, sig, trait_item.generics, body);
1066+
let m = clean_function(cx, sig, trait_item.generics, FunctionArgs::Body(body));
10551067
MethodItem(m, None)
10561068
}
10571069
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(names)) => {
1058-
let (generics, decl) = enter_impl_trait(cx, |cx| {
1059-
// NOTE: generics must be cleaned before args
1060-
let generics = clean_generics(trait_item.generics, cx);
1061-
let args = clean_args_from_types_and_names(cx, sig.decl.inputs, names);
1062-
let decl = clean_fn_decl_with_args(cx, sig.decl, args);
1063-
(generics, decl)
1064-
});
1065-
TyMethodItem(Box::new(Function { decl, generics }))
1070+
let m = clean_function(cx, sig, trait_item.generics, FunctionArgs::Names(names));
1071+
TyMethodItem(m)
10661072
}
10671073
hir::TraitItemKind::Type(bounds, Some(default)) => {
10681074
let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx));
@@ -1099,7 +1105,7 @@ pub(crate) fn clean_impl_item<'tcx>(
10991105
AssocConstItem(clean_ty(ty, cx), default)
11001106
}
11011107
hir::ImplItemKind::Fn(ref sig, body) => {
1102-
let m = clean_function(cx, sig, impl_.generics, body);
1108+
let m = clean_function(cx, sig, impl_.generics, FunctionArgs::Body(body));
11031109
let defaultness = cx.tcx.impl_defaultness(impl_.owner_id);
11041110
MethodItem(m, Some(defaultness))
11051111
}

Diff for: src/librustdoc/clean/types.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -694,13 +694,10 @@ impl Item {
694694
asyncness: hir::IsAsync::NotAsync,
695695
}
696696
}
697-
ItemKind::FunctionItem(_) | ItemKind::MethodItem(_, _) => {
697+
ItemKind::FunctionItem(_) | ItemKind::MethodItem(_, _) | ItemKind::TyMethodItem(_) => {
698698
let def_id = self.item_id.as_def_id().unwrap();
699699
build_fn_header(def_id, tcx, tcx.asyncness(def_id))
700700
}
701-
ItemKind::TyMethodItem(_) => {
702-
build_fn_header(self.item_id.as_def_id().unwrap(), tcx, hir::IsAsync::NotAsync)
703-
}
704701
_ => return None,
705702
};
706703
Some(header)

Diff for: src/test/rustdoc/async-trait-sig.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// edition:2021
2+
3+
#![feature(async_fn_in_trait)]
4+
#![allow(incomplete_features)]
5+
6+
pub trait Foo {
7+
// @has async_trait_sig/trait.Foo.html '//h4[@class="code-header"]' "async fn bar() -> i32"
8+
async fn bar() -> i32;
9+
10+
// @has async_trait_sig/trait.Foo.html '//h4[@class="code-header"]' "async fn baz() -> i32"
11+
async fn baz() -> i32 {
12+
1
13+
}
14+
}

0 commit comments

Comments
 (0)