Skip to content

Commit 6f0544a

Browse files
committed
Simplify doc-cfg rendering based on the current context
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.
1 parent d890e64 commit 6f0544a

File tree

5 files changed

+296
-52
lines changed

5 files changed

+296
-52
lines changed

src/librustdoc/clean/cfg.rs

+29
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,35 @@ impl Cfg {
201201
_ => false,
202202
}
203203
}
204+
205+
/// Attempt to simplify this cfg by assuming that `assume` is already known to be true, will
206+
/// return `None` if simplification managed to completely eliminate any requirements from this
207+
/// `Cfg`.
208+
pub(crate) fn simplify_with(&self, assume: &Cfg) -> Option<Cfg> {
209+
if self == assume {
210+
return None;
211+
}
212+
213+
if let Cfg::All(a) = self {
214+
let mut sub_cfgs: Vec<Cfg> = if let Cfg::All(b) = assume {
215+
a.iter().filter(|a| !b.contains(a)).cloned().collect()
216+
} else {
217+
a.iter().filter(|&a| a != assume).cloned().collect()
218+
};
219+
let len = sub_cfgs.len();
220+
return match len {
221+
0 => None,
222+
1 => sub_cfgs.pop(),
223+
_ => Some(Cfg::All(sub_cfgs)),
224+
};
225+
} else if let Cfg::All(b) = assume {
226+
if b.contains(self) {
227+
return None;
228+
}
229+
}
230+
231+
Some(self.clone())
232+
}
204233
}
205234

206235
impl ops::Not for Cfg {

0 commit comments

Comments
 (0)