X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibrustdoc%2Fclean%2Ftypes.rs;h=df6e030aeef71ff6a1235d633c154c8117674a62;hb=5f744f33f60f1961e756adf30606a08076350182;hp=b3c320555f9e53e08e030134b440432ed4722cab;hpb=e675073e73d13a3a2d21f605de873b96ef642595;p=rust.git diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index b3c320555f9..df6e030aeef 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -120,8 +120,7 @@ fn from(id: DefId) -> Self { crate module: Item, crate externs: Vec, crate primitives: ThinVec<(DefId, PrimitiveType)>, - // These are later on moved into `CACHEKEY`, leaving the map empty. - // Only here so that they can be filtered through the rustdoc passes. + /// Only here so that they can be filtered through the rustdoc passes. crate external_traits: Rc>>, crate collapsed: bool, } @@ -421,7 +420,7 @@ pub fn from_def_id_and_parts( def_id, name, kind, - Box::new(ast_attrs.clean(cx)), + box ast_attrs.clean(cx), cx, ast_attrs.cfg(cx.sess()), ) @@ -439,7 +438,7 @@ pub fn from_def_id_and_attrs_and_parts( Item { def_id: def_id.into(), - kind: Box::new(kind), + kind: box kind, name, attrs, visibility: cx.tcx.visibility(def_id).clean(cx), @@ -908,18 +907,10 @@ fn from_iter(iter: T) -> Self } } -/// The attributes on an [`Item`], including attributes like `#[derive(...)]` and `#[inline]`, -/// as well as doc comments. -#[derive(Clone, Debug, Default)] -crate struct Attributes { - crate doc_strings: Vec, - crate other_attrs: Vec, -} - -#[derive(Clone, Debug, PartialEq, Eq, Hash)] /// A link that has not yet been rendered. /// /// This link will be turned into a rendered link by [`Item::links`]. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] crate struct ItemLink { /// The original link written in the markdown pub(crate) link: String, @@ -944,6 +935,14 @@ pub struct RenderedLink { pub(crate) href: String, } +/// The attributes on an [`Item`], including attributes like `#[derive(...)]` and `#[inline]`, +/// as well as doc comments. +#[derive(Clone, Debug, Default)] +crate struct Attributes { + crate doc_strings: Vec, + crate other_attrs: Vec, +} + impl Attributes { crate fn lists(&self, name: Symbol) -> ListAttributesIter<'_> { self.other_attrs.lists(name) @@ -1114,10 +1113,7 @@ impl GenericBound { let path = external_path(cx, did, false, vec![], empty); inline::record_extern_fqn(cx, did, ItemType::Trait); GenericBound::TraitBound( - PolyTrait { - trait_: ResolvedPath { path, did, is_generic: false }, - generic_params: Vec::new(), - }, + PolyTrait { trait_: path, generic_params: Vec::new() }, hir::TraitBoundModifier::Maybe, ) } @@ -1125,7 +1121,7 @@ impl GenericBound { crate fn is_sized_bound(&self, cx: &DocContext<'_>) -> bool { use rustc_hir::TraitBoundModifier as TBM; if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, TBM::None) = *self { - if trait_.def_id() == cx.tcx.lang_items().sized_trait() { + if Some(trait_.def_id()) == cx.tcx.lang_items().sized_trait() { return true; } } @@ -1139,7 +1135,7 @@ impl GenericBound { None } - crate fn get_trait_type(&self) -> Option { + crate fn get_trait_path(&self) -> Option { if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, _) = *self { Some(trait_.clone()) } else { @@ -1371,90 +1367,56 @@ fn def_id_full(&self, cache: &Cache) -> Option { /// A trait reference, which may have higher ranked lifetimes. #[derive(Clone, PartialEq, Eq, Debug, Hash)] crate struct PolyTrait { - crate trait_: Type, + crate trait_: Path, crate generic_params: Vec, } -/// A representation of a type suitable for hyperlinking purposes. Ideally, one can get the original -/// type out of the AST/`TyCtxt` given one of these, if more information is needed. Most -/// importantly, it does not preserve mutability or boxes. +/// Rustdoc's representation of types, mostly based on the [`hir::Ty`]. #[derive(Clone, PartialEq, Eq, Debug, Hash)] crate enum Type { - /// Structs/enums/traits (most that would be an `hir::TyKind::Path`). - ResolvedPath { - path: Path, - did: DefId, - /// `true` if is a `T::Name` path for associated types. - is_generic: bool, - }, - /// `dyn for<'a> Trait<'a> + Send + 'static` + /// A named type, which could be a trait. + /// + /// This is mostly Rustdoc's version of [`hir::Path`]. It has to be different because Rustdoc's [`PathSegment`] can contain cleaned generics. + ResolvedPath { path: Path, did: DefId }, + /// A `dyn Trait` object: `dyn for<'a> Trait<'a> + Send + 'static` DynTrait(Vec, Option), - /// For parameterized types, so the consumer of the JSON don't go - /// looking for types which don't exist anywhere. + /// A type parameter. Generic(Symbol), - /// Primitives are the fixed-size numeric types (plus int/usize/float), char, - /// arrays, slices, and tuples. + /// A primitive (aka, builtin) type. Primitive(PrimitiveType), - /// `extern "ABI" fn` + /// A function pointer: `extern "ABI" fn(...) -> ...` BareFunction(Box), + /// A tuple type: `(i32, &str)`. Tuple(Vec), + /// A slice type (does *not* include the `&`): `[i32]` Slice(Box), - /// The `String` field is about the size or the constant representing the array's length. + /// An array type. + /// + /// The `String` field is a stringified version of the array's length parameter. Array(Box, String), - Never, + /// A raw pointer type: `*const i32`, `*mut i32` RawPointer(Mutability, Box), - BorrowedRef { - lifetime: Option, - mutability: Mutability, - type_: Box, - }, + /// A reference type: `&i32`, `&'a mut Foo` + BorrowedRef { lifetime: Option, mutability: Mutability, type_: Box }, - // `::Name` + /// A qualified path to an associated item: `::Name` QPath { name: Symbol, self_type: Box, + /// FIXME: This is a hack that should be removed; see [this discussion][1]. + /// + /// [1]: https://github.com/rust-lang/rust/pull/85479#discussion_r635729093 self_def_id: Option, - trait_: Box, + trait_: Path, }, - // `_` + /// A type that is inferred: `_` Infer, - // `impl TraitA + TraitB + ...` + /// An `impl Trait`: `impl TraitA + TraitB + ...` ImplTrait(Vec), } -#[derive(Clone, PartialEq, Eq, Hash, Copy, Debug)] -/// N.B. this has to be different from `hir::PrimTy` because it also includes types that aren't -/// paths, like `Unit`. -crate enum PrimitiveType { - Isize, - I8, - I16, - I32, - I64, - I128, - Usize, - U8, - U16, - U32, - U64, - U128, - F32, - F64, - Char, - Bool, - Str, - Slice, - Array, - Tuple, - Unit, - RawPointer, - Reference, - Fn, - Never, -} - crate trait GetDefId { /// Use this method to get the [`DefId`] of a [`clean`] AST node. /// This will return [`None`] when called on a primitive [`clean::Type`]. @@ -1498,14 +1460,14 @@ impl Type { } RawPointer(..) => Some(PrimitiveType::RawPointer), BareFunction(..) => Some(PrimitiveType::Fn), - Never => Some(PrimitiveType::Never), _ => None, } } - crate fn is_generic(&self) -> bool { - match *self { - ResolvedPath { is_generic, .. } => is_generic, + /// Checks if this is a `T::Name` path for an associated type. + crate fn is_assoc_ty(&self) -> bool { + match self { + ResolvedPath { path, .. } => path.is_assoc_ty(), _ => false, } } @@ -1518,34 +1480,8 @@ impl Type { } crate fn generics(&self) -> Option> { - match *self { - ResolvedPath { ref path, .. } => path.segments.last().and_then(|seg| { - if let GenericArgs::AngleBracketed { ref args, .. } = seg.args { - Some( - args.iter() - .filter_map(|arg| match arg { - GenericArg::Type(ty) => Some(ty), - _ => None, - }) - .collect(), - ) - } else { - None - } - }), - _ => None, - } - } - - crate fn bindings(&self) -> Option<&[TypeBinding]> { - match *self { - ResolvedPath { ref path, .. } => path.segments.last().and_then(|seg| { - if let GenericArgs::AngleBracketed { ref bindings, .. } = seg.args { - Some(&**bindings) - } else { - None - } - }), + match self { + ResolvedPath { path, .. } => path.generics(), _ => None, } } @@ -1563,19 +1499,13 @@ impl Type { QPath { self_type, trait_, name, .. } => (self_type, trait_, name), _ => return None, }; - let trait_did = match **trait_ { - ResolvedPath { did, .. } => did, - _ => return None, - }; - Some((&self_, trait_did, *name)) + Some((&self_, trait_.def_id(), *name)) } -} -impl Type { fn inner_def_id(&self, cache: Option<&Cache>) -> Option { let t: PrimitiveType = match *self { ResolvedPath { did, .. } => return Some(did), - DynTrait(ref bounds, _) => return bounds[0].trait_.inner_def_id(cache), + DynTrait(ref bounds, _) => return Some(bounds[0].trait_.def_id()), Primitive(p) => return cache.and_then(|c| c.primitive_locations.get(&p).cloned()), BorrowedRef { type_: box Generic(..), .. } => PrimitiveType::Reference, BorrowedRef { ref type_, .. } => return type_.inner_def_id(cache), @@ -1587,7 +1517,6 @@ fn inner_def_id(&self, cache: Option<&Cache>) -> Option { } } BareFunction(..) => PrimitiveType::Fn, - Never => PrimitiveType::Never, Slice(..) => PrimitiveType::Slice, Array(..) => PrimitiveType::Array, RawPointer(..) => PrimitiveType::RawPointer, @@ -1608,6 +1537,41 @@ fn def_id_full(&self, cache: &Cache) -> Option { } } +/// A primitive (aka, builtin) type. +/// +/// This represents things like `i32`, `str`, etc. +/// +/// N.B. This has to be different from [`hir::PrimTy`] because it also includes types that aren't +/// paths, like [`Self::Unit`]. +#[derive(Clone, PartialEq, Eq, Hash, Copy, Debug)] +crate enum PrimitiveType { + Isize, + I8, + I16, + I32, + I64, + I128, + Usize, + U8, + U16, + U32, + U64, + U128, + F32, + F64, + Char, + Bool, + Str, + Slice, + Array, + Tuple, + Unit, + RawPointer, + Reference, + Fn, + Never, +} + impl PrimitiveType { crate fn from_hir(prim: hir::PrimTy) -> PrimitiveType { use ast::{FloatTy, IntTy, UintTy}; @@ -1976,12 +1940,15 @@ impl Span { #[derive(Clone, PartialEq, Eq, Debug, Hash)] crate struct Path { - crate global: bool, crate res: Res, crate segments: Vec, } impl Path { + crate fn def_id(&self) -> DefId { + self.res.def_id() + } + crate fn last(&self) -> Symbol { self.segments.last().expect("segments were empty").name } @@ -1991,8 +1958,44 @@ impl Path { } crate fn whole_name(&self) -> String { - String::from(if self.global { "::" } else { "" }) - + &self.segments.iter().map(|s| s.name.to_string()).collect::>().join("::") + self.segments.iter().map(|s| s.name.to_string()).intersperse("::".into()).collect() + } + + /// Checks if this is a `T::Name` path for an associated type. + crate fn is_assoc_ty(&self) -> bool { + match self.res { + Res::SelfTy(..) if self.segments.len() != 1 => true, + Res::Def(DefKind::TyParam, _) if self.segments.len() != 1 => true, + Res::Def(DefKind::AssocTy, _) => true, + _ => false, + } + } + + crate fn generics(&self) -> Option> { + self.segments.last().and_then(|seg| { + if let GenericArgs::AngleBracketed { ref args, .. } = seg.args { + Some( + args.iter() + .filter_map(|arg| match arg { + GenericArg::Type(ty) => Some(ty), + _ => None, + }) + .collect(), + ) + } else { + None + } + }) + } + + crate fn bindings(&self) -> Option<&[TypeBinding]> { + self.segments.last().and_then(|seg| { + if let GenericArgs::AngleBracketed { ref bindings, .. } = seg.args { + Some(&**bindings) + } else { + None + } + }) } } @@ -2137,7 +2140,7 @@ impl Constant { crate span: Span, crate unsafety: hir::Unsafety, crate generics: Generics, - crate trait_: Option, + crate trait_: Option, crate for_: Type, crate items: Vec, crate negative_polarity: bool, @@ -2148,7 +2151,8 @@ impl Constant { impl Impl { crate fn provided_trait_methods(&self, tcx: TyCtxt<'_>) -> FxHashSet { self.trait_ - .def_id() + .as_ref() + .map(|t| t.def_id()) .map(|did| tcx.provided_trait_methods(did).map(|meth| meth.ident.name).collect()) .unwrap_or_default() }