Skip to content

Commit

Permalink
Simplify doc-cfg rendering based on the current context
Browse files Browse the repository at this point in the history
For sub-items on a page don't show cfg that has already been rendered on
a parent item. At its simplest this means not showing anything that is
shown in the portability message at the top of the page, but also for
things like fields of an enum variant if that variant itself is
cfg-gated then don't repeat those cfg on each field of the variant.

This does not touch trait implementation rendering, as that is more
complex and there are existing issues around how it deals with doc-cfg
that need to be fixed first.
  • Loading branch information
Nemo157 committed Oct 7, 2020
1 parent d890e64 commit 6f0544a
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 52 deletions.
29 changes: 29 additions & 0 deletions src/librustdoc/clean/cfg.rs
Expand Up @@ -201,6 +201,35 @@ impl Cfg {
_ => false,
}
}

/// Attempt to simplify this cfg by assuming that `assume` is already known to be true, will
/// return `None` if simplification managed to completely eliminate any requirements from this
/// `Cfg`.
pub(crate) fn simplify_with(&self, assume: &Cfg) -> Option<Cfg> {
if self == assume {
return None;
}

if let Cfg::All(a) = self {
let mut sub_cfgs: Vec<Cfg> = if let Cfg::All(b) = assume {
a.iter().filter(|a| !b.contains(a)).cloned().collect()
} else {
a.iter().filter(|&a| a != assume).cloned().collect()
};
let len = sub_cfgs.len();
return match len {
0 => None,
1 => sub_cfgs.pop(),
_ => Some(Cfg::All(sub_cfgs)),
};
} else if let Cfg::All(b) = assume {
if b.contains(self) {
return None;
}
}

Some(self.clone())
}
}

impl ops::Not for Cfg {
Expand Down

0 comments on commit 6f0544a

Please sign in to comment.