Replace under-used ImplPolarity enum with a boolean

This commit is contained in:
Guillaume Gomez 2021-01-08 22:54:35 +01:00
parent 3338bdb23d
commit 34d128a263
9 changed files with 27 additions and 32 deletions

View File

@ -84,14 +84,14 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
new_generics new_generics
}); });
let polarity; let negative_polarity;
let new_generics = match result { let new_generics = match result {
AutoTraitResult::PositiveImpl(new_generics) => { AutoTraitResult::PositiveImpl(new_generics) => {
polarity = None; negative_polarity = false;
new_generics new_generics
} }
AutoTraitResult::NegativeImpl => { AutoTraitResult::NegativeImpl => {
polarity = Some(ImplPolarity::Negative); negative_polarity = true;
// 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
@ -130,7 +130,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
trait_: Some(trait_ref.clean(self.cx).get_trait_type().unwrap()), trait_: Some(trait_ref.clean(self.cx).get_trait_type().unwrap()),
for_: ty.clean(self.cx), for_: ty.clean(self.cx),
items: Vec::new(), items: Vec::new(),
polarity, negative_polarity,
synthetic: true, synthetic: true,
blanket_impl: None, blanket_impl: None,
}), }),

View File

@ -131,7 +131,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
.in_definition_order() .in_definition_order()
.collect::<Vec<_>>() .collect::<Vec<_>>()
.clean(self.cx), .clean(self.cx),
polarity: None, negative_polarity: false,
synthetic: false, synthetic: false,
blanket_impl: Some(trait_ref.self_ty().clean(self.cx)), blanket_impl: Some(trait_ref.self_ty().clean(self.cx)),
}), }),

View File

@ -428,7 +428,7 @@ crate fn build_impl(
trait_, trait_,
for_, for_,
items: trait_items, items: trait_items,
polarity: Some(polarity.clean(cx)), negative_polarity: polarity.clean(cx),
synthetic: false, synthetic: false,
blanket_impl: None, blanket_impl: None,
}), }),

View File

@ -2069,13 +2069,14 @@ impl Clean<Item> for hir::Variant<'_> {
} }
} }
impl Clean<ImplPolarity> for ty::ImplPolarity { impl Clean<bool> for ty::ImplPolarity {
fn clean(&self, _: &DocContext<'_>) -> ImplPolarity { /// Returns whether the impl has negative polarity.
fn clean(&self, _: &DocContext<'_>) -> bool {
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 => ImplPolarity::Positive, &ty::ImplPolarity::Reservation => false,
&ty::ImplPolarity::Negative => ImplPolarity::Negative, &ty::ImplPolarity::Negative => true,
} }
} }
} }
@ -2116,7 +2117,7 @@ fn clean_impl(impl_: &hir::Item<'_>, cx: &DocContext<'_>) -> Vec<Item> {
trait_, trait_,
for_, for_,
items, items,
polarity: Some(cx.tcx.impl_polarity(def_id).clean(cx)), negative_polarity: cx.tcx.impl_polarity(def_id).clean(cx),
synthetic: false, synthetic: false,
blanket_impl: None, blanket_impl: None,
}); });

View File

@ -175,9 +175,11 @@ impl Item {
} }
crate fn is_crate(&self) -> bool { crate fn is_crate(&self) -> bool {
matches!(*self.kind, matches!(
*self.kind,
StrippedItem(box ModuleItem(Module { is_crate: true, .. })) StrippedItem(box ModuleItem(Module { is_crate: true, .. }))
| ModuleItem(Module { is_crate: true, .. })) | ModuleItem(Module { is_crate: true, .. })
)
} }
crate fn is_mod(&self) -> bool { crate fn is_mod(&self) -> bool {
self.type_() == ItemType::Module self.type_() == ItemType::Module
@ -1858,12 +1860,6 @@ crate struct Constant {
crate is_literal: bool, crate is_literal: bool,
} }
#[derive(Clone, PartialEq, Debug)]
crate enum ImplPolarity {
Positive,
Negative,
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
crate struct Impl { crate struct Impl {
crate unsafety: hir::Unsafety, crate unsafety: hir::Unsafety,
@ -1872,7 +1868,7 @@ crate struct Impl {
crate trait_: Option<Type>, crate trait_: Option<Type>,
crate for_: Type, crate for_: Type,
crate items: Vec<Item>, crate items: Vec<Item>,
crate polarity: Option<ImplPolarity>, crate negative_polarity: bool,
crate synthetic: bool, crate synthetic: bool,
crate blanket_impl: Option<Type>, crate blanket_impl: Option<Type>,
} }

View File

@ -870,7 +870,7 @@ impl clean::Impl {
} }
if let Some(ref ty) = self.trait_ { if let Some(ref ty) = self.trait_ {
if self.polarity == Some(clean::ImplPolarity::Negative) { if self.negative_polarity {
write!(f, "!")?; write!(f, "!")?;
} }

View File

@ -4327,16 +4327,15 @@ fn sidebar_assoc_items(cx: &Context<'_>, it: &clean::Item) -> String {
let mut ret = impls let mut ret = impls
.iter() .iter()
.filter_map(|i| { .filter_map(|it| {
let is_negative_impl = is_negative_impl(i.inner_impl()); if let Some(ref i) = it.inner_impl().trait_ {
if let Some(ref i) = i.inner_impl().trait_ {
let i_display = format!("{:#}", i.print()); let i_display = format!("{:#}", i.print());
let out = Escape(&i_display); let out = Escape(&i_display);
let encoded = small_url_encode(&format!("{:#}", i.print())); let encoded = small_url_encode(&format!("{:#}", i.print()));
let generated = format!( let generated = format!(
"<a href=\"#impl-{}\">{}{}</a>", "<a href=\"#impl-{}\">{}{}</a>",
encoded, encoded,
if is_negative_impl { "!" } else { "" }, if it.inner_impl().negative_polarity { "!" } else { "" },
out out
); );
if links.insert(generated.clone()) { Some(generated) } else { None } if links.insert(generated.clone()) { Some(generated) } else { None }
@ -4503,10 +4502,6 @@ fn extract_for_impl_name(item: &clean::Item) -> Option<(String, String)> {
} }
} }
fn is_negative_impl(i: &clean::Impl) -> bool {
i.polarity == Some(clean::ImplPolarity::Negative)
}
fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean::Trait) { fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean::Trait) {
let mut sidebar = String::new(); let mut sidebar = String::new();

View File

@ -422,7 +422,7 @@ impl From<clean::Impl> for Impl {
trait_, trait_,
for_, for_,
items, items,
polarity, negative_polarity,
synthetic, synthetic,
blanket_impl, blanket_impl,
} = impl_; } = impl_;
@ -436,7 +436,7 @@ impl From<clean::Impl> for Impl {
trait_: trait_.map(Into::into), trait_: trait_.map(Into::into),
for_: for_.into(), for_: for_.into(),
items: ids(items), items: ids(items),
negative: polarity == Some(clean::ImplPolarity::Negative), negative: negative_polarity,
synthetic, synthetic,
blanket_impl: blanket_impl.map(Into::into), blanket_impl: blanket_impl.map(Into::into),
} }

View File

@ -70,7 +70,10 @@ impl Events {
} }
fn is_comment(&self) -> bool { fn is_comment(&self) -> bool {
matches!(self, Events::StartLineComment(_) | Events::StartComment(_) | Events::EndComment(_)) matches!(
self,
Events::StartLineComment(_) | Events::StartComment(_) | Events::EndComment(_)
)
} }
} }