Preserve doc attributes on struct fields

This commit is contained in:
David Tolnay 2020-12-30 15:37:01 -08:00
parent 0bdabfdc98
commit b4a05c3621
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
5 changed files with 20 additions and 7 deletions

View File

@ -155,10 +155,11 @@ fn expand_struct(strct: &Struct) -> TokenStream {
let doc = &strct.doc;
let type_id = type_id(&strct.name);
let fields = strct.fields.iter().map(|field| {
let doc = &field.doc;
// This span on the pub makes "private type in public interface" errors
// appear in the right place.
let vis = field.visibility;
quote!(#vis #field)
quote!(#doc #vis #field)
});
let mut derives = None;
let derived_traits = derive::expand_struct(strct, &mut derives);

View File

@ -297,11 +297,13 @@ impl Eq for Var {}
impl PartialEq for Var {
fn eq(&self, other: &Var) -> bool {
let Var {
doc: _,
visibility: _,
ident,
ty,
} = self;
let Var {
doc: _,
visibility: _,
ident: ident2,
ty: ty2,
@ -313,6 +315,7 @@ impl PartialEq for Var {
impl Hash for Var {
fn hash<H: Hasher>(&self, state: &mut H) {
let Var {
doc: _,
visibility: _,
ident,
ty,

View File

@ -150,6 +150,7 @@ pub struct Signature {
}
pub struct Var {
pub doc: Doc,
pub visibility: Token![pub],
pub ident: Ident,
pub ty: Type,

View File

@ -97,6 +97,15 @@ fn parse_struct(cx: &mut Errors, item: ItemStruct, namespace: &Namespace) -> Res
let mut fields = Vec::new();
for field in named_fields.named {
let ident = field.ident.unwrap();
let mut doc = Doc::new();
attrs::parse(
cx,
&field.attrs,
attrs::Parser {
doc: Some(&mut doc),
..Default::default()
},
);
let ty = match parse_type(&field.ty) {
Ok(ty) => ty,
Err(err) => {
@ -111,6 +120,7 @@ fn parse_struct(cx: &mut Errors, item: ItemStruct, namespace: &Namespace) -> Res
Visibility::Inherited => ident.span(),
});
fields.push(Var {
doc,
visibility,
ident,
ty,
@ -527,8 +537,10 @@ fn parse_extern_fn(
};
let ty = parse_type(&arg.ty)?;
if ident != "self" {
let doc = Doc::new();
let visibility = Token![pub](ident.span());
args.push_value(Var {
doc,
visibility,
ident,
ty,
@ -1098,8 +1110,10 @@ fn parse_type_fn(ty: &TypeBareFn) -> Result<Type> {
Some(ident) => ident.0.clone(),
None => format_ident!("arg{}", i),
};
let doc = Doc::new();
let visibility = Token![pub](ident.span());
Ok(Var {
doc,
visibility,
ident,
ty,

View File

@ -16,12 +16,6 @@ error: missing documentation for a struct field
12 | pub undocumented_field: u8,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: missing documentation for a struct field
--> $DIR/deny_missing_docs.rs:18:9
|
18 | pub documented_field: u8,
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: missing documentation for a struct
--> $DIR/deny_missing_docs.rs:9:1
|