1
Fork 0

Use an enum to record polarity in clean::Impl

This commit is contained in:
Noah Lev 2021-11-07 08:52:28 -08:00
parent 7b7023cb72
commit 543aea6c03
8 changed files with 34 additions and 23 deletions

View file

@ -76,17 +76,17 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
new_generics new_generics
}); });
let negative_polarity; let polarity;
let new_generics = match result { let new_generics = match result {
AutoTraitResult::PositiveImpl(new_generics) => { AutoTraitResult::PositiveImpl(new_generics) => {
negative_polarity = false; polarity = ImplPolarity::Positive;
if discard_positive_impl { if discard_positive_impl {
return None; return None;
} }
new_generics new_generics
} }
AutoTraitResult::NegativeImpl => { AutoTraitResult::NegativeImpl => {
negative_polarity = true; polarity = ImplPolarity::Negative;
// For negative impls, we use the generic params, but *not* the predicates, // For negative impls, we use the generic params, but *not* the predicates,
// from the original type. Otherwise, the displayed impl appears to be a // from the original type. Otherwise, the displayed impl appears to be a
@ -120,7 +120,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
trait_: Some(trait_ref.clean(self.cx)), trait_: Some(trait_ref.clean(self.cx)),
for_: ty.clean(self.cx), for_: ty.clean(self.cx),
items: Vec::new(), items: Vec::new(),
negative_polarity, polarity,
kind: ImplKind::Auto, kind: ImplKind::Auto,
}), }),
cfg: None, cfg: None,

View file

@ -123,7 +123,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
.in_definition_order() .in_definition_order()
.collect::<Vec<_>>() .collect::<Vec<_>>()
.clean(self.cx), .clean(self.cx),
negative_polarity: false, polarity: ImplPolarity::Positive,
kind: ImplKind::Blanket(box trait_ref.self_ty().clean(self.cx)), kind: ImplKind::Blanket(box trait_ref.self_ty().clean(self.cx)),
}), }),
cfg: None, cfg: None,

View file

@ -497,7 +497,7 @@ crate fn build_impl(
trait_, trait_,
for_, for_,
items: trait_items, items: trait_items,
negative_polarity: polarity.clean(cx), polarity: polarity.clean(cx),
kind: ImplKind::Normal, kind: ImplKind::Normal,
}), }),
box merged_attrs, box merged_attrs,

View file

@ -1856,14 +1856,14 @@ impl Clean<Item> for hir::Variant<'_> {
} }
} }
impl Clean<bool> for ty::ImplPolarity { impl Clean<ImplPolarity> for ty::ImplPolarity {
/// Returns whether the impl has negative polarity. /// Returns whether the impl has negative polarity.
fn clean(&self, _: &mut DocContext<'_>) -> bool { fn clean(&self, _: &mut DocContext<'_>) -> ImplPolarity {
match self { match self {
&ty::ImplPolarity::Positive | ty::ImplPolarity::Positive |
// FIXME: do we want to do something else here? // FIXME: do we want to do something else here?
&ty::ImplPolarity::Reservation => false, ty::ImplPolarity::Reservation => ImplPolarity::Positive,
&ty::ImplPolarity::Negative => true, ty::ImplPolarity::Negative => ImplPolarity::Negative,
} }
} }
} }
@ -1894,7 +1894,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>
trait_, trait_,
for_, for_,
items, items,
negative_polarity: tcx.impl_polarity(def_id).clean(cx), polarity: tcx.impl_polarity(def_id).clean(cx),
kind: ImplKind::Normal, kind: ImplKind::Normal,
}); });
Item::from_hir_id_and_parts(hir_id, None, kind, cx) Item::from_hir_id_and_parts(hir_id, None, kind, cx)

View file

@ -2177,7 +2177,7 @@ crate struct Impl {
crate trait_: Option<Path>, crate trait_: Option<Path>,
crate for_: Type, crate for_: Type,
crate items: Vec<Item>, crate items: Vec<Item>,
crate negative_polarity: bool, crate polarity: ImplPolarity,
crate kind: ImplKind, crate kind: ImplKind,
} }
@ -2227,6 +2227,13 @@ impl ImplKind {
} }
} }
// FIXME: remove this and use ty::ImplPolarity instead
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
crate enum ImplPolarity {
Positive,
Negative,
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
crate struct Import { crate struct Import {
crate kind: ImportKind, crate kind: ImportKind,

View file

@ -990,8 +990,9 @@ impl clean::Impl {
} }
if let Some(ref ty) = self.trait_ { if let Some(ref ty) = self.trait_ {
if self.negative_polarity { match self.polarity {
write!(f, "!")?; clean::ImplPolarity::Positive => {}
clean::ImplPolarity::Negative => write!(f, "!")?,
} }
fmt::Display::fmt(&ty.print(cx), f)?; fmt::Display::fmt(&ty.print(cx), f)?;
write!(f, " for ")?; write!(f, " for ")?;

View file

@ -2033,12 +2033,12 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
let i_display = format!("{:#}", i.print(cx)); let i_display = format!("{:#}", i.print(cx));
let out = Escape(&i_display); let out = Escape(&i_display);
let encoded = small_url_encode(format!("{:#}", i.print(cx))); let encoded = small_url_encode(format!("{:#}", i.print(cx)));
let generated = format!( let prefix = match it.inner_impl().polarity {
"<a href=\"#impl-{}\">{}{}</a>", clean::ImplPolarity::Positive => "",
encoded, clean::ImplPolarity::Negative => "!",
if it.inner_impl().negative_polarity { "!" } else { "" }, };
out let generated =
); format!("<a href=\"#impl-{}\">{}{}</a>", encoded, prefix, out);
if links.insert(generated.clone()) { Some(generated) } else { None } if links.insert(generated.clone()) { Some(generated) } else { None }
} else { } else {
None None

View file

@ -500,8 +500,7 @@ 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 { unsafety, generics, trait_, for_, items, negative_polarity, kind } = let clean::Impl { unsafety, generics, trait_, for_, items, polarity, kind } = 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();
@ -513,6 +512,10 @@ impl FromWithTcx<clean::Impl> for Impl {
clean::ImplKind::Auto => (true, None), clean::ImplKind::Auto => (true, None),
clean::ImplKind::Blanket(ty) => (false, Some(*ty)), clean::ImplKind::Blanket(ty) => (false, Some(*ty)),
}; };
let negative_polarity = match polarity {
clean::ImplPolarity::Positive => false,
clean::ImplPolarity::Negative => true,
};
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),