Skip to content

Commit

Permalink
Backport rust-lang#64599 to beta: rustdoc render async function re-ex…
Browse files Browse the repository at this point in the history
…port
  • Loading branch information
csmoe authored and Mark-Simulacrum committed Sep 26, 2019
1 parent 1a5c963 commit 2ff6c65
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/librustc/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ rustc_queries! {
desc { |tcx| "checking if item is const fn: `{}`", tcx.def_path_str(key) }
}

query asyncness(key: DefId) -> hir::IsAsync {
desc { |tcx| "checking if the function is async: `{}`", tcx.def_path_str(key) }
}

/// Returns `true` if calls to the function may be promoted.
///
/// This is either because the function is e.g., a tuple-struct or tuple-variant
Expand Down
17 changes: 17 additions & 0 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3353,13 +3353,30 @@ fn issue33140_self_ty(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Ty<'_>> {
}
}

/// Check if a function is async.
fn asyncness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::IsAsync {
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap_or_else(|| {
bug!("asyncness: expected local `DefId`, got `{:?}`", def_id)
});

let node = tcx.hir().get(hir_id);

let fn_like = hir::map::blocks::FnLikeNode::from_node(node).unwrap_or_else(|| {
bug!("asyncness: expected fn-like node but got `{:?}`", def_id);
});

fn_like.asyncness()
}


pub fn provide(providers: &mut ty::query::Providers<'_>) {
context::provide(providers);
erase_regions::provide(providers);
layout::provide(providers);
util::provide(providers);
constness::provide(providers);
*providers = ty::query::Providers {
asyncness,
associated_item,
associated_item_def_ids,
adt_sized_constraint,
Expand Down
1 change: 1 addition & 0 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
fn_sig => { cdata.fn_sig(def_id.index, tcx) }
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) }
asyncness => { cdata.asyncness(def_id.index) }
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
static_mutability => { cdata.static_mutability(def_id.index) }
def_kind => { cdata.def_kind(def_id.index) }
Expand Down
9 changes: 9 additions & 0 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,15 @@ impl<'a, 'tcx> CrateMetadata {
constness == hir::Constness::Const
}

pub fn asyncness(&self, id: DefIndex) -> hir::IsAsync {
match self.entry(id).kind {
EntryKind::Fn(data) => data.decode(self).asyncness,
EntryKind::Method(data) => data.decode(self).fn_data.asyncness,
EntryKind::ForeignFn(data) => data.decode(self).asyncness,
_ => bug!("asyncness: expect functions entry."),
}
}

