2013-08-15 20:28:54 +00:00
|
|
|
//! This module contains the "cleaned" pieces of the AST, and the functions
|
|
|
|
//! that clean them.
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
mod auto_trait;
|
|
|
|
mod blanket_impl;
|
2020-11-14 22:59:58 +00:00
|
|
|
crate mod cfg;
|
|
|
|
crate mod inline;
|
2019-12-03 21:03:38 +00:00
|
|
|
mod simplify;
|
2020-11-14 22:59:58 +00:00
|
|
|
crate mod types;
|
|
|
|
crate mod utils;
|
2014-11-06 08:05:53 +00:00
|
|
|
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast as ast;
|
2020-01-11 12:15:20 +00:00
|
|
|
use rustc_attr as attr;
|
2021-09-15 10:03:03 +00:00
|
|
|
use rustc_const_eval::const_eval::is_unstable_const_fn;
|
2019-12-24 04:02:53 +00:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def::{CtorKind, DefKind, Res};
|
2021-07-05 16:36:32 +00:00
|
|
|
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
|
2019-12-22 22:42:04 +00:00
|
|
|
use rustc_index::vec::{Idx, IndexVec};
|
2020-01-06 22:31:06 +00:00
|
|
|
use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData};
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::middle::resolve_lifetime as rl;
|
|
|
|
use rustc_middle::ty::fold::TypeFolder;
|
2020-06-30 21:41:57 +00:00
|
|
|
use rustc_middle::ty::subst::{InternalSubsts, Subst};
|
2021-06-18 21:30:39 +00:00
|
|
|
use rustc_middle::ty::{self, AdtKind, DefIdTree, Lift, Ty, TyCtxt};
|
2021-05-02 19:11:13 +00:00
|
|
|
use rustc_middle::{bug, span_bug};
|
2020-10-10 13:31:14 +00:00
|
|
|
use rustc_span::hygiene::{AstPass, MacroKind};
|
2020-04-19 11:00:18 +00:00
|
|
|
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
2020-12-12 04:01:26 +00:00
|
|
|
use rustc_span::{self, ExpnKind};
|
2021-06-15 12:52:16 +00:00
|
|
|
use rustc_target::spec::abi::Abi;
|
|
|
|
use rustc_typeck::check::intrinsic::intrinsic_operation_unsafety;
|
2019-12-22 22:42:04 +00:00
|
|
|
use rustc_typeck::hir_ty_to_ty;
|
2018-06-04 21:44:35 +00:00
|
|
|
|
2021-09-02 16:54:32 +00:00
|
|
|
use std::assert_matches::assert_matches;
|
Generate documentation for auto-trait impls
A new section is added to both both struct and trait doc pages.
On struct/enum pages, a new 'Auto Trait Implementations' section displays any
synthetic implementations for auto traits. Currently, this is only done
for Send and Sync.
On trait pages, a new 'Auto Implementors' section displays all types
which automatically implement the trait. Effectively, this is a list of
all public types in the standard library.
Synthesized impls for a particular auto trait ('synthetic impls') take
into account generic bounds. For example, a type 'struct Foo<T>(T)' will
have 'impl<T> Send for Foo<T> where T: Send' generated for it.
Manual implementations of auto traits are also taken into account. If we have
the following types:
'struct Foo<T>(T)'
'struct Wrapper<T>(Foo<T>)'
'unsafe impl<T> Send for Wrapper<T>' // pretend that Wrapper<T> makes
this sound somehow
Then Wrapper will have the following impl generated:
'impl<T> Send for Wrapper<T>'
reflecting the fact that 'T: Send' need not hold for 'Wrapper<T>: Send'
to hold
Lifetimes, HRTBS, and projections (e.g. '<T as Iterator>::Item') are
taken into account by synthetic impls
However, if a type can *never* implement a particular auto trait
(e.g. 'struct MyStruct<T>(*const T)'), then a negative impl will be
generated (in this case, 'impl<T> !Send for MyStruct<T>')
All of this means that a user should be able to copy-paste a synthetic
impl into their code, without any observable changes in behavior
(assuming the rest of the program remains unchanged).
2017-11-22 21:16:55 +00:00
|
|
|
use std::collections::hash_map::Entry;
|
2017-10-16 19:07:26 +00:00
|
|
|
use std::default::Default;
|
2019-12-22 22:42:04 +00:00
|
|
|
use std::hash::Hash;
|
2014-05-23 07:42:33 +00:00
|
|
|
use std::rc::Rc;
|
2019-12-22 22:42:04 +00:00
|
|
|
use std::{mem, vec};
|
2013-10-02 22:39:32 +00:00
|
|
|
|
2019-06-21 03:23:05 +00:00
|
|
|
use crate::core::{self, DocContext, ImplTraitParam};
|
2019-02-23 07:40:07 +00:00
|
|
|
use crate::doctree;
|
2021-04-23 01:23:11 +00:00
|
|
|
use crate::formats::item_type::ItemType;
|
2019-02-23 07:40:07 +00:00
|
|
|
|
2019-12-09 16:53:42 +00:00
|
|
|
use utils::*;
|
|
|
|
|
2020-11-14 22:59:58 +00:00
|
|
|
crate use utils::{get_auto_trait_and_blanket_impls, krate, register_res};
|
2019-12-09 16:53:42 +00:00
|
|
|
|
2020-11-14 22:59:58 +00:00
|
|
|
crate use self::types::FnRetTy::*;
|
|
|
|
crate use self::types::ItemKind::*;
|
|
|
|
crate use self::types::SelfTy::*;
|
|
|
|
crate use self::types::Type::*;
|
|
|
|
crate use self::types::Visibility::{Inherited, Public};
|
|
|
|
crate use self::types::*;
|
Generate documentation for auto-trait impls
A new section is added to both both struct and trait doc pages.
On struct/enum pages, a new 'Auto Trait Implementations' section displays any
synthetic implementations for auto traits. Currently, this is only done
for Send and Sync.
On trait pages, a new 'Auto Implementors' section displays all types
which automatically implement the trait. Effectively, this is a list of
all public types in the standard library.
Synthesized impls for a particular auto trait ('synthetic impls') take
into account generic bounds. For example, a type 'struct Foo<T>(T)' will
have 'impl<T> Send for Foo<T> where T: Send' generated for it.
Manual implementations of auto traits are also taken into account. If we have
the following types:
'struct Foo<T>(T)'
'struct Wrapper<T>(Foo<T>)'
'unsafe impl<T> Send for Wrapper<T>' // pretend that Wrapper<T> makes
this sound somehow
Then Wrapper will have the following impl generated:
'impl<T> Send for Wrapper<T>'
reflecting the fact that 'T: Send' need not hold for 'Wrapper<T>: Send'
to hold
Lifetimes, HRTBS, and projections (e.g. '<T as Iterator>::Item') are
taken into account by synthetic impls
However, if a type can *never* implement a particular auto trait
(e.g. 'struct MyStruct<T>(*const T)'), then a negative impl will be
generated (in this case, 'impl<T> !Send for MyStruct<T>')
All of this means that a user should be able to copy-paste a synthetic
impl into their code, without any observable changes in behavior
(assuming the rest of the program remains unchanged).
2017-11-22 21:16:55 +00:00
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
crate trait Clean<T> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> T;
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<T: Clean<U>, U> Clean<Vec<U>> for [T] {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Vec<U> {
|
2014-09-06 16:13:40 +00:00
|
|
|
self.iter().map(|x| x.clean(cx)).collect()
|
2014-03-01 01:46:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<T: Clean<U>, U, V: Idx> Clean<IndexVec<V, U>> for IndexVec<V, T> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> IndexVec<V, U> {
|
2018-11-01 18:03:38 +00:00
|
|
|
self.iter().map(|x| x.clean(cx)).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<T: Clean<U>, U> Clean<U> for &T {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> U {
|
2019-11-28 20:47:10 +00:00
|
|
|
(**self).clean(cx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<T: Clean<U>, U> Clean<U> for Rc<T> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> U {
|
2014-09-06 16:13:40 +00:00
|
|
|
(**self).clean(cx)
|
2014-05-23 07:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Option<U> {
|
2016-02-28 11:11:13 +00:00
|
|
|
self.as_ref().map(|v| v.clean(cx))
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Item> for doctree::Module<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
2015-02-02 09:33:24 +00:00
|
|
|
let mut items: Vec<Item> = vec![];
|
2019-06-12 08:43:15 +00:00
|
|
|
items.extend(self.foreigns.iter().map(|x| x.clean(cx)));
|
2015-02-02 09:33:24 +00:00
|
|
|
items.extend(self.mods.iter().map(|x| x.clean(cx)));
|
2020-11-22 18:34:06 +00:00
|
|
|
items.extend(self.items.iter().map(|x| x.clean(cx)).flatten());
|
2014-04-26 20:08:36 +00:00
|
|
|
|
|
|
|
// determine if we should display the inner contents or
|
|
|
|
// the outer `mod` item for the source code.
|
2021-04-22 13:14:49 +00:00
|
|
|
|
|
|
|
let span = Span::new({
|
2021-05-01 13:33:49 +00:00
|
|
|
let where_outer = self.where_outer(cx.tcx);
|
2020-02-22 14:07:05 +00:00
|
|
|
let sm = cx.sess().source_map();
|
2021-05-01 13:33:49 +00:00
|
|
|
let outer = sm.lookup_char_pos(where_outer.lo());
|
2020-02-22 14:07:05 +00:00
|
|
|
let inner = sm.lookup_char_pos(self.where_inner.lo());
|
2014-04-26 20:08:36 +00:00
|
|
|
if outer.file.start_pos == inner.file.start_pos {
|
|
|
|
// mod foo { ... }
|
2021-05-01 13:33:49 +00:00
|
|
|
where_outer
|
2014-04-26 20:08:36 +00:00
|
|
|
} else {
|
2018-08-18 10:13:52 +00:00
|
|
|
// mod foo; (and a separate SourceFile for the contents)
|
2014-04-26 20:08:36 +00:00
|
|
|
self.where_inner
|
|
|
|
}
|
2021-03-24 04:52:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Item::from_hir_id_and_parts(
|
|
|
|
self.id,
|
|
|
|
Some(self.name),
|
|
|
|
ModuleItem(Module { items, span }),
|
|
|
|
cx,
|
|
|
|
)
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Attributes> for [ast::Attribute] {
|
2021-04-25 16:17:11 +00:00
|
|
|
fn clean(&self, _cx: &mut DocContext<'_>) -> Attributes {
|
|
|
|
Attributes::from_ast(self, None)
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<GenericBound> for hir::GenericBound<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> GenericBound {
|
2013-08-15 20:28:54 +00:00
|
|
|
match *self {
|
2018-06-14 11:23:46 +00:00
|
|
|
hir::GenericBound::Outlives(lt) => GenericBound::Outlives(lt.clean(cx)),
|
2020-08-04 13:22:16 +00:00
|
|
|
hir::GenericBound::LangItemTrait(lang_item, span, _, generic_args) => {
|
|
|
|
let def_id = cx.tcx.require_lang_item(lang_item, Some(span));
|
|
|
|
|
2021-09-16 03:43:19 +00:00
|
|
|
let trait_ref = ty::TraitRef::identity(cx.tcx, def_id).skip_binder();
|
2020-08-04 13:22:16 +00:00
|
|
|
|
|
|
|
let generic_args = generic_args.clean(cx);
|
|
|
|
let bindings = match generic_args {
|
|
|
|
GenericArgs::AngleBracketed { bindings, .. } => bindings,
|
|
|
|
_ => bug!("clean: parenthesized `GenericBound::LangItemTrait`"),
|
|
|
|
};
|
|
|
|
|
|
|
|
GenericBound::TraitBound(
|
|
|
|
PolyTrait { trait_: (trait_ref, &*bindings).clean(cx), generic_params: vec![] },
|
|
|
|
hir::TraitBoundModifier::None,
|
|
|
|
)
|
|
|
|
}
|
2018-06-14 11:23:46 +00:00
|
|
|
hir::GenericBound::Trait(ref t, modifier) => {
|
|
|
|
GenericBound::TraitBound(t.clean(cx), modifier)
|
|
|
|
}
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 17:15:19 +00:00
|
|
|
impl Clean<Path> for (ty::TraitRef<'_>, &[TypeBinding]) {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Path {
|
2020-03-23 04:04:03 +00:00
|
|
|
let (trait_ref, bounds) = *self;
|
2021-05-02 19:11:13 +00:00
|
|
|
let kind = cx.tcx.def_kind(trait_ref.def_id).into();
|
|
|
|
if !matches!(kind, ItemType::Trait | ItemType::TraitAlias) {
|
|
|
|
span_bug!(
|
|
|
|
cx.tcx.def_span(trait_ref.def_id),
|
|
|
|
"`TraitRef` had unexpected kind {:?}",
|
|
|
|
kind
|
|
|
|
);
|
|
|
|
}
|
2021-05-02 04:39:59 +00:00
|
|
|
inline::record_extern_fqn(cx, trait_ref.def_id, kind);
|
2021-09-11 22:55:21 +00:00
|
|
|
let path = external_path(cx, trait_ref.def_id, true, bounds.to_vec(), trait_ref.substs);
|
2014-12-16 16:50:52 +00:00
|
|
|
|
2018-03-27 18:45:24 +00:00
|
|
|
debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs);
|
2014-12-16 16:50:52 +00:00
|
|
|
|
2021-08-24 17:15:19 +00:00
|
|
|
path
|
2020-03-23 04:04:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 00:32:54 +00:00
|
|
|
impl Clean<Path> for ty::TraitRef<'tcx> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Path {
|
|
|
|
(*self, &[][..]).clean(cx)
|
2020-03-23 04:04:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<GenericBound> for (ty::PolyTraitRef<'_>, &[TypeBinding]) {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> GenericBound {
|
2020-03-23 04:04:03 +00:00
|
|
|
let (poly_trait_ref, bounds) = *self;
|
|
|
|
let poly_trait_ref = poly_trait_ref.lift_to_tcx(cx.tcx).unwrap();
|
|
|
|
|
2014-12-16 16:50:52 +00:00
|
|
|
// collect any late bound regions
|
2020-03-23 04:04:03 +00:00
|
|
|
let late_bound_regions: Vec<_> = cx
|
|
|
|
.tcx
|
|
|
|
.collect_referenced_late_bound_regions(&poly_trait_ref)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|br| match br {
|
2021-09-01 23:20:14 +00:00
|
|
|
ty::BrNamed(_, name) => Some(GenericParamDef {
|
|
|
|
name,
|
|
|
|
kind: GenericParamDefKind::Lifetime { outlives: vec![] },
|
|
|
|
}),
|
2020-03-23 04:04:03 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect();
|
2014-12-16 16:50:52 +00:00
|
|
|
|
2018-06-14 11:23:46 +00:00
|
|
|
GenericBound::TraitBound(
|
2016-03-24 05:10:52 +00:00
|
|
|
PolyTrait {
|
2020-06-24 21:40:33 +00:00
|
|
|
trait_: (poly_trait_ref.skip_binder(), bounds).clean(cx),
|
2020-03-23 04:04:03 +00:00
|
|
|
generic_params: late_bound_regions,
|
2015-05-25 13:06:38 +00:00
|
|
|
},
|
2019-12-22 22:42:04 +00:00
|
|
|
hir::TraitBoundModifier::None,
|
2016-03-24 05:10:52 +00:00
|
|
|
)
|
2014-05-03 09:08:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<'tcx> Clean<GenericBound> for ty::PolyTraitRef<'tcx> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> GenericBound {
|
2020-03-23 04:04:03 +00:00
|
|
|
(*self, &[][..]).clean(cx)
|
2018-03-27 18:45:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Lifetime> for hir::Lifetime {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Lifetime {
|
2020-04-13 16:52:40 +00:00
|
|
|
let def = cx.tcx.named_region(self.hir_id);
|
2021-10-01 15:12:39 +00:00
|
|
|
if let Some(
|
|
|
|
rl::Region::EarlyBound(_, node_id, _)
|
|
|
|
| rl::Region::LateBound(_, _, node_id, _)
|
|
|
|
| rl::Region::Free(_, node_id),
|
|
|
|
) = def
|
|
|
|
{
|
|
|
|
if let Some(lt) = cx.lt_substs.get(&node_id).cloned() {
|
|
|
|
return lt;
|
2016-09-01 07:21:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-17 13:02:09 +00:00
|
|
|
Lifetime(self.name.ident().name)
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Constant> for hir::ConstArg {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Constant {
|
2021-03-10 20:03:51 +00:00
|
|
|
Constant {
|
2020-04-08 13:53:06 +00:00
|
|
|
type_: cx
|
|
|
|
.tcx
|
|
|
|
.type_of(cx.tcx.hir().body_owner_def_id(self.value.body).to_def_id())
|
|
|
|
.clean(cx),
|
2021-03-10 20:03:51 +00:00
|
|
|
kind: ConstantKind::Anonymous { body: self.value.body },
|
2019-02-15 22:24:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Option<Lifetime>> for ty::RegionKind {
|
|
|
|
fn clean(&self, _cx: &mut DocContext<'_>) -> Option<Lifetime> {
|
2014-05-03 09:08:58 +00:00
|
|
|
match *self {
|
2014-10-05 14:35:04 +00:00
|
|
|
ty::ReStatic => Some(Lifetime::statik()),
|
2020-10-26 18:18:31 +00:00
|
|
|
ty::ReLateBound(_, ty::BoundRegion { kind: ty::BrNamed(_, name), .. }) => {
|
2020-12-18 18:24:55 +00:00
|
|
|
Some(Lifetime(name))
|
|
|
|
}
|
2020-12-17 13:02:09 +00:00
|
|
|
ty::ReEarlyBound(ref data) => Some(Lifetime(data.name)),
|
2014-05-03 09:08:58 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
ty::ReLateBound(..)
|
|
|
|
| ty::ReFree(..)
|
|
|
|
| ty::ReVar(..)
|
|
|
|
| ty::RePlaceholder(..)
|
2019-10-08 23:26:57 +00:00
|
|
|
| ty::ReEmpty(_)
|
2019-12-22 22:42:04 +00:00
|
|
|
| ty::ReErased => {
|
2019-07-23 18:06:00 +00:00
|
|
|
debug!("cannot clean region {:?}", self);
|
2019-01-23 00:54:30 +00:00
|
|
|
None
|
|
|
|
}
|
2014-05-03 09:08:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<WherePredicate> for hir::WherePredicate<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> WherePredicate {
|
2014-12-02 23:03:02 +00:00
|
|
|
match *self {
|
2021-09-02 16:54:32 +00:00
|
|
|
hir::WherePredicate::BoundPredicate(ref wbp) => {
|
|
|
|
let bound_params = wbp
|
|
|
|
.bound_generic_params
|
|
|
|
.into_iter()
|
|
|
|
.map(|param| {
|
|
|
|
// Higher-ranked params must be lifetimes.
|
|
|
|
// Higher-ranked lifetimes can't have bounds.
|
|
|
|
assert_matches!(
|
|
|
|
param,
|
|
|
|
hir::GenericParam {
|
|
|
|
kind: hir::GenericParamKind::Lifetime { .. },
|
|
|
|
bounds: [],
|
|
|
|
..
|
|
|
|
}
|
|
|
|
);
|
|
|
|
Lifetime(param.name.ident().name)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
WherePredicate::BoundPredicate {
|
|
|
|
ty: wbp.bounded_ty.clean(cx),
|
|
|
|
bounds: wbp.bounds.clean(cx),
|
|
|
|
bound_params,
|
|
|
|
}
|
|
|
|
}
|
2014-12-23 09:08:00 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
hir::WherePredicate::RegionPredicate(ref wrp) => WherePredicate::RegionPredicate {
|
|
|
|
lifetime: wrp.lifetime.clean(cx),
|
|
|
|
bounds: wrp.bounds.clean(cx),
|
|
|
|
},
|
2014-12-23 09:08:00 +00:00
|
|
|
|
2017-01-17 18:18:29 +00:00
|
|
|
hir::WherePredicate::EqPredicate(ref wrp) => {
|
2019-12-22 22:42:04 +00:00
|
|
|
WherePredicate::EqPredicate { lhs: wrp.lhs_ty.clean(cx), rhs: wrp.rhs_ty.clean(cx) }
|
2014-12-02 23:03:02 +00:00
|
|
|
}
|
2014-09-25 09:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<'a> Clean<Option<WherePredicate>> for ty::Predicate<'a> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Option<WherePredicate> {
|
2021-01-07 16:20:28 +00:00
|
|
|
let bound_predicate = self.kind();
|
2020-12-17 03:36:14 +00:00
|
|
|
match bound_predicate.skip_binder() {
|
2021-07-22 13:56:07 +00:00
|
|
|
ty::PredicateKind::Trait(pred) => Some(bound_predicate.rebind(pred).clean(cx)),
|
2021-01-07 16:20:28 +00:00
|
|
|
ty::PredicateKind::RegionOutlives(pred) => pred.clean(cx),
|
|
|
|
ty::PredicateKind::TypeOutlives(pred) => pred.clean(cx),
|
|
|
|
ty::PredicateKind::Projection(pred) => Some(pred.clean(cx)),
|
2021-06-12 08:56:25 +00:00
|
|
|
ty::PredicateKind::ConstEvaluatable(..) => None,
|
2021-01-07 16:20:28 +00:00
|
|
|
|
|
|
|
ty::PredicateKind::Subtype(..)
|
2020-11-21 12:06:16 +00:00
|
|
|
| ty::PredicateKind::Coerce(..)
|
2021-01-07 16:20:28 +00:00
|
|
|
| ty::PredicateKind::WellFormed(..)
|
|
|
|
| ty::PredicateKind::ObjectSafe(..)
|
|
|
|
| ty::PredicateKind::ClosureKind(..)
|
|
|
|
| ty::PredicateKind::ConstEquate(..)
|
|
|
|
| ty::PredicateKind::TypeWellFormedFromEnv(..) => panic!("not user writable"),
|
2015-01-11 07:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<'a> Clean<WherePredicate> for ty::PolyTraitPredicate<'a> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> WherePredicate {
|
2020-03-23 04:04:03 +00:00
|
|
|
let poly_trait_ref = self.map_bound(|pred| pred.trait_ref);
|
2015-01-11 07:50:46 +00:00
|
|
|
WherePredicate::BoundPredicate {
|
2020-05-23 18:12:06 +00:00
|
|
|
ty: poly_trait_ref.skip_binder().self_ty().clean(cx),
|
2020-03-23 04:04:03 +00:00
|
|
|
bounds: vec![poly_trait_ref.clean(cx)],
|
2021-05-02 12:48:28 +00:00
|
|
|
bound_params: Vec::new(),
|
2015-01-11 07:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<'tcx> Clean<Option<WherePredicate>>
|
2020-07-18 16:46:30 +00:00
|
|
|
for ty::OutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>>
|
2019-12-22 22:42:04 +00:00
|
|
|
{
|
2020-12-17 00:14:21 +00:00
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Option<WherePredicate> {
|
2020-07-18 16:46:30 +00:00
|
|
|
let ty::OutlivesPredicate(a, b) = self;
|
2019-01-23 00:54:30 +00:00
|
|
|
|
2020-03-29 18:19:14 +00:00
|
|
|
if let (ty::ReEmpty(_), ty::ReEmpty(_)) = (a, b) {
|
|
|
|
return None;
|
2019-01-23 00:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(WherePredicate::RegionPredicate {
|
2018-07-29 13:39:51 +00:00
|
|
|
lifetime: a.clean(cx).expect("failed to clean lifetime"),
|
2019-12-22 22:42:04 +00:00
|
|
|
bounds: vec![GenericBound::Outlives(b.clean(cx).expect("failed to clean bounds"))],
|
2019-01-23 00:54:30 +00:00
|
|
|
})
|
2015-01-11 07:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<'tcx> Clean<Option<WherePredicate>> for ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Option<WherePredicate> {
|
2020-07-18 16:46:30 +00:00
|
|
|
let ty::OutlivesPredicate(ty, lt) = self;
|
2015-01-11 07:50:46 +00:00
|
|
|
|
2020-03-29 18:19:14 +00:00
|
|
|
if let ty::ReEmpty(_) = lt {
|
|
|
|
return None;
|
2019-01-23 00:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(WherePredicate::BoundPredicate {
|
2015-01-11 07:50:46 +00:00
|
|
|
ty: ty.clean(cx),
|
2019-12-22 22:42:04 +00:00
|
|
|
bounds: vec![GenericBound::Outlives(lt.clean(cx).expect("failed to clean lifetimes"))],
|
2021-05-02 12:48:28 +00:00
|
|
|
bound_params: Vec::new(),
|
2019-01-23 00:54:30 +00:00
|
|
|
})
|
2015-01-11 07:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<'tcx> Clean<WherePredicate> for ty::ProjectionPredicate<'tcx> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> WherePredicate {
|
2020-07-18 16:46:30 +00:00
|
|
|
let ty::ProjectionPredicate { projection_ty, ty } = self;
|
2020-03-23 04:04:03 +00:00
|
|
|
WherePredicate::EqPredicate { lhs: projection_ty.clean(cx), rhs: ty.clean(cx) }
|
2015-01-11 07:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Type {
|
2019-12-08 22:02:33 +00:00
|
|
|
let lifted = self.lift_to_tcx(cx.tcx).unwrap();
|
2021-08-27 00:32:54 +00:00
|
|
|
let trait_ = lifted.trait_ref(cx.tcx).clean(cx);
|
2021-05-19 17:37:00 +00:00
|
|
|
let self_type = self.self_ty().clean(cx);
|
2015-01-11 07:50:46 +00:00
|
|
|
Type::QPath {
|
2020-12-16 16:21:08 +00:00
|
|
|
name: cx.tcx.associated_item(self.item_def_id).ident.name,
|
2021-10-22 03:17:47 +00:00
|
|
|
self_def_id: self_type.def_id(&cx.cache),
|
2021-09-20 23:49:47 +00:00
|
|
|
self_type: box self_type,
|
2021-08-27 00:36:48 +00:00
|
|
|
trait_,
|
2015-01-11 07:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<GenericParamDef> for ty::GenericParamDef {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> GenericParamDef {
|
2018-05-27 15:56:01 +00:00
|
|
|
let (name, kind) = match self.kind {
|
2021-09-01 23:20:14 +00:00
|
|
|
ty::GenericParamDefKind::Lifetime => {
|
|
|
|
(self.name, GenericParamDefKind::Lifetime { outlives: vec![] })
|
|
|
|
}
|
2019-06-21 03:23:05 +00:00
|
|
|
ty::GenericParamDefKind::Type { has_default, synthetic, .. } => {
|
2021-06-18 21:30:39 +00:00
|
|
|
let default = if has_default {
|
|
|
|
let mut default = cx.tcx.type_of(self.def_id).clean(cx);
|
|
|
|
|
|
|
|
// We need to reassign the `self_def_id`, if there's a parent (which is the
|
|
|
|
// `Self` type), so we can properly render `<Self as X>` casts, because the
|
|
|
|
// information about which type `Self` is, is only present here, but not in
|
|
|
|
// the cleaning process of the type itself. To resolve this and have the
|
|
|
|
// `self_def_id` set, we override it here.
|
|
|
|
// See https://github.com/rust-lang/rust/issues/85454
|
|
|
|
if let QPath { ref mut self_def_id, .. } = default {
|
|
|
|
*self_def_id = cx.tcx.parent(self.def_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(default)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-12-22 22:42:04 +00:00
|
|
|
(
|
2020-12-16 16:21:08 +00:00
|
|
|
self.name,
|
2019-12-22 22:42:04 +00:00
|
|
|
GenericParamDefKind::Type {
|
|
|
|
did: self.def_id,
|
|
|
|
bounds: vec![], // These are filled in from the where-clauses.
|
2021-10-18 02:38:41 +00:00
|
|
|
default: default.map(Box::new),
|
2019-12-22 22:42:04 +00:00
|
|
|
synthetic,
|
|
|
|
},
|
|
|
|
)
|
2018-05-27 15:56:01 +00:00
|
|
|
}
|
2021-06-03 08:01:25 +00:00
|
|
|
ty::GenericParamDefKind::Const { has_default, .. } => (
|
2020-12-16 16:21:08 +00:00
|
|
|
self.name,
|
2019-12-22 22:42:04 +00:00
|
|
|
GenericParamDefKind::Const {
|
2019-03-13 23:39:56 +00:00
|
|
|
did: self.def_id,
|
2021-10-18 02:48:42 +00:00
|
|
|
ty: Box::new(cx.tcx.type_of(self.def_id).clean(cx)),
|
2021-06-03 08:01:25 +00:00
|
|
|
default: match has_default {
|
2021-10-18 02:38:41 +00:00
|
|
|
true => Some(Box::new(cx.tcx.const_param_default(self.def_id).to_string())),
|
2021-06-03 08:01:25 +00:00
|
|
|
false => None,
|
|
|
|
},
|
2019-12-22 22:42:04 +00:00
|
|
|
},
|
|
|
|
),
|
2018-05-27 15:56:01 +00:00
|
|
|
};
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
GenericParamDef { name, kind }
|
2018-03-24 02:03:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<GenericParamDef> for hir::GenericParam<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> GenericParamDef {
|
2018-05-27 15:56:01 +00:00
|
|
|
let (name, kind) = match self.kind {
|
2018-05-28 12:33:28 +00:00
|
|
|
hir::GenericParamKind::Lifetime { .. } => {
|
2021-09-01 23:20:14 +00:00
|
|
|
let outlives = self
|
|
|
|
.bounds
|
|
|
|
.iter()
|
|
|
|
.map(|bound| match bound {
|
|
|
|
hir::GenericBound::Outlives(lt) => lt.clean(cx),
|
2018-05-28 12:33:28 +00:00
|
|
|
_ => panic!(),
|
2021-09-01 23:20:14 +00:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
(self.name.ident().name, GenericParamDefKind::Lifetime { outlives })
|
2018-05-27 15:56:01 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
hir::GenericParamKind::Type { ref default, synthetic } => (
|
2020-12-16 16:21:08 +00:00
|
|
|
self.name.ident().name,
|
2019-12-22 22:42:04 +00:00
|
|
|
GenericParamDefKind::Type {
|
2020-04-09 08:43:00 +00:00
|
|
|
did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(),
|
2018-05-28 12:33:28 +00:00
|
|
|
bounds: self.bounds.clean(cx),
|
2021-10-18 02:38:41 +00:00
|
|
|
default: default.clean(cx).map(Box::new),
|
2019-10-06 07:42:53 +00:00
|
|
|
synthetic,
|
2019-12-22 22:42:04 +00:00
|
|
|
},
|
|
|
|
),
|
2021-06-03 08:01:25 +00:00
|
|
|
hir::GenericParamKind::Const { ref ty, default } => (
|
2020-12-16 16:21:08 +00:00
|
|
|
self.name.ident().name,
|
2019-12-22 22:42:04 +00:00
|
|
|
GenericParamDefKind::Const {
|
2020-04-09 08:43:00 +00:00
|
|
|
did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(),
|
2021-10-18 02:48:42 +00:00
|
|
|
ty: Box::new(ty.clean(cx)),
|
2021-06-03 08:01:25 +00:00
|
|
|
default: default.map(|ct| {
|
|
|
|
let def_id = cx.tcx.hir().local_def_id(ct.hir_id);
|
2021-10-18 02:38:41 +00:00
|
|
|
Box::new(ty::Const::from_anon_const(cx.tcx, def_id).to_string())
|
2021-06-03 08:01:25 +00:00
|
|
|
}),
|
2019-12-22 22:42:04 +00:00
|
|
|
},
|
|
|
|
),
|
2018-05-27 15:56:01 +00:00
|
|
|
};
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
GenericParamDef { name, kind }
|
2017-10-16 19:07:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Generics> for hir::Generics<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Generics {
|
2018-05-14 03:07:34 +00:00
|
|
|
// Synthetic type-parameters are inserted after normal ones.
|
|
|
|
// In order for normal parameters to be able to refer to synthetic ones,
|
|
|
|
// scans them first.
|
2019-12-01 15:08:58 +00:00
|
|
|
fn is_impl_trait(param: &hir::GenericParam<'_>) -> bool {
|
2018-05-25 23:27:54 +00:00
|
|
|
match param.kind {
|
|
|
|
hir::GenericParamKind::Type { synthetic, .. } => {
|
|
|
|
synthetic == Some(hir::SyntheticTyParamKind::ImplTrait)
|
|
|
|
}
|
|
|
|
_ => false,
|
2018-05-14 03:07:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-23 01:22:47 +00:00
|
|
|
/// This can happen for `async fn`, e.g. `async fn f<'_>(&'_ self)`.
|
|
|
|
///
|
|
|
|
/// See [`lifetime_to_generic_param`] in [`rustc_ast_lowering`] for more information.
|
|
|
|
///
|
|
|
|
/// [`lifetime_to_generic_param`]: rustc_ast_lowering::LoweringContext::lifetime_to_generic_param
|
|
|
|
fn is_elided_lifetime(param: &hir::GenericParam<'_>) -> bool {
|
2020-12-31 01:49:44 +00:00
|
|
|
matches!(
|
|
|
|
param.kind,
|
|
|
|
hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Elided }
|
|
|
|
)
|
2020-12-23 01:22:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
let impl_trait_params = self
|
|
|
|
.params
|
2018-05-14 03:07:34 +00:00
|
|
|
.iter()
|
2018-05-27 15:56:01 +00:00
|
|
|
.filter(|param| is_impl_trait(param))
|
|
|
|
.map(|param| {
|
|
|
|
let param: GenericParamDef = param.clean(cx);
|
|
|
|
match param.kind {
|
2021-09-01 23:20:14 +00:00
|
|
|
GenericParamDefKind::Lifetime { .. } => unreachable!(),
|
2018-05-27 15:56:01 +00:00
|
|
|
GenericParamDefKind::Type { did, ref bounds, .. } => {
|
2021-05-08 08:04:03 +00:00
|
|
|
cx.impl_trait_bounds.insert(did.into(), bounds.clone());
|
2018-05-27 15:56:01 +00:00
|
|
|
}
|
2019-02-15 22:24:00 +00:00
|
|
|
GenericParamDefKind::Const { .. } => unreachable!(),
|
2018-05-14 03:07:34 +00:00
|
|
|
}
|
2018-05-27 15:56:01 +00:00
|
|
|
param
|
2018-05-14 03:07:34 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2018-05-03 13:24:50 +00:00
|
|
|
let mut params = Vec::with_capacity(self.params.len());
|
2020-12-23 01:22:47 +00:00
|
|
|
for p in self.params.iter().filter(|p| !is_impl_trait(p) && !is_elided_lifetime(p)) {
|
2018-05-03 13:24:50 +00:00
|
|
|
let p = p.clean(cx);
|
|
|
|
params.push(p);
|
|
|
|
}
|
2018-05-14 03:07:34 +00:00
|
|
|
params.extend(impl_trait_params);
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
let mut generics =
|
|
|
|
Generics { params, where_predicates: self.where_clause.predicates.clean(cx) };
|
2017-12-17 13:08:09 +00:00
|
|
|
|
|
|
|
// Some duplicates are generated for ?Sized bounds between type params and where
|
|
|
|
// predicates. The point in here is to move the bounds definitions from type params
|
|
|
|
// to where predicates when such cases occur.
|
2018-05-27 15:56:01 +00:00
|
|
|
for where_pred in &mut generics.where_predicates {
|
2017-12-17 13:08:09 +00:00
|
|
|
match *where_pred {
|
2021-05-02 12:48:28 +00:00
|
|
|
WherePredicate::BoundPredicate {
|
|
|
|
ty: Generic(ref name), ref mut bounds, ..
|
|
|
|
} => {
|
2021-08-15 08:17:36 +00:00
|
|
|
if bounds.is_empty() {
|
2018-05-27 15:56:01 +00:00
|
|
|
for param in &mut generics.params {
|
|
|
|
match param.kind {
|
2021-09-01 23:20:14 +00:00
|
|
|
GenericParamDefKind::Lifetime { .. } => {}
|
2018-05-27 15:56:01 +00:00
|
|
|
GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => {
|
|
|
|
if ¶m.name == name {
|
|
|
|
mem::swap(bounds, ty_bounds);
|
2019-12-22 22:42:04 +00:00
|
|
|
break;
|
2018-05-27 15:56:01 +00:00
|
|
|
}
|
2017-10-16 19:07:26 +00:00
|
|
|
}
|
2019-02-15 22:24:00 +00:00
|
|
|
GenericParamDefKind::Const { .. } => {}
|
2017-12-17 13:08:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => continue,
|
|
|
|
}
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
2018-05-27 15:56:01 +00:00
|
|
|
generics
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx>) {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Generics {
|
2015-01-11 07:50:46 +00:00
|
|
|
use self::WherePredicate as WP;
|
2019-07-09 07:37:55 +00:00
|
|
|
use std::collections::BTreeMap;
|
2015-01-11 07:50:46 +00:00
|
|
|
|
2016-08-10 17:39:09 +00:00
|
|
|
let (gens, preds) = *self;
|
2015-02-11 15:28:52 +00:00
|
|
|
|
2019-06-21 03:23:05 +00:00
|
|
|
// Don't populate `cx.impl_trait_bounds` before `clean`ning `where` clauses,
|
|
|
|
// since `Clean for ty::Predicate` would consume them.
|
2019-07-09 07:37:55 +00:00
|
|
|
let mut impl_trait = BTreeMap::<ImplTraitParam, Vec<GenericBound>>::default();
|
2019-06-21 03:23:05 +00:00
|
|
|
|
2015-04-07 04:17:51 +00:00
|
|
|
// Bounds in the type_params and lifetimes fields are repeated in the
|
|
|
|
// predicates field (see rustc_typeck::collect::ty_generics), so remove
|
|
|
|
// them.
|
2020-07-31 21:51:19 +00:00
|
|
|
let stripped_params = gens
|
2019-12-22 22:42:04 +00:00
|
|
|
.params
|
|
|
|
.iter()
|
2019-06-21 03:23:05 +00:00
|
|
|
.filter_map(|param| match param.kind {
|
2020-07-31 21:51:19 +00:00
|
|
|
ty::GenericParamDefKind::Lifetime => Some(param.clean(cx)),
|
2019-06-21 03:23:05 +00:00
|
|
|
ty::GenericParamDefKind::Type { synthetic, .. } => {
|
2019-10-18 02:22:50 +00:00
|
|
|
if param.name == kw::SelfUpper {
|
2019-06-21 03:23:05 +00:00
|
|
|
assert_eq!(param.index, 0);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
if synthetic == Some(hir::SyntheticTyParamKind::ImplTrait) {
|
|
|
|
impl_trait.insert(param.index.into(), vec![]);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(param.clean(cx))
|
2018-04-14 18:20:51 +00:00
|
|
|
}
|
2020-07-31 21:51:19 +00:00
|
|
|
ty::GenericParamDefKind::Const { .. } => Some(param.clean(cx)),
|
2019-12-22 22:42:04 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<GenericParamDef>>();
|
2015-01-11 07:50:46 +00:00
|
|
|
|
2019-07-08 11:42:45 +00:00
|
|
|
// param index -> [(DefId of trait, associated type name, type)]
|
2021-05-08 08:04:03 +00:00
|
|
|
let mut impl_trait_proj = FxHashMap::<u32, Vec<(DefId, Symbol, Ty<'tcx>)>>::default();
|
2019-07-08 08:59:26 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
let where_predicates = preds
|
|
|
|
.predicates
|
|
|
|
.iter()
|
2019-06-21 03:23:05 +00:00
|
|
|
.flat_map(|(p, _)| {
|
2019-07-08 11:42:45 +00:00
|
|
|
let mut projection = None;
|
2019-07-08 08:59:26 +00:00
|
|
|
let param_idx = (|| {
|
2021-01-07 16:20:28 +00:00
|
|
|
let bound_p = p.kind();
|
2020-12-17 03:36:14 +00:00
|
|
|
match bound_p.skip_binder() {
|
2021-07-22 13:56:07 +00:00
|
|
|
ty::PredicateKind::Trait(pred) => {
|
2020-08-03 22:18:29 +00:00
|
|
|
if let ty::Param(param) = pred.self_ty().kind() {
|
2020-06-19 08:05:05 +00:00
|
|
|
return Some(param.index);
|
|
|
|
}
|
2019-07-08 08:59:26 +00:00
|
|
|
}
|
2021-01-07 16:20:28 +00:00
|
|
|
ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) => {
|
2020-08-03 22:18:29 +00:00
|
|
|
if let ty::Param(param) = ty.kind() {
|
2020-06-19 08:05:05 +00:00
|
|
|
return Some(param.index);
|
|
|
|
}
|
2019-07-08 08:59:26 +00:00
|
|
|
}
|
2021-01-07 16:20:28 +00:00
|
|
|
ty::PredicateKind::Projection(p) => {
|
2020-08-03 22:18:29 +00:00
|
|
|
if let ty::Param(param) = p.projection_ty.self_ty().kind() {
|
2020-12-17 03:36:14 +00:00
|
|
|
projection = Some(bound_p.rebind(p));
|
2020-06-19 08:05:05 +00:00
|
|
|
return Some(param.index);
|
|
|
|
}
|
2019-07-08 08:59:26 +00:00
|
|
|
}
|
2020-06-19 08:05:05 +00:00
|
|
|
_ => (),
|
2019-06-21 03:23:05 +00:00
|
|
|
}
|
2019-07-08 08:59:26 +00:00
|
|
|
|
2019-06-21 03:23:05 +00:00
|
|
|
None
|
2019-07-08 08:59:26 +00:00
|
|
|
})();
|
2019-06-21 03:23:05 +00:00
|
|
|
|
2019-07-08 08:59:26 +00:00
|
|
|
if let Some(param_idx) = param_idx {
|
|
|
|
if let Some(b) = impl_trait.get_mut(¶m_idx.into()) {
|
2019-07-09 07:59:34 +00:00
|
|
|
let p = p.clean(cx)?;
|
|
|
|
|
2019-07-08 08:59:26 +00:00
|
|
|
b.extend(
|
|
|
|
p.get_bounds()
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
|
|
|
.cloned()
|
2019-12-22 22:42:04 +00:00
|
|
|
.filter(|b| !b.is_sized_bound(cx)),
|
2019-07-08 08:59:26 +00:00
|
|
|
);
|
|
|
|
|
2019-07-08 11:42:45 +00:00
|
|
|
let proj = projection
|
|
|
|
.map(|p| (p.skip_binder().projection_ty.clean(cx), p.skip_binder().ty));
|
|
|
|
if let Some(((_, trait_did, name), rhs)) =
|
|
|
|
proj.as_ref().and_then(|(lhs, rhs)| Some((lhs.projection()?, rhs)))
|
|
|
|
{
|
2021-08-03 05:24:31 +00:00
|
|
|
impl_trait_proj
|
|
|
|
.entry(param_idx)
|
|
|
|
.or_default()
|
|
|
|
.push((trait_did, name, rhs));
|
2019-07-08 08:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return None;
|
|
|
|
}
|
2019-06-21 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(p)
|
|
|
|
})
|
2018-09-16 17:15:49 +00:00
|
|
|
.collect::<Vec<_>>();
|
2015-02-11 15:28:52 +00:00
|
|
|
|
2019-07-09 07:37:55 +00:00
|
|
|
for (param, mut bounds) in impl_trait {
|
|
|
|
// Move trait bounds to the front.
|
2020-12-31 01:49:44 +00:00
|
|
|
bounds.sort_by_key(|b| !matches!(b, GenericBound::TraitBound(..)));
|
2019-06-21 03:23:05 +00:00
|
|
|
|
2019-07-08 11:42:45 +00:00
|
|
|
if let crate::core::ImplTraitParam::ParamIndex(idx) = param {
|
|
|
|
if let Some(proj) = impl_trait_proj.remove(&idx) {
|
|
|
|
for (trait_did, name, rhs) in proj {
|
2021-02-12 06:59:20 +00:00
|
|
|
let rhs = rhs.clean(cx);
|
2021-05-08 08:04:03 +00:00
|
|
|
simplify::merge_bounds(cx, &mut bounds, trait_did, name, &rhs);
|
2019-07-08 11:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
|
2021-02-19 22:27:30 +00:00
|
|
|
cx.impl_trait_bounds.insert(param, bounds);
|
2019-07-08 11:42:45 +00:00
|
|
|
}
|
2019-06-21 03:23:05 +00:00
|
|
|
|
2019-07-09 07:59:34 +00:00
|
|
|
// Now that `cx.impl_trait_bounds` is populated, we can process
|
|
|
|
// remaining predicates which could contain `impl Trait`.
|
2019-12-22 22:42:04 +00:00
|
|
|
let mut where_predicates =
|
|
|
|
where_predicates.into_iter().flat_map(|p| p.clean(cx)).collect::<Vec<_>>();
|
2019-07-09 07:59:34 +00:00
|
|
|
|
2020-09-12 11:27:57 +00:00
|
|
|
// Type parameters have a Sized bound by default unless removed with
|
2017-12-17 13:08:09 +00:00
|
|
|
// ?Sized. Scan through the predicates and mark any type parameter with
|
2015-04-07 04:17:51 +00:00
|
|
|
// a Sized bound, removing the bounds as we find them.
|
2015-04-07 07:16:35 +00:00
|
|
|
//
|
|
|
|
// Note that associated types also have a sized bound by default, but we
|
2015-05-05 23:49:07 +00:00
|
|
|
// don't actually know the set of associated types right here so that's
|
2015-04-07 07:16:35 +00:00
|
|
|
// handled in cleaning associated types
|
2018-10-16 08:44:26 +00:00
|
|
|
let mut sized_params = FxHashSet::default();
|
2019-12-22 22:42:04 +00:00
|
|
|
where_predicates.retain(|pred| match *pred {
|
2021-05-02 12:48:28 +00:00
|
|
|
WP::BoundPredicate { ty: Generic(ref g), ref bounds, .. } => {
|
2019-12-22 22:42:04 +00:00
|
|
|
if bounds.iter().any(|b| b.is_sized_bound(cx)) {
|
2020-12-29 18:33:48 +00:00
|
|
|
sized_params.insert(*g);
|
2019-12-22 22:42:04 +00:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
2015-01-11 07:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
_ => true,
|
2015-04-07 07:16:35 +00:00
|
|
|
});
|
2015-02-11 15:28:52 +00:00
|
|
|
|
2015-04-07 07:16:35 +00:00
|
|
|
// Run through the type parameters again and insert a ?Sized
|
2015-04-07 04:17:51 +00:00
|
|
|
// unbound for any we didn't find to be Sized.
|
2020-07-31 21:51:19 +00:00
|
|
|
for tp in &stripped_params {
|
|
|
|
if matches!(tp.kind, types::GenericParamDefKind::Type { .. })
|
|
|
|
&& !sized_params.contains(&tp.name)
|
|
|
|
{
|
2015-01-11 07:50:46 +00:00
|
|
|
where_predicates.push(WP::BoundPredicate {
|
2020-12-29 18:33:48 +00:00
|
|
|
ty: Type::Generic(tp.name),
|
2018-06-14 11:08:58 +00:00
|
|
|
bounds: vec![GenericBound::maybe_sized(cx)],
|
2021-05-02 12:48:28 +00:00
|
|
|
bound_params: Vec::new(),
|
2015-01-11 07:50:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// It would be nice to collect all of the bounds on a type and recombine
|
2018-11-27 02:59:49 +00:00
|
|
|
// them if possible, to avoid e.g., `where T: Foo, T: Bar, T: Sized, T: 'a`
|
2015-01-11 07:50:46 +00:00
|
|
|
// and instead see `where T: Foo + Bar + Sized + 'a`
|
|
|
|
|
2014-05-03 09:08:58 +00:00
|
|
|
Generics {
|
2020-07-31 21:51:19 +00:00
|
|
|
params: stripped_params,
|
2015-04-07 19:20:24 +00:00
|
|
|
where_predicates: simplify::where_clauses(cx, where_predicates),
|
2014-05-03 09:08:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
fn clean_fn_or_proc_macro(
|
|
|
|
item: &hir::Item<'_>,
|
|
|
|
sig: &'a hir::FnSig<'a>,
|
|
|
|
generics: &'a hir::Generics<'a>,
|
2020-11-23 02:20:08 +00:00
|
|
|
body_id: hir::BodyId,
|
|
|
|
name: &mut Symbol,
|
2020-12-17 00:14:21 +00:00
|
|
|
cx: &mut DocContext<'_>,
|
2020-11-23 02:20:08 +00:00
|
|
|
) -> ItemKind {
|
2021-01-24 12:17:54 +00:00
|
|
|
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
|
|
|
let macro_kind = attrs.iter().find_map(|a| {
|
2020-11-23 02:20:08 +00:00
|
|
|
if a.has_name(sym::proc_macro) {
|
|
|
|
Some(MacroKind::Bang)
|
|
|
|
} else if a.has_name(sym::proc_macro_derive) {
|
|
|
|
Some(MacroKind::Derive)
|
|
|
|
} else if a.has_name(sym::proc_macro_attribute) {
|
|
|
|
Some(MacroKind::Attr)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
match macro_kind {
|
|
|
|
Some(kind) => {
|
|
|
|
if kind == MacroKind::Derive {
|
2021-01-24 12:17:54 +00:00
|
|
|
*name = attrs
|
2020-11-23 02:20:08 +00:00
|
|
|
.lists(sym::proc_macro_derive)
|
|
|
|
.find_map(|mi| mi.ident())
|
|
|
|
.expect("proc-macro derives require a name")
|
|
|
|
.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut helpers = Vec::new();
|
2021-01-24 12:17:54 +00:00
|
|
|
for mi in attrs.lists(sym::proc_macro_derive) {
|
2020-11-23 02:20:08 +00:00
|
|
|
if !mi.has_name(sym::attributes) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(list) = mi.meta_item_list() {
|
|
|
|
for inner_mi in list {
|
|
|
|
if let Some(ident) = inner_mi.ident() {
|
|
|
|
helpers.push(ident.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-17 13:02:09 +00:00
|
|
|
ProcMacroItem(ProcMacro { kind, helpers })
|
2020-11-23 02:20:08 +00:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let mut func = (sig, generics, body_id).clean(cx);
|
2021-01-30 16:47:51 +00:00
|
|
|
let def_id = item.def_id.to_def_id();
|
2020-11-23 02:20:08 +00:00
|
|
|
func.header.constness =
|
2021-09-15 10:03:03 +00:00
|
|
|
if cx.tcx.is_const_fn(def_id) && is_unstable_const_fn(cx.tcx, def_id).is_none() {
|
2020-11-23 02:20:08 +00:00
|
|
|
hir::Constness::Const
|
|
|
|
} else {
|
|
|
|
hir::Constness::NotConst
|
|
|
|
};
|
|
|
|
FunctionItem(func)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<'a> Clean<Function> for (&'a hir::FnSig<'a>, &'a hir::Generics<'a>, hir::BodyId) {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Function {
|
2019-12-22 22:42:04 +00:00
|
|
|
let (generics, decl) =
|
2021-02-12 06:59:20 +00:00
|
|
|
enter_impl_trait(cx, |cx| (self.1.clean(cx), (&*self.0.decl, self.2).clean(cx)));
|
2021-01-28 15:07:24 +00:00
|
|
|
Function { decl, generics, header: self.0.header }
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], &'a [Ident]) {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Arguments {
|
2016-12-20 20:46:11 +00:00
|
|
|
Arguments {
|
2019-12-22 22:42:04 +00:00
|
|
|
values: self
|
|
|
|
.0
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, ty)| {
|
2021-01-07 19:03:59 +00:00
|
|
|
let mut name = self.1.get(i).map_or(kw::Empty, |ident| ident.name);
|
2019-12-22 22:42:04 +00:00
|
|
|
if name.is_empty() {
|
2020-12-16 16:21:08 +00:00
|
|
|
name = kw::Underscore;
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
|
|
|
Argument { name, type_: ty.clean(cx) }
|
|
|
|
})
|
|
|
|
.collect(),
|
2016-12-20 20:46:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], hir::BodyId) {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Arguments {
|
2018-12-04 12:45:36 +00:00
|
|
|
let body = cx.tcx.hir().body(self.1);
|
2016-12-20 20:46:11 +00:00
|
|
|
|
|
|
|
Arguments {
|
2019-12-22 22:42:04 +00:00
|
|
|
values: self
|
|
|
|
.0
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, ty)| Argument {
|
2021-10-01 15:12:39 +00:00
|
|
|
name: name_from_pat(body.params[i].pat),
|
2016-12-20 20:46:11 +00:00
|
|
|
type_: ty.clean(cx),
|
2019-12-22 22:42:04 +00:00
|
|
|
})
|
|
|
|
.collect(),
|
2016-12-20 20:46:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<'a, A: Copy> Clean<FnDecl> for (&'a hir::FnDecl<'a>, A)
|
2019-12-22 22:42:04 +00:00
|
|
|
where
|
2020-12-17 00:14:21 +00:00
|
|
|
(&'a [hir::Ty<'a>], A): Clean<Arguments>,
|
2016-12-20 20:46:11 +00:00
|
|
|
{
|
2020-12-17 00:14:21 +00:00
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> FnDecl {
|
2013-08-15 20:28:54 +00:00
|
|
|
FnDecl {
|
2020-12-17 00:14:21 +00:00
|
|
|
inputs: (self.0.inputs, self.1).clean(cx),
|
2016-12-20 20:46:11 +00:00
|
|
|
output: self.0.output.clean(cx),
|
2019-08-10 11:38:17 +00:00
|
|
|
c_variadic: self.0.c_variadic,
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> FnDecl {
|
2014-05-24 01:47:01 +00:00
|
|
|
let (did, sig) = *self;
|
2020-04-12 12:45:41 +00:00
|
|
|
let mut names = if did.is_local() { &[] } else { cx.tcx.fn_arg_names(did) }.iter();
|
2018-03-23 13:06:28 +00:00
|
|
|
|
2014-05-03 09:08:58 +00:00
|
|
|
FnDecl {
|
2016-11-29 02:35:38 +00:00
|
|
|
output: Return(sig.skip_binder().output().clean(cx)),
|
2019-08-10 11:38:17 +00:00
|
|
|
c_variadic: sig.skip_binder().c_variadic,
|
2014-05-03 09:08:58 +00:00
|
|
|
inputs: Arguments {
|
2019-12-22 22:42:04 +00:00
|
|
|
values: sig
|
|
|
|
.skip_binder()
|
|
|
|
.inputs()
|
|
|
|
.iter()
|
|
|
|
.map(|t| Argument {
|
2014-09-06 16:13:40 +00:00
|
|
|
type_: t.clean(cx),
|
2021-01-07 19:03:59 +00:00
|
|
|
name: names.next().map_or(kw::Empty, |i| i.name),
|
2019-12-22 22:42:04 +00:00
|
|
|
})
|
|
|
|
.collect(),
|
2014-05-03 09:08:58 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<FnRetTy> for hir::FnRetTy<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> FnRetTy {
|
2013-08-15 20:28:54 +00:00
|
|
|
match *self {
|
2020-01-05 00:50:05 +00:00
|
|
|
Self::Return(ref typ) => Return(typ.clean(cx)),
|
|
|
|
Self::DefaultReturn(..) => DefaultReturn,
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<bool> for hir::IsAuto {
|
2021-02-12 06:59:20 +00:00
|
|
|
fn clean(&self, _: &mut DocContext<'_>) -> bool {
|
2018-01-23 01:04:24 +00:00
|
|
|
match *self {
|
|
|
|
hir::IsAuto::Yes => true,
|
|
|
|
hir::IsAuto::No => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 04:01:56 +00:00
|
|
|
impl Clean<Path> for hir::TraitRef<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Path {
|
2021-02-12 06:59:20 +00:00
|
|
|
let path = self.path.clean(cx);
|
2021-08-24 17:15:19 +00:00
|
|
|
register_res(cx, path.res);
|
|
|
|
path
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<PolyTrait> for hir::PolyTraitRef<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> PolyTrait {
|
2014-12-16 16:50:52 +00:00
|
|
|
PolyTrait {
|
|
|
|
trait_: self.trait_ref.clean(cx),
|
2019-12-22 22:42:04 +00:00
|
|
|
generic_params: self.bound_generic_params.clean(cx),
|
2014-12-16 16:50:52 +00:00
|
|
|
}
|
2014-11-07 11:53:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Item> for hir::TraitItem<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
2021-01-30 19:46:50 +00:00
|
|
|
let local_did = self.def_id.to_def_id();
|
2021-02-12 06:59:20 +00:00
|
|
|
cx.with_param_env(local_did, |cx| {
|
2020-10-18 15:27:16 +00:00
|
|
|
let inner = match self.kind {
|
|
|
|
hir::TraitItemKind::Const(ref ty, default) => {
|
2021-02-11 19:37:36 +00:00
|
|
|
AssocConstItem(ty.clean(cx), default.map(|e| print_const_expr(cx.tcx, e)))
|
2020-07-29 16:15:28 +00:00
|
|
|
}
|
2020-10-18 15:27:16 +00:00
|
|
|
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
|
|
|
|
let mut m = (sig, &self.generics, body).clean(cx);
|
|
|
|
if m.header.constness == hir::Constness::Const
|
|
|
|
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
|
|
|
|
{
|
|
|
|
m.header.constness = hir::Constness::NotConst;
|
|
|
|
}
|
|
|
|
MethodItem(m, None)
|
2020-07-29 16:15:28 +00:00
|
|
|
}
|
2021-10-01 15:12:39 +00:00
|
|
|
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(names)) => {
|
2021-02-12 06:59:20 +00:00
|
|
|
let (generics, decl) = enter_impl_trait(cx, |cx| {
|
2020-10-18 15:27:16 +00:00
|
|
|
(self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
|
|
|
|
});
|
2021-01-28 15:07:24 +00:00
|
|
|
let mut t = Function { header: sig.header, decl, generics };
|
2020-10-18 15:27:16 +00:00
|
|
|
if t.header.constness == hir::Constness::Const
|
|
|
|
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
|
|
|
|
{
|
|
|
|
t.header.constness = hir::Constness::NotConst;
|
|
|
|
}
|
|
|
|
TyMethodItem(t)
|
|
|
|
}
|
2021-10-01 15:12:39 +00:00
|
|
|
hir::TraitItemKind::Type(bounds, ref default) => {
|
2020-10-18 15:27:16 +00:00
|
|
|
AssocTypeItem(bounds.clean(cx), default.clean(cx))
|
|
|
|
}
|
|
|
|
};
|
2021-01-22 23:02:49 +00:00
|
|
|
let what_rustc_thinks =
|
|
|
|
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx);
|
|
|
|
// Trait items always inherit the trait's visibility -- we don't want to show `pub`.
|
|
|
|
Item { visibility: Inherited, ..what_rustc_thinks }
|
2020-10-18 15:27:16 +00:00
|
|
|
})
|
2014-08-04 20:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Item> for hir::ImplItem<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
2021-01-30 22:25:03 +00:00
|
|
|
let local_did = self.def_id.to_def_id();
|
2021-02-12 06:59:20 +00:00
|
|
|
cx.with_param_env(local_did, |cx| {
|
2020-10-18 15:27:16 +00:00
|
|
|
let inner = match self.kind {
|
|
|
|
hir::ImplItemKind::Const(ref ty, expr) => {
|
2021-02-11 19:37:36 +00:00
|
|
|
AssocConstItem(ty.clean(cx), Some(print_const_expr(cx.tcx, expr)))
|
2020-07-29 16:15:28 +00:00
|
|
|
}
|
2020-10-18 15:27:16 +00:00
|
|
|
hir::ImplItemKind::Fn(ref sig, body) => {
|
|
|
|
let mut m = (sig, &self.generics, body).clean(cx);
|
|
|
|
if m.header.constness == hir::Constness::Const
|
|
|
|
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
|
|
|
|
{
|
|
|
|
m.header.constness = hir::Constness::NotConst;
|
|
|
|
}
|
|
|
|
MethodItem(m, Some(self.defaultness))
|
|
|
|
}
|
2021-01-03 20:38:46 +00:00
|
|
|
hir::ImplItemKind::TyAlias(ref hir_ty) => {
|
|
|
|
let type_ = hir_ty.clean(cx);
|
|
|
|
let item_type = hir_ty_to_ty(cx.tcx, hir_ty).clean(cx);
|
2021-01-03 21:08:21 +00:00
|
|
|
TypedefItem(
|
|
|
|
Typedef {
|
|
|
|
type_,
|
|
|
|
generics: Generics::default(),
|
|
|
|
item_type: Some(item_type),
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
)
|
2020-10-18 15:27:16 +00:00
|
|
|
}
|
|
|
|
};
|
2021-01-22 23:02:49 +00:00
|
|
|
|
|
|
|
let what_rustc_thinks =
|
|
|
|
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx);
|
2021-01-30 22:25:03 +00:00
|
|
|
let parent_item = cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(self.hir_id()));
|
2021-01-22 23:02:49 +00:00
|
|
|
if let hir::ItemKind::Impl(impl_) = &parent_item.kind {
|
|
|
|
if impl_.of_trait.is_some() {
|
|
|
|
// Trait impl items always inherit the impl's visibility --
|
|
|
|
// we don't want to show `pub`.
|
|
|
|
Item { visibility: Inherited, ..what_rustc_thinks }
|
|
|
|
} else {
|
|
|
|
what_rustc_thinks
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic!("found impl item with non-impl parent {:?}", parent_item);
|
|
|
|
}
|
2020-10-18 15:27:16 +00:00
|
|
|
})
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Item> for ty::AssocItem {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
2021-02-12 06:59:20 +00:00
|
|
|
let tcx = cx.tcx;
|
2020-11-14 08:45:10 +00:00
|
|
|
let kind = match self.kind {
|
2019-05-19 08:26:08 +00:00
|
|
|
ty::AssocKind::Const => {
|
2021-02-12 06:59:20 +00:00
|
|
|
let ty = tcx.type_of(self.def_id);
|
2017-11-29 22:05:38 +00:00
|
|
|
let default = if self.defaultness.has_value() {
|
2021-03-10 16:31:51 +00:00
|
|
|
Some(inline::print_inlined_const(tcx, self.def_id))
|
2017-11-29 22:05:38 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-05-19 08:26:08 +00:00
|
|
|
AssocConstItem(ty.clean(cx), default)
|
2016-05-08 18:19:29 +00:00
|
|
|
}
|
2020-04-01 02:09:50 +00:00
|
|
|
ty::AssocKind::Fn => {
|
2019-12-22 22:42:04 +00:00
|
|
|
let generics =
|
2021-02-12 06:59:20 +00:00
|
|
|
(tcx.generics_of(self.def_id), tcx.explicit_predicates_of(self.def_id))
|
2019-12-22 22:42:04 +00:00
|
|
|
.clean(cx);
|
2021-02-12 06:59:20 +00:00
|
|
|
let sig = tcx.fn_sig(self.def_id);
|
2017-02-13 08:51:06 +00:00
|
|
|
let mut decl = (self.def_id, sig).clean(cx);
|
2016-11-10 00:06:34 +00:00
|
|
|
|
2020-04-01 02:09:50 +00:00
|
|
|
if self.fn_has_self_parameter {
|
2016-11-10 00:06:34 +00:00
|
|
|
let self_ty = match self.container {
|
2021-02-12 06:59:20 +00:00
|
|
|
ty::ImplContainer(def_id) => tcx.type_of(def_id),
|
|
|
|
ty::TraitContainer(_) => tcx.types.self_param,
|
2016-11-10 00:06:34 +00:00
|
|
|
};
|
2020-06-24 21:40:33 +00:00
|
|
|
let self_arg_ty = sig.input(0).skip_binder();
|
2016-11-10 00:06:34 +00:00
|
|
|
if self_arg_ty == self_ty {
|
2020-12-16 16:21:08 +00:00
|
|
|
decl.inputs.values[0].type_ = Generic(kw::SelfUpper);
|
2020-08-03 22:18:29 +00:00
|
|
|
} else if let ty::Ref(_, ty, _) = *self_arg_ty.kind() {
|
2018-05-02 13:21:05 +00:00
|
|
|
if ty == self_ty {
|
2016-11-10 00:06:34 +00:00
|
|
|
match decl.inputs.values[0].type_ {
|
2019-12-22 22:42:04 +00:00
|
|
|
BorrowedRef { ref mut type_, .. } => {
|
2020-12-16 16:21:08 +00:00
|
|
|
**type_ = Generic(kw::SelfUpper)
|
2016-12-04 01:18:11 +00:00
|
|
|
}
|
2016-11-10 00:06:34 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-14 16:00:02 +00:00
|
|
|
|
2016-11-10 00:06:34 +00:00
|
|
|
let provided = match self.container {
|
2017-11-29 22:05:38 +00:00
|
|
|
ty::ImplContainer(_) => true,
|
2019-12-22 22:42:04 +00:00
|
|
|
ty::TraitContainer(_) => self.defaultness.has_value(),
|
2016-11-10 00:06:34 +00:00
|
|
|
};
|
|
|
|
if provided {
|
2021-04-25 11:59:10 +00:00
|
|
|
let constness = if tcx.is_const_fn_raw(self.def_id) {
|
2017-11-29 22:05:38 +00:00
|
|
|
hir::Constness::Const
|
|
|
|
} else {
|
|
|
|
hir::Constness::NotConst
|
|
|
|
};
|
2021-02-12 06:59:20 +00:00
|
|
|
let asyncness = tcx.asyncness(self.def_id);
|
2019-04-14 22:53:51 +00:00
|
|
|
let defaultness = match self.container {
|
|
|
|
ty::ImplContainer(_) => Some(self.defaultness),
|
|
|
|
ty::TraitContainer(_) => None,
|
|
|
|
};
|
2020-11-17 04:19:58 +00:00
|
|
|
MethodItem(
|
|
|
|
Function {
|
|
|
|
generics,
|
|
|
|
decl,
|
|
|
|
header: hir::FnHeader {
|
|
|
|
unsafety: sig.unsafety(),
|
|
|
|
abi: sig.abi(),
|
|
|
|
constness,
|
|
|
|
asyncness,
|
|
|
|
},
|
2019-03-05 01:29:21 +00:00
|
|
|
},
|
2019-04-14 22:53:51 +00:00
|
|
|
defaultness,
|
2020-11-17 04:19:58 +00:00
|
|
|
)
|
2016-11-10 00:06:34 +00:00
|
|
|
} else {
|
2020-11-17 03:53:10 +00:00
|
|
|
TyMethodItem(Function {
|
2017-08-07 05:54:09 +00:00
|
|
|
generics,
|
|
|
|
decl,
|
2018-05-17 05:55:18 +00:00
|
|
|
header: hir::FnHeader {
|
|
|
|
unsafety: sig.unsafety(),
|
|
|
|
abi: sig.abi(),
|
2018-06-23 00:27:58 +00:00
|
|
|
constness: hir::Constness::NotConst,
|
|
|
|
asyncness: hir::IsAsync::NotAsync,
|
2019-03-07 01:44:28 +00:00
|
|
|
},
|
2016-11-10 00:06:34 +00:00
|
|
|
})
|
2016-05-08 18:19:29 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-19 08:26:08 +00:00
|
|
|
ty::AssocKind::Type => {
|
2020-12-16 16:21:08 +00:00
|
|
|
let my_name = self.ident.name;
|
2016-11-10 00:06:34 +00:00
|
|
|
|
2020-06-30 21:41:57 +00:00
|
|
|
if let ty::TraitContainer(_) = self.container {
|
2021-02-12 06:59:20 +00:00
|
|
|
let bounds = tcx.explicit_item_bounds(self.def_id);
|
2020-06-30 21:41:57 +00:00
|
|
|
let predicates = ty::GenericPredicates { parent: None, predicates: bounds };
|
2021-02-12 06:59:20 +00:00
|
|
|
let generics = (tcx.generics_of(self.def_id), predicates).clean(cx);
|
2019-12-22 22:42:04 +00:00
|
|
|
let mut bounds = generics
|
|
|
|
.where_predicates
|
|
|
|
.iter()
|
|
|
|
.filter_map(|pred| {
|
|
|
|
let (name, self_type, trait_, bounds) = match *pred {
|
|
|
|
WherePredicate::BoundPredicate {
|
2021-05-19 17:37:00 +00:00
|
|
|
ty: QPath { ref name, ref self_type, ref trait_, .. },
|
2019-12-22 22:42:04 +00:00
|
|
|
ref bounds,
|
2021-05-02 12:48:28 +00:00
|
|
|
..
|
2019-12-22 22:42:04 +00:00
|
|
|
} => (name, self_type, trait_, bounds),
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
if *name != my_name {
|
|
|
|
return None;
|
|
|
|
}
|
2021-08-29 03:46:53 +00:00
|
|
|
if trait_.def_id() != self.container.id() {
|
2021-08-24 17:15:19 +00:00
|
|
|
return None;
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
|
|
|
match **self_type {
|
2020-12-16 16:21:08 +00:00
|
|
|
Generic(ref s) if *s == kw::SelfUpper => {}
|
2019-12-22 22:42:04 +00:00
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
Some(bounds)
|
|
|
|
})
|
|
|
|
.flat_map(|i| i.iter().cloned())
|
|
|
|
.collect::<Vec<_>>();
|
2017-11-29 22:05:38 +00:00
|
|
|
// Our Sized/?Sized bound didn't get handled when creating the generics
|
|
|
|
// because we didn't actually get our whole set of bounds until just now
|
|
|
|
// (some of them may have come from the trait). If we do have a sized
|
|
|
|
// bound, we remove it, and if we don't then we add the `?Sized` bound
|
|
|
|
// at the end.
|
|
|
|
match bounds.iter().position(|b| b.is_sized_bound(cx)) {
|
2019-12-22 22:42:04 +00:00
|
|
|
Some(i) => {
|
|
|
|
bounds.remove(i);
|
|
|
|
}
|
2018-06-14 11:08:58 +00:00
|
|
|
None => bounds.push(GenericBound::maybe_sized(cx)),
|
2017-11-29 22:05:38 +00:00
|
|
|
}
|
2016-11-10 00:06:34 +00:00
|
|
|
|
2017-11-29 22:05:38 +00:00
|
|
|
let ty = if self.defaultness.has_value() {
|
2021-02-12 06:59:20 +00:00
|
|
|
Some(tcx.type_of(self.def_id))
|
2017-11-29 22:05:38 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2016-11-10 00:06:34 +00:00
|
|
|
|
2019-05-19 08:26:08 +00:00
|
|
|
AssocTypeItem(bounds, ty.clean(cx))
|
2016-11-10 00:06:34 +00:00
|
|
|
} else {
|
2021-02-14 12:18:17 +00:00
|
|
|
// FIXME: when could this happen? Associated items in inherent impls?
|
2021-02-12 06:59:20 +00:00
|
|
|
let type_ = tcx.type_of(self.def_id).clean(cx);
|
2019-12-22 22:42:04 +00:00
|
|
|
TypedefItem(
|
|
|
|
Typedef {
|
2020-01-10 01:07:13 +00:00
|
|
|
type_,
|
2019-12-22 22:42:04 +00:00
|
|
|
generics: Generics { params: Vec::new(), where_predicates: Vec::new() },
|
2021-01-03 21:08:21 +00:00
|
|
|
item_type: None,
|
2017-11-29 22:05:38 +00:00
|
|
|
},
|
2019-12-22 22:42:04 +00:00
|
|
|
true,
|
|
|
|
)
|
2017-11-29 22:05:38 +00:00
|
|
|
}
|
2015-04-07 18:50:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-15 00:30:36 +00:00
|
|
|
Item::from_def_id_and_parts(self.def_id, Some(self.ident.name), kind, cx)
|
2014-05-03 09:08:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
|
2021-08-27 00:47:29 +00:00
|
|
|
let hir::Ty { hir_id: _, span, ref kind } = *hir_ty;
|
2020-10-02 19:10:49 +00:00
|
|
|
let qpath = match kind {
|
|
|
|
hir::TyKind::Path(qpath) => qpath,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2020-10-02 20:21:43 +00:00
|
|
|
|
2020-10-02 19:10:49 +00:00
|
|
|
match qpath {
|
|
|
|
hir::QPath::Resolved(None, ref path) => {
|
|
|
|
if let Res::Def(DefKind::TyParam, did) = path.res {
|
2021-02-19 22:27:30 +00:00
|
|
|
if let Some(new_ty) = cx.ty_substs.get(&did).cloned() {
|
2020-10-02 19:10:49 +00:00
|
|
|
return new_ty;
|
|
|
|
}
|
2021-05-08 08:04:03 +00:00
|
|
|
if let Some(bounds) = cx.impl_trait_bounds.remove(&did.into()) {
|
2020-10-02 19:10:49 +00:00
|
|
|
return ImplTrait(bounds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 04:21:35 +00:00
|
|
|
if let Some(expanded) = maybe_expand_private_type_alias(cx, path) {
|
|
|
|
expanded
|
|
|
|
} else {
|
|
|
|
let path = path.clean(cx);
|
|
|
|
resolve_type(cx, path)
|
2020-10-02 19:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-01 15:12:39 +00:00
|
|
|
hir::QPath::Resolved(Some(ref qself), p) => {
|
2020-10-02 20:21:43 +00:00
|
|
|
// Try to normalize `<X as Y>::T` to a type
|
|
|
|
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
|
2020-10-18 15:27:16 +00:00
|
|
|
if let Some(normalized_value) = normalize(cx, ty) {
|
2020-10-02 20:21:43 +00:00
|
|
|
return normalized_value.clean(cx);
|
|
|
|
}
|
|
|
|
|
2021-10-03 19:29:17 +00:00
|
|
|
let trait_segments = &p.segments[..p.segments.len() - 1];
|
2021-05-19 17:37:00 +00:00
|
|
|
let trait_def = cx.tcx.associated_item(p.res.def_id()).container.id();
|
2021-08-27 00:36:48 +00:00
|
|
|
let trait_ = self::Path {
|
2021-05-19 17:37:00 +00:00
|
|
|
res: Res::Def(DefKind::Trait, trait_def),
|
2020-10-02 19:10:49 +00:00
|
|
|
segments: trait_segments.clean(cx),
|
|
|
|
};
|
2021-08-27 00:36:48 +00:00
|
|
|
register_res(cx, trait_.res);
|
2020-10-02 19:10:49 +00:00
|
|
|
Type::QPath {
|
2020-12-16 16:21:08 +00:00
|
|
|
name: p.segments.last().expect("segments were empty").ident.name,
|
2021-05-19 17:37:00 +00:00
|
|
|
self_def_id: Some(DefId::local(qself.hir_id.owner.local_def_index)),
|
2021-09-20 23:49:47 +00:00
|
|
|
self_type: box qself.clean(cx),
|
2021-08-27 00:36:48 +00:00
|
|
|
trait_,
|
2020-10-02 19:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-01 15:12:39 +00:00
|
|
|
hir::QPath::TypeRelative(ref qself, segment) => {
|
2020-10-02 19:10:49 +00:00
|
|
|
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
|
2021-09-01 21:59:07 +00:00
|
|
|
let res = match ty.kind() {
|
|
|
|
ty::Projection(proj) => Res::Def(DefKind::Trait, proj.trait_ref(cx.tcx).def_id),
|
|
|
|
// Rustdoc handles `ty::Error`s by turning them into `Type::Infer`s.
|
|
|
|
ty::Error(_) => return Type::Infer,
|
|
|
|
_ => bug!("clean: expected associated type, found `{:?}`", ty),
|
2020-10-02 20:21:43 +00:00
|
|
|
};
|
2021-08-27 00:36:48 +00:00
|
|
|
let trait_ = hir::Path { span, res, segments: &[] }.clean(cx);
|
|
|
|
register_res(cx, trait_.res);
|
2020-10-02 19:10:49 +00:00
|
|
|
Type::QPath {
|
2020-12-16 16:21:08 +00:00
|
|
|
name: segment.ident.name,
|
2021-05-19 17:37:00 +00:00
|
|
|
self_def_id: res.opt_def_id(),
|
2021-09-20 23:49:47 +00:00
|
|
|
self_type: box qself.clean(cx),
|
2021-08-27 00:36:48 +00:00
|
|
|
trait_,
|
2020-10-02 19:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hir::QPath::LangItem(..) => bug!("clean: requiring documentation of lang item"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 04:21:35 +00:00
|
|
|
fn maybe_expand_private_type_alias(cx: &mut DocContext<'_>, path: &hir::Path<'_>) -> Option<Type> {
|
2021-10-31 04:26:38 +00:00
|
|
|
let Res::Def(DefKind::TyAlias, def_id) = path.res else { return None };
|
|
|
|
// Substitute private type aliases
|
|
|
|
let Some(def_id) = def_id.as_local() else { return None };
|
|
|
|
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
|
|
|
|
let alias = if !cx.cache.access_levels.is_exported(def_id.to_def_id()) {
|
|
|
|
&cx.tcx.hir().expect_item(hir_id).kind
|
|
|
|
} else {
|
|
|
|
return None;
|
2021-10-31 04:21:35 +00:00
|
|
|
};
|
2021-10-31 04:26:38 +00:00
|
|
|
let hir::ItemKind::TyAlias(ty, generics) = alias else { return None };
|
2021-10-31 04:21:35 +00:00
|
|
|
|
2021-10-31 04:26:38 +00:00
|
|
|
let provided_params = &path.segments.last().expect("segments were empty");
|
|
|
|
let mut ty_substs = FxHashMap::default();
|
|
|
|
let mut lt_substs = FxHashMap::default();
|
|
|
|
let mut ct_substs = FxHashMap::default();
|
|
|
|
let generic_args = provided_params.args();
|
|
|
|
|
|
|
|
let mut indices: hir::GenericParamCount = Default::default();
|
|
|
|
for param in generics.params.iter() {
|
|
|
|
match param.kind {
|
|
|
|
hir::GenericParamKind::Lifetime { .. } => {
|
|
|
|
let mut j = 0;
|
|
|
|
let lifetime = generic_args.args.iter().find_map(|arg| match arg {
|
|
|
|
hir::GenericArg::Lifetime(lt) => {
|
|
|
|
if indices.lifetimes == j {
|
|
|
|
return Some(lt);
|
2021-10-31 04:21:35 +00:00
|
|
|
}
|
2021-10-31 04:26:38 +00:00
|
|
|
j += 1;
|
|
|
|
None
|
2021-10-31 04:21:35 +00:00
|
|
|
}
|
2021-10-31 04:26:38 +00:00
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
if let Some(lt) = lifetime.cloned() {
|
|
|
|
let lt_def_id = cx.tcx.hir().local_def_id(param.hir_id);
|
|
|
|
let cleaned = if !lt.is_elided() {
|
|
|
|
lt.clean(cx)
|
|
|
|
} else {
|
|
|
|
self::types::Lifetime::elided()
|
|
|
|
};
|
|
|
|
lt_substs.insert(lt_def_id.to_def_id(), cleaned);
|
|
|
|
}
|
|
|
|
indices.lifetimes += 1;
|
|
|
|
}
|
|
|
|
hir::GenericParamKind::Type { ref default, .. } => {
|
|
|
|
let ty_param_def_id = cx.tcx.hir().local_def_id(param.hir_id);
|
|
|
|
let mut j = 0;
|
|
|
|
let type_ = generic_args.args.iter().find_map(|arg| match arg {
|
|
|
|
hir::GenericArg::Type(ty) => {
|
|
|
|
if indices.types == j {
|
|
|
|
return Some(ty);
|
2021-10-31 04:21:35 +00:00
|
|
|
}
|
2021-10-31 04:26:38 +00:00
|
|
|
j += 1;
|
|
|
|
None
|
2021-10-31 04:21:35 +00:00
|
|
|
}
|
2021-10-31 04:26:38 +00:00
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
if let Some(ty) = type_ {
|
|
|
|
ty_substs.insert(ty_param_def_id.to_def_id(), ty.clean(cx));
|
|
|
|
} else if let Some(default) = *default {
|
|
|
|
ty_substs.insert(ty_param_def_id.to_def_id(), default.clean(cx));
|
|
|
|
}
|
|
|
|
indices.types += 1;
|
|
|
|
}
|
|
|
|
hir::GenericParamKind::Const { .. } => {
|
|
|
|
let const_param_def_id = cx.tcx.hir().local_def_id(param.hir_id);
|
|
|
|
let mut j = 0;
|
|
|
|
let const_ = generic_args.args.iter().find_map(|arg| match arg {
|
|
|
|
hir::GenericArg::Const(ct) => {
|
|
|
|
if indices.consts == j {
|
|
|
|
return Some(ct);
|
2021-10-31 04:21:35 +00:00
|
|
|
}
|
2021-10-31 04:26:38 +00:00
|
|
|
j += 1;
|
|
|
|
None
|
2021-10-31 04:21:35 +00:00
|
|
|
}
|
2021-10-31 04:26:38 +00:00
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
if let Some(ct) = const_ {
|
|
|
|
ct_substs.insert(const_param_def_id.to_def_id(), ct.clean(cx));
|
2021-10-31 04:21:35 +00:00
|
|
|
}
|
2021-10-31 04:26:38 +00:00
|
|
|
// FIXME(const_generics_defaults)
|
|
|
|
indices.consts += 1;
|
2021-10-31 04:21:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-31 04:26:38 +00:00
|
|
|
|
|
|
|
Some(cx.enter_alias(ty_substs, lt_substs, ct_substs, |cx| ty.clean(cx)))
|
2021-10-31 04:21:35 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Type> for hir::Ty<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Type {
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir::*;
|
2018-08-20 22:43:02 +00:00
|
|
|
|
2019-09-26 16:25:31 +00:00
|
|
|
match self.kind {
|
2021-09-27 12:20:52 +00:00
|
|
|
TyKind::Never => Primitive(PrimitiveType::Never),
|
2021-09-20 23:49:47 +00:00
|
|
|
TyKind::Ptr(ref m) => RawPointer(m.mutbl, box m.ty.clean(cx)),
|
2018-07-11 15:36:06 +00:00
|
|
|
TyKind::Rptr(ref l, ref m) => {
|
2020-12-23 01:22:47 +00:00
|
|
|
// There are two times a `Fresh` lifetime can be created:
|
|
|
|
// 1. For `&'_ x`, written by the user. This corresponds to `lower_lifetime` in `rustc_ast_lowering`.
|
|
|
|
// 2. For `&x` as a parameter to an `async fn`. This corresponds to `elided_ref_lifetime in `rustc_ast_lowering`.
|
2020-12-23 02:26:17 +00:00
|
|
|
// See #59286 for more information.
|
2020-12-23 01:22:47 +00:00
|
|
|
// Ideally we would only hide the `'_` for case 2., but I don't know a way to distinguish it.
|
|
|
|
// Turning `fn f(&'_ self)` into `fn f(&self)` isn't the worst thing in the world, though;
|
|
|
|
// there's no case where it could cause the function to fail to compile.
|
|
|
|
let elided =
|
|
|
|
l.is_elided() || matches!(l.name, LifetimeName::Param(ParamName::Fresh(_)));
|
|
|
|
let lifetime = if elided { None } else { Some(l.clean(cx)) };
|
2021-09-20 23:49:47 +00:00
|
|
|
BorrowedRef { lifetime, mutability: m.mutbl, type_: box m.ty.clean(cx) }
|
2017-01-09 15:46:11 +00:00
|
|
|
}
|
2021-09-20 23:49:47 +00:00
|
|
|
TyKind::Slice(ref ty) => Slice(box ty.clean(cx)),
|
2018-07-11 15:36:06 +00:00
|
|
|
TyKind::Array(ref ty, ref length) => {
|
2019-06-27 09:28:14 +00:00
|
|
|
let def_id = cx.tcx.hir().local_def_id(length.hir_id);
|
2020-09-03 19:34:39 +00:00
|
|
|
// NOTE(min_const_generics): We can't use `const_eval_poly` for constants
|
|
|
|
// as we currently do not supply the parent generics to anonymous constants
|
|
|
|
// but do allow `ConstKind::Param`.
|
|
|
|
//
|
|
|
|
// `const_eval_poly` tries to to first substitute generic parameters which
|
|
|
|
// results in an ICE while manually constructing the constant and using `eval`
|
|
|
|
// does nothing for `ConstKind::Param`.
|
|
|
|
let ct = ty::Const::from_anon_const(cx.tcx, def_id);
|
|
|
|
let param_env = cx.tcx.param_env(def_id);
|
|
|
|
let length = print_const(cx, ct.eval(cx.tcx, param_env));
|
2021-09-20 23:49:47 +00:00
|
|
|
Array(box ty.clean(cx), length)
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2021-10-01 15:12:39 +00:00
|
|
|
TyKind::Tup(tys) => Tuple(tys.clean(cx)),
|
2020-06-07 17:56:17 +00:00
|
|
|
TyKind::OpaqueDef(item_id, _) => {
|
2021-01-30 11:06:04 +00:00
|
|
|
let item = cx.tcx.hir().item(item_id);
|
2019-09-26 16:51:36 +00:00
|
|
|
if let hir::ItemKind::OpaqueTy(ref ty) = item.kind {
|
2018-10-02 10:31:05 +00:00
|
|
|
ImplTrait(ty.bounds.clean(cx))
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
2021-10-01 15:12:39 +00:00
|
|
|
TyKind::Path(_) => clean_qpath(self, cx),
|
|
|
|
TyKind::TraitObject(bounds, ref lifetime, _) => {
|
2021-06-18 19:47:42 +00:00
|
|
|
let bounds = bounds.iter().map(|bound| bound.clean(cx)).collect();
|
|
|
|
let lifetime = if !lifetime.is_elided() { Some(lifetime.clean(cx)) } else { None };
|
|
|
|
DynTrait(bounds, lifetime)
|
2014-03-01 01:46:09 +00:00
|
|
|
}
|
2021-09-20 23:49:47 +00:00
|
|
|
TyKind::BareFn(ref barefn) => BareFunction(box barefn.clean(cx)),
|
2021-09-01 21:59:07 +00:00
|
|
|
// Rustdoc handles `TyKind::Err`s by turning them into `Type::Infer`s.
|
2018-07-11 15:36:06 +00:00
|
|
|
TyKind::Infer | TyKind::Err => Infer,
|
2019-09-26 16:25:31 +00:00
|
|
|
TyKind::Typeof(..) => panic!("unimplemented type {:?}", self.kind),
|
2013-09-24 20:53:09 +00:00
|
|
|
}
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 20:21:43 +00:00
|
|
|
/// Returns `None` if the type could not be normalized
|
2021-02-12 06:59:20 +00:00
|
|
|
fn normalize(cx: &mut DocContext<'tcx>, ty: Ty<'_>) -> Option<Ty<'tcx>> {
|
2020-11-29 00:19:41 +00:00
|
|
|
// HACK: low-churn fix for #79459 while we wait for a trait normalization fix
|
|
|
|
if !cx.tcx.sess.opts.debugging_opts.normalize_docs {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-10-02 20:21:43 +00:00
|
|
|
use crate::rustc_trait_selection::infer::TyCtxtInferExt;
|
|
|
|
use crate::rustc_trait_selection::traits::query::normalize::AtExt;
|
|
|
|
use rustc_middle::traits::ObligationCause;
|
|
|
|
|
|
|
|
// Try to normalize `<X as Y>::T` to a type
|
2020-10-18 15:27:16 +00:00
|
|
|
let lifted = ty.lift_to_tcx(cx.tcx).unwrap();
|
|
|
|
let normalized = cx.tcx.infer_ctxt().enter(|infcx| {
|
2020-10-02 20:21:43 +00:00
|
|
|
infcx
|
2021-02-21 01:12:22 +00:00
|
|
|
.at(&ObligationCause::dummy(), cx.param_env)
|
2020-10-02 20:21:43 +00:00
|
|
|
.normalize(lifted)
|
|
|
|
.map(|resolved| infcx.resolve_vars_if_possible(resolved.value))
|
|
|
|
});
|
|
|
|
match normalized {
|
|
|
|
Ok(normalized_value) => {
|
2020-11-26 12:59:51 +00:00
|
|
|
debug!("normalized {:?} to {:?}", ty, normalized_value);
|
2020-10-02 20:21:43 +00:00
|
|
|
Some(normalized_value)
|
|
|
|
}
|
|
|
|
Err(err) => {
|
2020-11-26 12:59:51 +00:00
|
|
|
debug!("failed to normalize {:?}: {:?}", ty, err);
|
2020-10-02 20:21:43 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<'tcx> Clean<Type> for Ty<'tcx> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Type {
|
2021-07-11 03:02:10 +00:00
|
|
|
trace!("cleaning type: {:?}", self);
|
2020-11-26 12:59:51 +00:00
|
|
|
let ty = normalize(cx, self).unwrap_or(self);
|
2020-10-02 20:21:43 +00:00
|
|
|
match *ty.kind() {
|
2021-09-27 12:20:52 +00:00
|
|
|
ty::Never => Primitive(PrimitiveType::Never),
|
2018-08-22 00:35:55 +00:00
|
|
|
ty::Bool => Primitive(PrimitiveType::Bool),
|
|
|
|
ty::Char => Primitive(PrimitiveType::Char),
|
|
|
|
ty::Int(int_ty) => Primitive(int_ty.into()),
|
|
|
|
ty::Uint(uint_ty) => Primitive(uint_ty.into()),
|
|
|
|
ty::Float(float_ty) => Primitive(float_ty.into()),
|
|
|
|
ty::Str => Primitive(PrimitiveType::Str),
|
2021-09-20 23:49:47 +00:00
|
|
|
ty::Slice(ty) => Slice(box ty.clean(cx)),
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Array(ty, n) => {
|
2020-10-16 19:59:49 +00:00
|
|
|
let mut n = cx.tcx.lift(n).expect("array lift failed");
|
2019-11-29 19:42:56 +00:00
|
|
|
n = n.eval(cx.tcx, ty::ParamEnv::reveal_all());
|
2018-01-29 09:32:11 +00:00
|
|
|
let n = print_const(cx, n);
|
2021-09-20 23:49:47 +00:00
|
|
|
Array(box ty.clean(cx), n)
|
|
|
|
}
|
|
|
|
ty::RawPtr(mt) => RawPointer(mt.mutbl, box mt.ty.clean(cx)),
|
|
|
|
ty::Ref(r, ty, mutbl) => {
|
|
|
|
BorrowedRef { lifetime: r.clean(cx), mutability: mutbl, type_: box ty.clean(cx) }
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
|
|
|
ty::FnDef(..) | ty::FnPtr(_) => {
|
2020-10-16 19:59:49 +00:00
|
|
|
let ty = cx.tcx.lift(*self).expect("FnPtr lift failed");
|
2017-05-13 14:11:52 +00:00
|
|
|
let sig = ty.fn_sig(cx.tcx);
|
2020-05-30 11:30:58 +00:00
|
|
|
let def_id = DefId::local(CRATE_DEF_INDEX);
|
2021-09-20 23:49:47 +00:00
|
|
|
BareFunction(box BareFunctionDecl {
|
2017-05-13 14:11:52 +00:00
|
|
|
unsafety: sig.unsafety(),
|
2017-10-16 19:07:26 +00:00
|
|
|
generic_params: Vec::new(),
|
2020-05-30 11:30:58 +00:00
|
|
|
decl: (def_id, sig).clean(cx),
|
2017-05-13 14:11:52 +00:00
|
|
|
abi: sig.abi(),
|
2021-09-20 23:49:47 +00:00
|
|
|
})
|
2017-05-13 14:11:52 +00:00
|
|
|
}
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Adt(def, substs) => {
|
2015-07-20 19:13:36 +00:00
|
|
|
let did = def.did;
|
2016-09-05 22:26:02 +00:00
|
|
|
let kind = match def.adt_kind() {
|
2021-04-23 01:23:11 +00:00
|
|
|
AdtKind::Struct => ItemType::Struct,
|
|
|
|
AdtKind::Union => ItemType::Union,
|
|
|
|
AdtKind::Enum => ItemType::Enum,
|
2014-05-03 09:08:58 +00:00
|
|
|
};
|
2015-12-05 10:58:04 +00:00
|
|
|
inline::record_extern_fqn(cx, did, kind);
|
2021-09-11 22:55:21 +00:00
|
|
|
let path = external_path(cx, did, false, vec![], substs);
|
2021-08-23 03:08:48 +00:00
|
|
|
ResolvedPath { path, did }
|
2014-05-03 09:08:58 +00:00
|
|
|
}
|
2018-08-22 00:35:29 +00:00
|
|
|
ty::Foreign(did) => {
|
2021-04-23 01:23:11 +00:00
|
|
|
inline::record_extern_fqn(cx, did, ItemType::ForeignType);
|
2021-09-11 22:55:21 +00:00
|
|
|
let path = external_path(cx, did, false, vec![], InternalSubsts::empty());
|
2021-08-23 03:08:48 +00:00
|
|
|
ResolvedPath { path, did }
|
2017-09-03 18:53:58 +00:00
|
|
|
}
|
2021-10-01 15:12:39 +00:00
|
|
|
ty::Dynamic(obj, ref reg) => {
|
2018-12-15 15:35:55 +00:00
|
|
|
// HACK: pick the first `did` as the `did` of the trait object. Someone
|
|
|
|
// might want to implement "native" support for marker-trait-only
|
|
|
|
// trait objects.
|
|
|
|
let mut dids = obj.principal_def_id().into_iter().chain(obj.auto_traits());
|
2019-12-22 22:42:04 +00:00
|
|
|
let did = dids
|
|
|
|
.next()
|
|
|
|
.unwrap_or_else(|| panic!("found trait object `{:?}` with no traits?", self));
|
2018-12-15 15:35:55 +00:00
|
|
|
let substs = match obj.principal() {
|
|
|
|
Some(principal) => principal.skip_binder().substs,
|
|
|
|
// marker traits have no substs.
|
2019-12-22 22:42:04 +00:00
|
|
|
_ => cx.tcx.intern_substs(&[]),
|
2018-12-15 15:35:55 +00:00
|
|
|
};
|
|
|
|
|
2021-04-23 01:23:11 +00:00
|
|
|
inline::record_extern_fqn(cx, did, ItemType::Trait);
|
2018-10-13 17:41:27 +00:00
|
|
|
|
2021-06-18 19:47:42 +00:00
|
|
|
let lifetime = reg.clean(cx);
|
|
|
|
let mut bounds = vec![];
|
|
|
|
|
2018-12-15 15:35:55 +00:00
|
|
|
for did in dids {
|
2018-10-13 17:41:27 +00:00
|
|
|
let empty = cx.tcx.intern_substs(&[]);
|
2021-09-11 22:55:21 +00:00
|
|
|
let path = external_path(cx, did, false, vec![], empty);
|
2021-04-23 01:23:11 +00:00
|
|
|
inline::record_extern_fqn(cx, did, ItemType::Trait);
|
2021-08-24 17:15:19 +00:00
|
|
|
let bound = PolyTrait { trait_: path, generic_params: Vec::new() };
|
2021-06-18 19:47:42 +00:00
|
|
|
bounds.push(bound);
|
2018-10-13 17:41:27 +00:00
|
|
|
}
|
2016-11-12 22:46:16 +00:00
|
|
|
|
2018-10-13 17:41:27 +00:00
|
|
|
let mut bindings = vec![];
|
|
|
|
for pb in obj.projection_bounds() {
|
|
|
|
bindings.push(TypeBinding {
|
2020-12-16 16:21:08 +00:00
|
|
|
name: cx.tcx.associated_item(pb.item_def_id()).ident.name,
|
2019-12-22 22:42:04 +00:00
|
|
|
kind: TypeBindingKind::Equality { ty: pb.skip_binder().ty.clean(cx) },
|
2018-10-13 17:41:27 +00:00
|
|
|
});
|
|
|
|
}
|
2016-08-04 12:52:57 +00:00
|
|
|
|
2021-09-11 22:55:21 +00:00
|
|
|
let path = external_path(cx, did, false, bindings, substs);
|
2021-08-24 17:15:19 +00:00
|
|
|
bounds.insert(0, PolyTrait { trait_: path, generic_params: Vec::new() });
|
2021-06-18 19:47:42 +00:00
|
|
|
|
|
|
|
DynTrait(bounds, lifetime)
|
2014-12-26 09:36:04 +00:00
|
|
|
}
|
2021-10-01 15:12:39 +00:00
|
|
|
ty::Tuple(t) => Tuple(t.iter().map(|t| t.expect_ty()).collect::<Vec<_>>().clean(cx)),
|
2014-05-03 09:08:58 +00:00
|
|
|
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Projection(ref data) => data.clean(cx),
|
2014-12-17 19:16:28 +00:00
|
|
|
|
2019-06-21 03:23:05 +00:00
|
|
|
ty::Param(ref p) => {
|
2021-02-19 22:27:30 +00:00
|
|
|
if let Some(bounds) = cx.impl_trait_bounds.remove(&p.index.into()) {
|
2019-06-21 03:23:05 +00:00
|
|
|
ImplTrait(bounds)
|
|
|
|
} else {
|
2020-12-16 16:21:08 +00:00
|
|
|
Generic(p.name)
|
2019-06-21 03:23:05 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-03 09:08:58 +00:00
|
|
|
|
2018-08-23 19:51:32 +00:00
|
|
|
ty::Opaque(def_id, substs) => {
|
2016-07-22 15:56:22 +00:00
|
|
|
// Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
|
2020-06-30 21:41:57 +00:00
|
|
|
// by looking up the bounds associated with the def_id.
|
2020-10-16 19:59:49 +00:00
|
|
|
let substs = cx.tcx.lift(substs).expect("Opaque lift failed");
|
2020-06-30 21:41:57 +00:00
|
|
|
let bounds = cx
|
|
|
|
.tcx
|
|
|
|
.explicit_item_bounds(def_id)
|
|
|
|
.iter()
|
|
|
|
.map(|(bound, _)| bound.subst(cx.tcx, substs))
|
|
|
|
.collect::<Vec<_>>();
|
2018-03-27 19:26:44 +00:00
|
|
|
let mut regions = vec![];
|
|
|
|
let mut has_sized = false;
|
2019-12-22 22:42:04 +00:00
|
|
|
let mut bounds = bounds
|
|
|
|
.iter()
|
2020-06-30 21:41:57 +00:00
|
|
|
.filter_map(|bound| {
|
2021-01-07 16:20:28 +00:00
|
|
|
let bound_predicate = bound.kind();
|
2020-12-17 03:36:14 +00:00
|
|
|
let trait_ref = match bound_predicate.skip_binder() {
|
2021-07-22 13:56:07 +00:00
|
|
|
ty::PredicateKind::Trait(tr) => bound_predicate.rebind(tr.trait_ref),
|
2021-01-07 16:20:28 +00:00
|
|
|
ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(_ty, reg)) => {
|
2020-07-18 16:46:30 +00:00
|
|
|
if let Some(r) = reg.clean(cx) {
|
2020-06-21 12:42:47 +00:00
|
|
|
regions.push(GenericBound::Outlives(r));
|
|
|
|
}
|
|
|
|
return None;
|
2020-04-24 20:58:41 +00:00
|
|
|
}
|
2020-06-21 12:42:47 +00:00
|
|
|
_ => return None,
|
2019-12-22 22:42:04 +00:00
|
|
|
};
|
2018-03-27 18:59:09 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
if let Some(sized) = cx.tcx.lang_items().sized_trait() {
|
|
|
|
if trait_ref.def_id() == sized {
|
|
|
|
has_sized = true;
|
|
|
|
return None;
|
2018-03-27 18:45:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 04:04:03 +00:00
|
|
|
let bounds: Vec<_> = bounds
|
2019-12-22 22:42:04 +00:00
|
|
|
.iter()
|
2020-06-30 21:41:57 +00:00
|
|
|
.filter_map(|bound| {
|
2021-01-07 16:20:28 +00:00
|
|
|
if let ty::PredicateKind::Projection(proj) =
|
|
|
|
bound.kind().skip_binder()
|
2020-06-19 08:05:05 +00:00
|
|
|
{
|
2019-12-22 22:42:04 +00:00
|
|
|
if proj.projection_ty.trait_ref(cx.tcx)
|
2020-06-24 21:40:33 +00:00
|
|
|
== trait_ref.skip_binder()
|
2019-12-22 22:42:04 +00:00
|
|
|
{
|
|
|
|
Some(TypeBinding {
|
|
|
|
name: cx
|
|
|
|
.tcx
|
|
|
|
.associated_item(proj.projection_ty.item_def_id)
|
|
|
|
.ident
|
2020-12-16 16:21:08 +00:00
|
|
|
.name,
|
2019-12-22 22:42:04 +00:00
|
|
|
kind: TypeBindingKind::Equality {
|
|
|
|
ty: proj.ty.clean(cx),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2020-03-23 04:04:03 +00:00
|
|
|
Some((trait_ref, &bounds[..]).clean(cx))
|
2019-12-22 22:42:04 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2018-03-27 19:26:44 +00:00
|
|
|
bounds.extend(regions);
|
|
|
|
if !has_sized && !bounds.is_empty() {
|
2018-06-14 11:08:58 +00:00
|
|
|
bounds.insert(0, GenericBound::maybe_sized(cx));
|
2018-03-27 19:26:44 +00:00
|
|
|
}
|
|
|
|
ImplTrait(bounds)
|
2016-07-22 15:56:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Closure(..) | ty::Generator(..) => Tuple(vec![]), // FIXME(pcwalton)
|
2014-05-29 05:26:56 +00:00
|
|
|
|
2018-10-22 18:37:56 +00:00
|
|
|
ty::Bound(..) => panic!("Bound"),
|
2018-11-02 17:48:24 +00:00
|
|
|
ty::Placeholder(..) => panic!("Placeholder"),
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::GeneratorWitness(..) => panic!("GeneratorWitness"),
|
|
|
|
ty::Infer(..) => panic!("Infer"),
|
2020-05-06 04:02:09 +00:00
|
|
|
ty::Error(_) => panic!("Error"),
|
2014-05-03 09:08:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Constant {
|
2021-03-10 16:31:51 +00:00
|
|
|
// FIXME: instead of storing the stringified expression, store `self` directly instead.
|
2021-03-10 20:03:51 +00:00
|
|
|
Constant {
|
|
|
|
type_: self.ty.clean(cx),
|
|
|
|
kind: ConstantKind::TyConst { expr: self.to_string() },
|
|
|
|
}
|
2019-03-13 23:38:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Item> for hir::FieldDef<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
2020-11-15 19:09:26 +00:00
|
|
|
let what_rustc_thinks = Item::from_hir_id_and_parts(
|
|
|
|
self.hir_id,
|
|
|
|
Some(self.ident.name),
|
|
|
|
StructFieldItem(self.ty.clean(cx)),
|
|
|
|
cx,
|
|
|
|
);
|
|
|
|
// Don't show `pub` for fields on enum variants; they are always public
|
|
|
|
Item { visibility: self.vis.clean(cx), ..what_rustc_thinks }
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Item> for ty::FieldDef {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
2020-11-15 19:09:26 +00:00
|
|
|
let what_rustc_thinks = Item::from_def_id_and_parts(
|
|
|
|
self.did,
|
2020-12-15 00:30:36 +00:00
|
|
|
Some(self.ident.name),
|
2020-11-15 19:09:26 +00:00
|
|
|
StructFieldItem(cx.tcx.type_of(self.did).clean(cx)),
|
|
|
|
cx,
|
|
|
|
);
|
|
|
|
// Don't show `pub` for fields on enum variants; they are always public
|
|
|
|
Item { visibility: self.vis.clean(cx), ..what_rustc_thinks }
|
2014-05-23 07:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Visibility> for hir::Visibility<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Visibility {
|
2019-09-13 02:32:12 +00:00
|
|
|
match self.node {
|
2018-07-01 18:05:10 +00:00
|
|
|
hir::VisibilityKind::Public => Visibility::Public,
|
|
|
|
hir::VisibilityKind::Inherited => Visibility::Inherited,
|
2020-11-15 19:09:26 +00:00
|
|
|
hir::VisibilityKind::Crate(_) => {
|
|
|
|
let krate = DefId::local(CRATE_DEF_INDEX);
|
2020-12-16 23:10:04 +00:00
|
|
|
Visibility::Restricted(krate)
|
2020-11-15 19:09:26 +00:00
|
|
|
}
|
2018-07-01 18:05:10 +00:00
|
|
|
hir::VisibilityKind::Restricted { ref path, .. } => {
|
2018-05-12 17:25:09 +00:00
|
|
|
let path = path.clean(cx);
|
2019-04-20 16:36:05 +00:00
|
|
|
let did = register_res(cx, path.res);
|
2020-12-16 23:10:04 +00:00
|
|
|
Visibility::Restricted(did)
|
2018-05-12 17:25:09 +00:00
|
|
|
}
|
2019-09-13 02:32:12 +00:00
|
|
|
}
|
2016-03-25 06:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Visibility> for ty::Visibility {
|
|
|
|
fn clean(&self, _cx: &mut DocContext<'_>) -> Visibility {
|
2020-11-15 19:09:26 +00:00
|
|
|
match *self {
|
|
|
|
ty::Visibility::Public => Visibility::Public,
|
2020-12-16 23:10:04 +00:00
|
|
|
// NOTE: this is not quite right: `ty` uses `Invisible` to mean 'private',
|
|
|
|
// while rustdoc really does mean inherited. That means that for enum variants, such as
|
|
|
|
// `pub enum E { V }`, `V` will be marked as `Public` by `ty`, but as `Inherited` by rustdoc.
|
|
|
|
// This is the main reason `impl Clean for hir::Visibility` still exists; various parts of clean
|
|
|
|
// override `tcx.visibility` explicitly to make sure this distinction is captured.
|
2020-11-15 19:09:26 +00:00
|
|
|
ty::Visibility::Invisible => Visibility::Inherited,
|
2020-12-16 23:10:04 +00:00
|
|
|
ty::Visibility::Restricted(module) => Visibility::Restricted(module),
|
2020-11-15 19:09:26 +00:00
|
|
|
}
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<VariantStruct> for rustc_hir::VariantData<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> VariantStruct {
|
2013-08-15 20:28:54 +00:00
|
|
|
VariantStruct {
|
2021-01-20 22:19:46 +00:00
|
|
|
struct_type: CtorKind::from_hir(self),
|
2015-10-25 15:33:51 +00:00
|
|
|
fields: self.fields().iter().map(|x| x.clean(cx)).collect(),
|
2013-10-14 03:37:43 +00:00
|
|
|
fields_stripped: false,
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 12:43:22 +00:00
|
|
|
impl Clean<Vec<Item>> for hir::VariantData<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Vec<Item> {
|
|
|
|
self.fields().iter().map(|x| x.clean(cx)).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Item> for ty::VariantDef {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
2016-09-14 21:51:46 +00:00
|
|
|
let kind = match self.ctor_kind {
|
2021-01-15 06:17:23 +00:00
|
|
|
CtorKind::Const => Variant::CLike,
|
|
|
|
CtorKind::Fn => Variant::Tuple(
|
2021-08-27 12:43:22 +00:00
|
|
|
self.fields
|
|
|
|
.iter()
|
|
|
|
.map(|field| {
|
|
|
|
let name = Some(field.ident.name);
|
|
|
|
let kind = StructFieldItem(cx.tcx.type_of(field.did).clean(cx));
|
|
|
|
let what_rustc_thinks =
|
|
|
|
Item::from_def_id_and_parts(field.did, name, kind, cx);
|
|
|
|
// don't show `pub` for fields, which are always public
|
|
|
|
Item { visibility: Visibility::Inherited, ..what_rustc_thinks }
|
|
|
|
})
|
|
|
|
.collect(),
|
2019-12-22 22:42:04 +00:00
|
|
|
),
|
2021-01-15 06:17:23 +00:00
|
|
|
CtorKind::Fictive => Variant::Struct(VariantStruct {
|
2021-01-20 22:19:46 +00:00
|
|
|
struct_type: CtorKind::Fictive,
|
2019-12-22 22:42:04 +00:00
|
|
|
fields_stripped: false,
|
|
|
|
fields: self
|
|
|
|
.fields
|
|
|
|
.iter()
|
2020-11-24 03:03:37 +00:00
|
|
|
.map(|field| {
|
2020-12-15 00:30:36 +00:00
|
|
|
let name = Some(field.ident.name);
|
2020-11-24 03:03:37 +00:00
|
|
|
let kind = StructFieldItem(cx.tcx.type_of(field.did).clean(cx));
|
|
|
|
let what_rustc_thinks =
|
|
|
|
Item::from_def_id_and_parts(field.did, name, kind, cx);
|
|
|
|
// don't show `pub` for fields, which are always public
|
|
|
|
Item { visibility: Visibility::Inherited, ..what_rustc_thinks }
|
2019-12-22 22:42:04 +00:00
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
}),
|
2014-05-23 23:14:54 +00:00
|
|
|
};
|
2021-01-15 02:42:59 +00:00
|
|
|
let what_rustc_thinks =
|
|
|
|
Item::from_def_id_and_parts(self.def_id, Some(self.ident.name), VariantItem(kind), cx);
|
2020-11-15 19:09:26 +00:00
|
|
|
// don't show `pub` for fields, which are always public
|
|
|
|
Item { visibility: Inherited, ..what_rustc_thinks }
|
2014-05-23 23:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Variant> for hir::VariantData<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Variant {
|
2019-03-23 21:06:58 +00:00
|
|
|
match self {
|
2021-01-15 06:17:23 +00:00
|
|
|
hir::VariantData::Struct(..) => Variant::Struct(self.clean(cx)),
|
2021-08-27 12:43:22 +00:00
|
|
|
hir::VariantData::Tuple(..) => Variant::Tuple(self.clean(cx)),
|
2021-01-15 06:17:23 +00:00
|
|
|
hir::VariantData::Unit(..) => Variant::CLike,
|
2016-10-01 19:09:27 +00:00
|
|
|
}
|
2015-10-01 21:03:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Path> for hir::Path<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Path {
|
2021-08-23 01:11:16 +00:00
|
|
|
Path { res: self.res, segments: self.segments.clean(cx) }
|
2013-09-12 19:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<GenericArgs> for hir::GenericArgs<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> GenericArgs {
|
2017-07-28 22:13:40 +00:00
|
|
|
if self.parenthesized {
|
2019-05-08 19:57:06 +00:00
|
|
|
let output = self.bindings[0].ty().clean(cx);
|
2021-08-23 04:35:01 +00:00
|
|
|
let output =
|
|
|
|
if output != Type::Tuple(Vec::new()) { Some(Box::new(output)) } else { None };
|
|
|
|
GenericArgs::Parenthesized { inputs: self.inputs().clean(cx), output }
|
2017-07-28 22:13:40 +00:00
|
|
|
} else {
|
2018-02-13 11:32:37 +00:00
|
|
|
GenericArgs::AngleBracketed {
|
2019-12-22 22:42:04 +00:00
|
|
|
args: self
|
|
|
|
.args
|
|
|
|
.iter()
|
2020-08-06 23:48:24 +00:00
|
|
|
.map(|arg| match arg {
|
|
|
|
hir::GenericArg::Lifetime(lt) if !lt.is_elided() => {
|
|
|
|
GenericArg::Lifetime(lt.clean(cx))
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2020-08-06 23:48:24 +00:00
|
|
|
hir::GenericArg::Lifetime(_) => GenericArg::Lifetime(Lifetime::elided()),
|
|
|
|
hir::GenericArg::Type(ty) => GenericArg::Type(ty.clean(cx)),
|
2021-09-01 22:26:07 +00:00
|
|
|
hir::GenericArg::Const(ct) => GenericArg::Const(Box::new(ct.clean(cx))),
|
2021-04-24 21:41:57 +00:00
|
|
|
hir::GenericArg::Infer(_inf) => GenericArg::Infer,
|
2019-12-22 22:42:04 +00:00
|
|
|
})
|
|
|
|
.collect(),
|
2017-07-28 22:13:40 +00:00
|
|
|
bindings: self.bindings.clean(cx),
|
2014-11-04 02:52:52 +00:00
|
|
|
}
|
2014-12-16 20:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 02:52:52 +00:00
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<PathSegment> for hir::PathSegment<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> PathSegment {
|
2021-01-02 18:45:11 +00:00
|
|
|
PathSegment { name: self.ident.name, args: self.args().clean(cx) }
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<BareFunctionDecl> for hir::BareFnTy<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> BareFunctionDecl {
|
2021-02-12 06:59:20 +00:00
|
|
|
let (generic_params, decl) = enter_impl_trait(cx, |cx| {
|
2021-02-15 23:30:06 +00:00
|
|
|
(self.generic_params.clean(cx), (&*self.decl, self.param_names).clean(cx))
|
2018-05-03 13:24:50 +00:00
|
|
|
});
|
2019-12-22 22:42:04 +00:00
|
|
|
BareFunctionDecl { unsafety: self.unsafety, abi: self.abi, decl, generic_params }
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Vec<Item> {
|
2020-11-21 04:50:13 +00:00
|
|
|
use hir::ItemKind;
|
|
|
|
|
2020-11-21 15:10:03 +00:00
|
|
|
let (item, renamed) = self;
|
2021-01-30 16:47:51 +00:00
|
|
|
let def_id = item.def_id.to_def_id();
|
|
|
|
let mut name = renamed.unwrap_or_else(|| cx.tcx.hir().name(item.hir_id()));
|
2021-02-12 06:59:20 +00:00
|
|
|
cx.with_param_env(def_id, |cx| {
|
2020-10-18 15:27:16 +00:00
|
|
|
let kind = match item.kind {
|
2021-02-11 19:37:36 +00:00
|
|
|
ItemKind::Static(ty, mutability, body_id) => {
|
|
|
|
StaticItem(Static { type_: ty.clean(cx), mutability, expr: Some(body_id) })
|
|
|
|
}
|
2021-03-10 20:03:51 +00:00
|
|
|
ItemKind::Const(ty, body_id) => ConstantItem(Constant {
|
|
|
|
type_: ty.clean(cx),
|
|
|
|
kind: ConstantKind::Local { body: body_id, def_id },
|
|
|
|
}),
|
2020-10-18 15:27:16 +00:00
|
|
|
ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
|
|
|
|
bounds: ty.bounds.clean(cx),
|
|
|
|
generics: ty.generics.clean(cx),
|
|
|
|
}),
|
2021-01-03 20:38:46 +00:00
|
|
|
ItemKind::TyAlias(hir_ty, ref generics) => {
|
|
|
|
let rustdoc_ty = hir_ty.clean(cx);
|
2021-01-03 21:08:21 +00:00
|
|
|
let ty = hir_ty_to_ty(cx.tcx, hir_ty).clean(cx);
|
2020-10-18 15:27:16 +00:00
|
|
|
TypedefItem(
|
2021-01-03 21:08:21 +00:00
|
|
|
Typedef {
|
|
|
|
type_: rustdoc_ty,
|
|
|
|
generics: generics.clean(cx),
|
|
|
|
item_type: Some(ty),
|
|
|
|
},
|
2020-10-18 15:27:16 +00:00
|
|
|
false,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ItemKind::Enum(ref def, ref generics) => EnumItem(Enum {
|
|
|
|
variants: def.variants.iter().map(|v| v.clean(cx)).collect(),
|
|
|
|
generics: generics.clean(cx),
|
|
|
|
variants_stripped: false,
|
|
|
|
}),
|
|
|
|
ItemKind::TraitAlias(ref generics, bounds) => TraitAliasItem(TraitAlias {
|
2020-11-23 02:32:18 +00:00
|
|
|
generics: generics.clean(cx),
|
|
|
|
bounds: bounds.clean(cx),
|
2020-10-18 15:27:16 +00:00
|
|
|
}),
|
|
|
|
ItemKind::Union(ref variant_data, ref generics) => UnionItem(Union {
|
|
|
|
generics: generics.clean(cx),
|
|
|
|
fields: variant_data.fields().clean(cx),
|
|
|
|
fields_stripped: false,
|
|
|
|
}),
|
|
|
|
ItemKind::Struct(ref variant_data, ref generics) => StructItem(Struct {
|
2021-01-20 22:19:46 +00:00
|
|
|
struct_type: CtorKind::from_hir(variant_data),
|
2020-10-18 15:27:16 +00:00
|
|
|
generics: generics.clean(cx),
|
|
|
|
fields: variant_data.fields().clean(cx),
|
|
|
|
fields_stripped: false,
|
|
|
|
}),
|
2021-01-30 16:47:51 +00:00
|
|
|
ItemKind::Impl(ref impl_) => return clean_impl(impl_, item.hir_id(), cx),
|
2020-10-18 15:27:16 +00:00
|
|
|
// proc macros can have a name set by attributes
|
|
|
|
ItemKind::Fn(ref sig, ref generics, body_id) => {
|
|
|
|
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
|
|
|
|
}
|
2021-08-05 23:58:46 +00:00
|
|
|
ItemKind::Macro(ref macro_def) => MacroItem(Macro {
|
2021-10-01 15:12:39 +00:00
|
|
|
source: display_macro_source(cx, name, macro_def, def_id, &item.vis),
|
2021-08-05 23:58:46 +00:00
|
|
|
}),
|
2021-10-01 15:12:39 +00:00
|
|
|
ItemKind::Trait(is_auto, unsafety, ref generics, bounds, item_ids) => {
|
2020-10-18 15:27:16 +00:00
|
|
|
let items = item_ids
|
|
|
|
.iter()
|
|
|
|
.map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx))
|
|
|
|
.collect();
|
|
|
|
TraitItem(Trait {
|
|
|
|
unsafety,
|
|
|
|
items,
|
|
|
|
generics: generics.clean(cx),
|
|
|
|
bounds: bounds.clean(cx),
|
|
|
|
is_auto: is_auto.clean(cx),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ItemKind::ExternCrate(orig_name) => {
|
|
|
|
return clean_extern_crate(item, name, orig_name, cx);
|
|
|
|
}
|
2021-01-14 19:49:58 +00:00
|
|
|
ItemKind::Use(path, kind) => {
|
|
|
|
return clean_use_statement(item, name, path, kind, cx);
|
|
|
|
}
|
2020-10-18 15:27:16 +00:00
|
|
|
_ => unreachable!("not yet converted"),
|
|
|
|
};
|
2020-11-21 04:50:13 +00:00
|
|
|
|
2020-12-15 00:30:36 +00:00
|
|
|
vec![Item::from_def_id_and_parts(def_id, Some(name), kind, cx)]
|
2020-10-18 15:27:16 +00:00
|
|
|
})
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Item> for hir::Variant<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
2021-01-15 02:42:59 +00:00
|
|
|
let kind = VariantItem(self.data.clean(cx));
|
2020-11-21 04:50:13 +00:00
|
|
|
let what_rustc_thinks =
|
|
|
|
Item::from_hir_id_and_parts(self.id, Some(self.ident.name), kind, cx);
|
|
|
|
// don't show `pub` for variants, which are always public
|
|
|
|
Item { visibility: Inherited, ..what_rustc_thinks }
|
2014-10-07 00:41:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<bool> for ty::ImplPolarity {
|
2021-01-08 21:54:35 +00:00
|
|
|
/// Returns whether the impl has negative polarity.
|
2020-12-17 00:14:21 +00:00
|
|
|
fn clean(&self, _: &mut DocContext<'_>) -> bool {
|
2015-01-21 04:35:57 +00:00
|
|
|
match self {
|
2019-07-13 21:09:46 +00:00
|
|
|
&ty::ImplPolarity::Positive |
|
|
|
|
// FIXME: do we want to do something else here?
|
2021-01-08 21:54:35 +00:00
|
|
|
&ty::ImplPolarity::Reservation => false,
|
|
|
|
&ty::ImplPolarity::Negative => true,
|
2015-01-21 04:35:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>) -> Vec<Item> {
|
2021-02-12 06:59:20 +00:00
|
|
|
let tcx = cx.tcx;
|
2020-11-22 18:34:06 +00:00
|
|
|
let mut ret = Vec::new();
|
2020-11-22 22:46:21 +00:00
|
|
|
let trait_ = impl_.of_trait.clean(cx);
|
|
|
|
let items =
|
2021-02-12 06:59:20 +00:00
|
|
|
impl_.items.iter().map(|ii| tcx.hir().impl_item(ii.id).clean(cx)).collect::<Vec<_>>();
|
|
|
|
let def_id = tcx.hir().local_def_id(hir_id);
|
2020-11-22 18:34:06 +00:00
|
|
|
|
|
|
|
// If this impl block is an implementation of the Deref trait, then we
|
|
|
|
// need to try inlining the target's inherent impl blocks as well.
|
2021-08-27 02:16:58 +00:00
|
|
|
if trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait() {
|
2020-11-22 18:34:06 +00:00
|
|
|
build_deref_target_impls(cx, &items, &mut ret);
|
|
|
|
}
|
|
|
|
|
2020-11-22 22:46:21 +00:00
|
|
|
let for_ = impl_.self_ty.clean(cx);
|
2021-10-22 03:17:47 +00:00
|
|
|
let type_alias = for_.def_id(&cx.cache).and_then(|did| match tcx.def_kind(did) {
|
2021-02-12 06:59:20 +00:00
|
|
|
DefKind::TyAlias => Some(tcx.type_of(did).clean(cx)),
|
2020-11-22 18:34:06 +00:00
|
|
|
_ => None,
|
|
|
|
});
|
2021-08-24 04:01:56 +00:00
|
|
|
let mut make_item = |trait_: Option<Path>, for_: Type, items: Vec<Item>| {
|
2020-11-22 18:34:06 +00:00
|
|
|
let kind = ImplItem(Impl {
|
2021-03-24 05:20:13 +00:00
|
|
|
span: types::rustc_span(tcx.hir().local_def_id(hir_id).to_def_id(), tcx),
|
2020-11-22 22:46:21 +00:00
|
|
|
unsafety: impl_.unsafety,
|
|
|
|
generics: impl_.generics.clean(cx),
|
2020-11-22 18:34:06 +00:00
|
|
|
trait_,
|
|
|
|
for_,
|
|
|
|
items,
|
2021-02-12 06:59:20 +00:00
|
|
|
negative_polarity: tcx.impl_polarity(def_id).clean(cx),
|
2020-11-22 18:34:06 +00:00
|
|
|
synthetic: false,
|
|
|
|
blanket_impl: None,
|
2020-01-15 17:52:04 +00:00
|
|
|
});
|
2020-11-22 22:46:21 +00:00
|
|
|
Item::from_hir_id_and_parts(hir_id, None, kind, cx)
|
2020-11-22 18:34:06 +00:00
|
|
|
};
|
|
|
|
if let Some(type_alias) = type_alias {
|
|
|
|
ret.push(make_item(trait_.clone(), type_alias, items.clone()));
|
2015-04-13 23:23:32 +00:00
|
|
|
}
|
2020-11-22 18:34:06 +00:00
|
|
|
ret.push(make_item(trait_, for_, items));
|
|
|
|
ret
|
2015-04-13 23:23:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
fn clean_extern_crate(
|
|
|
|
krate: &hir::Item<'_>,
|
2020-11-23 02:51:57 +00:00
|
|
|
name: Symbol,
|
|
|
|
orig_name: Option<Symbol>,
|
2020-12-17 00:14:21 +00:00
|
|
|
cx: &mut DocContext<'_>,
|
2020-11-23 02:51:57 +00:00
|
|
|
) -> Vec<Item> {
|
|
|
|
// this is the ID of the `extern crate` statement
|
2021-01-30 16:47:51 +00:00
|
|
|
let cnum = cx.tcx.extern_mod_stmt_cnum(krate.def_id).unwrap_or(LOCAL_CRATE);
|
2020-11-23 02:51:57 +00:00
|
|
|
// this is the ID of the crate itself
|
|
|
|
let crate_def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
|
2021-01-24 12:17:54 +00:00
|
|
|
let attrs = cx.tcx.hir().attrs(krate.hir_id());
|
2020-11-23 02:51:57 +00:00
|
|
|
let please_inline = krate.vis.node.is_pub()
|
2021-01-24 12:17:54 +00:00
|
|
|
&& attrs.iter().any(|a| {
|
2020-11-23 02:51:57 +00:00
|
|
|
a.has_name(sym::doc)
|
|
|
|
&& match a.meta_item_list() {
|
|
|
|
Some(l) => attr::list_contains_name(&l, sym::inline),
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if please_inline {
|
|
|
|
let mut visited = FxHashSet::default();
|
2019-01-11 01:27:44 +00:00
|
|
|
|
2020-11-23 02:51:57 +00:00
|
|
|
let res = Res::Def(DefKind::Mod, crate_def_id);
|
|
|
|
|
|
|
|
if let Some(items) = inline::try_inline(
|
|
|
|
cx,
|
2021-01-30 16:47:51 +00:00
|
|
|
cx.tcx.parent_module(krate.hir_id()).to_def_id(),
|
2021-07-06 12:43:03 +00:00
|
|
|
Some(krate.def_id.to_def_id()),
|
2020-11-23 02:51:57 +00:00
|
|
|
res,
|
|
|
|
name,
|
2021-01-24 12:17:54 +00:00
|
|
|
Some(attrs),
|
2020-11-23 02:51:57 +00:00
|
|
|
&mut visited,
|
|
|
|
) {
|
|
|
|
return items;
|
|
|
|
}
|
2014-12-26 08:55:16 +00:00
|
|
|
}
|
2021-04-25 16:17:11 +00:00
|
|
|
|
2020-11-23 02:51:57 +00:00
|
|
|
// FIXME: using `from_def_id_and_kind` breaks `rustdoc/masked` for some reason
|
|
|
|
vec![Item {
|
2021-03-05 15:04:24 +00:00
|
|
|
name: Some(name),
|
2021-09-20 23:49:47 +00:00
|
|
|
attrs: box attrs.clean(cx),
|
2021-04-29 19:36:54 +00:00
|
|
|
def_id: crate_def_id.into(),
|
2020-11-23 02:51:57 +00:00
|
|
|
visibility: krate.vis.clean(cx),
|
2021-09-20 23:49:47 +00:00
|
|
|
kind: box ExternCrateItem { src: orig_name },
|
2020-11-04 20:59:35 +00:00
|
|
|
cfg: attrs.cfg(cx.tcx, &cx.cache.hidden_cfg),
|
2020-11-23 02:51:57 +00:00
|
|
|
}]
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
fn clean_use_statement(
|
|
|
|
import: &hir::Item<'_>,
|
2021-01-14 19:49:58 +00:00
|
|
|
name: Symbol,
|
2020-12-17 00:14:21 +00:00
|
|
|
path: &hir::Path<'_>,
|
2021-01-14 19:49:58 +00:00
|
|
|
kind: hir::UseKind,
|
2020-12-17 00:14:21 +00:00
|
|
|
cx: &mut DocContext<'_>,
|
2021-01-14 19:49:58 +00:00
|
|
|
) -> Vec<Item> {
|
|
|
|
// We need this comparison because some imports (for std types for example)
|
|
|
|
// are "inserted" as well but directly by the compiler and they should not be
|
|
|
|
// taken into account.
|
|
|
|
if import.span.ctxt().outer_expn_data().kind == ExpnKind::AstPass(AstPass::StdImports) {
|
|
|
|
return Vec::new();
|
|
|
|
}
|
|
|
|
|
2021-01-24 12:17:54 +00:00
|
|
|
let attrs = cx.tcx.hir().attrs(import.hir_id());
|
|
|
|
let inline_attr = attrs.lists(sym::doc).get_word_attr(sym::inline);
|
2021-01-14 19:49:58 +00:00
|
|
|
let pub_underscore = import.vis.node.is_pub() && name == kw::Underscore;
|
|
|
|
|
2021-02-12 16:48:00 +00:00
|
|
|
if pub_underscore {
|
|
|
|
if let Some(ref inline) = inline_attr {
|
|
|
|
rustc_errors::struct_span_err!(
|
|
|
|
cx.tcx.sess,
|
|
|
|
inline.span(),
|
|
|
|
E0780,
|
|
|
|
"anonymous imports cannot be inlined"
|
|
|
|
)
|
|
|
|
.span_label(import.span, "anonymous import")
|
|
|
|
.emit();
|
|
|
|
}
|
2021-01-14 19:49:58 +00:00
|
|
|
}
|
2020-12-31 09:07:51 +00:00
|
|
|
|
2021-01-14 19:49:58 +00:00
|
|
|
// We consider inlining the documentation of `pub use` statements, but we
|
|
|
|
// forcefully don't inline if this is not public or if the
|
|
|
|
// #[doc(no_inline)] attribute is present.
|
|
|
|
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
|
2021-07-07 11:47:28 +00:00
|
|
|
let mut denied = !(import.vis.node.is_pub()
|
|
|
|
|| (cx.render_options.document_private && import.vis.node.is_pub_restricted()))
|
2021-01-14 19:49:58 +00:00
|
|
|
|| pub_underscore
|
2021-01-24 12:17:54 +00:00
|
|
|
|| attrs.iter().any(|a| {
|
2021-01-14 19:49:58 +00:00
|
|
|
a.has_name(sym::doc)
|
|
|
|
&& match a.meta_item_list() {
|
|
|
|
Some(l) => {
|
|
|
|
attr::list_contains_name(&l, sym::no_inline)
|
|
|
|
|| attr::list_contains_name(&l, sym::hidden)
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2021-01-14 19:49:58 +00:00
|
|
|
None => false,
|
2018-06-15 23:16:43 +00:00
|
|
|
}
|
2021-01-14 19:49:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Also check whether imports were asked to be inlined, in case we're trying to re-export a
|
|
|
|
// crate in Rust 2018+
|
|
|
|
let path = path.clean(cx);
|
|
|
|
let inner = if kind == hir::UseKind::Glob {
|
|
|
|
if !denied {
|
|
|
|
let mut visited = FxHashSet::default();
|
|
|
|
if let Some(items) = inline::try_inline_glob(cx, path.res, &mut visited) {
|
|
|
|
return items;
|
2018-06-15 23:16:43 +00:00
|
|
|
}
|
2021-01-14 19:49:58 +00:00
|
|
|
}
|
|
|
|
Import::new_glob(resolve_use_source(cx, path), true)
|
|
|
|
} else {
|
2021-02-12 16:48:00 +00:00
|
|
|
if inline_attr.is_none() {
|
2021-01-14 19:49:58 +00:00
|
|
|
if let Res::Def(DefKind::Mod, did) = path.res {
|
|
|
|
if !did.is_local() && did.index == CRATE_DEF_INDEX {
|
|
|
|
// if we're `pub use`ing an extern crate root, don't inline it unless we
|
|
|
|
// were specifically asked for it
|
|
|
|
denied = true;
|
2018-11-09 01:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-14 19:49:58 +00:00
|
|
|
}
|
|
|
|
if !denied {
|
|
|
|
let mut visited = FxHashSet::default();
|
2021-07-06 12:43:03 +00:00
|
|
|
let import_def_id = import.def_id.to_def_id();
|
2021-01-14 19:49:58 +00:00
|
|
|
|
|
|
|
if let Some(mut items) = inline::try_inline(
|
|
|
|
cx,
|
2021-01-30 16:47:51 +00:00
|
|
|
cx.tcx.parent_module(import.hir_id()).to_def_id(),
|
2021-07-06 12:43:03 +00:00
|
|
|
Some(import_def_id),
|
2021-01-14 19:49:58 +00:00
|
|
|
path.res,
|
|
|
|
name,
|
2021-01-24 12:17:54 +00:00
|
|
|
Some(attrs),
|
2021-01-14 19:49:58 +00:00
|
|
|
&mut visited,
|
|
|
|
) {
|
|
|
|
items.push(Item::from_def_id_and_parts(
|
2021-07-06 12:43:03 +00:00
|
|
|
import_def_id,
|
2021-01-14 19:49:58 +00:00
|
|
|
None,
|
|
|
|
ImportItem(Import::new_simple(name, resolve_use_source(cx, path), false)),
|
2020-10-04 00:06:30 +00:00
|
|
|
cx,
|
2021-01-14 19:49:58 +00:00
|
|
|
));
|
|
|
|
return items;
|
2014-03-01 01:46:09 +00:00
|
|
|
}
|
2021-01-14 19:49:58 +00:00
|
|
|
}
|
|
|
|
Import::new_simple(name, resolve_use_source(cx, path), true)
|
|
|
|
};
|
2018-07-12 20:00:57 +00:00
|
|
|
|
2021-01-30 16:47:51 +00:00
|
|
|
vec![Item::from_def_id_and_parts(import.def_id.to_def_id(), None, ImportItem(inner), cx)]
|
2013-08-15 20:28:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Symbol>) {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
2020-11-22 19:03:02 +00:00
|
|
|
let (item, renamed) = self;
|
2021-09-17 01:12:45 +00:00
|
|
|
let def_id = item.def_id.to_def_id();
|
|
|
|
cx.with_param_env(def_id, |cx| {
|
2020-10-18 15:27:16 +00:00
|
|
|
let kind = match item.kind {
|
2021-10-01 15:12:39 +00:00
|
|
|
hir::ForeignItemKind::Fn(decl, names, ref generics) => {
|
2021-01-31 23:33:38 +00:00
|
|
|
let abi = cx.tcx.hir().get_foreign_abi(item.hir_id());
|
2021-02-12 06:59:20 +00:00
|
|
|
let (generics, decl) = enter_impl_trait(cx, |cx| {
|
2021-10-01 15:12:39 +00:00
|
|
|
(generics.clean(cx), (&*decl, &names[..]).clean(cx))
|
2020-10-18 15:27:16 +00:00
|
|
|
});
|
|
|
|
ForeignFunctionItem(Function {
|
|
|
|
decl,
|
|
|
|
generics,
|
|
|
|
header: hir::FnHeader {
|
2021-06-15 12:52:16 +00:00
|
|
|
unsafety: if abi == Abi::RustIntrinsic {
|
|
|
|
intrinsic_operation_unsafety(item.ident.name)
|
|
|
|
} else {
|
|
|
|
hir::Unsafety::Unsafe
|
|
|
|
},
|
2020-10-18 15:27:16 +00:00
|
|
|
abi,
|
|
|
|
constness: hir::Constness::NotConst,
|
|
|
|
asyncness: hir::IsAsync::NotAsync,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2021-02-11 19:37:36 +00:00
|
|
|
hir::ForeignItemKind::Static(ref ty, mutability) => {
|
|
|
|
ForeignStaticItem(Static { type_: ty.clean(cx), mutability, expr: None })
|
|
|
|
}
|
2020-10-18 15:27:16 +00:00
|
|
|
hir::ForeignItemKind::Type => ForeignTypeItem,
|
|
|
|
};
|
2018-07-12 20:00:57 +00:00
|
|
|
|
2020-10-18 15:27:16 +00:00
|
|
|
Item::from_hir_id_and_parts(
|
2021-01-31 23:33:38 +00:00
|
|
|
item.hir_id(),
|
2020-12-02 04:18:46 +00:00
|
|
|
Some(renamed.unwrap_or(item.ident.name)),
|
2020-10-18 15:27:16 +00:00
|
|
|
kind,
|
|
|
|
cx,
|
|
|
|
)
|
|
|
|
})
|
2013-09-26 18:57:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<TypeBinding> for hir::TypeBinding<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> TypeBinding {
|
2020-12-16 16:21:08 +00:00
|
|
|
TypeBinding { name: self.ident.name, kind: self.kind.clean(cx) }
|
2019-05-08 19:57:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 00:14:21 +00:00
|
|
|
impl Clean<TypeBindingKind> for hir::TypeBindingKind<'_> {
|
|
|
|
fn clean(&self, cx: &mut DocContext<'_>) -> TypeBindingKind {
|
2019-05-08 19:57:06 +00:00
|
|
|
match *self {
|
2019-12-22 22:42:04 +00:00
|
|
|
hir::TypeBindingKind::Equality { ref ty } => {
|
|
|
|
TypeBindingKind::Equality { ty: ty.clean(cx) }
|
|
|
|
}
|
2021-10-01 15:12:39 +00:00
|
|
|
hir::TypeBindingKind::Constraint { bounds } => {
|
2020-02-29 02:05:14 +00:00
|
|
|
TypeBindingKind::Constraint { bounds: bounds.iter().map(|b| b.clean(cx)).collect() }
|
|
|
|
}
|
2015-01-08 00:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|