Skip to content

Commit 543aea6

Browse files
committed
Use an enum to record polarity in clean::Impl
1 parent 7b7023c commit 543aea6

File tree

8 files changed

+34
-23
lines changed

8 files changed

+34
-23
lines changed

src/librustdoc/clean/auto_trait.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,17 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
7676
new_generics
7777
});
7878

79-
let negative_polarity;
79+
let polarity;
8080
let new_generics = match result {
8181
AutoTraitResult::PositiveImpl(new_generics) => {
82-
negative_polarity = false;
82+
polarity = ImplPolarity::Positive;
8383
if discard_positive_impl {
8484
return None;
8585
}
8686
new_generics
8787
}
8888
AutoTraitResult::NegativeImpl => {
89-
negative_polarity = true;
89+
polarity = ImplPolarity::Negative;
9090

9191
// For negative impls, we use the generic params, but *not* the predicates,
9292
// from the original type. Otherwise, the displayed impl appears to be a
@@ -120,7 +120,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
120120
trait_: Some(trait_ref.clean(self.cx)),
121121
for_: ty.clean(self.cx),
122122
items: Vec::new(),
123-
negative_polarity,
123+
polarity,
124124
kind: ImplKind::Auto,
125125
}),
126126
cfg: None,

src/librustdoc/clean/blanket_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
123123
.in_definition_order()
124124
.collect::<Vec<_>>()
125125
.clean(self.cx),
126-
negative_polarity: false,
126+
polarity: ImplPolarity::Positive,
127127
kind: ImplKind::Blanket(box trait_ref.self_ty().clean(self.cx)),
128128
}),
129129
cfg: None,

src/librustdoc/clean/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ crate fn build_impl(
497497
trait_,
498498
for_,
499499
items: trait_items,
500-
negative_polarity: polarity.clean(cx),
500+
polarity: polarity.clean(cx),
501501
kind: ImplKind::Normal,
502502
}),
503503
box merged_attrs,

src/librustdoc/clean/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,14 +1856,14 @@ impl Clean<Item> for hir::Variant<'_> {
18561856
}
18571857
}
18581858

1859-
impl Clean<bool> for ty::ImplPolarity {
1859+
impl Clean<ImplPolarity> for ty::ImplPolarity {
18601860
/// Returns whether the impl has negative polarity.
1861-
fn clean(&self, _: &mut DocContext<'_>) -> bool {
1861+
fn clean(&self, _: &mut DocContext<'_>) -> ImplPolarity {
18621862
match self {
1863-
&ty::ImplPolarity::Positive |
1863+
ty::ImplPolarity::Positive |
18641864
// FIXME: do we want to do something else here?
1865-
&ty::ImplPolarity::Reservation => false,
1866-
&ty::ImplPolarity::Negative => true,
1865+
ty::ImplPolarity::Reservation => ImplPolarity::Positive,
1866+
ty::ImplPolarity::Negative => ImplPolarity::Negative,
18671867
}
18681868
}
18691869
}
@@ -1894,7 +1894,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>
18941894
trait_,
18951895
for_,
18961896
items,
1897-
negative_polarity: tcx.impl_polarity(def_id).clean(cx),
1897+
polarity: tcx.impl_polarity(def_id).clean(cx),
18981898
kind: ImplKind::Normal,
18991899
});
19001900
Item::from_hir_id_and_parts(hir_id, None, kind, cx)

src/librustdoc/clean/types.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2177,7 +2177,7 @@ crate struct Impl {
21772177
crate trait_: Option<Path>,
21782178
crate for_: Type,
21792179
crate items: Vec<Item>,
2180-
crate negative_polarity: bool,
2180+
crate polarity: ImplPolarity,
21812181
crate kind: ImplKind,
21822182
}
21832183

@@ -2227,6 +2227,13 @@ impl ImplKind {
22272227
}
22282228
}
22292229

2230+
// FIXME: remove this and use ty::ImplPolarity instead
2231+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
2232+
crate enum ImplPolarity {
2233+
Positive,
2234+
Negative,
2235+
}
2236+
22302237
#[derive(Clone, Debug)]
22312238
crate struct Import {
22322239
crate kind: ImportKind,

src/librustdoc/html/format.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,9 @@ impl clean::Impl {
990990
}
991991

992992
if let Some(ref ty) = self.trait_ {
993-
if self.negative_polarity {
994-
write!(f, "!")?;
993+
match self.polarity {
994+
clean::ImplPolarity::Positive => {}
995+
clean::ImplPolarity::Negative => write!(f, "!")?,
995996
}
996997
fmt::Display::fmt(&ty.print(cx), f)?;
997998
write!(f, " for ")?;

src/librustdoc/html/render/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,12 +2033,12 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
20332033
let i_display = format!("{:#}", i.print(cx));
20342034
let out = Escape(&i_display);
20352035
let encoded = small_url_encode(format!("{:#}", i.print(cx)));
2036-
let generated = format!(
2037-
"<a href=\"#impl-{}\">{}{}</a>",
2038-
encoded,
2039-
if it.inner_impl().negative_polarity { "!" } else { "" },
2040-
out
2041-
);
2036+
let prefix = match it.inner_impl().polarity {
2037+
clean::ImplPolarity::Positive => "",
2038+
clean::ImplPolarity::Negative => "!",
2039+
};
2040+
let generated =
2041+
format!("<a href=\"#impl-{}\">{}{}</a>", encoded, prefix, out);
20422042
if links.insert(generated.clone()) { Some(generated) } else { None }
20432043
} else {
20442044
None

src/librustdoc/json/conversions.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,7 @@ 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 { unsafety, generics, trait_, for_, items, negative_polarity, kind } =
504-
impl_;
503+
let clean::Impl { unsafety, generics, trait_, for_, items, polarity, kind } = impl_;
505504
// FIXME: should `trait_` be a clean::Path equivalent in JSON?
506505
let trait_ = trait_.map(|path| {
507506
let did = path.def_id();
@@ -513,6 +512,10 @@ impl FromWithTcx<clean::Impl> for Impl {
513512
clean::ImplKind::Auto => (true, None),
514513
clean::ImplKind::Blanket(ty) => (false, Some(*ty)),
515514
};
515+
let negative_polarity = match polarity {
516+
clean::ImplPolarity::Positive => false,
517+
clean::ImplPolarity::Negative => true,
518+
};
516519
Impl {
517520
is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
518521
generics: generics.into_tcx(tcx),

0 commit comments

Comments
 (0)