Skip to content

Commit 7b7023c

Browse files
committed
rustdoc: Refactor Impl.{synthetic,blanket_impl} into enum
This change has two advantages: 1. It makes the possible states clearer, and it makes it impossible to construct invalid states, such as a blanket impl that is also an auto trait impl. 2. It shrinks the size of `Impl` a bit, since now there is only one field, rather than two.
1 parent c32ee54 commit 7b7023c

File tree

12 files changed

+66
-36
lines changed

12 files changed

+66
-36
lines changed

src/librustdoc/clean/auto_trait.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
121121
for_: ty.clean(self.cx),
122122
items: Vec::new(),
123123
negative_polarity,
124-
synthetic: true,
125-
blanket_impl: None,
124+
kind: ImplKind::Auto,
126125
}),
127126
cfg: None,
128127
})

src/librustdoc/clean/blanket_impl.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
124124
.collect::<Vec<_>>()
125125
.clean(self.cx),
126126
negative_polarity: false,
127-
synthetic: false,
128-
blanket_impl: Some(box trait_ref.self_ty().clean(self.cx)),
127+
kind: ImplKind::Blanket(box trait_ref.self_ty().clean(self.cx)),
129128
}),
130129
cfg: None,
131130
});

src/librustdoc/clean/inline.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ 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::{self, utils, Attributes, AttributesExt, ItemId, NestedAttributesExt, Type};
17+
use crate::clean::{
18+
self, utils, Attributes, AttributesExt, ImplKind, ItemId, NestedAttributesExt, Type,
19+
};
1820
use crate::core::DocContext;
1921
use crate::formats::item_type::ItemType;
2022

@@ -496,8 +498,7 @@ crate fn build_impl(
496498
for_,
497499
items: trait_items,
498500
negative_polarity: polarity.clean(cx),
499-
synthetic: false,
500-
blanket_impl: None,
501+
kind: ImplKind::Normal,
501502
}),
502503
box merged_attrs,
503504
cx,

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,8 +1895,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>
18951895
for_,
18961896
items,
18971897
negative_polarity: tcx.impl_polarity(def_id).clean(cx),
1898-
synthetic: false,
1899-
blanket_impl: None,
1898+
kind: ImplKind::Normal,
19001899
});
19011900
Item::from_hir_id_and_parts(hir_id, None, kind, cx)
19021901
};