pub fn is_foreign_item(&self, id: DefIndex) -> bool {
match self.entry(id).kind {
EntryKind::ForeignImmStatic |
Expand Down
6 changes: 5 additions & 1 deletion src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ impl EncodeContext<'tcx> {
EntryKind::AssocConst(container, const_qualif, rendered_const)
}
ty::AssocKind::Method => {
let fn_data = if let hir::TraitItemKind::Method(_, ref m) = ast_item.node {
let fn_data = if let hir::TraitItemKind::Method(method_sig, m) = &ast_item.node {
let param_names = match *m {
hir::TraitMethod::Required(ref names) => {
self.encode_fn_param_names(names)
Expand All @@ -885,6 +885,7 @@ impl EncodeContext<'tcx> {
}
};
FnData {
asyncness: method_sig.header.asyncness,
constness: hir::Constness::NotConst,
param_names,
sig: self.lazy(&tcx.fn_sig(def_id)),
Expand Down Expand Up @@ -982,6 +983,7 @@ impl EncodeContext<'tcx> {
ty::AssocKind::Method => {
let fn_data = if let hir::ImplItemKind::Method(ref sig, body) = ast_item.node {
FnData {
asyncness: sig.header.asyncness,
constness: sig.header.constness,
param_names: self.encode_fn_param_names_for_body(body),
sig: self.lazy(&tcx.fn_sig(def_id)),
Expand Down Expand Up @@ -1128,6 +1130,7 @@ impl EncodeContext<'tcx> {
}
hir::ItemKind::Fn(_, header, .., body) => {
let data = FnData {
asyncness: header.asyncness,
constness: header.constness,
param_names: self.encode_fn_param_names_for_body(body),
sig: self.lazy(tcx.fn_sig(def_id)),
Expand Down Expand Up @@ -1675,6 +1678,7 @@ impl EncodeContext<'tcx> {
let kind = match nitem.node {
hir::ForeignItemKind::Fn(_, ref names, _) => {
let data = FnData {
asyncness: hir::IsAsync::NotAsync,
constness: hir::Constness::NotConst,
param_names: self.encode_fn_param_names(names),
sig: self.lazy(tcx.fn_sig(def_id)),
Expand Down
1 change: 1 addition & 0 deletions src/librustc_metadata/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ pub struct MacroDef {

#[derive(RustcEncodable, RustcDecodable)]
pub struct FnData<'tcx> {
pub asyncness: hir::IsAsync,
pub constness: hir::Constness,
pub param_names: Lazy<[ast::Name]>,
pub sig: Lazy<ty::PolyFnSig<'tcx>>,
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ fn build_external_function(cx: &DocContext<'_>, did: DefId) -> clean::Function {
} else {
hir::Constness::NotConst
};

let asyncness = cx.tcx.asyncness(did);
let predicates = cx.tcx.predicates_of(did);
let (generics, decl) = clean::enter_impl_trait(cx, || {
((cx.tcx.generics_of(did), &predicates).clean(cx), (did, sig).clean(cx))
Expand All @@ -230,7 +230,7 @@ fn build_external_function(cx: &DocContext<'_>, did: DefId) -> clean::Function {
unsafety: sig.unsafety(),
abi: sig.abi(),
constness,
asyncness: hir::IsAsync::NotAsync,
asyncness,
},
all_types,
ret_types,
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2403,6 +2403,7 @@ impl Clean<Item> for ty::AssocItem {
} else {
hir::Constness::NotConst
};
let asyncness = cx.tcx.asyncness(self.def_id);
let defaultness = match self.container {
ty::ImplContainer(_) => Some(self.defaultness),
ty::TraitContainer(_) => None,
Expand All @@ -2414,7 +2415,7 @@ impl Clean<Item> for ty::AssocItem {
unsafety: sig.unsafety(),
abi: sig.abi(),
constness,
asyncness: hir::IsAsync::NotAsync,
asyncness,
},
defaultness,
all_types,
Expand Down
10 changes: 10 additions & 0 deletions src/test/rustdoc/inline_cross/auxiliary/impl_trait_aux.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// edition:2018

use std::ops::Deref;

pub fn func<'a>(_x: impl Clone + Into<Vec<u8>> + 'a) {}
Expand All @@ -11,8 +13,16 @@ pub fn func3(_x: impl Iterator<Item = impl Iterator<Item = u8>> + Clone) {}

pub fn func4<T: Iterator<Item = impl Clone>>(_x: T) {}

pub async fn async_fn() {}

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) {}
}
10 changes: 9 additions & 1 deletion src/test/rustdoc/inline_cross/impl_trait.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// aux-build:impl_trait_aux.rs
// edition:2018

extern crate impl_trait_aux;

Expand All @@ -20,13 +21,20 @@ pub use impl_trait_aux::func2;
// @!has - '//pre[@class="rust fn"]' 'where'
pub use impl_trait_aux::func3;


// @has impl_trait/fn.func4.html
// @has - '//pre[@class="rust fn"]' "func4<T>("
// @has - '//pre[@class="rust fn"]' "T: Iterator<Item = impl Clone>,"
pub use impl_trait_aux::func4;

// @has impl_trait/fn.async_fn.html
// @has - '//pre[@class="rust fn"]' "pub async fn async_fn()"
pub use impl_trait_aux::async_fn;

// @has impl_trait/struct.Foo.html
// @has - '//code[@id="method.v"]' "pub fn method<'a>(_x: impl Clone + Into<Vec<u8>> + 'a)"
// @!has - '//code[@id="method.v"]' '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 2ff6c65

Please sign in to comment.