Skip to content

Commit 862962b

Browse files
committed
Auto merge of #91195 - camelid:path-did, r=jyn514
rustdoc: Remove `ResolvedPath.did` `ResolvedPath.did` was not actually the same as `.path.def_id()`. Instead, `.did` referred to the `DefId` of the page to be used as a hyperlink target. For example, a link to `Struct::method()` would use `Struct`'s `DefId` as its `.did` field. This behavior is confusing, easy to accidentally misuse, and can instead be obtained on-demand when computing hyperlink targets. It's also likely part of the reason `kind_side_channel` exists. I'm currently working on some experimental refactorings in `collect_intra_doc_links` that I believe require -- or at least benefit from -- removing `.did`. r? `@jyn514`
2 parents 23a4366 + 617bbb2 commit 862962b

File tree

9 files changed

+44
-42
lines changed

9 files changed

+44
-42
lines changed

src/librustdoc/clean/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1411,12 +1411,12 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
14111411
};
14121412
inline::record_extern_fqn(cx, did, kind);
14131413
let path = external_path(cx, did, false, vec![], substs);
1414-
ResolvedPath { path, did }
1414+
ResolvedPath { path }
14151415
}
14161416
ty::Foreign(did) => {
14171417
inline::record_extern_fqn(cx, did, ItemType::ForeignType);
14181418
let path = external_path(cx, did, false, vec![], InternalSubsts::empty());
1419-
ResolvedPath { path, did }
1419+
ResolvedPath { path }
14201420
}
14211421
ty::Dynamic(obj, ref reg) => {
14221422
// HACK: pick the first `did` as the `did` of the trait object. Someone

src/librustdoc/clean/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ crate enum Type {
14191419
/// A named type, which could be a trait.
14201420
///
14211421
/// This is mostly Rustdoc's version of [`hir::Path`]. It has to be different because Rustdoc's [`PathSegment`] can contain cleaned generics.
1422-
ResolvedPath { path: Path, did: DefId },
1422+
ResolvedPath { path: Path },
14231423
/// A `dyn Trait` object: `dyn for<'a> Trait<'a> + Send + 'static`
14241424
DynTrait(Vec<PolyTrait>, Option<Lifetime>),
14251425
/// A type parameter.
@@ -1522,7 +1522,7 @@ impl Type {
15221522

15231523
fn inner_def_id(&self, cache: Option<&Cache>) -> Option<DefId> {
15241524
let t: PrimitiveType = match *self {
1525-
ResolvedPath { did, .. } => return Some(did),
1525+
ResolvedPath { ref path } => return Some(path.def_id()),
15261526
DynTrait(ref bounds, _) => return Some(bounds[0].trait_.def_id()),
15271527
Primitive(p) => return cache.and_then(|c| c.primitive_locations.get(&p).cloned()),
15281528
BorrowedRef { type_: box Generic(..), .. } => PrimitiveType::Reference,

src/librustdoc/clean/utils.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ crate fn build_deref_target_impls(cx: &mut DocContext<'_>, items: &[Item], ret:
187187
for &did in prim.impls(tcx).iter().filter(|did| !did.is_local()) {
188188
inline::build_impl(cx, None, did, None, ret);
189189
}
190-
} else if let ResolvedPath { did, .. } = *target {
190+
} else if let ResolvedPath { path } = target {
191+
let did = path.def_id();
191192
if !did.is_local() {
192193
inline::build_impls(cx, None, did, None, ret);
193194
}
@@ -360,8 +361,8 @@ crate fn resolve_type(cx: &mut DocContext<'_>, path: Path) -> Type {
360361
Res::SelfTy(..) if path.segments.len() == 1 => Generic(kw::SelfUpper),
361362
Res::Def(DefKind::TyParam, _) if path.segments.len() == 1 => Generic(path.segments[0].name),
362363
_ => {
363-
let did = register_res(cx, path.res);
364-
ResolvedPath { path, did }
364+
let _ = register_res(cx, path.res);
365+
ResolvedPath { path }
365366
}
366367
}
367368
}
@@ -393,20 +394,12 @@ crate fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
393394
debug!("register_res({:?})", res);
394395

395396
let (did, kind) = match res {
396-
Res::Def(DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst, i) => {
397-
// associated items are documented, but on the page of their parent
398-
(cx.tcx.parent(i).unwrap(), ItemType::Trait)
399-
}
400-
Res::Def(DefKind::Variant, i) => {
401-
// variant items are documented, but on the page of their parent
402-
(cx.tcx.parent(i).expect("cannot get parent def id"), ItemType::Enum)
403-
}
404-
// Each of these have their own page.
397+
// These should be added to the cache using `record_extern_fqn`.
405398
Res::Def(
406399
kind
407400
@
408-
(Fn | TyAlias | Enum | Trait | Struct | Union | Mod | ForeignTy | Const | Static
409-
| Macro(..) | TraitAlias),
401+
(AssocTy | AssocFn | AssocConst | Variant | Fn | TyAlias | Enum | Trait | Struct
402+
| Union | Mod | ForeignTy | Const | Static | Macro(..) | TraitAlias),
410403
i,
411404
) => (i, kind.into()),
412405
// This is part of a trait definition; document the trait.

src/librustdoc/formats/cache.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
401401
clean::ImplItem(ref i) => {
402402
self.cache.parent_is_trait_impl = i.trait_.is_some();
403403
match i.for_ {
404-
clean::ResolvedPath { did, .. } => {
405-
self.cache.parent_stack.push(did);
404+
clean::ResolvedPath { ref path } => {
405+
self.cache.parent_stack.push(path.def_id());
406406
true
407407
}
408408
clean::DynTrait(ref bounds, _)
@@ -436,9 +436,9 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
436436
// Note: matching twice to restrict the lifetime of the `i` borrow.
437437
let mut dids = FxHashSet::default();
438438
match i.for_ {
439-
clean::ResolvedPath { did, .. }
440-
| clean::BorrowedRef { type_: box clean::ResolvedPath { did, .. }, .. } => {
441-
dids.insert(did);
439+
clean::ResolvedPath { ref path }
440+
| clean::BorrowedRef { type_: box clean::ResolvedPath { ref path }, .. } => {
441+
dids.insert(path.def_id());
442442
}
443443
clean::DynTrait(ref bounds, _)
444444
| clean::BorrowedRef { type_: box clean::DynTrait(ref bounds, _), .. } => {

src/librustdoc/html/format.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ use rustc_attr::{ConstStability, StabilityLevel};
1313
use rustc_data_structures::captures::Captures;
1414
use rustc_data_structures::fx::FxHashSet;
1515
use rustc_hir as hir;
16+
use rustc_hir::def::DefKind;
1617
use rustc_hir::def_id::DefId;
1718
use rustc_middle::ty;
19+
use rustc_middle::ty::DefIdTree;
1820
use rustc_middle::ty::TyCtxt;
1921
use rustc_span::def_id::CRATE_DEF_INDEX;
2022
use rustc_target::spec::abi::Abi;
@@ -502,7 +504,16 @@ crate fn href_with_root_path(
502504
cx: &Context<'_>,
503505
root_path: Option<&str>,
504506
) -> Result<(String, ItemType, Vec<String>), HrefError> {
505-
let cache = &cx.cache();
507+
let tcx = cx.tcx();
508+
let def_kind = tcx.def_kind(did);
509+
let did = match def_kind {
510+
DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
511+
// documented on their parent's page
512+
tcx.parent(did).unwrap()
513+
}
514+
_ => did,
515+
};
516+
let cache = cx.cache();
506517
let relative_to = &cx.current;
507518
fn to_module_fqp(shortty: ItemType, fqp: &[String]) -> &[String] {
508519
if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] }
@@ -751,8 +762,9 @@ fn fmt_type<'cx>(
751762

752763
match *t {
753764
clean::Generic(name) => write!(f, "{}", name),
754-
clean::ResolvedPath { did, ref path } => {
765+
clean::ResolvedPath { ref path } => {
755766
// Paths like `T::Output` and `Self::Output` should be rendered with all segments.
767+
let did = path.def_id();
756768
resolved_path(f, did, path, path.is_assoc_ty(), use_absolute, cx)
757769
}
758770
clean::DynTrait(ref bounds, ref lt) => {

src/librustdoc/html/render/cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ crate fn get_real_types<'tcx>(
371371
let mut ty_generics = Vec::new();
372372
for bound in bound.get_bounds().unwrap_or(&[]) {
373373
if let Some(path) = bound.get_trait_path() {
374-
let ty = Type::ResolvedPath { did: path.def_id(), path };
374+
let ty = Type::ResolvedPath { path };
375375
get_real_types(generics, &ty, tcx, recurse + 1, &mut ty_generics, cache);
376376
}
377377
}

src/librustdoc/html/render/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1227,8 +1227,8 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) ->
12271227
| SelfTy::SelfExplicit(clean::BorrowedRef { mutability, .. }) => {
12281228
(mutability == Mutability::Mut, false, false)
12291229
}
1230-
SelfTy::SelfExplicit(clean::ResolvedPath { did, .. }) => {
1231-
(false, Some(did) == tcx.lang_items().owned_box(), false)
1230+
SelfTy::SelfExplicit(clean::ResolvedPath { path }) => {
1231+
(false, Some(path.def_id()) == tcx.lang_items().owned_box(), false)
12321232
}
12331233
SelfTy::SelfValue => (false, false, true),
12341234
_ => (false, false, false),
@@ -2520,7 +2520,7 @@ fn collect_paths_for_type(first_ty: clean::Type, cache: &Cache) -> Vec<String> {
25202520
}
25212521

25222522
match ty {
2523-
clean::Type::ResolvedPath { did, .. } => process_path(did),
2523+
clean::Type::ResolvedPath { path } => process_path(path.def_id()),
25242524
clean::Type::Tuple(tys) => {
25252525
work.extend(tys.into_iter());
25262526
}

src/librustdoc/html/render/print_item.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -727,10 +727,11 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
727727
let mut implementor_dups: FxHashMap<Symbol, (DefId, bool)> = FxHashMap::default();
728728
for implementor in implementors {
729729
match implementor.inner_impl().for_ {
730-
clean::ResolvedPath { ref path, did, .. }
731-
| clean::BorrowedRef {
732-
type_: box clean::ResolvedPath { ref path, did, .. }, ..
733-
} if !path.is_assoc_ty() => {
730+
clean::ResolvedPath { ref path }
731+
| clean::BorrowedRef { type_: box clean::ResolvedPath { ref path }, .. }
732+
if !path.is_assoc_ty() =>
733+
{
734+
let did = path.def_id();
734735
let &mut (prev_did, ref mut has_duplicates) =
735736
implementor_dups.entry(path.last()).or_insert((did, false));
736737
if prev_did != did {

src/librustdoc/json/conversions.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,7 @@ impl FromWithTcx<clean::GenericBound> for GenericBound {
365365
match bound {
366366
TraitBound(clean::PolyTrait { trait_, generic_params }, modifier) => {
367367
// FIXME: should `trait_` be a clean::Path equivalent in JSON?
368-
let trait_ =
369-
clean::ResolvedPath { did: trait_.def_id(), path: trait_ }.into_tcx(tcx);
368+
let trait_ = clean::ResolvedPath { path: trait_ }.into_tcx(tcx);
370369
GenericBound::TraitBound {
371370
trait_,
372371
generic_params: generic_params.into_iter().map(|x| x.into_tcx(tcx)).collect(),
@@ -391,9 +390,9 @@ impl FromWithTcx<clean::Type> for Type {
391390
fn from_tcx(ty: clean::Type, tcx: TyCtxt<'_>) -> Self {
392391
use clean::Type::*;
393392
match ty {
394-
ResolvedPath { path, did } => Type::ResolvedPath {
393+
ResolvedPath { path } => Type::ResolvedPath {
395394
name: path.whole_name(),
396-
id: from_item_id(did.into()),
395+
id: from_item_id(path.def_id().into()),
397396
args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))),
398397
param_names: Vec::new(),
399398
},
@@ -436,7 +435,7 @@ impl FromWithTcx<clean::Type> for Type {
436435
},
437436
QPath { name, self_type, trait_, .. } => {
438437
// FIXME: should `trait_` be a clean::Path equivalent in JSON?
439-
let trait_ = ResolvedPath { did: trait_.def_id(), path: trait_ }.into_tcx(tcx);
438+
let trait_ = ResolvedPath { path: trait_ }.into_tcx(tcx);
440439
Type::QualifiedPath {
441440
name: name.to_string(),
442441
self_type: Box::new((*self_type).into_tcx(tcx)),
@@ -502,10 +501,7 @@ impl FromWithTcx<clean::Impl> for Impl {
502501
let provided_trait_methods = impl_.provided_trait_methods(tcx);
503502
let clean::Impl { unsafety, generics, trait_, for_, items, polarity, kind } = impl_;
504503
// FIXME: should `trait_` be a clean::Path equivalent in JSON?
505-
let trait_ = trait_.map(|path| {
506-
let did = path.def_id();
507-
clean::ResolvedPath { path, did }.into_tcx(tcx)
508-
});
504+
let trait_ = trait_.map(|path| clean::ResolvedPath { path }.into_tcx(tcx));
509505
// FIXME: use something like ImplKind in JSON?
510506
let (synthetic, blanket_impl) = match kind {
511507
clean::ImplKind::Normal => (false, None),

0 commit comments

Comments
 (0)