From b4a05c3621964bb2d5d8e9bf9ccb651aebf4f7ff Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 30 Dec 2020 15:37:01 -0800 Subject: [PATCH] Preserve doc attributes on struct fields --- macro/src/expand.rs | 3 ++- syntax/impls.rs | 3 +++ syntax/mod.rs | 1 + syntax/parse.rs | 14 ++++++++++++++ tests/ui/deny_missing_docs.stderr | 6 ------ 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/macro/src/expand.rs b/macro/src/expand.rs index 8ba765af..3b031155 100644 --- a/macro/src/expand.rs +++ b/macro/src/expand.rs @@ -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); diff --git a/syntax/impls.rs b/syntax/impls.rs index c7e1a2c2..624a64d4 100644 --- a/syntax/impls.rs +++ b/syntax/impls.rs @@ -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(&self, state: &mut H) { let Var { + doc: _, visibility: _, ident, ty, diff --git a/syntax/mod.rs b/syntax/mod.rs index f6bdf72e..092f4037 100644 --- a/syntax/mod.rs +++ b/syntax/mod.rs @@ -150,6 +150,7 @@ pub struct Signature { } pub struct Var { + pub doc: Doc, pub visibility: Token![pub], pub ident: Ident, pub ty: Type, diff --git a/syntax/parse.rs b/syntax/parse.rs index 4bfabcd5..3d970d83 100644 --- a/syntax/parse.rs +++ b/syntax/parse.rs @@ -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 { 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, diff --git a/tests/ui/deny_missing_docs.stderr b/tests/ui/deny_missing_docs.stderr index 96dec460..f31be34a 100644 --- a/tests/ui/deny_missing_docs.stderr +++ b/tests/ui/deny_missing_docs.stderr @@ -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 |