From fc7697d504c00a5f74837c9edd174e933f4ec9e3 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 21 Feb 2021 13:51:24 +0800 Subject: [PATCH] Reflect visibility to generated modules. --- codegen/src/function.rs | 17 +++++++++-------- codegen/src/module.rs | 5 +++-- codegen/src/test/function.rs | 14 +++++++------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/codegen/src/function.rs b/codegen/src/function.rs index d3a7f792..bbed092d 100644 --- a/codegen/src/function.rs +++ b/codegen/src/function.rs @@ -16,6 +16,7 @@ use quote::{quote, quote_spanned, ToTokens}; use syn::{ parse::{Parse, ParseStream, Parser}, spanned::Spanned, + VisPublic, Visibility, }; use crate::attrs::{ExportInfo, ExportScope, ExportedParams}; @@ -273,7 +274,7 @@ impl ExportedParams for ExportedFnParams { pub(crate) struct ExportedFn { entire_span: proc_macro2::Span, signature: syn::Signature, - is_public: bool, + visibility: syn::Visibility, pass_context: bool, return_dynamic: bool, mut_receiver: bool, @@ -298,8 +299,7 @@ impl Parse for ExportedFn { // #[cfg] attributes are not allowed on functions due to what is generated for them crate::attrs::deny_cfg_attr(&fn_all.attrs)?; - // Determine if the function is public. - let is_public = matches!(fn_all.vis, syn::Visibility::Public(_)); + let visibility = fn_all.vis; // Determine if the function requires a call context if let Some(first_arg) = fn_all.sig.inputs.first() { @@ -408,7 +408,7 @@ impl Parse for ExportedFn { Ok(ExportedFn { entire_span, signature: fn_all.sig, - is_public, + visibility, pass_context, return_dynamic, mut_receiver, @@ -425,7 +425,7 @@ impl ExportedFn { pub(crate) fn update_scope(&mut self, parent_scope: &ExportScope) { let keep = match (self.params.skip, parent_scope) { (true, _) => false, - (_, ExportScope::PubOnly) => self.is_public, + (_, ExportScope::PubOnly) => self.is_public(), (_, ExportScope::Prefix(s)) => self.name().to_string().starts_with(s), (_, ExportScope::All) => true, }; @@ -449,7 +449,7 @@ impl ExportedFn { } pub(crate) fn is_public(&self) -> bool { - self.is_public + !matches!(self.visibility, syn::Visibility::Inherited) } pub(crate) fn span(&self) -> &proc_macro2::Span { @@ -593,9 +593,10 @@ impl ExportedFn { let input_types_block = self.generate_input_types("Token"); let return_type_block = self.generate_return_type("Token"); let dyn_result_fn_block = self.generate_dynamic_fn(); + let vis = self.visibility; quote! { - #[allow(unused)] - pub mod #name { + #[automatically_derived] + #vis mod #name { use super::*; struct Token(); #impl_block diff --git a/codegen/src/module.rs b/codegen/src/module.rs index fa9526ec..89b9bc11 100644 --- a/codegen/src/module.rs +++ b/codegen/src/module.rs @@ -257,6 +257,7 @@ impl Module { params, .. } = self; + let mod_vis = mod_all.vis; let mod_name = mod_all.ident.clone(); let (_, orig_content) = mod_all.content.take().unwrap(); let mod_attrs = mem::take(&mut mod_all.attrs); @@ -282,7 +283,7 @@ impl Module { // Regenerate the module with the new content added. Ok(quote! { #(#mod_attrs)* - pub mod #mod_name { + #mod_vis mod #mod_name { #(#orig_content)* #(#inner_modules)* #mod_gen @@ -292,7 +293,7 @@ impl Module { // Regenerate the original module as-is. Ok(quote! { #(#mod_attrs)* - pub mod #mod_name { + #mod_vis mod #mod_name { #(#orig_content)* } }) diff --git a/codegen/src/test/function.rs b/codegen/src/test/function.rs index 34e2d38c..238b89c2 100644 --- a/codegen/src/test/function.rs +++ b/codegen/src/test/function.rs @@ -272,7 +272,7 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(unused)] + #[automatically_derived] pub mod rhai_fn_do_nothing { use super::*; struct Token(); @@ -325,7 +325,7 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(unused)] + #[automatically_derived] pub mod rhai_fn_do_something { use super::*; struct Token(); @@ -379,7 +379,7 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(unused)] + #[automatically_derived] pub mod rhai_fn_do_something { use super::*; struct Token(); @@ -436,7 +436,7 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(unused)] + #[automatically_derived] pub mod rhai_fn_return_dynamic { use super::*; struct Token(); @@ -523,7 +523,7 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(unused)] + #[automatically_derived] pub mod rhai_fn_add_together { use super::*; struct Token(); @@ -579,7 +579,7 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(unused)] + #[automatically_derived] pub mod rhai_fn_increment { use super::*; struct Token(); @@ -641,7 +641,7 @@ mod generate_tests { }; let expected_tokens = quote! { - #[allow(unused)] + #[automatically_derived] pub mod rhai_fn_special_print { use super::*; struct Token();