]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #99235 - WaffleLapkin:rustdoc_implement_support_for_must_implement...
authorMatthias Krüger <matthias.krueger@famsik.de>
Tue, 26 Jul 2022 14:57:46 +0000 (16:57 +0200)
committerGitHub <noreply@github.com>
Tue, 26 Jul 2022 14:57:46 +0000 (16:57 +0200)
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>

1  2 
src/librustdoc/clean/mod.rs

index c9ef4748a48451619cadda1289bbd09d54d5d97e,2c63f381851f4e33ac80ec21426b1ebcbf90c385..07237438a0d3267697a2f754d4fbe369e1bd0b93
@@@ -237,22 -237,13 +237,22 @@@ impl<'tcx> Clean<'tcx, Lifetime> for hi
      }
  }
  
 -impl<'tcx> Clean<'tcx, Constant> for hir::ConstArg {
 -    fn clean(&self, cx: &mut DocContext<'tcx>) -> Constant {
 -        let def_id = cx.tcx.hir().body_owner_def_id(self.value.body).to_def_id();
 -        Constant {
 -            type_: clean_middle_ty(cx.tcx.type_of(def_id), cx, Some(def_id)),
 -            kind: ConstantKind::Anonymous { body: self.value.body },
 -        }
 +pub(crate) fn clean_const<'tcx>(constant: &hir::ConstArg, cx: &mut DocContext<'tcx>) -> Constant {
 +    let def_id = cx.tcx.hir().body_owner_def_id(constant.value.body).to_def_id();
 +    Constant {
 +        type_: clean_middle_ty(cx.tcx.type_of(def_id), cx, Some(def_id)),
 +        kind: ConstantKind::Anonymous { body: constant.value.body },
 +    }
 +}
 +
 +pub(crate) fn clean_middle_const<'tcx>(
 +    constant: ty::Const<'tcx>,
 +    cx: &mut DocContext<'tcx>,
 +) -> Constant {
 +    // FIXME: instead of storing the stringified expression, store `self` directly instead.
 +    Constant {
 +        type_: clean_middle_ty(constant.ty(), cx, None),
 +        kind: ConstantKind::TyConst { expr: constant.to_string() },
      }
  }
  
@@@ -401,7 -392,7 +401,7 @@@ impl<'tcx> Clean<'tcx, Term> for ty::Te
      fn clean(&self, cx: &mut DocContext<'tcx>) -> Term {
          match self {
              ty::Term::Ty(ty) => Term::Type(clean_middle_ty(*ty, cx, None)),
 -            ty::Term::Const(c) => Term::Constant(c.clean(cx)),
 +            ty::Term::Const(c) => Term::Constant(clean_middle_const(*c, cx)),
          }
      }
  }
@@@ -412,7 -403,7 +412,7 @@@ impl<'tcx> Clean<'tcx, Term> for hir::T
              hir::Term::Ty(ty) => Term::Type(clean_ty(ty, cx)),
              hir::Term::Const(c) => {
                  let def_id = cx.tcx.hir().local_def_id(c.hir_id);
 -                Term::Constant(ty::Const::from_anon_const(cx.tcx, def_id).clean(cx))
 +                Term::Constant(clean_middle_const(ty::Const::from_anon_const(cx.tcx, def_id), cx))
              }
          }
      }
@@@ -1477,10 -1468,8 +1477,10 @@@ fn maybe_expand_private_type_alias<'tcx
                      _ => None,
                  });
                  if let Some(ct) = const_ {
 -                    substs
 -                        .insert(const_param_def_id.to_def_id(), SubstParam::Constant(ct.clean(cx)));
 +                    substs.insert(
 +                        const_param_def_id.to_def_id(),
 +                        SubstParam::Constant(clean_const(ct, cx)),
 +                    );
                  }
                  // FIXME(const_generics_defaults)
                  indices.consts += 1;
