Reflect visibility to generated modules.

This commit is contained in:
Stephen Chung 2021-02-21 13:51:24 +08:00
parent 03c31a969a
commit fc7697d504
3 changed files with 19 additions and 17 deletions

View File

@ -16,6 +16,7 @@ use quote::{quote, quote_spanned, ToTokens};
use syn::{ use syn::{
parse::{Parse, ParseStream, Parser}, parse::{Parse, ParseStream, Parser},
spanned::Spanned, spanned::Spanned,
VisPublic, Visibility,
}; };
use crate::attrs::{ExportInfo, ExportScope, ExportedParams}; use crate::attrs::{ExportInfo, ExportScope, ExportedParams};
@ -273,7 +274,7 @@ impl ExportedParams for ExportedFnParams {
pub(crate) struct ExportedFn { pub(crate) struct ExportedFn {
entire_span: proc_macro2::Span, entire_span: proc_macro2::Span,
signature: syn::Signature, signature: syn::Signature,
is_public: bool, visibility: syn::Visibility,
pass_context: bool, pass_context: bool,
return_dynamic: bool, return_dynamic: bool,
mut_receiver: 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 // #[cfg] attributes are not allowed on functions due to what is generated for them
crate::attrs::deny_cfg_attr(&fn_all.attrs)?; crate::attrs::deny_cfg_attr(&fn_all.attrs)?;
// Determine if the function is public. let visibility = fn_all.vis;
let is_public = matches!(fn_all.vis, syn::Visibility::Public(_));
// Determine if the function requires a call context // Determine if the function requires a call context
if let Some(first_arg) = fn_all.sig.inputs.first() { if let Some(first_arg) = fn_all.sig.inputs.first() {
@ -408,7 +408,7 @@ impl Parse for ExportedFn {
Ok(ExportedFn { Ok(ExportedFn {
entire_span, entire_span,
signature: fn_all.sig, signature: fn_all.sig,
is_public, visibility,
pass_context, pass_context,
return_dynamic, return_dynamic,
mut_receiver, mut_receiver,
@ -425,7 +425,7 @@ impl ExportedFn {
pub(crate) fn update_scope(&mut self, parent_scope: &ExportScope) { pub(crate) fn update_scope(&mut self, parent_scope: &ExportScope) {
let keep = match (self.params.skip, parent_scope) { let keep = match (self.params.skip, parent_scope) {
(true, _) => false, (true, _) => false,
(_, ExportScope::PubOnly) => self.is_public, (_, ExportScope::PubOnly) => self.is_public(),
(_, ExportScope::Prefix(s)) => self.name().to_string().starts_with(s), (_, ExportScope::Prefix(s)) => self.name().to_string().starts_with(s),
(_, ExportScope::All) => true, (_, ExportScope::All) => true,
}; };
@ -449,7 +449,7 @@ impl ExportedFn {
} }
pub(crate) fn is_public(&self) -> bool { pub(crate) fn is_public(&self) -> bool {
self.is_public !matches!(self.visibility, syn::Visibility::Inherited)
} }
pub(crate) fn span(&self) -> &proc_macro2::Span { pub(crate) fn span(&self) -> &proc_macro2::Span {
@ -593,9 +593,10 @@ impl ExportedFn {
let input_types_block = self.generate_input_types("Token"); let input_types_block = self.generate_input_types("Token");
let return_type_block = self.generate_return_type("Token"); let return_type_block = self.generate_return_type("Token");
let dyn_result_fn_block = self.generate_dynamic_fn(); let dyn_result_fn_block = self.generate_dynamic_fn();
let vis = self.visibility;
quote! { quote! {
#[allow(unused)] #[automatically_derived]
pub mod #name { #vis mod #name {
use super::*; use super::*;
struct Token(); struct Token();
#impl_block #impl_block

View File

@ -257,6 +257,7 @@ impl Module {
params, params,
.. ..
} = self; } = self;
let mod_vis = mod_all.vis;
let mod_name = mod_all.ident.clone(); let mod_name = mod_all.ident.clone();
let (_, orig_content) = mod_all.content.take().unwrap(); let (_, orig_content) = mod_all.content.take().unwrap();
let mod_attrs = mem::take(&mut mod_all.attrs); let mod_attrs = mem::take(&mut mod_all.attrs);
@ -282,7 +283,7 @@ impl Module {
// Regenerate the module with the new content added. // Regenerate the module with the new content added.
Ok(quote! { Ok(quote! {
#(#mod_attrs)* #(#mod_attrs)*
pub mod #mod_name { #mod_vis mod #mod_name {
#(#orig_content)* #(#orig_content)*
#(#inner_modules)* #(#inner_modules)*
#mod_gen #mod_gen
@ -292,7 +293,7 @@ impl Module {
// Regenerate the original module as-is. // Regenerate the original module as-is.
Ok(quote! { Ok(quote! {
#(#mod_attrs)* #(#mod_attrs)*
pub mod #mod_name { #mod_vis mod #mod_name {
#(#orig_content)* #(#orig_content)*
} }
}) })

View File

@ -272,7 +272,7 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(unused)] #[automatically_derived]
pub mod rhai_fn_do_nothing { pub mod rhai_fn_do_nothing {
use super::*; use super::*;
struct Token(); struct Token();
@ -325,7 +325,7 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(unused)] #[automatically_derived]
pub mod rhai_fn_do_something { pub mod rhai_fn_do_something {
use super::*; use super::*;
struct Token(); struct Token();
@ -379,7 +379,7 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(unused)] #[automatically_derived]
pub mod rhai_fn_do_something { pub mod rhai_fn_do_something {
use super::*; use super::*;
struct Token(); struct Token();
@ -436,7 +436,7 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(unused)] #[automatically_derived]
pub mod rhai_fn_return_dynamic { pub mod rhai_fn_return_dynamic {
use super::*; use super::*;
struct Token(); struct Token();
@ -523,7 +523,7 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(unused)] #[automatically_derived]
pub mod rhai_fn_add_together { pub mod rhai_fn_add_together {
use super::*; use super::*;
struct Token(); struct Token();
@ -579,7 +579,7 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(unused)] #[automatically_derived]
pub mod rhai_fn_increment { pub mod rhai_fn_increment {
use super::*; use super::*;
struct Token(); struct Token();
@ -641,7 +641,7 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(unused)] #[automatically_derived]
pub mod rhai_fn_special_print { pub mod rhai_fn_special_print {
use super::*; use super::*;
struct Token(); struct Token();