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.
This commit is contained in:
Noah Lev 2021-11-06 23:10:01 -07:00
parent c32ee54380
commit 7b7023cb72
12 changed files with 66 additions and 36 deletions

View File

@ -121,8 +121,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
for_: ty.clean(self.cx), for_: ty.clean(self.cx),
items: Vec::new(), items: Vec::new(),
negative_polarity, negative_polarity,
synthetic: true, kind: ImplKind::Auto,
blanket_impl: None,
}), }),
cfg: None, cfg: None,
}) })

View File

@ -124,8 +124,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
.collect::<Vec<_>>() .collect::<Vec<_>>()
.clean(self.cx), .clean(self.cx),
negative_polarity: false, negative_polarity: false,
synthetic: false, kind: ImplKind::Blanket(box trait_ref.self_ty().clean(self.cx)),
blanket_impl: Some(box trait_ref.self_ty().clean(self.cx)),
}), }),
cfg: None, cfg: None,
}); });

View File

@ -14,7 +14,9 @@ use rustc_middle::ty::{self, TyCtxt};
use rustc_span::hygiene::MacroKind; use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::symbol::{kw, sym, Symbol};
use crate::clean::{self, utils, Attributes, AttributesExt, ItemId, NestedAttributesExt, Type}; use crate::clean::{
self, utils, Attributes, AttributesExt, ImplKind, ItemId, NestedAttributesExt, Type,
};
use crate::core::DocContext; use crate::core::DocContext;
use crate::formats::item_type::ItemType; use crate::formats::item_type::ItemType;
@ -496,8 +498,7 @@ crate fn build_impl(
for_, for_,
items: trait_items, items: trait_items,
negative_polarity: polarity.clean(cx), negative_polarity: polarity.clean(cx),
synthetic: false, kind: ImplKind::Normal,
blanket_impl: None,
}), }),
box merged_attrs, box merged_attrs,
cx, cx,

View File

@ -1895,8 +1895,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>
for_, for_,
items, items,
negative_polarity: tcx.impl_polarity(def_id).clean(cx), negative_polarity: tcx.impl_polarity(def_id).clean(cx),
synthetic: false, kind: ImplKind::Normal,
blanket_impl: None,
}); });
Item::from_hir_id_and_parts(hir_id, None, kind, cx) Item::from_hir_id_and_parts(hir_id, None, kind, cx)
}; };

View File

