Skip to content

Commit 7777f95

Browse files
authored
Rollup merge of #88895 - camelid:cleanup-pt2, r=jyn514
rustdoc: Cleanup `clean` part 2 Split out from #88379. This contains the following commits from that PR: - Remove `Type::ResolvedPath.is_generic` - Rename `is_generic()` to `is_assoc_ty()` r? `@jyn514`
2 parents addb4da + dace2ee commit 7777f95

File tree

8 files changed

+56
-79
lines changed

8 files changed

+56
-79
lines changed

src/librustdoc/clean/auto_trait.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
354354
let (poly_trait, output) =
355355
(data.0.as_ref().unwrap().clone(), data.1.as_ref().cloned().map(Box::new));
356356
let new_ty = match poly_trait.trait_ {
357-
Type::ResolvedPath { ref path, ref did, ref is_generic } => {
357+
Type::ResolvedPath { ref path, ref did } => {
358358
let mut new_path = path.clone();
359359
let last_segment =
360360
new_path.segments.pop().expect("segments were empty");
@@ -389,11 +389,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
389389
.segments
390390
.push(PathSegment { name: last_segment.name, args: new_params });
391391

392-
Type::ResolvedPath {
393-
path: new_path,
394-
did: *did,
395-
is_generic: *is_generic,
396-
}
392+
Type::ResolvedPath { path: new_path, did: *did }
397393
}
398394
_ => panic!("Unexpected data: {:?}, {:?}", ty, data),
399395
};
@@ -563,11 +559,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
563559
Type::QPath { name: left_name, ref self_type, ref trait_, .. } => {
564560
let ty = &*self_type;
565561
match **trait_ {
566-
Type::ResolvedPath {
567-
path: ref trait_path,
568-
ref did,
569-
ref is_generic,
570-
} => {
562+
Type::ResolvedPath { path: ref trait_path, ref did } => {
571563
let mut new_trait_path = trait_path.clone();
572564

573565
if self.is_fn_ty(trait_) && left_name == sym::Output {
@@ -612,7 +604,6 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
612604
trait_: Type::ResolvedPath {
613605
path: new_trait_path,
614606
did: *did,
615-
is_generic: *is_generic,
616607
},
617608
generic_params: Vec::new(),
618609
},

src/librustdoc/clean/mod.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl Clean<Type> for (ty::TraitRef<'_>, &[TypeBinding]) {
168168

169169
debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs);
170170

171-
ResolvedPath { path, did: trait_ref.def_id, is_generic: false }
171+
ResolvedPath { path, did: trait_ref.def_id }
172172
}
173173
}
174174

@@ -1440,12 +1440,12 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
14401440
};
14411441
inline::record_extern_fqn(cx, did, kind);
14421442
let path = external_path(cx, did, false, vec![], substs);
1443-
ResolvedPath { path, did, is_generic: false }
1443+
ResolvedPath { path, did }
14441444
}
14451445
ty::Foreign(did) => {
14461446
inline::record_extern_fqn(cx, did, ItemType::ForeignType);
14471447
let path = external_path(cx, did, false, vec![], InternalSubsts::empty());
1448-
ResolvedPath { path, did, is_generic: false }
1448+
ResolvedPath { path, did }
14491449
}
14501450
ty::Dynamic(ref obj, ref reg) => {
14511451
// HACK: pick the first `did` as the `did` of the trait object. Someone
@@ -1471,7 +1471,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
14711471
let path = external_path(cx, did, false, vec![], empty);
14721472
inline::record_extern_fqn(cx, did, ItemType::Trait);
14731473
let bound = PolyTrait {
1474-
trait_: ResolvedPath { path, did, is_generic: false },
1474+
trait_: ResolvedPath { path, did },
14751475
generic_params: Vec::new(),
14761476
};
14771477
bounds.push(bound);
@@ -1488,10 +1488,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
14881488
let path = external_path(cx, did, false, bindings, substs);
14891489
bounds.insert(
14901490
0,
1491-
PolyTrait {
1492-
trait_: ResolvedPath { path, did, is_generic: false },
1493-
generic_params: Vec::new(),
1494-
},
1491+
PolyTrait { trait_: ResolvedPath { path, did }, generic_params: Vec::new() },
14951492
);
14961493

14971494
DynTrait(bounds, lifetime)

src/librustdoc/clean/types.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -1114,10 +1114,7 @@ impl GenericBound {
11141114
let path = external_path(cx, did, false, vec![], empty);
11151115
inline::record_extern_fqn(cx, did, ItemType::Trait);
11161116
GenericBound::TraitBound(
1117-
PolyTrait {
1118-
trait_: ResolvedPath { path, did, is_generic: false },
1119-
generic_params: Vec::new(),
1120-
},
1117+
PolyTrait { trait_: ResolvedPath { path, did }, generic_params: Vec::new() },
11211118
hir::TraitBoundModifier::Maybe,
11221119
)
11231120
}
@@ -1384,8 +1381,6 @@ crate enum Type {
13841381
ResolvedPath {
13851382
path: Path,
13861383
did: DefId,
1387-
/// `true` if is a `T::Name` path for associated types.
1388-
is_generic: bool,
13891384
},
13901385
/// `dyn for<'a> Trait<'a> + Send + 'static`
13911386
DynTrait(Vec<PolyTrait>, Option<Lifetime>),
@@ -1503,9 +1498,10 @@ impl Type {
15031498
}
15041499
}
15051500

1506-
crate fn is_generic(&self) -> bool {
1507-
match *self {
1508-
ResolvedPath { is_generic, .. } => is_generic,
1501+
/// Checks if this is a `T::Name` path for an associated type.
1502+
crate fn is_assoc_ty(&self) -> bool {
1503+
match self {
1504+
ResolvedPath { path, .. } => path.is_assoc_ty(),
15091505
_ => false,
15101506
}
15111507
}
@@ -1994,6 +1990,16 @@ impl Path {
19941990
String::from(if self.global { "::" } else { "" })
19951991
+ &self.segments.iter().map(|s| s.name.to_string()).collect::<Vec<_>>().join("::")
19961992
}
1993+
1994+
/// Checks if this is a `T::Name` path for an associated type.
1995+
crate fn is_assoc_ty(&self) -> bool {
1996+
match self.res {
1997+
Res::SelfTy(..) if self.segments.len() != 1 => true,
1998+
Res::Def(DefKind::TyParam, _) if self.segments.len() != 1 => true,
1999+
Res::Def(DefKind::AssocTy, _) => true,
2000+
_ => false,
2001+
}
2002+
}
19972003
}
19982004

19992005
#[derive(Clone, PartialEq, Eq, Debug, Hash)]

src/librustdoc/clean/utils.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ pub(super) fn external_path(
159159

160160
crate fn strip_type(ty: Type) -> Type {
161161
match ty {
162-
Type::ResolvedPath { path, did, is_generic } => {
163-
Type::ResolvedPath { path: strip_path(&path), did, is_generic }
164-
}
162+
Type::ResolvedPath { path, did } => Type::ResolvedPath { path: strip_path(&path), did },
165163
Type::DynTrait(mut bounds, lt) => {
166164
let first = bounds.remove(0);
167165
let stripped_trait = strip_type(first.trait_);
@@ -404,19 +402,15 @@ crate fn print_const_expr(tcx: TyCtxt<'_>, body: hir::BodyId) -> String {
404402
crate fn resolve_type(cx: &mut DocContext<'_>, path: Path) -> Type {
405403
debug!("resolve_type({:?})", path);
406404

407-
let is_generic = match path.res {
408-
Res::PrimTy(p) => return Primitive(PrimitiveType::from(p)),
409-
Res::SelfTy(..) if path.segments.len() == 1 => {
410-
return Generic(kw::SelfUpper);
411-
}
412-
Res::Def(DefKind::TyParam, _) if path.segments.len() == 1 => {
413-
return Generic(path.segments[0].name);
405+
match path.res {
406+
Res::PrimTy(p) => Primitive(PrimitiveType::from(p)),
407+
Res::SelfTy(..) if path.segments.len() == 1 => Generic(kw::SelfUpper),
408+
Res::Def(DefKind::TyParam, _) if path.segments.len() == 1 => Generic(path.segments[0].name),
409+
_ => {
410+
let did = register_res(cx, path.res);
411+
ResolvedPath { path, did }
414412
}
415-
Res::SelfTy(..) | Res::Def(DefKind::TyParam | DefKind::AssocTy, _) => true,
416-
_ => false,
417-
};
418-
let did = register_res(cx, path.res);
419-
ResolvedPath { path, did, is_generic }
413+
}
420414
}
421415

422416
crate fn get_auto_trait_and_blanket_impls(

src/librustdoc/html/format.rs

+13-24
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,9 @@ fn fmt_type<'cx>(
752752

753753
match *t {
754754
clean::Generic(name) => write!(f, "{}", name),
755-
clean::ResolvedPath { did, ref path, is_generic } => {
755+
clean::ResolvedPath { did, ref path } => {
756756
// Paths like `T::Output` and `Self::Output` should be rendered with all segments.
757-
resolved_path(f, did, path, is_generic, use_absolute, cx)
757+
resolved_path(f, did, path, path.is_assoc_ty(), use_absolute, cx)
758758
}
759759
clean::DynTrait(ref bounds, ref lt) => {
760760
f.write_str("dyn ")?;
@@ -825,28 +825,17 @@ fn fmt_type<'cx>(
825825
hir::Mutability::Mut => "mut",
826826
hir::Mutability::Not => "const",
827827
};
828-
match **t {
829-
clean::Generic(_) | clean::ResolvedPath { is_generic: true, .. } => {
830-
if f.alternate() {
831-
primitive_link(
832-
f,
833-
clean::PrimitiveType::RawPointer,
834-
&format!("*{} {:#}", m, t.print(cx)),
835-
cx,
836-
)
837-
} else {
838-
primitive_link(
839-
f,
840-
clean::PrimitiveType::RawPointer,
841-
&format!("*{} {}", m, t.print(cx)),
842-
cx,
843-
)
844-
}
845-
}
846-
_ => {
847-
primitive_link(f, clean::PrimitiveType::RawPointer, &format!("*{} ", m), cx)?;
848-
fmt::Display::fmt(&t.print(cx), f)
849-
}
828+
829+
if matches!(**t, clean::Generic(_)) || t.is_assoc_ty() {
830+
let text = if f.alternate() {
831+
format!("*{} {:#}", m, t.print(cx))
832+
} else {
833+
format!("*{} {}", m, t.print(cx))
834+
};
835+
primitive_link(f, clean::PrimitiveType::RawPointer, &text, cx)
836+
} else {
837+
primitive_link(f, clean::PrimitiveType::RawPointer, &format!("*{} ", m), cx)?;
838+
fmt::Display::fmt(&t.print(cx), f)
850839
}
851840
}
852841
clean::BorrowedRef { lifetime: ref l, mutability, type_: ref ty } => {

src/librustdoc/html/render/print_item.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -712,11 +712,10 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
712712
let mut implementor_dups: FxHashMap<Symbol, (DefId, bool)> = FxHashMap::default();
713713
for implementor in implementors {
714714
match implementor.inner_impl().for_ {
715-
clean::ResolvedPath { ref path, did, is_generic: false, .. }
715+
clean::ResolvedPath { ref path, did, .. }
716716
| clean::BorrowedRef {
717-
type_: box clean::ResolvedPath { ref path, did, is_generic: false, .. },
718-
..
719-
} => {
717+
type_: box clean::ResolvedPath { ref path, did, .. }, ..
718+
} if !path.is_assoc_ty() => {
720719
let &mut (prev_did, ref mut has_duplicates) =
721720
implementor_dups.entry(path.last()).or_insert((did, false));
722721
if prev_did != did {
@@ -1410,11 +1409,12 @@ fn render_implementor(
14101409
// If there's already another implementor that has the same abridged name, use the
14111410
// full path, for example in `std::iter::ExactSizeIterator`
14121411
let use_absolute = match implementor.inner_impl().for_ {
1413-
clean::ResolvedPath { ref path, is_generic: false, .. }
1414-
| clean::BorrowedRef {
1415-
type_: box clean::ResolvedPath { ref path, is_generic: false, .. },
1416-
..
1417-
} => implementor_dups[&path.last()].1,
1412+
clean::ResolvedPath { ref path, .. }
1413+
| clean::BorrowedRef { type_: box clean::ResolvedPath { ref path, .. }, .. }
1414+
if !path.is_assoc_ty() =>
1415+
{
1416+
implementor_dups[&path.last()].1
1417+
}
14181418
_ => false,
14191419
};
14201420
render_impl(

src/librustdoc/json/conversions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ impl FromWithTcx<clean::Type> for Type {
387387
fn from_tcx(ty: clean::Type, tcx: TyCtxt<'_>) -> Self {
388388
use clean::Type::*;
389389
match ty {
390-
ResolvedPath { path, did, is_generic: _ } => Type::ResolvedPath {
390+
ResolvedPath { path, did } => Type::ResolvedPath {
391391
name: path.whole_name(),
392392
id: from_item_id(did.into()),
393393
args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))),

src/librustdoc/passes/stripper.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl<'a> DocFolder for ImplStripper<'a> {
128128
return None;
129129
}
130130
if let Some(did) = imp.for_.def_id() {
131-
if did.is_local() && !imp.for_.is_generic() && !self.retained.contains(&did.into())
131+
if did.is_local() && !imp.for_.is_assoc_ty() && !self.retained.contains(&did.into())
132132
{
133133
debug!("ImplStripper: impl item for stripped type; removing");
134134
return None;

0 commit comments

Comments
 (0)