Skip to content

Commit e3eebfe

Browse files
authored
Rollup merge of #90154 - camelid:remove-getdefid, r=jyn514
rustdoc: Remove `GetDefId` See the individual commit messages for details. r? `@jyn514`
2 parents e269e6b + 3ad0834 commit e3eebfe

File tree

12 files changed

+69
-99
lines changed

12 files changed

+69
-99
lines changed

library/core/src/str/traits.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ unsafe impl SliceIndex<str> for ops::Range<usize> {
234234
/// Implements substring slicing with syntax `&self[.. end]` or `&mut
235235
/// self[.. end]`.
236236
///
237-
/// Returns a slice of the given string from the byte range [`0`, `end`).
237+
/// Returns a slice of the given string from the byte range \[0, `end`).
238238
/// Equivalent to `&self[0 .. end]` or `&mut self[0 .. end]`.
239239
///
240240
/// This operation is *O*(1).
@@ -304,9 +304,8 @@ unsafe impl SliceIndex<str> for ops::RangeTo<usize> {
304304
/// Implements substring slicing with syntax `&self[begin ..]` or `&mut
305305
/// self[begin ..]`.
306306
///
307-
/// Returns a slice of the given string from the byte range [`begin`,
308-
/// `len`). Equivalent to `&self[begin .. len]` or `&mut self[begin ..
309-
/// len]`.
307+
/// Returns a slice of the given string from the byte range \[`begin`, `len`).
308+
/// Equivalent to `&self[begin .. len]` or `&mut self[begin .. len]`.
310309
///
311310
/// This operation is *O*(1).
312311
///
@@ -433,7 +432,7 @@ unsafe impl SliceIndex<str> for ops::RangeInclusive<usize> {
433432
/// Implements substring slicing with syntax `&self[..= end]` or `&mut
434433
/// self[..= end]`.
435434
///
436-
/// Returns a slice of the given string from the byte range [0, `end`].
435+
/// Returns a slice of the given string from the byte range \[0, `end`\].
437436
/// Equivalent to `&self [0 .. end + 1]`, except if `end` has the maximum
438437
/// value for `usize`.
439438
///

library/std/src/sys_common/wtf8.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ impl Wtf8 {
686686
}
687687
}
688688

689-
/// Returns a slice of the given string for the byte range [`begin`..`end`).
689+
/// Returns a slice of the given string for the byte range \[`begin`..`end`).
690690
///
691691
/// # Panics
692692
///

src/librustdoc/clean/inline.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ use rustc_middle::ty::{self, TyCtxt};
1414
use rustc_span::hygiene::MacroKind;
1515
use rustc_span::symbol::{kw, sym, Symbol};
1616

17-
use crate::clean::{
18-
self, utils, Attributes, AttributesExt, GetDefId, ItemId, NestedAttributesExt, Type,
19-
};
17+
use crate::clean::{self, utils, Attributes, AttributesExt, ItemId, NestedAttributesExt, Type};
2018
use crate::core::DocContext;
2119
use crate::formats::item_type::ItemType;
2220

@@ -325,7 +323,7 @@ fn merge_attrs(
325323
}
326324
}
327325

328-
/// Builds a specific implementation of a type. The `did` could be a type method or trait method.
326+
/// Inline an `impl`, inherent or of a trait. The `did` must be for an `impl`.
329327
crate fn build_impl(
330328
cx: &mut DocContext<'_>,
331329
parent_module: impl Into<Option<DefId>>,
@@ -376,7 +374,7 @@ crate fn build_impl(
376374
// Only inline impl if the implementing type is
377375
// reachable in rustdoc generated documentation
378376
if !did.is_local() {
379-
if let Some(did) = for_.def_id() {
377+
if let Some(did) = for_.def_id(&cx.cache) {
380378
if !cx.cache.access_levels.is_public(did) {
381379
return;
382380
}
@@ -464,7 +462,7 @@ crate fn build_impl(
464462
}
465463

466464
while let Some(ty) = stack.pop() {
467-
if let Some(did) = ty.def_id() {
465+
if let Some(did) = ty.def_id(&cx.cache) {
468466
if tcx.get_attrs(did).lists(sym::doc).has_word(sym::hidden) {
469467
return;
470468
}
@@ -481,7 +479,11 @@ crate fn build_impl(
481479
let (merged_attrs, cfg) = merge_attrs(cx, parent_module.into(), load_attrs(cx, did), attrs);
482480
trace!("merged_attrs={:?}", merged_attrs);
483481

484-
trace!("build_impl: impl {:?} for {:?}", trait_.as_ref().map(|t| t.def_id()), for_.def_id());
482+
trace!(
483+
"build_impl: impl {:?} for {:?}",
484+
trait_.as_ref().map(|t| t.def_id()),
485+
for_.def_id(&cx.cache)
486+
);
485487
ret.push(clean::Item::from_def_id_and_attrs_and_parts(
486488
did,
487489
None,

src/librustdoc/clean/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
383383
let self_type = self.self_ty().clean(cx);
384384
Type::QPath {
385385
name: cx.tcx.associated_item(self.item_def_id).ident.name,
386-
self_def_id: self_type.def_id(),
386+
self_def_id: self_type.def_id(&cx.cache),
387387
self_type: box self_type,
388388
trait_,
389389
}
@@ -1883,7 +1883,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>
18831883
}
18841884

18851885
let for_ = impl_.self_ty.clean(cx);
1886-
let type_alias = for_.def_id().and_then(|did| match tcx.def_kind(did) {
1886+
let type_alias = for_.def_id(&cx.cache).and_then(|did| match tcx.def_kind(did) {
18871887
DefKind::TyAlias => Some(tcx.type_of(did).clean(cx)),
18881888
_ => None,
18891889
});

src/librustdoc/clean/types.rs

+21-56
Original file line numberDiff line numberDiff line change
@@ -1370,17 +1370,10 @@ crate enum FnRetTy {
13701370
DefaultReturn,
13711371
}
13721372

1373-
impl GetDefId for FnRetTy {
1374-
fn def_id(&self) -> Option<DefId> {
1375-
match *self {
1376-
Return(ref ty) => ty.def_id(),
1377-
DefaultReturn => None,
1378-
}
1379-
}
1380-
1381-
fn def_id_full(&self, cache: &Cache) -> Option<DefId> {
1382-
match *self {
1383-
Return(ref ty) => ty.def_id_full(cache),
1373+
impl FnRetTy {
1374+
crate fn as_return(&self) -> Option<&Type> {
1375+
match self {
1376+
Return(ret) => Some(ret),
13841377
DefaultReturn => None,
13851378
}
13861379
}
@@ -1458,34 +1451,6 @@ crate enum Type {
14581451
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
14591452
rustc_data_structures::static_assert_size!(Type, 72);
14601453

1461-
crate trait GetDefId {
1462-
/// Use this method to get the [`DefId`] of a [`clean`] AST node.
1463-
/// This will return [`None`] when called on a primitive [`clean::Type`].
1464-
/// Use [`Self::def_id_full`] if you want to include primitives.
1465-
///
1466-
/// [`clean`]: crate::clean
1467-
/// [`clean::Type`]: crate::clean::Type
1468-
// FIXME: get rid of this function and always use `def_id_full`
1469-
fn def_id(&self) -> Option<DefId>;
1470-
1471-
/// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s.
1472-
///
1473-
/// See [`Self::def_id`] for more.
1474-
///
1475-
/// [clean]: crate::clean
1476-
fn def_id_full(&self, cache: &Cache) -> Option<DefId>;
1477-
}
1478-
1479-
impl<T: GetDefId> GetDefId for Option<T> {
1480-
fn def_id(&self) -> Option<DefId> {
1481-
self.as_ref().and_then(|d| d.def_id())
1482-
}
1483-
1484-
fn def_id_full(&self, cache: &Cache) -> Option<DefId> {
1485-
self.as_ref().and_then(|d| d.def_id_full(cache))
1486-
}
1487-
}
1488-
14891454
impl Type {
14901455
crate fn primitive_type(&self) -> Option<PrimitiveType> {
14911456
match *self {
@@ -1564,17 +1529,27 @@ impl Type {
15641529
QPath { ref self_type, .. } => return self_type.inner_def_id(cache),
15651530
Generic(_) | Infer | ImplTrait(_) => return None,
15661531
};
1567-
cache.and_then(|c| Primitive(t).def_id_full(c))
1532+
cache.and_then(|c| Primitive(t).def_id(c))
15681533
}
1569-
}
15701534

1571-
impl GetDefId for Type {
1572-
fn def_id(&self) -> Option<DefId> {
1573-
self.inner_def_id(None)
1535+
/// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s.
1536+
///
1537+
/// See [`Self::def_id_no_primitives`] for more.
1538+
///
1539+
/// [clean]: crate::clean
1540+
crate fn def_id(&self, cache: &Cache) -> Option<DefId> {
1541+
self.inner_def_id(Some(cache))
15741542
}
15751543

1576-
fn def_id_full(&self, cache: &Cache) -> Option<DefId> {
1577-
self.inner_def_id(Some(cache))
1544+
/// Use this method to get the [`DefId`] of a [`clean`] AST node.
1545+
/// This will return [`None`] when called on a primitive [`clean::Type`].
1546+
/// Use [`Self::def_id`] if you want to include primitives.
1547+
///
1548+
/// [`clean`]: crate::clean
1549+
/// [`clean::Type`]: crate::clean::Type
1550+
// FIXME: get rid of this function and always use `def_id`
1551+
crate fn def_id_no_primitives(&self) -> Option<DefId> {
1552+
self.inner_def_id(None)
15781553
}
15791554
}
15801555

@@ -2092,16 +2067,6 @@ crate struct Typedef {
20922067
crate item_type: Option<Type>,
20932068
}
20942069

2095-
impl GetDefId for Typedef {
2096-
fn def_id(&self) -> Option<DefId> {
2097-
self.type_.def_id()
2098-
}
2099-
2100-
fn def_id_full(&self, cache: &Cache) -> Option<DefId> {
2101-
self.type_.def_id_full(cache)
2102-
}
2103-
}
2104-
21052070
#[derive(Clone, Debug)]
21062071
crate struct OpaqueTy {
21072072
crate bounds: Vec<GenericBound>,

src/librustdoc/formats/cache.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_middle::middle::privacy::AccessLevels;
66
use rustc_middle::ty::TyCtxt;
77
use rustc_span::symbol::sym;
88

9-
use crate::clean::{self, GetDefId, ItemId, PrimitiveType};
9+
use crate::clean::{self, ItemId, PrimitiveType};
1010
use crate::config::RenderOptions;
1111
use crate::fold::DocFolder;
1212
use crate::formats::item_type::ItemType;
@@ -206,7 +206,9 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
206206
|| i.trait_
207207
.as_ref()
208208
.map_or(false, |t| self.cache.masked_crates.contains(&t.def_id().krate))
209-
|| i.for_.def_id().map_or(false, |d| self.cache.masked_crates.contains(&d.krate))
209+
|| i.for_
210+
.def_id(self.cache)
211+
.map_or(false, |d| self.cache.masked_crates.contains(&d.krate))
210212
{
211213
return None;
212214
}
@@ -454,7 +456,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
454456

455457
if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) {
456458
for bound in generics {
457-
if let Some(did) = bound.def_id() {
459+
if let Some(did) = bound.def_id(self.cache) {
458460
dids.insert(did);
459461
}
460462
}

src/librustdoc/html/render/cache.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use rustc_span::symbol::Symbol;
77
use serde::ser::{Serialize, SerializeStruct, Serializer};
88

99
use crate::clean;
10-
use crate::clean::types::{
11-
FnDecl, FnRetTy, GenericBound, Generics, GetDefId, Type, WherePredicate,
12-
};
10+
use crate::clean::types::{FnDecl, FnRetTy, GenericBound, Generics, Type, WherePredicate};
1311
use crate::formats::cache::Cache;
1412
use crate::formats::item_type::ItemType;
1513
use crate::html::markdown::short_markdown_summary;
@@ -278,7 +276,7 @@ crate fn get_real_types<'tcx>(
278276
res: &mut FxHashSet<(Type, ItemType)>,
279277
) -> usize {
280278
fn insert(res: &mut FxHashSet<(Type, ItemType)>, tcx: TyCtxt<'_>, ty: Type) -> usize {
281-
if let Some(kind) = ty.def_id().map(|did| tcx.def_kind(did).into()) {
279+
if let Some(kind) = ty.def_id_no_primitives().map(|did| tcx.def_kind(did).into()) {
282280
res.insert((ty, kind));
283281
1
284282
} else if ty.is_primitive() {
@@ -298,7 +296,9 @@ crate fn get_real_types<'tcx>(
298296

299297
if let Type::Generic(arg_s) = *arg {
300298
if let Some(where_pred) = generics.where_predicates.iter().find(|g| match g {
301-
WherePredicate::BoundPredicate { ty, .. } => ty.def_id() == arg.def_id(),
299+
WherePredicate::BoundPredicate { ty, .. } => {
300+
ty.def_id_no_primitives() == arg.def_id_no_primitives()
301+
}
302302
_ => false,
303303
}) {
304304
let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]);
@@ -365,7 +365,8 @@ crate fn get_all_types<'tcx>(
365365
if !args.is_empty() {
366366
all_types.extend(args);
367367
} else {
368-
if let Some(kind) = arg.type_.def_id().map(|did| tcx.def_kind(did).into()) {
368+
if let Some(kind) = arg.type_.def_id_no_primitives().map(|did| tcx.def_kind(did).into())
369+
{
369370
all_types.insert((arg.type_.clone(), kind));
370371
}
371372
}
@@ -376,7 +377,9 @@ crate fn get_all_types<'tcx>(
376377
let mut ret = FxHashSet::default();
377378
get_real_types(generics, return_type, tcx, 0, &mut ret);
378379
if ret.is_empty() {
379-
if let Some(kind) = return_type.def_id().map(|did| tcx.def_kind(did).into()) {
380+
if let Some(kind) =
381+
return_type.def_id_no_primitives().map(|did| tcx.def_kind(did).into())
382+
{
380383
ret.insert((return_type.clone(), kind));
381384
}
382385
}

src/librustdoc/html/render/mod.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use rustc_span::{
6262
use serde::ser::SerializeSeq;
6363
use serde::{Serialize, Serializer};
6464

65-
use crate::clean::{self, GetDefId, ItemId, RenderedLink, SelfTy};
65+
use crate::clean::{self, ItemId, RenderedLink, SelfTy};
6666
use crate::docfs::PathError;
6767
use crate::error::Error;
6868
use crate::formats::cache::Cache;
@@ -1184,8 +1184,8 @@ fn render_deref_methods(
11841184
debug!("Render deref methods for {:#?}, target {:#?}", impl_.inner_impl().for_, target);
11851185
let what =
11861186
AssocItemRender::DerefFor { trait_: deref_type, type_: real_target, deref_mut_: deref_mut };
1187-
if let Some(did) = target.def_id_full(cache) {
1188-
if let Some(type_did) = impl_.inner_impl().for_.def_id_full(cache) {
1187+
if let Some(did) = target.def_id(cache) {
1188+
if let Some(type_did) = impl_.inner_impl().for_.def_id(cache) {
11891189
// `impl Deref<Target = S> for S`
11901190
if did == type_did {
11911191
// Avoid infinite cycles
@@ -1231,7 +1231,7 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) ->
12311231
fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String {
12321232
let mut out = Buffer::html();
12331233

1234-
if let Some(did) = decl.output.def_id_full(cx.cache()) {
1234+
if let Some(did) = decl.output.as_return().and_then(|t| t.def_id(cx.cache())) {
12351235
if let Some(impls) = cx.cache().impls.get(&did) {
12361236
for i in impls {
12371237
let impl_ = i.inner_impl();
@@ -2074,8 +2074,8 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &[
20742074
})
20752075
{
20762076
debug!("found target, real_target: {:?} {:?}", target, real_target);
2077-
if let Some(did) = target.def_id_full(c) {
2078-
if let Some(type_did) = impl_.inner_impl().for_.def_id_full(c) {
2077+
if let Some(did) = target.def_id(c) {
2078+
if let Some(type_did) = impl_.inner_impl().for_.def_id(c) {
20792079
// `impl Deref<Target = S> for S`
20802080
if did == type_did {
20812081
// Avoid infinite cycles
@@ -2085,7 +2085,7 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &[
20852085
}
20862086
let deref_mut = v.iter().any(|i| i.trait_did() == cx.tcx().lang_items().deref_mut_trait());
20872087
let inner_impl = target
2088-
.def_id_full(c)
2088+
.def_id(c)
20892089
.or_else(|| {
20902090
target.primitive_type().and_then(|prim| c.primitive_locations.get(&prim).cloned())
20912091
})
@@ -2246,10 +2246,7 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean
22462246
let mut res = implementors
22472247
.iter()
22482248
.filter(|i| {
2249-
i.inner_impl()
2250-
.for_
2251-
.def_id_full(cache)
2252-
.map_or(false, |d| !cache.paths.contains_key(&d))
2249+
i.inner_impl().for_.def_id(cache).map_or(false, |d| !cache.paths.contains_key(&d))
22532250
})
22542251
.filter_map(|i| extract_for_impl_name(&i.impl_item, cx))
22552252
.collect::<Vec<_>>();

src/librustdoc/html/render/print_item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::{
2121
render_impl, render_stability_since_raw, write_srclink, AssocItemLink, Context,
2222
ImplRenderingParameters,
2323
};
24-
use crate::clean::{self, GetDefId};
24+
use crate::clean;
2525
use crate::formats::item_type::ItemType;
2626
use crate::formats::{AssocItemRender, Impl, RenderMode};
2727
use crate::html::escape::Escape;
@@ -742,7 +742,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
742742
}
743743

744744
let (local, foreign) = implementors.iter().partition::<Vec<_>, _>(|i| {
745-
i.inner_impl().for_.def_id_full(cache).map_or(true, |d| cache.paths.contains_key(&d))
745+
i.inner_impl().for_.def_id(cache).map_or(true, |d| cache.paths.contains_key(&d))
746746
});
747747

748748
let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) =

src/librustdoc/passes/collect_trait_impls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
7070

7171
if let Some(prim) = target.primitive_type() {
7272
cleaner.prims.insert(prim);
73-
} else if let Some(did) = target.def_id() {
73+
} else if let Some(did) = target.def_id(&cx.cache) {
7474
cleaner.items.insert(did.into());
7575
}
7676
}
@@ -187,7 +187,7 @@ impl BadImplStripper {
187187
true
188188
} else if let Some(prim) = ty.primitive_type() {
189189
self.prims.contains(&prim)
190-
} else if let Some(did) = ty.def_id() {
190+
} else if let Some(did) = ty.def_id_no_primitives() {
191191
self.keep_impl_with_def_id(did.into())
192192
} else {
193193
false

0 commit comments

Comments
 (0)