2014-05-24 11:56:38 -07:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Support for inlining external documentation into the current AST.
|
|
|
|
|
2016-04-06 13:51:55 +03:00
|
|
|
use std::iter::once;
|
2015-04-07 16:18:49 -07:00
|
|
|
|
2014-05-24 11:56:38 -07:00
|
|
|
use syntax::ast;
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir;
|
2014-05-24 11:56:38 -07:00
|
|
|
|
2016-03-29 12:54:26 +03:00
|
|
|
use rustc::hir::def::Def;
|
|
|
|
use rustc::hir::def_id::DefId;
|
2016-09-08 19:05:50 +03:00
|
|
|
use rustc::hir::map::DefPathData;
|
2016-05-03 13:09:42 +02:00
|
|
|
use rustc::hir::print as pprust;
|
2016-08-26 19:23:42 +03:00
|
|
|
use rustc::ty::{self, TyCtxt, VariantKind};
|
2016-08-24 10:55:03 +02:00
|
|
|
use rustc::util::nodemap::FnvHashSet;
|
2016-03-30 13:43:36 +02:00
|
|
|
|
|
|
|
use rustc_const_eval::lookup_const_by_id;
|
2014-05-24 11:56:38 -07:00
|
|
|
|
2016-04-17 08:54:48 +02:00
|
|
|
use core::{DocContext, DocAccessLevels};
|
2014-05-24 11:56:38 -07:00
|
|
|
use doctree;
|
2016-04-15 16:34:48 +02:00
|
|
|
use clean::{self, GetDefId};
|
2014-05-24 11:56:38 -07:00
|
|
|
|
2016-05-03 13:09:42 +02:00
|
|
|
use super::Clean;
|
2014-05-24 11:56:38 -07:00
|
|
|
|
|
|
|
/// Attempt to inline the definition of a local node id into this AST.
|
|
|
|
///
|
|
|
|
/// This function will fetch the definition of the id specified, and if it is
|
|
|
|
/// from another crate it will attempt to inline the documentation from the
|
|
|
|
/// other crate into this crate.
|
|
|
|
///
|
|
|
|
/// This is primarily used for `pub use` statements which are, in general,
|
|
|
|
/// implementation details. Inlining the documentation should help provide a
|
|
|
|
/// better experience when reading the documentation in this use case.
|
|
|
|
///
|
|
|
|
/// The returned value is `None` if the `id` could not be inlined, and `Some`
|
|
|
|
/// of a vector of items if it was successfully expanded.
|
2015-09-20 14:51:40 +03:00
|
|
|
pub fn try_inline(cx: &DocContext, id: ast::NodeId, into: Option<ast::Name>)
|
2014-07-25 10:16:41 -07:00
|
|
|
-> Option<Vec<clean::Item>> {
|
2014-09-06 19:13:40 +03:00
|
|
|
let tcx = match cx.tcx_opt() {
|
|
|
|
Some(tcx) => tcx,
|
|
|
|
None => return None,
|
2014-05-24 11:56:38 -07:00
|
|
|
};
|
2016-06-03 23:15:00 +03:00
|
|
|
let def = match tcx.expect_def_or_none(id) {
|
|
|
|
Some(def) => def,
|
2014-05-24 11:56:38 -07:00
|
|
|
None => return None,
|
|
|
|
};
|
2014-05-14 15:31:30 -04:00
|
|
|
let did = def.def_id();
|
2015-08-16 06:32:28 -04:00
|
|
|
if did.is_local() { return None }
|
2014-09-06 19:13:40 +03:00
|
|
|
try_inline_def(cx, tcx, def).map(|vec| {
|
2014-09-14 20:27:36 -07:00
|
|
|
vec.into_iter().map(|mut item| {
|
2014-07-25 10:16:41 -07:00
|
|
|
match into {
|
|
|
|
Some(into) if item.name.is_some() => {
|
2014-09-06 19:13:40 +03:00
|
|
|
item.name = Some(into.clean(cx));
|
2014-07-25 10:16:41 -07:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
item
|
|
|
|
}).collect()
|
|
|
|
})
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
fn try_inline_def<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
def: Def) -> Option<Vec<clean::Item>> {
|
2014-05-24 11:56:38 -07:00
|
|
|
let mut ret = Vec::new();
|
2014-05-14 15:31:30 -04:00
|
|
|
let did = def.def_id();
|
2014-05-24 11:56:38 -07:00
|
|
|
let inner = match def {
|
2016-01-20 22:31:10 +03:00
|
|
|
Def::Trait(did) => {
|
2016-10-01 00:34:00 -04:00
|
|
|
record_extern_fqn(cx, did, clean::TypeKind::Trait);
|
2016-04-21 18:05:15 +02:00
|
|
|
ret.extend(build_impls(cx, tcx, did));
|
2014-09-06 19:13:40 +03:00
|
|
|
clean::TraitItem(build_external_trait(cx, tcx, did))
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
2016-01-20 22:31:10 +03:00
|
|
|
Def::Fn(did) => {
|
2016-10-01 00:34:00 -04:00
|
|
|
record_extern_fqn(cx, did, clean::TypeKind::Function);
|
2014-10-31 15:00:35 +13:00
|
|
|
clean::FunctionItem(build_external_function(cx, tcx, did))
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
2016-01-20 22:31:10 +03:00
|
|
|
Def::Struct(did)
|
2016-01-17 22:57:54 +03:00
|
|
|
// If this is a struct constructor, we skip it
|
2016-09-08 19:05:50 +03:00
|
|
|
if tcx.def_key(did).disambiguated_data.data != DefPathData::StructCtor => {
|
2016-10-01 00:34:00 -04:00
|
|
|
record_extern_fqn(cx, did, clean::TypeKind::Struct);
|
2015-06-10 17:22:20 +01:00
|
|
|
ret.extend(build_impls(cx, tcx, did));
|
2014-09-06 19:13:40 +03:00
|
|
|
clean::StructItem(build_struct(cx, tcx, did))
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
2016-08-10 21:00:17 +03:00
|
|
|
Def::Union(did) => {
|
2016-10-01 00:34:00 -04:00
|
|
|
record_extern_fqn(cx, did, clean::TypeKind::Union);
|
2016-08-10 21:00:17 +03:00
|
|
|
ret.extend(build_impls(cx, tcx, did));
|
|
|
|
clean::UnionItem(build_union(cx, tcx, did))
|
|
|
|
}
|
2016-01-20 22:31:10 +03:00
|
|
|
Def::TyAlias(did) => {
|
2016-10-01 00:34:00 -04:00
|
|
|
record_extern_fqn(cx, did, clean::TypeKind::Typedef);
|
2015-06-10 17:22:20 +01:00
|
|
|
ret.extend(build_impls(cx, tcx, did));
|
2016-09-19 23:49:01 +03:00
|
|
|
clean::TypedefItem(build_type_alias(cx, tcx, did), false)
|
2014-09-16 09:13:00 +12:00
|
|
|
}
|
2016-01-20 22:31:10 +03:00
|
|
|
Def::Enum(did) => {
|
2016-10-01 00:34:00 -04:00
|
|
|
record_extern_fqn(cx, did, clean::TypeKind::Enum);
|
2015-06-10 17:22:20 +01:00
|
|
|
ret.extend(build_impls(cx, tcx, did));
|
2016-09-19 23:49:01 +03:00
|
|
|
clean::EnumItem(build_enum(cx, tcx, did))
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
// Assume that the enum type is reexported next to the variant, and
|
|
|
|
// variants don't show up in documentation specially.
|
2016-01-20 22:31:10 +03:00
|
|
|
Def::Variant(..) => return Some(Vec::new()),
|
|
|
|
Def::Mod(did) => {
|
2016-10-01 00:34:00 -04:00
|
|
|
record_extern_fqn(cx, did, clean::TypeKind::Module);
|
2014-05-24 11:56:38 -07:00
|
|
|
clean::ModuleItem(build_module(cx, tcx, did))
|
|
|
|
}
|
2016-01-20 22:31:10 +03:00
|
|
|
Def::Static(did, mtbl) => {
|
2016-10-01 00:34:00 -04:00
|
|
|
record_extern_fqn(cx, did, clean::TypeKind::Static);
|
2014-09-06 19:13:40 +03:00
|
|
|
clean::StaticItem(build_static(cx, tcx, did, mtbl))
|
2014-06-05 17:20:59 -07:00
|
|
|
}
|
2016-01-20 22:31:10 +03:00
|
|
|
Def::Const(did) | Def::AssociatedConst(did) => {
|
2016-10-01 00:34:00 -04:00
|
|
|
record_extern_fqn(cx, did, clean::TypeKind::Const);
|
2014-12-12 04:13:36 -08:00
|
|
|
clean::ConstantItem(build_const(cx, tcx, did))
|
|
|
|
}
|
2014-05-24 11:56:38 -07:00
|
|
|
_ => return None,
|
|
|
|
};
|
2016-04-07 05:59:02 +02:00
|
|
|
cx.renderinfo.borrow_mut().inlined.insert(did);
|
2014-05-24 11:56:38 -07:00
|
|
|
ret.push(clean::Item {
|
|
|
|
source: clean::Span::empty(),
|
2015-08-12 20:22:25 +02:00
|
|
|
name: Some(tcx.item_name(did).to_string()),
|
2014-09-06 19:13:40 +03:00
|
|
|
attrs: load_attrs(cx, tcx, did),
|
2014-05-24 11:56:38 -07:00
|
|
|
inner: inner,
|
2016-04-11 08:15:14 +00:00
|
|
|
visibility: Some(clean::Public),
|
2016-03-17 00:15:31 +02:00
|
|
|
stability: tcx.lookup_stability(did).clean(cx),
|
|
|
|
deprecation: tcx.lookup_deprecation(did).clean(cx),
|
2014-05-24 11:56:38 -07:00
|
|
|
def_id: did,
|
|
|
|
});
|
|
|
|
Some(ret)
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn load_attrs<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
did: DefId) -> Vec<clean::Attribute> {
|
2015-11-22 21:02:04 +02:00
|
|
|
tcx.get_attrs(did).iter().map(|a| a.clean(cx)).collect()
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Record an external fully qualified name in the external_paths cache.
|
|
|
|
///
|
|
|
|
/// These names are used later on by HTML rendering to generate things like
|
|
|
|
/// source links back to the original item.
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn record_extern_fqn(cx: &DocContext, did: DefId, kind: clean::TypeKind) {
|
2016-02-28 12:11:13 +01:00
|
|
|
if let Some(tcx) = cx.tcx_opt() {
|
2016-04-06 13:51:55 +03:00
|
|
|
let crate_name = tcx.sess.cstore.crate_name(did.krate).to_string();
|
2016-06-18 18:41:13 +01:00
|
|
|
let relative = tcx.def_path(did).data.into_iter().filter_map(|elem| {
|
|
|
|
// extern blocks have an empty name
|
|
|
|
let s = elem.data.to_string();
|
|
|
|
if !s.is_empty() {
|
|
|
|
Some(s)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-04-06 13:51:55 +03:00
|
|
|
});
|
|
|
|
let fqn = once(crate_name).chain(relative).collect();
|
2016-04-07 05:59:02 +02:00
|
|
|
cx.renderinfo.borrow_mut().external_paths.insert(did, (fqn, kind));
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn build_external_trait<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
did: DefId) -> clean::Trait {
|
2015-06-25 23:42:17 +03:00
|
|
|
let def = tcx.lookup_trait_def(did);
|
|
|
|
let trait_items = tcx.trait_items(did).clean(cx);
|
|
|
|
let predicates = tcx.lookup_predicates(did);
|
2016-08-10 20:39:09 +03:00
|
|
|
let generics = (def.generics, &predicates).clean(cx);
|
2015-04-07 00:16:35 -07:00
|
|
|
let generics = filter_non_trait_generics(did, generics);
|
|
|
|
let (generics, supertrait_bounds) = separate_supertrait_bounds(generics);
|
2014-05-24 11:56:38 -07:00
|
|
|
clean::Trait {
|
2014-12-09 19:59:20 -05:00
|
|
|
unsafety: def.unsafety,
|
2015-04-07 00:16:35 -07:00
|
|
|
generics: generics,
|
2015-03-10 12:28:44 +02:00
|
|
|
items: trait_items,
|
2015-04-07 00:16:35 -07:00
|
|
|
bounds: supertrait_bounds,
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
fn build_external_function<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
did: DefId) -> clean::Function {
|
2015-06-25 23:42:17 +03:00
|
|
|
let t = tcx.lookup_item_type(did);
|
2015-04-07 14:22:55 -07:00
|
|
|
let (decl, style, abi) = match t.ty.sty {
|
2016-08-26 19:23:42 +03:00
|
|
|
ty::TyFnDef(.., ref f) => ((did, &f.sig).clean(cx), f.unsafety, f.abi),
|
2014-10-31 15:00:35 +13:00
|
|
|
_ => panic!("bad function"),
|
|
|
|
};
|
2015-11-19 16:27:17 +01:00
|
|
|
|
2015-11-25 17:02:59 +02:00
|
|
|
let constness = if tcx.sess.cstore.is_const_fn(did) {
|
2015-11-19 16:27:17 +01:00
|
|
|
hir::Constness::Const
|
|
|
|
} else {
|
|
|
|
hir::Constness::NotConst
|
|
|
|
};
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
let predicates = tcx.lookup_predicates(did);
|
2014-05-24 11:56:38 -07:00
|
|
|
clean::Function {
|
2014-10-31 15:00:35 +13:00
|
|
|
decl: decl,
|
2016-08-10 20:39:09 +03:00
|
|
|
generics: (t.generics, &predicates).clean(cx),
|
2014-12-09 10:36:46 -05:00
|
|
|
unsafety: style,
|
2015-11-19 16:27:17 +01:00
|
|
|
constness: constness,
|
2015-04-07 14:22:55 -07:00
|
|
|
abi: abi,
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:49:01 +03:00
|
|
|
fn build_enum<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
did: DefId) -> clean::Enum {
|
|
|
|
let t = tcx.lookup_item_type(did);
|
|
|
|
let predicates = tcx.lookup_predicates(did);
|
|
|
|
|
|
|
|
clean::Enum {
|
|
|
|
generics: (t.generics, &predicates).clean(cx),
|
|
|
|
variants_stripped: false,
|
|
|
|
variants: tcx.lookup_adt_def(did).variants.clean(cx),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
fn build_struct<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
did: DefId) -> clean::Struct {
|
2015-06-25 23:42:17 +03:00
|
|
|
let t = tcx.lookup_item_type(did);
|
|
|
|
let predicates = tcx.lookup_predicates(did);
|
2015-08-02 22:52:50 +03:00
|
|
|
let variant = tcx.lookup_adt_def(did).struct_variant();
|
2014-05-24 11:56:38 -07:00
|
|
|
|
|
|
|
clean::Struct {
|
2016-08-26 19:23:42 +03:00
|
|
|
struct_type: match variant.kind {
|
|
|
|
VariantKind::Struct => doctree::Plain,
|
|
|
|
VariantKind::Tuple => doctree::Tuple,
|
|
|
|
VariantKind::Unit => doctree::Unit,
|
2014-05-24 11:56:38 -07:00
|
|
|
},
|
2016-08-10 20:39:09 +03:00
|
|
|
generics: (t.generics, &predicates).clean(cx),
|
2015-08-02 22:52:50 +03:00
|
|
|
fields: variant.fields.clean(cx),
|
2014-05-24 11:56:38 -07:00
|
|
|
fields_stripped: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:00:17 +03:00
|
|
|
fn build_union<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
did: DefId) -> clean::Union {
|
|
|
|
let t = tcx.lookup_item_type(did);
|
|
|
|
let predicates = tcx.lookup_predicates(did);
|
|
|
|
let variant = tcx.lookup_adt_def(did).struct_variant();
|
|
|
|
|
|
|
|
clean::Union {
|
|
|
|
struct_type: doctree::Plain,
|
2016-08-18 15:43:24 +03:00
|
|
|
generics: (t.generics, &predicates).clean(cx),
|
2016-08-10 21:00:17 +03:00
|
|
|
fields: variant.fields.clean(cx),
|
|
|
|
fields_stripped: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:49:01 +03:00
|
|
|
fn build_type_alias<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
did: DefId) -> clean::Typedef {
|
2015-06-25 23:42:17 +03:00
|
|
|
let t = tcx.lookup_item_type(did);
|
|
|
|
let predicates = tcx.lookup_predicates(did);
|
2014-05-24 11:56:38 -07:00
|
|
|
|
2016-09-19 23:49:01 +03:00
|
|
|
clean::Typedef {
|
2014-09-06 19:13:40 +03:00
|
|
|
type_: t.ty.clean(cx),
|
2016-08-10 20:39:09 +03:00
|
|
|
generics: (t.generics, &predicates).clean(cx),
|
2016-09-19 23:49:01 +03:00
|
|
|
}
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn build_impls<'a, 'tcx>(cx: &DocContext,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
did: DefId) -> Vec<clean::Item> {
|
2015-06-25 23:42:17 +03:00
|
|
|
tcx.populate_inherent_implementations_for_type_if_necessary(did);
|
2014-05-24 11:56:38 -07:00
|
|
|
let mut impls = Vec::new();
|
|
|
|
|
2016-02-28 12:11:13 +01:00
|
|
|
if let Some(i) = tcx.inherent_impls.borrow().get(&did) {
|
|
|
|
for &did in i.iter() {
|
|
|
|
build_impl(cx, tcx, did, &mut impls);
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
}
|
2016-09-19 23:49:01 +03:00
|
|
|
// If this is the first time we've inlined something from another crate, then
|
|
|
|
// we inline *all* impls from all the crates into this crate. Note that there's
|
2014-05-29 01:35:44 -07:00
|
|
|
// currently no way for us to filter this based on type, and we likely need
|
|
|
|
// many impls for a variety of reasons.
|
|
|
|
//
|
|
|
|
// Primarily, the impls will be used to populate the documentation for this
|
|
|
|
// type being inlined, but impls can also be used when generating
|
|
|
|
// documentation for primitives (no way to find those specifically).
|
2016-09-19 23:49:01 +03:00
|
|
|
if cx.populated_all_crate_impls.get() {
|
|
|
|
return impls;
|
|
|
|
}
|
2014-05-29 01:35:44 -07:00
|
|
|
|
2016-09-19 23:49:01 +03:00
|
|
|
cx.populated_all_crate_impls.set(true);
|
|
|
|
|
|
|
|
for did in tcx.sess.cstore.implementations_of_trait(None) {
|
|
|
|
build_impl(cx, tcx, did, &mut impls);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also try to inline primitive impls from other crates.
|
|
|
|
let primitive_impls = [
|
|
|
|
tcx.lang_items.isize_impl(),
|
|
|
|
tcx.lang_items.i8_impl(),
|
|
|
|
tcx.lang_items.i16_impl(),
|
|
|
|
tcx.lang_items.i32_impl(),
|
|
|
|
tcx.lang_items.i64_impl(),
|
|
|
|
tcx.lang_items.usize_impl(),
|
|
|
|
tcx.lang_items.u8_impl(),
|
|
|
|
tcx.lang_items.u16_impl(),
|
|
|
|
tcx.lang_items.u32_impl(),
|
|
|
|
tcx.lang_items.u64_impl(),
|
|
|
|
tcx.lang_items.f32_impl(),
|
|
|
|
tcx.lang_items.f64_impl(),
|
|
|
|
tcx.lang_items.char_impl(),
|
|
|
|
tcx.lang_items.str_impl(),
|
|
|
|
tcx.lang_items.slice_impl(),
|
|
|
|
tcx.lang_items.slice_impl(),
|
|
|
|
tcx.lang_items.const_ptr_impl()
|
|
|
|
];
|
|
|
|
|
|
|
|
for def_id in primitive_impls.iter().filter_map(|&def_id| def_id) {
|
|
|
|
if !def_id.is_local() {
|
|
|
|
tcx.populate_implementations_for_primitive_if_necessary(def_id);
|
|
|
|
build_impl(cx, tcx, def_id, &mut impls);
|
2014-05-29 01:35:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 17:30:26 +02:00
|
|
|
impls
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn build_impl<'a, 'tcx>(cx: &DocContext,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
did: DefId,
|
|
|
|
ret: &mut Vec<clean::Item>) {
|
2016-04-07 05:59:02 +02:00
|
|
|
if !cx.renderinfo.borrow_mut().inlined.insert(did) {
|
2015-04-13 16:23:32 -07:00
|
|
|
return
|
2014-06-01 11:16:18 -07:00
|
|
|
}
|
|
|
|
|
2015-03-12 19:15:52 -07:00
|
|
|
let attrs = load_attrs(cx, tcx, did);
|
2015-11-22 21:02:04 +02:00
|
|
|
let associated_trait = tcx.impl_trait_ref(did);
|
2016-04-15 16:34:48 +02:00
|
|
|
|
|
|
|
// Only inline impl if the implemented trait is
|
|
|
|
// reachable in rustdoc generated documentation
|
|
|
|
if let Some(traitref) = associated_trait {
|
2016-04-17 08:54:48 +02:00
|
|
|
if !cx.access_levels.borrow().is_doc_reachable(traitref.def_id) {
|
2015-04-13 16:23:32 -07:00
|
|
|
return
|
2014-07-25 08:26:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 19:15:52 -07:00
|
|
|
// If this is a defaulted impl, then bail out early here
|
2015-11-22 21:02:04 +02:00
|
|
|
if tcx.sess.cstore.is_default_impl(did) {
|
2015-04-13 16:23:32 -07:00
|
|
|
return ret.push(clean::Item {
|
2015-03-12 19:15:52 -07:00
|
|
|
inner: clean::DefaultImplItem(clean::DefaultImpl {
|
|
|
|
// FIXME: this should be decoded
|
2015-07-31 00:04:06 -07:00
|
|
|
unsafety: hir::Unsafety::Normal,
|
2015-03-12 19:15:52 -07:00
|
|
|
trait_: match associated_trait.as_ref().unwrap().clean(cx) {
|
|
|
|
clean::TraitBound(polyt, _) => polyt.trait_,
|
|
|
|
clean::RegionBound(..) => unreachable!(),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
source: clean::Span::empty(),
|
|
|
|
name: None,
|
|
|
|
attrs: attrs,
|
2016-04-11 08:15:14 +00:00
|
|
|
visibility: Some(clean::Inherited),
|
2016-03-17 00:15:31 +02:00
|
|
|
stability: tcx.lookup_stability(did).clean(cx),
|
|
|
|
deprecation: tcx.lookup_deprecation(did).clean(cx),
|
2015-03-12 19:15:52 -07:00
|
|
|
def_id: did,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-15 16:34:48 +02:00
|
|
|
let ty = tcx.lookup_item_type(did);
|
|
|
|
let for_ = ty.ty.clean(cx);
|
|
|
|
|
|
|
|
// Only inline impl if the implementing type is
|
|
|
|
// reachable in rustdoc generated documentation
|
|
|
|
if let Some(did) = for_.def_id() {
|
2016-04-17 08:54:48 +02:00
|
|
|
if !cx.access_levels.borrow().is_doc_reachable(did) {
|
2016-04-15 16:34:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
let predicates = tcx.lookup_predicates(did);
|
2016-09-19 23:49:01 +03:00
|
|
|
let trait_items = tcx.sess.cstore.impl_or_trait_items(did)
|
2014-08-04 13:56:56 -07:00
|
|
|
.iter()
|
2016-09-05 10:54:38 +03:00
|
|
|
.filter_map(|&did| {
|
|
|
|
match tcx.impl_or_trait_item(did) {
|
2015-03-15 19:35:25 -06:00
|
|
|
ty::ConstTraitItem(ref assoc_const) => {
|
|
|
|
let did = assoc_const.def_id;
|
2015-06-25 23:42:17 +03:00
|
|
|
let type_scheme = tcx.lookup_item_type(did);
|
2015-09-30 22:28:27 +03:00
|
|
|
let default = if assoc_const.has_value {
|
2016-05-03 13:09:42 +02:00
|
|
|
Some(pprust::expr_to_string(
|
|
|
|
lookup_const_by_id(tcx, did, None).unwrap().0))
|
2015-09-30 22:28:27 +03:00
|
|
|
} else {
|
|
|
|
None
|
2015-03-15 19:35:25 -06:00
|
|
|
};
|
|
|
|
Some(clean::Item {
|
|
|
|
name: Some(assoc_const.name.clean(cx)),
|
|
|
|
inner: clean::AssociatedConstItem(
|
|
|
|
type_scheme.ty.clean(cx),
|
|
|
|
default,
|
|
|
|
),
|
|
|
|
source: clean::Span::empty(),
|
|
|
|
attrs: vec![],
|
|
|
|
visibility: None,
|
2016-03-17 00:15:31 +02:00
|
|
|
stability: tcx.lookup_stability(did).clean(cx),
|
|
|
|
deprecation: tcx.lookup_deprecation(did).clean(cx),
|
2015-03-15 19:35:25 -06:00
|
|
|
def_id: did
|
|
|
|
})
|
|
|
|
}
|
2014-08-04 13:56:56 -07:00
|
|
|
ty::MethodTraitItem(method) => {
|
2016-03-25 06:08:11 +00:00
|
|
|
if method.vis != ty::Visibility::Public && associated_trait.is_none() {
|
2014-08-04 13:56:56 -07:00
|
|
|
return None
|
|
|
|
}
|
2014-09-06 19:13:40 +03:00
|
|
|
let mut item = method.clean(cx);
|
2014-08-04 13:56:56 -07:00
|
|
|
item.inner = match item.inner.clone() {
|
|
|
|
clean::TyMethodItem(clean::TyMethod {
|
2016-05-08 21:19:29 +03:00
|
|
|
unsafety, decl, generics, abi
|
2014-08-04 13:56:56 -07:00
|
|
|
}) => {
|
2015-11-25 17:02:59 +02:00
|
|
|
let constness = if tcx.sess.cstore.is_const_fn(did) {
|
2015-11-19 16:27:17 +01:00
|
|
|
hir::Constness::Const
|
|
|
|
} else {
|
|
|
|
hir::Constness::NotConst
|
|
|
|
};
|
|
|
|
|
2014-08-04 13:56:56 -07:00
|
|
|
clean::MethodItem(clean::Method {
|
2014-12-09 10:36:46 -05:00
|
|
|
unsafety: unsafety,
|
2015-11-19 16:27:17 +01:00
|
|
|
constness: constness,
|
2014-08-04 13:56:56 -07:00
|
|
|
decl: decl,
|
|
|
|
generics: generics,
|
2015-02-06 00:51:38 -08:00
|
|
|
abi: abi
|
2014-08-04 13:56:56 -07:00
|
|
|
})
|
|
|
|
}
|
2014-10-09 15:17:22 -04:00
|
|
|
_ => panic!("not a tymethod"),
|
2014-08-04 13:56:56 -07:00
|
|
|
};
|
|
|
|
Some(item)
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
2015-01-17 22:39:01 -08:00
|
|
|
ty::TypeTraitItem(ref assoc_ty) => {
|
|
|
|
let did = assoc_ty.def_id;
|
2016-06-13 20:10:32 +03:00
|
|
|
let typedef = clean::Typedef {
|
2016-08-08 23:39:49 +03:00
|
|
|
type_: assoc_ty.ty.unwrap().clean(cx),
|
2016-08-10 20:39:09 +03:00
|
|
|
generics: clean::Generics {
|
|
|
|
lifetimes: vec![],
|
|
|
|
type_params: vec![],
|
|
|
|
where_predicates: vec![]
|
|
|
|
}
|
2016-06-13 20:10:32 +03:00
|
|
|
};
|
2015-01-17 22:39:01 -08:00
|
|
|
Some(clean::Item {
|
|
|
|
name: Some(assoc_ty.name.clean(cx)),
|
2015-05-21 14:17:37 +02:00
|
|
|
inner: clean::TypedefItem(typedef, true),
|
2015-01-17 22:39:01 -08:00
|
|
|
source: clean::Span::empty(),
|
|
|
|
attrs: vec![],
|
|
|
|
visibility: None,
|
2016-03-17 00:15:31 +02:00
|
|
|
stability: tcx.lookup_stability(did).clean(cx),
|
|
|
|
deprecation: tcx.lookup_deprecation(did).clean(cx),
|
2015-01-17 22:39:01 -08:00
|
|
|
def_id: did
|
|
|
|
})
|
2014-08-05 19:44:21 -07:00
|
|
|
}
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
2015-04-13 16:23:32 -07:00
|
|
|
}).collect::<Vec<_>>();
|
2015-11-22 21:02:04 +02:00
|
|
|
let polarity = tcx.trait_impl_polarity(did);
|
2015-04-13 16:23:32 -07:00
|
|
|
let trait_ = associated_trait.clean(cx).map(|bound| {
|
|
|
|
match bound {
|
|
|
|
clean::TraitBound(polyt, _) => polyt.trait_,
|
|
|
|
clean::RegionBound(..) => unreachable!(),
|
|
|
|
}
|
|
|
|
});
|
2016-03-24 06:10:52 +01:00
|
|
|
if trait_.def_id() == cx.deref_trait_did.get() {
|
|
|
|
super::build_deref_target_impls(cx, &trait_items, ret);
|
2015-04-13 16:23:32 -07:00
|
|
|
}
|
2016-03-24 06:10:52 +01:00
|
|
|
|
|
|
|
let provided = trait_.def_id().map(|did| {
|
|
|
|
cx.tcx().provided_trait_methods(did)
|
|
|
|
.into_iter()
|
|
|
|
.map(|meth| meth.name.to_string())
|
|
|
|
.collect()
|
2016-08-24 10:55:03 +02:00
|
|
|
}).unwrap_or(FnvHashSet());
|
2016-03-24 06:10:52 +01:00
|
|
|
|
2015-04-13 16:23:32 -07:00
|
|
|
ret.push(clean::Item {
|
2014-05-24 11:56:38 -07:00
|
|
|
inner: clean::ImplItem(clean::Impl {
|
2015-07-31 00:04:06 -07:00
|
|
|
unsafety: hir::Unsafety::Normal, // FIXME: this should be decoded
|
2016-03-24 06:10:52 +01:00
|
|
|
provided_trait_methods: provided,
|
2015-04-13 16:23:32 -07:00
|
|
|
trait_: trait_,
|
2016-04-15 16:34:48 +02:00
|
|
|
for_: for_,
|
2016-08-10 20:39:09 +03:00
|
|
|
generics: (ty.generics, &predicates).clean(cx),
|
2014-08-04 13:56:56 -07:00
|
|
|
items: trait_items,
|
2016-09-19 23:49:01 +03:00
|
|
|
polarity: Some(polarity.clean(cx)),
|
2014-05-24 11:56:38 -07:00
|
|
|
}),
|
|
|
|
source: clean::Span::empty(),
|
|
|
|
name: None,
|
|
|
|
attrs: attrs,
|
2016-04-11 08:15:14 +00:00
|
|
|
visibility: Some(clean::Inherited),
|
2016-03-17 00:15:31 +02:00
|
|
|
stability: tcx.lookup_stability(did).clean(cx),
|
|
|
|
deprecation: tcx.lookup_deprecation(did).clean(cx),
|
2014-05-24 11:56:38 -07:00
|
|
|
def_id: did,
|
2014-07-25 08:26:17 -07:00
|
|
|
});
|
2016-02-26 01:45:24 +01:00
|
|
|
}
|
2014-07-25 08:26:17 -07:00
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
fn build_module<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
did: DefId) -> clean::Module {
|
2014-05-24 11:56:38 -07:00
|
|
|
let mut items = Vec::new();
|
2014-07-25 10:37:23 -07:00
|
|
|
fill_in(cx, tcx, did, &mut items);
|
|
|
|
return clean::Module {
|
|
|
|
items: items,
|
|
|
|
is_crate: false,
|
|
|
|
};
|
2014-05-24 11:56:38 -07:00
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
fn fill_in<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
did: DefId, items: &mut Vec<clean::Item>) {
|
2015-04-07 16:18:49 -07:00
|
|
|
// If we're reexporting a reexport it may actually reexport something in
|
|
|
|
// two namespaces, so the target may be listed twice. Make sure we only
|
|
|
|
// visit each node at most once.
|
2016-08-24 10:55:03 +02:00
|
|
|
let mut visited = FnvHashSet();
|
2015-11-22 21:02:04 +02:00
|
|
|
for item in tcx.sess.cstore.item_children(did) {
|
2016-09-15 11:05:45 +03:00
|
|
|
if tcx.sess.cstore.visibility(item.def_id) == ty::Visibility::Public {
|
|
|
|
if !visited.insert(item.def_id) { continue }
|
|
|
|
if let Some(def) = tcx.sess.cstore.describe_def(item.def_id) {
|
|
|
|
if let Some(i) = try_inline_def(cx, tcx, def) {
|
|
|
|
items.extend(i)
|
|
|
|
}
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
}
|
2015-11-22 21:02:04 +02:00
|
|
|
}
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
}
|
2014-06-05 17:20:59 -07:00
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
fn build_const<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
did: DefId) -> clean::Constant {
|
2016-03-30 13:43:36 +02:00
|
|
|
let (expr, ty) = lookup_const_by_id(tcx, did, None).unwrap_or_else(|| {
|
2014-12-20 00:09:35 -08:00
|
|
|
panic!("expected lookup_const_by_id to succeed for {:?}", did);
|
2014-12-12 04:13:36 -08:00
|
|
|
});
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("converting constant expr {:?} to snippet", expr);
|
2014-12-12 04:13:36 -08:00
|
|
|
let sn = pprust::expr_to_string(expr);
|
|
|
|
debug!("got snippet {}", sn);
|
|
|
|
|
|
|
|
clean::Constant {
|
2016-03-04 12:33:23 +01:00
|
|
|
type_: ty.map(|t| t.clean(cx)).unwrap_or_else(|| tcx.lookup_item_type(did).ty.clean(cx)),
|
2014-12-12 04:13:36 -08:00
|
|
|
expr: sn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
fn build_static<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
did: DefId,
|
|
|
|
mutable: bool) -> clean::Static {
|
2014-06-05 17:20:59 -07:00
|
|
|
clean::Static {
|
2015-06-25 23:42:17 +03:00
|
|
|
type_: tcx.lookup_item_type(did).ty.clean(cx),
|
2014-06-05 17:20:59 -07:00
|
|
|
mutability: if mutable {clean::Mutable} else {clean::Immutable},
|
|
|
|
expr: "\n\n\n".to_string(), // trigger the "[definition]" links
|
|
|
|
}
|
|
|
|
}
|
2015-04-07 00:16:35 -07:00
|
|
|
|
|
|
|
/// A trait's generics clause actually contains all of the predicates for all of
|
|
|
|
/// its associated types as well. We specifically move these clauses to the
|
|
|
|
/// associated types instead when displaying, so when we're genering the
|
|
|
|
/// generics for the trait itself we need to be sure to remove them.
|
2016-06-13 20:10:32 +03:00
|
|
|
/// We also need to remove the implied "recursive" Self: Trait bound.
|
2015-04-07 00:16:35 -07:00
|
|
|
///
|
|
|
|
/// The inverse of this filtering logic can be found in the `Clean`
|
|
|
|
/// implementation for `AssociatedType`
|
2015-08-16 06:32:28 -04:00
|
|
|
fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics)
|
2015-04-07 00:16:35 -07:00
|
|
|
-> clean::Generics {
|
2016-06-13 20:10:32 +03:00
|
|
|
for pred in &mut g.where_predicates {
|
|
|
|
match *pred {
|
|
|
|
clean::WherePredicate::BoundPredicate {
|
|
|
|
ty: clean::Generic(ref s),
|
|
|
|
ref mut bounds
|
|
|
|
} if *s == "Self" => {
|
|
|
|
bounds.retain(|bound| {
|
|
|
|
match *bound {
|
|
|
|
clean::TyParamBound::TraitBound(clean::PolyTrait {
|
|
|
|
trait_: clean::ResolvedPath { did, .. },
|
|
|
|
..
|
|
|
|
}, _) => did != trait_did,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 00:16:35 -07:00
|
|
|
g.where_predicates.retain(|pred| {
|
|
|
|
match *pred {
|
|
|
|
clean::WherePredicate::BoundPredicate {
|
|
|
|
ty: clean::QPath {
|
|
|
|
self_type: box clean::Generic(ref s),
|
|
|
|
trait_: box clean::ResolvedPath { did, .. },
|
|
|
|
name: ref _name,
|
2016-06-13 20:10:32 +03:00
|
|
|
}, ref bounds
|
|
|
|
} => !(*s == "Self" && did == trait_did) && !bounds.is_empty(),
|
2015-04-07 00:16:35 -07:00
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
});
|
2016-10-01 16:47:43 -04:00
|
|
|
g
|
2015-04-07 00:16:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Supertrait bounds for a trait are also listed in the generics coming from
|
|
|
|
/// the metadata for a crate, so we want to separate those out and create a new
|
|
|
|
/// list of explicit supertrait bounds to render nicely.
|
|
|
|
fn separate_supertrait_bounds(mut g: clean::Generics)
|
|
|
|
-> (clean::Generics, Vec<clean::TyParamBound>) {
|
|
|
|
let mut ty_bounds = Vec::new();
|
|
|
|
g.where_predicates.retain(|pred| {
|
|
|
|
match *pred {
|
|
|
|
clean::WherePredicate::BoundPredicate {
|
|
|
|
ty: clean::Generic(ref s),
|
|
|
|
ref bounds
|
|
|
|
} if *s == "Self" => {
|
|
|
|
ty_bounds.extend(bounds.iter().cloned());
|
|
|
|
false
|
|
|
|
}
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
(g, ty_bounds)
|
|
|
|
}
|