@@@ -1775,26 -1764,35 +1775,26 @@@ pub(crate) fn clean_middle_ty<'tcx>
      }
  }
  
 -impl<'tcx> Clean<'tcx, Constant> for ty::Const<'tcx> {
 -    fn clean(&self, cx: &mut DocContext<'tcx>) -> Constant {
 -        // FIXME: instead of storing the stringified expression, store `self` directly instead.
 -        Constant {
 -            type_: clean_middle_ty(self.ty(), cx, None),
 -            kind: ConstantKind::TyConst { expr: self.to_string() },
 -        }
 -    }
 -}
 -
 -impl<'tcx> Clean<'tcx, Item> for hir::FieldDef<'tcx> {
 -    fn clean(&self, cx: &mut DocContext<'tcx>) -> Item {
 -        let def_id = cx.tcx.hir().local_def_id(self.hir_id).to_def_id();
 -        clean_field(def_id, self.ident.name, clean_ty(self.ty, cx), cx)
 -    }
 +pub(crate) fn clean_field<'tcx>(field: &hir::FieldDef<'tcx>, cx: &mut DocContext<'tcx>) -> Item {
 +    let def_id = cx.tcx.hir().local_def_id(field.hir_id).to_def_id();
 +    clean_field_with_def_id(def_id, field.ident.name, clean_ty(field.ty, cx), cx)
  }
  
 -impl<'tcx> Clean<'tcx, Item> for ty::FieldDef {
 -    fn clean(&self, cx: &mut DocContext<'tcx>) -> Item {
 -        clean_field(
 -            self.did,
 -            self.name,
 -            clean_middle_ty(cx.tcx.type_of(self.did), cx, Some(self.did)),
 -            cx,
 -        )
 -    }
 +pub(crate) fn clean_middle_field<'tcx>(field: &ty::FieldDef, cx: &mut DocContext<'tcx>) -> Item {
 +    clean_field_with_def_id(
 +        field.did,
 +        field.name,
 +        clean_middle_ty(cx.tcx.type_of(field.did), cx, Some(field.did)),
 +        cx,
 +    )
  }
  
 -fn clean_field(def_id: DefId, name: Symbol, ty: Type, cx: &mut DocContext<'_>) -> Item {
 +pub(crate) fn clean_field_with_def_id(
 +    def_id: DefId,
 +    name: Symbol,
 +    ty: Type,
 +    cx: &mut DocContext<'_>,
 +) -> Item {
      let what_rustc_thinks =
          Item::from_def_id_and_parts(def_id, Some(name), StructFieldItem(ty), cx);
      if is_field_vis_inherited(cx.tcx, def_id) {
@@@ -1832,14 -1830,14 +1832,14 @@@ impl<'tcx> Clean<'tcx, VariantStruct> f
      fn clean(&self, cx: &mut DocContext<'tcx>) -> VariantStruct {
          VariantStruct {
              struct_type: CtorKind::from_hir(self),
 -            fields: self.fields().iter().map(|x| x.clean(cx)).collect(),
 +            fields: self.fields().iter().map(|x| clean_field(x, cx)).collect(),
          }
      }
  }
  
  impl<'tcx> Clean<'tcx, Vec<Item>> for hir::VariantData<'tcx> {
      fn clean(&self, cx: &mut DocContext<'tcx>) -> Vec<Item> {
 -        self.fields().iter().map(|x| x.clean(cx)).collect()
 +        self.fields().iter().map(|x| clean_field(x, cx)).collect()
      }
  }
  
@@@ -1847,12 -1845,12 +1847,12 @@@ impl<'tcx> Clean<'tcx, Item> for ty::Va
      fn clean(&self, cx: &mut DocContext<'tcx>) -> Item {
          let kind = match self.ctor_kind {
              CtorKind::Const => Variant::CLike,
 -            CtorKind::Fn => {
 -                Variant::Tuple(self.fields.iter().map(|field| field.clean(cx)).collect())
 -            }
 +            CtorKind::Fn => Variant::Tuple(
 +                self.fields.iter().map(|field| clean_middle_field(field, cx)).collect(),
 +            ),
              CtorKind::Fictive => Variant::Struct(VariantStruct {
                  struct_type: CtorKind::Fictive,
 -                fields: self.fields.iter().map(|field| field.clean(cx)).collect(),
 +                fields: self.fields.iter().map(|field| clean_middle_field(field, cx)).collect(),
              }),
          };
          let what_rustc_thinks =
@@@ -1896,7 -1894,7 +1896,7 @@@ impl<'tcx> Clean<'tcx, GenericArgs> fo
                      }
                      hir::GenericArg::Lifetime(_) => GenericArg::Lifetime(Lifetime::elided()),
                      hir::GenericArg::Type(ty) => GenericArg::Type(clean_ty(ty, cx)),
 -                    hir::GenericArg::Const(ct) => GenericArg::Const(Box::new(ct.clean(cx))),
 +                    hir::GenericArg::Const(ct) => GenericArg::Const(Box::new(clean_const(ct, cx))),
                      hir::GenericArg::Infer(_inf) => GenericArg::Infer,
                  })
                  .collect::<Vec<_>>()
@@@ -1972,12 -1970,12 +1972,12 @@@ fn clean_maybe_renamed_item<'tcx>
              }),
              ItemKind::Union(ref variant_data, generics) => UnionItem(Union {
                  generics: generics.clean(cx),
 -                fields: variant_data.fields().iter().map(|x| x.clean(cx)).collect(),
 +                fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
              }),
              ItemKind::Struct(ref variant_data, generics) => StructItem(Struct {
                  struct_type: CtorKind::from_hir(variant_data),
                  generics: generics.clean(cx),
 -                fields: variant_data.fields().iter().map(|x| x.clean(cx)).collect(),
 +                fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
              }),
              ItemKind::Impl(impl_) => return clean_impl(impl_, item.hir_id(), cx),
              // proc macros can have a name set by attributes
              ItemKind::Trait(_, _, generics, bounds, item_ids) => {
                  let items =
                      item_ids.iter().map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx)).collect();
                  TraitItem(Trait {
                      def_id,
                      items,