src/librustdoc/clean/types.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ impl Item {
393393
};
394394
match kind {
395395
ItemKind::ModuleItem(Module { span, .. }) => *span,
396-
ItemKind::ImplItem(Impl { synthetic: true, .. }) => Span::dummy(),
397-
ItemKind::ImplItem(Impl { blanket_impl: Some(_), .. }) => {
396+
ItemKind::ImplItem(Impl { kind: ImplKind::Auto, .. }) => Span::dummy(),
397+
ItemKind::ImplItem(Impl { kind: ImplKind::Blanket(_), .. }) => {
398398
if let ItemId::Blanket { impl_id, .. } = self.def_id {
399399
rustc_span(impl_id, tcx)
400400
} else {
@@ -2178,11 +2178,22 @@ crate struct Impl {
21782178
crate for_: Type,
21792179
crate items: Vec<Item>,
21802180
crate negative_polarity: bool,
2181-
crate synthetic: bool,
2182-
crate blanket_impl: Option<Box<Type>>,
2181+
crate kind: ImplKind,
21832182
}
21842183

21852184
impl Impl {
2185+
crate fn is_auto_impl(&self) -> bool {
2186+
self.kind.is_auto()
2187+
}
2188+
2189+
crate fn is_blanket_impl(&self) -> bool {
2190+
self.kind.is_blanket()
2191+
}
2192+
2193+
crate fn blanket_impl_ty(&self) -> Option<&Type> {
2194+
self.kind.as_blanket_ty()
2195+
}
2196+
21862197
crate fn provided_trait_methods(&self, tcx: TyCtxt<'_>) -> FxHashSet<Symbol> {
21872198
self.trait_
21882199
.as_ref()
@@ -2192,6 +2203,30 @@ impl Impl {
21922203
}
21932204
}
21942205

2206+
#[derive(Clone, Debug)]
2207+
crate enum ImplKind {
2208+
Normal,
2209+
Auto,
2210+
Blanket(Box<Type>),
2211+
}
2212+
2213+
impl ImplKind {
2214+
crate fn is_auto(&self) -> bool {
2215+
matches!(self, ImplKind::Auto)
2216+
}
2217+
2218+
crate fn is_blanket(&self) -> bool {
2219+
matches!(self, ImplKind::Blanket(_))
2220+
}
2221+
2222+
crate fn as_blanket_ty(&self) -> Option<&Type> {
2223+
match self {
2224+
ImplKind::Blanket(ty) => Some(ty),
2225+
_ => None,
2226+
}
2227+
}
2228+
}
2229+
21952230
#[derive(Clone, Debug)]
21962231
crate struct Import {
21972232
crate kind: ImportKind,

src/librustdoc/formats/cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
228228
// Collect all the implementors of traits.
229229
if let clean::ImplItem(ref i) = *item.kind {
230230
if let Some(trait_) = &i.trait_ {
231-
if i.blanket_impl.is_none() {
231+
if !i.is_blanket_impl() {
232232
self.cache
233233
.implementors
234234
.entry(trait_.def_id())

src/librustdoc/html/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ impl clean::Impl {
997997
write!(f, " for ")?;
998998
}
999999

1000-
if let Some(ref ty) = self.blanket_impl {
1000+
if let Some(ref ty) = self.blanket_impl_ty() {
10011001
fmt_type(ty, f, use_absolute, cx)?;
10021002
} else {
10031003
fmt_type(&self.for_, f, use_absolute, cx)?;

src/librustdoc/html/render/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,9 +1147,9 @@ fn render_assoc_items_inner(
11471147
}
11481148

11491149
let (synthetic, concrete): (Vec<&&Impl>, Vec<&&Impl>) =
1150-
traits.iter().partition(|t| t.inner_impl().synthetic);
1150+
traits.iter().partition(|t| t.inner_impl().is_auto_impl());
11511151
let (blanket_impl, concrete): (Vec<&&Impl>, _) =
1152-
concrete.into_iter().partition(|t| t.inner_impl().blanket_impl.is_some());
1152+
concrete.into_iter().partition(|t| t.inner_impl().is_blanket_impl());
11531153

11541154
let mut impls = Buffer::empty_from(w);
11551155
render_impls(cx, &mut impls, &concrete, containing_item);
@@ -2058,10 +2058,9 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
20582058
};
20592059

20602060
let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) =
2061-
v.iter().partition::<Vec<_>, _>(|i| i.inner_impl().synthetic);
2062-
let (blanket_impl, concrete): (Vec<&Impl>, Vec<&Impl>) = concrete
2063-
.into_iter()
2064-
.partition::<Vec<_>, _>(|i| i.inner_impl().blanket_impl.is_some());
2061+
v.iter().partition::<Vec<_>, _>(|i| i.inner_impl().is_auto_impl());
2062+
let (blanket_impl, concrete): (Vec<&Impl>, Vec<&Impl>) =
2063+
concrete.into_iter().partition::<Vec<_>, _>(|i| i.inner_impl().is_blanket_impl());
20652064

20662065
let concrete_format = format_impls(concrete);
20672066
let synthetic_format = format_impls(synthetic);

src/librustdoc/html/render/print_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
746746
});
747747

748748
let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) =
749-
local.iter().partition(|i| i.inner_impl().synthetic);
749+
local.iter().partition(|i| i.inner_impl().is_auto_impl());
750750

751751
synthetic.sort_by(|a, b| compare_impl(a, b, cx));
752752
concrete.sort_by(|a, b| compare_impl(a, b, cx));

src/librustdoc/html/render/write_shared.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ pub(super) fn write_shared(
585585
} else {
586586
Some(Implementor {
587587
text: imp.inner_impl().print(false, cx).to_string(),
588-
synthetic: imp.inner_impl().synthetic,
588+
synthetic: imp.inner_impl().is_auto_impl(),
589589
types: collect_paths_for_type(imp.inner_impl().for_.clone(), cache),
590590
})
591591
}

src/librustdoc/json/conversions.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -500,21 +500,19 @@ impl FromWithTcx<clean::Trait> for Trait {
500500
impl FromWithTcx<clean::Impl> for Impl {
501501
fn from_tcx(impl_: clean::Impl, tcx: TyCtxt<'_>) -> Self {
502502
let provided_trait_methods = impl_.provided_trait_methods(tcx);
503-
let clean::Impl {
504-
unsafety,
505-
generics,
506-
trait_,
507-
for_,
508-
items,
509-
negative_polarity,
510-
synthetic,
511-
blanket_impl,
512-
} = impl_;
503+
let clean::Impl { unsafety, generics, trait_, for_, items, negative_polarity, kind } =
504+
impl_;
513505
// FIXME: should `trait_` be a clean::Path equivalent in JSON?
514506
let trait_ = trait_.map(|path| {
515507
let did = path.def_id();
516508
clean::ResolvedPath { path, did }.into_tcx(tcx)
517509
});
510+
// FIXME: use something like ImplKind in JSON?
511+
let (synthetic, blanket_impl) = match kind {
512+
clean::ImplKind::Normal => (false, None),
513+
clean::ImplKind::Auto => (true, None),
514+
clean::ImplKind::Blanket(ty) => (false, Some(*ty)),
515+
};
518516
Impl {
519517
is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
520518
generics: generics.into_tcx(tcx),
@@ -527,7 +525,7 @@ impl FromWithTcx<clean::Impl> for Impl {
527525
items: ids(items),
528526
negative: negative_polarity,
529527
synthetic,
530-
blanket_impl: blanket_impl.map(|x| (*x).into_tcx(tcx)),
528+
blanket_impl: blanket_impl.map(|x| x.into_tcx(tcx)),
531529
}
532530
}
533531
}

src/librustdoc/passes/collect_trait_impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ crate fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) -> Crate
111111
}
112112

113113
new_items.retain(|it| {
114-
if let ImplItem(Impl { ref for_, ref trait_, ref blanket_impl, .. }) = *it.kind {
114+
if let ImplItem(Impl { ref for_, ref trait_, ref kind, .. }) = *it.kind {
115115
cleaner.keep_impl(
116116
for_,
117117
trait_.as_ref().map(|t| t.def_id()) == cx.tcx.lang_items().deref_trait(),
118118
) || trait_.as_ref().map_or(false, |t| cleaner.keep_impl_with_def_id(t.def_id().into()))
119-
|| blanket_impl.is_some()
119+
|| kind.is_blanket()
120120
} else {
121121
true
122122
}

0 commit comments

Comments
 (0)