Skip to content

Commit 811b4b8

Browse files
authored
Rollup merge of #99235 - WaffleLapkin:rustdoc_implement_support_for_must_implement, r=GuillaumeGomez
rustdoc: Add support for `#[rustc_must_implement_one_of]` This PR adds support for `#[rustc_must_implement_one_of]` attribute added in #92164. There is a desire to eventually use this attribute of `Read`, so making it show up in docs is a good thing. I "stole" the styling from cfg notes, not sure what would be a proper styling. Currently it looks like this: ![2022-07-14_15-00](https://user-images.githubusercontent.com/38225716/178968170-913c1dd5-8875-4a95-9848-b075a0bb8998.png) <details><summary>Code to reproduce</summary> <p> ```rust #![feature(rustc_attrs)] #[rustc_must_implement_one_of(a, b)] pub trait Trait { fn req(); fn a(){ Self::b() } fn b(){ Self::a() } } ``` </p> </details>
2 parents 2614e43 + ed8c2c2 commit 811b4b8

File tree

6 files changed

+32
-6
lines changed

6 files changed

+32
-6
lines changed

src/librustdoc/clean/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,7 @@ fn clean_maybe_renamed_item<'tcx>(
19931993
ItemKind::Trait(_, _, generics, bounds, item_ids) => {
19941994
let items =
19951995
item_ids.iter().map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx)).collect();
1996+
19961997
TraitItem(Trait {
19971998
def_id,
19981999
items,

src/librustdoc/html/render/print_item.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
use clean::AttributesExt;
22

3-
use std::cmp::Ordering;
4-
use std::fmt;
5-
use std::rc::Rc;
6-
73
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
84
use rustc_hir as hir;
95
use rustc_hir::def::CtorKind;
@@ -15,6 +11,9 @@ use rustc_middle::ty::{Adt, TyCtxt};
1511
use rustc_span::hygiene::MacroKind;
1612
use rustc_span::symbol::{kw, sym, Symbol};
1713
use rustc_target::abi::{Layout, Primitive, TagEncoding, Variants};
14+
use std::cmp::Ordering;
15+
use std::fmt;
16+
use std::rc::Rc;
1817

1918
use super::{
2019
collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_section,
@@ -37,6 +36,7 @@ use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
3736
use crate::html::url_parts_builder::UrlPartsBuilder;
3837

3938
use askama::Template;
39+
use itertools::Itertools;
4040

4141
const ITEM_TABLE_OPEN: &str = "<div class=\"item-table\">";
4242
const ITEM_TABLE_CLOSE: &str = "</div>";
@@ -539,6 +539,8 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
539539
let count_types = required_types.len() + provided_types.len();
540540
let count_consts = required_consts.len() + provided_consts.len();
541541
let count_methods = required_methods.len() + provided_methods.len();
542+
let must_implement_one_of_functions =
543+
cx.tcx().trait_def(t.def_id).must_implement_one_of.clone();
542544

543545
// Output the trait definition
544546
wrap_into_docblock(w, |w| {
@@ -784,13 +786,22 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
784786
}
785787

786788
// Output the documentation for each function individually
787-
if !required_methods.is_empty() {
789+
if !required_methods.is_empty() || must_implement_one_of_functions.is_some() {
788790
write_small_section_header(
789791
w,
790792
"required-methods",
791793
"Required Methods",
792794
"<div class=\"methods\">",
793795
);
796+
797+
if let Some(list) = must_implement_one_of_functions.as_deref() {
798+
write!(
799+
w,
800+
"<div class=\"stab must_implement\">At least one of the `{}` methods is required.</div>",
801+
list.iter().join("`, `")
802+
);
803+
}
804+
794805
for m in required_methods {
795806
trait_item(w, cx, m, it);
796807
}

src/librustdoc/html/static/css/themes/ayu.css

+3-1
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,13 @@ details.rustdoc-toggle > summary::before {
205205

206206
/* Created this empty rule to satisfy the theme checks. */
207207
.stab.empty-impl {}
208+
.stab.must_implement {}
208209

209210
.stab.unstable,
210211
.stab.deprecated,
211212
.stab.portability,
212-
.stab.empty-impl {
213+
.stab.empty-impl,
214+
.stab.must_implement {
213215
color: #c5c5c5;
214216
background: #314559 !important;
215217
border-style: none !important;

src/librustdoc/html/static/css/themes/dark.css

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ details.rustdoc-toggle > summary::before {
180180
.stab.empty-impl { background: #FFF5D6; border-color: #FFC600; color: #2f2f2f; }
181181
.stab.unstable { background: #FFF5D6; border-color: #FFC600; color: #2f2f2f; }
182182
.stab.deprecated { background: #ffc4c4; border-color: #db7b7b; color: #2f2f2f; }
183+
.stab.must_implement { background: #F3DFFF; border-color: #b07bdb; color: #2f2f2f; }
183184
.stab.portability { background: #F3DFFF; border-color: #b07bdb; color: #2f2f2f; }
184185
.stab.portability > code { background: none; }
185186

src/librustdoc/html/static/css/themes/light.css

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ details.rustdoc-toggle > summary::before {
163163
.stab.empty-impl { background: #FFF5D6; border-color: #FFC600; }
164164
.stab.unstable { background: #FFF5D6; border-color: #FFC600; }
165165
.stab.deprecated { background: #ffc4c4; border-color: #db7b7b; }
166+
.stab.must_implement { background: #F3DFFF; border-color: #b07bdb; }
166167
.stab.portability { background: #F3DFFF; border-color: #b07bdb; }
167168
.stab.portability > code { background: none; }
168169

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![crate_name = "c"]
2+
#![feature(rustc_attrs)]
3+
4+
#[rustc_must_implement_one_of(a, b)]
5+
// @matches c/trait.Trait.html '//*[@class="stab must_implement"]' \
6+
// 'At least one of the `a`, `b` methods is required.$'
7+
pub trait Trait {
8+
fn a() {}
9+
fn b() {}
10+
}

0 commit comments

Comments
 (0)