@ -393,8 +393,8 @@ impl Item {
}; };
match kind { match kind {
ItemKind::ModuleItem(Module { span, .. }) => *span, ItemKind::ModuleItem(Module { span, .. }) => *span,
ItemKind::ImplItem(Impl { synthetic: true, .. }) => Span::dummy(), ItemKind::ImplItem(Impl { kind: ImplKind::Auto, .. }) => Span::dummy(),
ItemKind::ImplItem(Impl { blanket_impl: Some(_), .. }) => { ItemKind::ImplItem(Impl { kind: ImplKind::Blanket(_), .. }) => {
if let ItemId::Blanket { impl_id, .. } = self.def_id { if let ItemId::Blanket { impl_id, .. } = self.def_id {
rustc_span(impl_id, tcx) rustc_span(impl_id, tcx)
} else { } else {
@ -2178,11 +2178,22 @@ crate struct Impl {
crate for_: Type, crate for_: Type,
crate items: Vec<Item>, crate items: Vec<Item>,
crate negative_polarity: bool, crate negative_polarity: bool,
crate synthetic: bool, crate kind: ImplKind,
crate blanket_impl: Option<Box<Type>>,
} }
impl Impl { impl Impl {
crate fn is_auto_impl(&self) -> bool {
self.kind.is_auto()
}
crate fn is_blanket_impl(&self) -> bool {
self.kind.is_blanket()
}
crate fn blanket_impl_ty(&self) -> Option<&Type> {
self.kind.as_blanket_ty()
}
crate fn provided_trait_methods(&self, tcx: TyCtxt<'_>) -> FxHashSet<Symbol> { crate fn provided_trait_methods(&self, tcx: TyCtxt<'_>) -> FxHashSet<Symbol> {
self.trait_ self.trait_
.as_ref() .as_ref()
@ -2192,6 +2203,30 @@ impl Impl {
} }
} }
#[derive(Clone, Debug)]
crate enum ImplKind {
Normal,
Auto,
Blanket(Box<Type>),
}
impl ImplKind {
crate fn is_auto(&self) -> bool {
matches!(self, ImplKind::Auto)
}
crate fn is_blanket(&self) -> bool {
matches!(self, ImplKind::Blanket(_))
}
crate fn as_blanket_ty(&self) -> Option<&Type> {
match self {
ImplKind::Blanket(ty) => Some(ty),
_ => None,
}
}
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
crate struct Import { crate struct Import {
crate kind: ImportKind, crate kind: ImportKind,

View File

@ -228,7 +228,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
// Collect all the implementors of traits. // Collect all the implementors of traits.
if let clean::ImplItem(ref i) = *item.kind { if let clean::ImplItem(ref i) = *item.kind {
if let Some(trait_) = &i.trait_ { if let Some(trait_) = &i.trait_ {
if i.blanket_impl.is_none() { if !i.is_blanket_impl() {
self.cache self.cache
.implementors .implementors
.entry(trait_.def_id()) .entry(trait_.def_id())

View File

@ -997,7 +997,7 @@ impl clean::Impl {
write!(f, " for ")?; write!(f, " for ")?;
} }
if let Some(ref ty) = self.blanket_impl { if let Some(ref ty) = self.blanket_impl_ty() {
fmt_type(ty, f, use_absolute, cx)?; fmt_type(ty, f, use_absolute, cx)?;
} else { } else {
fmt_type(&self.for_, f, use_absolute, cx)?; fmt_type(&self.for_, f, use_absolute, cx)?;

View File

@ -1147,9 +1147,9 @@ fn render_assoc_items_inner(
} }
let (synthetic, concrete): (Vec<&&Impl>, Vec<&&Impl>) = let (synthetic, concrete): (Vec<&&Impl>, Vec<&&Impl>) =
traits.iter().partition(|t| t.inner_impl().synthetic); traits.iter().partition(|t| t.inner_impl().is_auto_impl());
let (blanket_impl, concrete): (Vec<&&Impl>, _) = let (blanket_impl, concrete): (Vec<&&Impl>, _) =
concrete.into_iter().partition(|t| t.inner_impl().blanket_impl.is_some()); concrete.into_iter().partition(|t| t.inner_impl().is_blanket_impl());
let mut impls = Buffer::empty_from(w); let mut impls = Buffer::empty_from(w);
render_impls(cx, &mut impls, &concrete, containing_item); render_impls(cx, &mut impls, &concrete, containing_item);
@ -2058,10 +2058,9 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
}; };
let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) = let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) =
v.iter().partition::<Vec<_>, _>(|i| i.inner_impl().synthetic); v.iter().partition::<Vec<_>, _>(|i| i.inner_impl().is_auto_impl());
let (blanket_impl, concrete): (Vec<&Impl>, Vec<&Impl>) = concrete let (blanket_impl, concrete): (Vec<&Impl>, Vec<&Impl>) =
.into_iter() concrete.into_iter().partition::<Vec<_>, _>(|i| i.inner_impl().is_blanket_impl());
.partition::<Vec<_>, _>(|i| i.inner_impl().blanket_impl.is_some());
let concrete_format = format_impls(concrete); let concrete_format = format_impls(concrete);
let synthetic_format = format_impls(synthetic); let synthetic_format = format_impls(synthetic);

View File

@ -746,7 +746,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
}); });
let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) = let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) =
local.iter().partition(|i| i.inner_impl().synthetic); local.iter().partition(|i| i.inner_impl().is_auto_impl());
synthetic.sort_by(|a, b| compare_impl(a, b, cx)); synthetic.sort_by(|a, b| compare_impl(a, b, cx));
concrete.sort_by(|a, b| compare_impl(a, b, cx)); concrete.sort_by(|a, b| compare_impl(a, b, cx));

View File

@ -585,7 +585,7 @@ pub(super) fn write_shared(
} else { } else {
Some(Implementor { Some(Implementor {
text: imp.inner_impl().print(false, cx).to_string(), text: imp.inner_impl().print(false, cx).to_string(),
synthetic: imp.inner_impl().synthetic, synthetic: imp.inner_impl().is_auto_impl(),
types: collect_paths_for_type(imp.inner_impl().for_.clone(), cache), types: collect_paths_for_type(imp.inner_impl().for_.clone(), cache),
}) })
} }

View File

@ -500,21 +500,19 @@ impl FromWithTcx<clean::Trait> for Trait {
impl FromWithTcx<clean::Impl> for Impl { impl FromWithTcx<clean::Impl> for Impl {
fn from_tcx(impl_: clean::Impl, tcx: TyCtxt<'_>) -> Self { fn from_tcx(impl_: clean::Impl, tcx: TyCtxt<'_>) -> Self {
let provided_trait_methods = impl_.provided_trait_methods(tcx); let provided_trait_methods = impl_.provided_trait_methods(tcx);
let clean::Impl { let clean::Impl { unsafety, generics, trait_, for_, items, negative_polarity, kind } =
unsafety, impl_;
generics,
trait_,
for_,
items,
negative_polarity,
synthetic,
blanket_impl,
} = impl_;
// FIXME: should `trait_` be a clean::Path equivalent in JSON? // FIXME: should `trait_` be a clean::Path equivalent in JSON?
let trait_ = trait_.map(|path| { let trait_ = trait_.map(|path| {
let did = path.def_id(); let did = path.def_id();
clean::ResolvedPath { path, did }.into_tcx(tcx) clean::ResolvedPath { path, did }.into_tcx(tcx)
}); });
// FIXME: use something like ImplKind in JSON?
let (synthetic, blanket_impl) = match kind {
clean::ImplKind::Normal => (false, None),
clean::ImplKind::Auto => (true, None),
clean::ImplKind::Blanket(ty) => (false, Some(*ty)),
};
Impl { Impl {
is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe, is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
generics: generics.into_tcx(tcx), generics: generics.into_tcx(tcx),
@ -527,7 +525,7 @@ impl FromWithTcx<clean::Impl> for Impl {
items: ids(items), items: ids(items),
negative: negative_polarity, negative: negative_polarity,
synthetic, synthetic,
blanket_impl: blanket_impl.map(|x| (*x).into_tcx(tcx)), blanket_impl: blanket_impl.map(|x| x.into_tcx(tcx)),
} }
} }
} }

View File

@ -111,12 +111,12 @@ crate fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) -> Crate
} }
new_items.retain(|it| { new_items.retain(|it| {
if let ImplItem(Impl { ref for_, ref trait_, ref blanket_impl, .. }) = *it.kind { if let ImplItem(Impl { ref for_, ref trait_, ref kind, .. }) = *it.kind {
cleaner.keep_impl( cleaner.keep_impl(
for_, for_,
trait_.as_ref().map(|t| t.def_id()) == cx.tcx.lang_items().deref_trait(), trait_.as_ref().map(|t| t.def_id()) == cx.tcx.lang_items().deref_trait(),
) || trait_.as_ref().map_or(false, |t| cleaner.keep_impl_with_def_id(t.def_id().into())) ) || trait_.as_ref().map_or(false, |t| cleaner.keep_impl_with_def_id(t.def_id().into()))
|| blanket_impl.is_some() || kind.is_blanket()
} else { } else {
true true
} }