Skip to content

Commit

Permalink
Rollup merge of #128923 - GuillaumeGomez:negative-impls-items, r=fmease
Browse files Browse the repository at this point in the history
[rustdoc] Stop showing impl items for negative impls

Fixes #128799.

As discussed with `@fmease,` they have a broader patch in progress, so this (small) PR will at least allow for them to have a regression test. :)

r? `@fmease`
  • Loading branch information
GuillaumeGomez committed Aug 10, 2024
2 parents 50e9fd1 + 5baf7c2 commit 893b9f3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
4 changes: 4 additions & 0 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,10 @@ impl Impl {
.map(|did| tcx.provided_trait_methods(did).map(|meth| meth.name).collect())
.unwrap_or_default()
}

pub(crate) fn is_negative_trait_impl(&self) -> bool {
matches!(self.polarity, ty::ImplPolarity::Negative)
}
}

#[derive(Clone, Debug)]
Expand Down
5 changes: 2 additions & 3 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,9 +1285,8 @@ impl clean::Impl {
f.write_str(" ")?;

if let Some(ref ty) = self.trait_ {
match self.polarity {
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => {}
ty::ImplPolarity::Negative => write!(f, "!")?,
if self.is_negative_trait_impl() {
write!(f, "!")?;
}
ty.print(cx).fmt(f)?;
write!(f, " for ")?;
Expand Down
37 changes: 21 additions & 16 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1780,20 +1780,23 @@ fn render_impl(

let mut impl_items = Buffer::empty_from(w);
let mut default_impl_items = Buffer::empty_from(w);
let impl_ = i.inner_impl();

for trait_item in &i.inner_impl().items {
doc_impl_item(
&mut default_impl_items,
&mut impl_items,
cx,
trait_item,
if trait_.is_some() { &i.impl_item } else { parent },
link,
render_mode,
false,
trait_,
rendering_params,
);
if !impl_.is_negative_trait_impl() {
for trait_item in &impl_.items {
doc_impl_item(
&mut default_impl_items,
&mut impl_items,
cx,
trait_item,
if trait_.is_some() { &i.impl_item } else { parent },
link,
render_mode,
false,
trait_,
rendering_params,
);
}
}

fn render_default_items(
Expand Down Expand Up @@ -1844,13 +1847,15 @@ fn render_impl(
// We don't emit documentation for default items if they appear in the
// Implementations on Foreign Types or Implementors sections.
if rendering_params.show_default_items {
if let Some(t) = trait_ {
if let Some(t) = trait_
&& !impl_.is_negative_trait_impl()
{
render_default_items(
&mut default_impl_items,
&mut impl_items,
cx,
t,
i.inner_impl(),
impl_,
&i.impl_item,
render_mode,
rendering_params,
Expand Down Expand Up @@ -1882,7 +1887,7 @@ fn render_impl(
}

if let Some(ref dox) = i.impl_item.opt_doc_value() {
if trait_.is_none() && i.inner_impl().items.is_empty() {
if trait_.is_none() && impl_.items.is_empty() {
w.write_str(
"<div class=\"item-info\">\
<div class=\"stab empty-impl\">This impl block contains no items.</div>\
Expand Down
26 changes: 26 additions & 0 deletions tests/rustdoc/negative-impl-no-items.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// This test ensures that negative impls don't have items listed inside them.

#![feature(negative_impls)]
#![crate_name = "foo"]

pub struct Thing;

//@ has 'foo/struct.Thing.html'
// We check the full path to ensure there is no `<details>` element.
//@ has - '//div[@id="trait-implementations-list"]/section[@id="impl-Iterator-for-Thing"]/h3' \
// 'impl !Iterator for Thing'
impl !Iterator for Thing {}

// This struct will allow us to compare both paths.
pub struct Witness;

//@ has 'foo/struct.Witness.html'
//@ has - '//div[@id="trait-implementations-list"]/details//section[@id="impl-Iterator-for-Witness"]/h3' \
// 'impl Iterator for Witness'
impl Iterator for Witness {
type Item = u8;

fn next(&mut self) -> Option<Self::Item> {
None
}
}

0 comments on commit 893b9f3

Please sign in to comment.