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
});
let negative_polarity;
let polarity;
let new_generics = match result {
AutoTraitResult::PositiveImpl(new_generics) => {
negative_polarity = false;
polarity = ImplPolarity::Positive;
if discard_positive_impl {
return None;
}
new_generics
}
AutoTraitResult::NegativeImpl => {
negative_polarity = true;
polarity = ImplPolarity::Negative;
// For negative impls, we use the generic params, but *not* the predicates,
// 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)),
for_: ty.clean(self.cx),
items: Vec::new(),
negative_polarity,
polarity,
kind: ImplKind::Auto,
}),
cfg: None,

View File

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

View File

@ -497,7 +497,7 @@ crate fn build_impl(
trait_,
for_,
items: trait_items,
negative_polarity: polarity.clean(cx),
polarity: polarity.clean(cx),
kind: ImplKind::Normal,
}),
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.
fn clean(&self, _: &mut DocContext<'_>) -> bool {
fn clean(&self, _: &mut DocContext<'_>) -> ImplPolarity {
match self {
&ty::ImplPolarity::Positive |
ty::ImplPolarity::Positive |
// FIXME: do we want to do something else here?
&ty::ImplPolarity::Reservation => false,
&ty::ImplPolarity::Negative => true,
ty::ImplPolarity::Reservation => ImplPolarity::Positive,
ty::ImplPolarity::Negative => ImplPolarity::Negative,
}
}
}
@ -1894,7 +1894,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>
trait_,
for_,
items,
negative_polarity: tcx.impl_polarity(def_id).clean(cx),
polarity: tcx.impl_polarity(def_id).clean(cx),
kind: ImplKind::Normal,
});
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 for_: Type,
crate items: Vec<Item>,
crate negative_polarity: bool,
crate polarity: ImplPolarity,
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)]
crate struct Import {
crate kind: ImportKind,

View File

@ -990,8 +990,9 @@ impl clean::Impl {
}
if let Some(ref ty) = self.trait_ {
if self.negative_polarity {
write!(f, "!")?;
match self.polarity {
clean::ImplPolarity::Positive => {}
clean::ImplPolarity::Negative => write!(f, "!")?,
}
fmt::Display::fmt(&ty.print(cx), f)?;
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 out = Escape(&i_display);
let encoded = small_url_encode(format!("{:#}", i.print(cx)));
let generated = format!(
"<a href=\"#impl-{}\">{}{}</a>",
encoded,
if it.inner_impl().negative_polarity { "!" } else { "" },
out
);
let prefix = match it.inner_impl().polarity {
clean::ImplPolarity::Positive => "",
clean::ImplPolarity::Negative => "!",
};
let generated =
format!("<a href=\"#impl-{}\">{}{}</a>", encoded, prefix, out);
if links.insert(generated.clone()) { Some(generated) } else { None }
} else {
None

View File

@ -500,8 +500,7 @@ impl FromWithTcx<clean::Trait> for Trait {
impl FromWithTcx<clean::Impl> for Impl {
fn from_tcx(impl_: clean::Impl, tcx: TyCtxt<'_>) -> Self {
let provided_trait_methods = impl_.provided_trait_methods(tcx);
let clean::Impl { unsafety, generics, trait_, for_, items, negative_polarity, kind } =
impl_;
let clean::Impl { unsafety, generics, trait_, for_, items, polarity, kind } = impl_;
// FIXME: should `trait_` be a clean::Path equivalent in JSON?
let trait_ = trait_.map(|path| {
let did = path.def_id();
@ -513,6 +512,10 @@ impl FromWithTcx<clean::Impl> for Impl {
clean::ImplKind::Auto => (true, None),
clean::ImplKind::Blanket(ty) => (false, Some(*ty)),
};
let negative_polarity = match polarity {
clean::ImplPolarity::Positive => false,
clean::ImplPolarity::Negative => true,
};
Impl {
is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
generics: generics.into_tcx(tcx),