From b987fd78c2956fde0c9fe8a9ab4b9154dc5671da Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Thu, 21 Oct 2021 19:54:14 -0700 Subject: Fix docs for `inline::build_impl` Based on looking at the source code, it looks like the `did` needs to be for an impl, not functions in an impl. --- src/librustdoc/clean/inline.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index e11b802a09a..87e17a771a5 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -325,7 +325,7 @@ fn merge_attrs( } } -/// Builds a specific implementation of a type. The `did` could be a type method or trait method. +/// Inline an `impl`, inherent or of a trait. The `did` must be for an `impl`. crate fn build_impl( cx: &mut DocContext<'_>, parent_module: impl Into>, -- cgit 1.4.1-3-g733a5 From 1da8659fa6f4b73cd33885f7e0caeecd273a4e61 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Thu, 21 Oct 2021 19:56:12 -0700 Subject: Remove unused impl of `GetDefId` for `Typedef` --- src/librustdoc/clean/types.rs | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'src') diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index d25e166629f..14dc50dedc8 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -2092,16 +2092,6 @@ crate struct Typedef { crate item_type: Option, } -impl GetDefId for Typedef { - fn def_id(&self) -> Option { - self.type_.def_id() - } - - fn def_id_full(&self, cache: &Cache) -> Option { - self.type_.def_id_full(cache) - } -} - #[derive(Clone, Debug)] crate struct OpaqueTy { crate bounds: Vec, -- cgit 1.4.1-3-g733a5 From 6d82ee839d105c5fe6a142aabbbc7e48f55ee195 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Thu, 21 Oct 2021 20:01:31 -0700 Subject: Remove `GetDefId` impl for `FnRetTy` It was only used in one place, so it seems better to use ordinary functions. --- src/librustdoc/clean/types.rs | 15 ++++----------- src/librustdoc/html/render/mod.rs | 2 +- 2 files changed, 5 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 14dc50dedc8..c872d6480fe 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1370,17 +1370,10 @@ crate enum FnRetTy { DefaultReturn, } -impl GetDefId for FnRetTy { - fn def_id(&self) -> Option { - match *self { - Return(ref ty) => ty.def_id(), - DefaultReturn => None, - } - } - - fn def_id_full(&self, cache: &Cache) -> Option { - match *self { - Return(ref ty) => ty.def_id_full(cache), +impl FnRetTy { + crate fn as_return(&self) -> Option<&Type> { + match self { + Return(ret) => Some(ret), DefaultReturn => None, } } diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 69c5c2c4abc..a559d19d4f3 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1215,7 +1215,7 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) -> fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String { let mut out = Buffer::html(); - if let Some(did) = decl.output.def_id_full(cx.cache()) { + if let Some(did) = decl.output.as_return().and_then(|t| t.def_id_full(cx.cache())) { if let Some(impls) = cx.cache().impls.get(&did) { for i in impls { let impl_ = i.inner_impl(); -- cgit 1.4.1-3-g733a5 From 7fb13062757e0140bca4d1b2653a2cbc104d0edd Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Thu, 21 Oct 2021 20:03:11 -0700 Subject: Remove unused impl of `GetDefId` for `Option` --- src/librustdoc/clean/types.rs | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'src') diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index c872d6480fe..a3c6482bae7 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1469,16 +1469,6 @@ crate trait GetDefId { fn def_id_full(&self, cache: &Cache) -> Option; } -impl GetDefId for Option { - fn def_id(&self) -> Option { - self.as_ref().and_then(|d| d.def_id()) - } - - fn def_id_full(&self, cache: &Cache) -> Option { - self.as_ref().and_then(|d| d.def_id_full(cache)) - } -} - impl Type { crate fn primitive_type(&self) -> Option { match *self { -- cgit 1.4.1-3-g733a5 From bf0cc9005085fbf19aad215db96a519f3836d15f Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Thu, 21 Oct 2021 20:05:38 -0700 Subject: Replace `GetDefId` with inherent methods Now that it's only implemented for `Type`, using inherent methods instead means that imports are no longer necessary. Also, `GetDefId` is only meant to be used with `Type`, so it shouldn't be a trait. --- src/librustdoc/clean/inline.rs | 4 +--- src/librustdoc/clean/types.rs | 36 +++++++++++++------------------- src/librustdoc/formats/cache.rs | 2 +- src/librustdoc/html/render/cache.rs | 4 +--- src/librustdoc/html/render/mod.rs | 2 +- src/librustdoc/html/render/print_item.rs | 2 +- src/librustdoc/passes/stripper.rs | 2 +- 7 files changed, 20 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 87e17a771a5..20cf0eec1ac 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -14,9 +14,7 @@ use rustc_middle::ty::{self, TyCtxt}; use rustc_span::hygiene::MacroKind; use rustc_span::symbol::{kw, sym, Symbol}; -use crate::clean::{ - self, utils, Attributes, AttributesExt, GetDefId, ItemId, NestedAttributesExt, Type, -}; +use crate::clean::{self, utils, Attributes, AttributesExt, ItemId, NestedAttributesExt, Type}; use crate::core::DocContext; use crate::formats::item_type::ItemType; diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index a3c6482bae7..0395c6a729e 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1451,24 +1451,6 @@ crate enum Type { #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] rustc_data_structures::static_assert_size!(Type, 72); -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`]. - /// Use [`Self::def_id_full`] if you want to include primitives. - /// - /// [`clean`]: crate::clean - /// [`clean::Type`]: crate::clean::Type - // FIXME: get rid of this function and always use `def_id_full` - fn def_id(&self) -> Option; - - /// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s. - /// - /// See [`Self::def_id`] for more. - /// - /// [clean]: crate::clean - fn def_id_full(&self, cache: &Cache) -> Option; -} - impl Type { crate fn primitive_type(&self) -> Option { match *self { @@ -1549,14 +1531,24 @@ impl Type { }; cache.and_then(|c| Primitive(t).def_id_full(c)) } -} -impl GetDefId for Type { - fn def_id(&self) -> Option { + /// Use this method to get the [`DefId`] of a [`clean`] AST node. + /// This will return [`None`] when called on a primitive [`clean::Type`]. + /// Use [`Self::def_id_full`] if you want to include primitives. + /// + /// [`clean`]: crate::clean + /// [`clean::Type`]: crate::clean::Type + // FIXME: get rid of this function and always use `def_id_full` + crate fn def_id(&self) -> Option { self.inner_def_id(None) } - fn def_id_full(&self, cache: &Cache) -> Option { + /// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s. + /// + /// See [`Self::def_id`] for more. + /// + /// [clean]: crate::clean + crate fn def_id_full(&self, cache: &Cache) -> Option { self.inner_def_id(Some(cache)) } } diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 8b883ffaaf0..37371a4b69e 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -6,7 +6,7 @@ use rustc_middle::middle::privacy::AccessLevels; use rustc_middle::ty::TyCtxt; use rustc_span::symbol::sym; -use crate::clean::{self, GetDefId, ItemId, PrimitiveType}; +use crate::clean::{self, ItemId, PrimitiveType}; use crate::config::RenderOptions; use crate::fold::DocFolder; use crate::formats::item_type::ItemType; diff --git a/src/librustdoc/html/render/cache.rs b/src/librustdoc/html/render/cache.rs index 9c05c80d55d..4cc037f933e 100644 --- a/src/librustdoc/html/render/cache.rs +++ b/src/librustdoc/html/render/cache.rs @@ -6,9 +6,7 @@ use rustc_span::symbol::Symbol; use serde::ser::{Serialize, SerializeStruct, Serializer}; use crate::clean; -use crate::clean::types::{ - FnDecl, FnRetTy, GenericBound, Generics, GetDefId, Type, WherePredicate, -}; +use crate::clean::types::{FnDecl, FnRetTy, GenericBound, Generics, Type, WherePredicate}; use crate::formats::cache::Cache; use crate::formats::item_type::ItemType; use crate::html::markdown::short_markdown_summary; diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index a559d19d4f3..b9ad917371c 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -57,7 +57,7 @@ use rustc_span::symbol::{kw, sym, Symbol}; use serde::ser::SerializeSeq; use serde::{Serialize, Serializer}; -use crate::clean::{self, GetDefId, ItemId, RenderedLink, SelfTy}; +use crate::clean::{self, ItemId, RenderedLink, SelfTy}; use crate::docfs::PathError; use crate::error::Error; use crate::formats::cache::Cache; diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 58cd1018c31..4b439b8ed71 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -21,7 +21,7 @@ use super::{ render_impl, render_stability_since_raw, write_srclink, AssocItemLink, Context, ImplRenderingParameters, }; -use crate::clean::{self, GetDefId}; +use crate::clean::{self}; use crate::formats::item_type::ItemType; use crate::formats::{AssocItemRender, Impl, RenderMode}; use crate::html::escape::Escape; diff --git a/src/librustdoc/passes/stripper.rs b/src/librustdoc/passes/stripper.rs index 8b1fd662f85..a160e31a450 100644 --- a/src/librustdoc/passes/stripper.rs +++ b/src/librustdoc/passes/stripper.rs @@ -2,7 +2,7 @@ use rustc_hir::def_id::DefId; use rustc_middle::middle::privacy::AccessLevels; use std::mem; -use crate::clean::{self, GetDefId, Item, ItemIdSet}; +use crate::clean::{self, Item, ItemIdSet}; use crate::fold::{strip_item, DocFolder}; crate struct Stripper<'a> { -- cgit 1.4.1-3-g733a5 From 0853c33c3b489d5bf0045bf306d6555097f595f4 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Thu, 21 Oct 2021 20:12:20 -0700 Subject: Use `def_id_full()` where easily possible In general, it should be preferred over `Type::def_id()`. See each method's docs for more. --- src/librustdoc/clean/inline.rs | 4 ++-- src/librustdoc/clean/mod.rs | 4 ++-- src/librustdoc/formats/cache.rs | 6 ++++-- src/librustdoc/passes/collect_trait_impls.rs | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 20cf0eec1ac..3d5c1fee168 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -374,7 +374,7 @@ crate fn build_impl( // Only inline impl if the implementing type is // reachable in rustdoc generated documentation if !did.is_local() { - if let Some(did) = for_.def_id() { + if let Some(did) = for_.def_id_full(&cx.cache) { if !cx.cache.access_levels.is_public(did) { return; } @@ -462,7 +462,7 @@ crate fn build_impl( } while let Some(ty) = stack.pop() { - if let Some(did) = ty.def_id() { + if let Some(did) = ty.def_id_full(&cx.cache) { if tcx.get_attrs(did).lists(sym::doc).has_word(sym::hidden) { return; } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 075efd29b59..d34cbb44339 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -385,7 +385,7 @@ impl<'tcx> Clean for ty::ProjectionTy<'tcx> { let self_type = self.self_ty().clean(cx); Type::QPath { name: cx.tcx.associated_item(self.item_def_id).ident.name, - self_def_id: self_type.def_id(), + self_def_id: self_type.def_id_full(&cx.cache), self_type: box self_type, trait_, } @@ -1887,7 +1887,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_> } let for_ = impl_.self_ty.clean(cx); - let type_alias = for_.def_id().and_then(|did| match tcx.def_kind(did) { + let type_alias = for_.def_id_full(&cx.cache).and_then(|did| match tcx.def_kind(did) { DefKind::TyAlias => Some(tcx.type_of(did).clean(cx)), _ => None, }); diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 37371a4b69e..afaeded88b4 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -206,7 +206,9 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { || i.trait_ .as_ref() .map_or(false, |t| self.cache.masked_crates.contains(&t.def_id().krate)) - || i.for_.def_id().map_or(false, |d| self.cache.masked_crates.contains(&d.krate)) + || i.for_ + .def_id_full(self.cache) + .map_or(false, |d| self.cache.masked_crates.contains(&d.krate)) { return None; } @@ -454,7 +456,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) { for bound in generics { - if let Some(did) = bound.def_id() { + if let Some(did) = bound.def_id_full(self.cache) { dids.insert(did); } } diff --git a/src/librustdoc/passes/collect_trait_impls.rs b/src/librustdoc/passes/collect_trait_impls.rs index 319dd7b42b0..f8f5118042f 100644 --- a/src/librustdoc/passes/collect_trait_impls.rs +++ b/src/librustdoc/passes/collect_trait_impls.rs @@ -70,7 +70,7 @@ crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate { if let Some(prim) = target.primitive_type() { cleaner.prims.insert(prim); - } else if let Some(did) = target.def_id() { + } else if let Some(did) = target.def_id_full(&cx.cache) { cleaner.items.insert(did.into()); } } -- cgit 1.4.1-3-g733a5 From 6e3561e149870fc60f27a3035747a0fe14ffd9f5 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Thu, 21 Oct 2021 20:14:56 -0700 Subject: Rename `Type::def_id()` to `Type::def_id_no_primitives()` The old name was confusing because it's easy to assume that using `def_id()` is fine, but in some situations it's incorrect. In general, `def_id_full()` should be preferred, so `def_id_full()` should have a shorter name. That will happen in the next commit. --- src/librustdoc/clean/inline.rs | 6 +++++- src/librustdoc/clean/types.rs | 4 ++-- src/librustdoc/html/render/cache.rs | 13 +++++++++---- src/librustdoc/passes/collect_trait_impls.rs | 2 +- src/librustdoc/passes/stripper.rs | 4 ++-- 5 files changed, 19 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 3d5c1fee168..854f332dd99 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -479,7 +479,11 @@ crate fn build_impl( let (merged_attrs, cfg) = merge_attrs(cx, parent_module.into(), load_attrs(cx, did), attrs); trace!("merged_attrs={:?}", merged_attrs); - trace!("build_impl: impl {:?} for {:?}", trait_.as_ref().map(|t| t.def_id()), for_.def_id()); + trace!( + "build_impl: impl {:?} for {:?}", + trait_.as_ref().map(|t| t.def_id()), + for_.def_id_no_primitives() + ); ret.push(clean::Item::from_def_id_and_attrs_and_parts( did, None, diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 0395c6a729e..bd3308c1898 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1539,13 +1539,13 @@ impl Type { /// [`clean`]: crate::clean /// [`clean::Type`]: crate::clean::Type // FIXME: get rid of this function and always use `def_id_full` - crate fn def_id(&self) -> Option { + crate fn def_id_no_primitives(&self) -> Option { self.inner_def_id(None) } /// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s. /// - /// See [`Self::def_id`] for more. + /// See [`Self::def_id_no_primitives`] for more. /// /// [clean]: crate::clean crate fn def_id_full(&self, cache: &Cache) -> Option { diff --git a/src/librustdoc/html/render/cache.rs b/src/librustdoc/html/render/cache.rs index 4cc037f933e..20c5d6dfd8a 100644 --- a/src/librustdoc/html/render/cache.rs +++ b/src/librustdoc/html/render/cache.rs @@ -276,7 +276,7 @@ crate fn get_real_types<'tcx>( res: &mut FxHashSet<(Type, ItemType)>, ) -> usize { fn insert(res: &mut FxHashSet<(Type, ItemType)>, tcx: TyCtxt<'_>, ty: Type) -> usize { - if let Some(kind) = ty.def_id().map(|did| tcx.def_kind(did).into()) { + if let Some(kind) = ty.def_id_no_primitives().map(|did| tcx.def_kind(did).into()) { res.insert((ty, kind)); 1 } else if ty.is_primitive() { @@ -296,7 +296,9 @@ crate fn get_real_types<'tcx>( if let &Type::Generic(arg_s) = arg { if let Some(where_pred) = generics.where_predicates.iter().find(|g| match g { - WherePredicate::BoundPredicate { ty, .. } => ty.def_id() == arg.def_id(), + WherePredicate::BoundPredicate { ty, .. } => { + ty.def_id_no_primitives() == arg.def_id_no_primitives() + } _ => false, }) { let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]); @@ -363,7 +365,8 @@ crate fn get_all_types<'tcx>( if !args.is_empty() { all_types.extend(args); } else { - if let Some(kind) = arg.type_.def_id().map(|did| tcx.def_kind(did).into()) { + if let Some(kind) = arg.type_.def_id_no_primitives().map(|did| tcx.def_kind(did).into()) + { all_types.insert((arg.type_.clone(), kind)); } } @@ -374,7 +377,9 @@ crate fn get_all_types<'tcx>( let mut ret = FxHashSet::default(); get_real_types(generics, &return_type, tcx, 0, &mut ret); if ret.is_empty() { - if let Some(kind) = return_type.def_id().map(|did| tcx.def_kind(did).into()) { + if let Some(kind) = + return_type.def_id_no_primitives().map(|did| tcx.def_kind(did).into()) + { ret.insert((return_type.clone(), kind)); } } diff --git a/src/librustdoc/passes/collect_trait_impls.rs b/src/librustdoc/passes/collect_trait_impls.rs index f8f5118042f..0b2ee0d12c1 100644 --- a/src/librustdoc/passes/collect_trait_impls.rs +++ b/src/librustdoc/passes/collect_trait_impls.rs @@ -187,7 +187,7 @@ impl BadImplStripper { true } else if let Some(prim) = ty.primitive_type() { self.prims.contains(&prim) - } else if let Some(did) = ty.def_id() { + } else if let Some(did) = ty.def_id_no_primitives() { self.keep_impl_with_def_id(did.into()) } else { false diff --git a/src/librustdoc/passes/stripper.rs b/src/librustdoc/passes/stripper.rs index a160e31a450..74a9a2da06d 100644 --- a/src/librustdoc/passes/stripper.rs +++ b/src/librustdoc/passes/stripper.rs @@ -127,7 +127,7 @@ impl<'a> DocFolder for ImplStripper<'a> { if imp.trait_.is_none() && imp.items.is_empty() { return None; } - if let Some(did) = imp.for_.def_id() { + if let Some(did) = imp.for_.def_id_no_primitives() { if did.is_local() && !imp.for_.is_assoc_ty() && !self.retained.contains(&did.into()) { debug!("ImplStripper: impl item for stripped type; removing"); @@ -142,7 +142,7 @@ impl<'a> DocFolder for ImplStripper<'a> { } if let Some(generics) = imp.trait_.as_ref().and_then(|t| t.generics()) { for typaram in generics { - if let Some(did) = typaram.def_id() { + if let Some(did) = typaram.def_id_no_primitives() { if did.is_local() && !self.retained.contains(&did.into()) { debug!( "ImplStripper: stripped item in trait's generics; removing impl" -- cgit 1.4.1-3-g733a5 From f93cf66507ea87e041144ab9c1b06d2dc549f6bc Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Thu, 21 Oct 2021 20:17:47 -0700 Subject: Rename `Type::def_id_full()` to `Type::def_id()` It should be preferred over `def_id_no_primitives()`, so it should have a shorter name. I also put it before `def_id_no_primitives()` so that it shows up first in the docs. --- src/librustdoc/clean/inline.rs | 4 ++-- src/librustdoc/clean/mod.rs | 4 ++-- src/librustdoc/clean/types.rs | 24 ++++++++++++------------ src/librustdoc/formats/cache.rs | 4 ++-- src/librustdoc/html/render/mod.rs | 17 +++++++---------- src/librustdoc/html/render/print_item.rs | 4 ++-- src/librustdoc/passes/collect_trait_impls.rs | 2 +- 7 files changed, 28 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 854f332dd99..f36e11cbb0a 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -374,7 +374,7 @@ crate fn build_impl( // Only inline impl if the implementing type is // reachable in rustdoc generated documentation if !did.is_local() { - if let Some(did) = for_.def_id_full(&cx.cache) { + if let Some(did) = for_.def_id(&cx.cache) { if !cx.cache.access_levels.is_public(did) { return; } @@ -462,7 +462,7 @@ crate fn build_impl( } while let Some(ty) = stack.pop() { - if let Some(did) = ty.def_id_full(&cx.cache) { + if let Some(did) = ty.def_id(&cx.cache) { if tcx.get_attrs(did).lists(sym::doc).has_word(sym::hidden) { return; } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index d34cbb44339..ce0c4ebe718 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -385,7 +385,7 @@ impl<'tcx> Clean for ty::ProjectionTy<'tcx> { let self_type = self.self_ty().clean(cx); Type::QPath { name: cx.tcx.associated_item(self.item_def_id).ident.name, - self_def_id: self_type.def_id_full(&cx.cache), + self_def_id: self_type.def_id(&cx.cache), self_type: box self_type, trait_, } @@ -1887,7 +1887,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_> } let for_ = impl_.self_ty.clean(cx); - let type_alias = for_.def_id_full(&cx.cache).and_then(|did| match tcx.def_kind(did) { + let type_alias = for_.def_id(&cx.cache).and_then(|did| match tcx.def_kind(did) { DefKind::TyAlias => Some(tcx.type_of(did).clean(cx)), _ => None, }); diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index bd3308c1898..4b0a7aed0c2 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1529,28 +1529,28 @@ impl Type { QPath { ref self_type, .. } => return self_type.inner_def_id(cache), Generic(_) | Infer | ImplTrait(_) => return None, }; - cache.and_then(|c| Primitive(t).def_id_full(c)) + cache.and_then(|c| Primitive(t).def_id(c)) + } + + /// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s. + /// + /// See [`Self::def_id_no_primitives`] for more. + /// + /// [clean]: crate::clean + crate fn def_id(&self, cache: &Cache) -> Option { + self.inner_def_id(Some(cache)) } /// Use this method to get the [`DefId`] of a [`clean`] AST node. /// This will return [`None`] when called on a primitive [`clean::Type`]. - /// Use [`Self::def_id_full`] if you want to include primitives. + /// Use [`Self::def_id`] if you want to include primitives. /// /// [`clean`]: crate::clean /// [`clean::Type`]: crate::clean::Type - // FIXME: get rid of this function and always use `def_id_full` + // FIXME: get rid of this function and always use `def_id` crate fn def_id_no_primitives(&self) -> Option { self.inner_def_id(None) } - - /// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s. - /// - /// See [`Self::def_id_no_primitives`] for more. - /// - /// [clean]: crate::clean - crate fn def_id_full(&self, cache: &Cache) -> Option { - self.inner_def_id(Some(cache)) - } } /// A primitive (aka, builtin) type. diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index afaeded88b4..54988871f41 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -207,7 +207,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { .as_ref() .map_or(false, |t| self.cache.masked_crates.contains(&t.def_id().krate)) || i.for_ - .def_id_full(self.cache) + .def_id(self.cache) .map_or(false, |d| self.cache.masked_crates.contains(&d.krate)) { return None; @@ -456,7 +456,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) { for bound in generics { - if let Some(did) = bound.def_id_full(self.cache) { + if let Some(did) = bound.def_id(self.cache) { dids.insert(did); } } diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index b9ad917371c..8e064d6d8bf 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1168,8 +1168,8 @@ fn render_deref_methods( debug!("Render deref methods for {:#?}, target {:#?}", impl_.inner_impl().for_, target); let what = AssocItemRender::DerefFor { trait_: deref_type, type_: real_target, deref_mut_: deref_mut }; - if let Some(did) = target.def_id_full(cache) { - if let Some(type_did) = impl_.inner_impl().for_.def_id_full(cache) { + if let Some(did) = target.def_id(cache) { + if let Some(type_did) = impl_.inner_impl().for_.def_id(cache) { // `impl Deref for S` if did == type_did { // Avoid infinite cycles @@ -1215,7 +1215,7 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) -> fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String { let mut out = Buffer::html(); - if let Some(did) = decl.output.as_return().and_then(|t| t.def_id_full(cx.cache())) { + if let Some(did) = decl.output.as_return().and_then(|t| t.def_id(cx.cache())) { if let Some(impls) = cx.cache().impls.get(&did) { for i in impls { let impl_ = i.inner_impl(); @@ -2058,8 +2058,8 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V }) { debug!("found target, real_target: {:?} {:?}", target, real_target); - if let Some(did) = target.def_id_full(c) { - if let Some(type_did) = impl_.inner_impl().for_.def_id_full(c) { + if let Some(did) = target.def_id(c) { + if let Some(type_did) = impl_.inner_impl().for_.def_id(c) { // `impl Deref for S` if did == type_did { // Avoid infinite cycles @@ -2069,7 +2069,7 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V } let deref_mut = v.iter().any(|i| i.trait_did() == cx.tcx().lang_items().deref_mut_trait()); let inner_impl = target - .def_id_full(c) + .def_id(c) .or_else(|| { target.primitive_type().and_then(|prim| c.primitive_locations.get(&prim).cloned()) }) @@ -2232,10 +2232,7 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean let mut res = implementors .iter() .filter(|i| { - i.inner_impl() - .for_ - .def_id_full(cache) - .map_or(false, |d| !cache.paths.contains_key(&d)) + i.inner_impl().for_.def_id(cache).map_or(false, |d| !cache.paths.contains_key(&d)) }) .filter_map(|i| extract_for_impl_name(&i.impl_item, cx)) .collect::>(); diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 4b439b8ed71..94177aa24b8 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -21,7 +21,7 @@ use super::{ render_impl, render_stability_since_raw, write_srclink, AssocItemLink, Context, ImplRenderingParameters, }; -use crate::clean::{self}; +use crate::clean; use crate::formats::item_type::ItemType; use crate::formats::{AssocItemRender, Impl, RenderMode}; use crate::html::escape::Escape; @@ -742,7 +742,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra } let (local, foreign) = implementors.iter().partition::, _>(|i| { - i.inner_impl().for_.def_id_full(cache).map_or(true, |d| cache.paths.contains_key(&d)) + i.inner_impl().for_.def_id(cache).map_or(true, |d| cache.paths.contains_key(&d)) }); let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) = diff --git a/src/librustdoc/passes/collect_trait_impls.rs b/src/librustdoc/passes/collect_trait_impls.rs index 0b2ee0d12c1..91a0cb413eb 100644 --- a/src/librustdoc/passes/collect_trait_impls.rs +++ b/src/librustdoc/passes/collect_trait_impls.rs @@ -70,7 +70,7 @@ crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate { if let Some(prim) = target.primitive_type() { cleaner.prims.insert(prim); - } else if let Some(did) = target.def_id_full(&cx.cache) { + } else if let Some(did) = target.def_id(&cx.cache) { cleaner.items.insert(did.into()); } } -- cgit 1.4.1-3-g733a5 From 865d99f82be5718abce2c75a9ed87b1ed028910e Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Fri, 22 Oct 2021 13:24:51 -0700 Subject: docs: Escape brackets to satisfy the linkchecker My change to use `Type::def_id()` (formerly `Type::def_id_full()`) in more places caused some docs to show up that used to be missed by rustdoc. Those docs contained unescaped square brackets, which triggered linkcheck errors. This commit escapes the square brackets and adds this particular instance to the linkcheck exception list. --- library/core/src/str/traits.rs | 9 ++++----- library/std/src/sys_common/wtf8.rs | 2 +- src/tools/linkchecker/main.rs | 2 ++ 3 files changed, 7 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/library/core/src/str/traits.rs b/library/core/src/str/traits.rs index e225776bc64..95267624748 100644 --- a/library/core/src/str/traits.rs +++ b/library/core/src/str/traits.rs @@ -234,7 +234,7 @@ unsafe impl SliceIndex for ops::Range { /// Implements substring slicing with syntax `&self[.. end]` or `&mut /// self[.. end]`. /// -/// Returns a slice of the given string from the byte range [`0`, `end`). +/// Returns a slice of the given string from the byte range \[0, `end`). /// Equivalent to `&self[0 .. end]` or `&mut self[0 .. end]`. /// /// This operation is *O*(1). @@ -304,9 +304,8 @@ unsafe impl SliceIndex for ops::RangeTo { /// Implements substring slicing with syntax `&self[begin ..]` or `&mut /// self[begin ..]`. /// -/// Returns a slice of the given string from the byte range [`begin`, -/// `len`). Equivalent to `&self[begin .. len]` or `&mut self[begin .. -/// len]`. +/// Returns a slice of the given string from the byte range \[`begin`, `len`). +/// Equivalent to `&self[begin .. len]` or `&mut self[begin .. len]`. /// /// This operation is *O*(1). /// @@ -433,7 +432,7 @@ unsafe impl SliceIndex for ops::RangeInclusive { /// Implements substring slicing with syntax `&self[..= end]` or `&mut /// self[..= end]`. /// -/// Returns a slice of the given string from the byte range [0, `end`]. +/// Returns a slice of the given string from the byte range \[0, `end`\]. /// Equivalent to `&self [0 .. end + 1]`, except if `end` has the maximum /// value for `usize`. /// diff --git a/library/std/src/sys_common/wtf8.rs b/library/std/src/sys_common/wtf8.rs index 9508bd7da59..0629859bd9d 100644 --- a/library/std/src/sys_common/wtf8.rs +++ b/library/std/src/sys_common/wtf8.rs @@ -686,7 +686,7 @@ impl Wtf8 { } } -/// Returns a slice of the given string for the byte range [`begin`..`end`). +/// Returns a slice of the given string for the byte range \[`begin`..`end`). /// /// # Panics /// diff --git a/src/tools/linkchecker/main.rs b/src/tools/linkchecker/main.rs index 94ebbb33e8d..94e82e3d9f7 100644 --- a/src/tools/linkchecker/main.rs +++ b/src/tools/linkchecker/main.rs @@ -85,6 +85,8 @@ const INTRA_DOC_LINK_EXCEPTIONS: &[(&str, &[&str])] = &[ ("core/slice/trait.SliceIndex.html", &["begin, end"]), ("alloc/slice/trait.SliceIndex.html", &["begin, end"]), ("std/slice/trait.SliceIndex.html", &["begin, end"]), + ("core/primitive.str.html", &["begin, end"]), + ("std/primitive.str.html", &["begin, end"]), ]; -- cgit 1.4.1-3-g733a5 From 3ad0834700ec3db9952a7b69daf805c04a98d8e0 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Fri, 22 Oct 2021 16:50:24 -0700 Subject: Fix another place that used `def_id_no_primitives()` --- src/librustdoc/clean/inline.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index f36e11cbb0a..c5e05875819 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -482,7 +482,7 @@ crate fn build_impl( trace!( "build_impl: impl {:?} for {:?}", trait_.as_ref().map(|t| t.def_id()), - for_.def_id_no_primitives() + for_.def_id(&cx.cache) ); ret.push(clean::Item::from_def_id_and_attrs_and_parts( did, -- cgit 1.4.1-3-g733a5