Merge pull request #42 from jhwgh1968/submodules_cfg

Add #[cfg] support in submodules
This commit is contained in:
Stephen Chung 2020-08-22 15:54:01 +08:00 committed by GitHub
commit 812a1e690b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 317 additions and 46 deletions

View File

@ -148,6 +148,16 @@ impl Parse for ExportedFn {
let entire_span = fn_all.span(); let entire_span = fn_all.span();
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap(); let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
// #[cfg] attributes are not allowed on functions due to what is generated for them
if let Some(cfg_attr) = fn_all.attrs.iter().find(|a| {
a.path
.get_ident()
.map(|i| i.to_string() == "cfg")
.unwrap_or(false)
}) {
return Err(syn::Error::new(cfg_attr.span(), "cfg attributes not allowed on this item"));
}
// Determine if the function is public. // Determine if the function is public.
let is_public = match fn_all.vis { let is_public = match fn_all.vis {
syn::Visibility::Public(_) => true, syn::Visibility::Public(_) => true,
@ -1057,12 +1067,3 @@ mod generate_tests {
assert_streams_eq(item_fn.generate(), expected_tokens); assert_streams_eq(item_fn.generate(), expected_tokens);
} }
} }
#[cfg(test)]
mod ui_tests {
#[test]
fn all() {
let t = trybuild::TestCases::new();
t.compile_fail("ui_tests/*.rs");
}
}

View File

@ -11,11 +11,24 @@ use std::vec as new_vec;
#[cfg(no_std)] #[cfg(no_std)]
use core::mem; use core::mem;
#[cfg(not(no_std))]
use std::mem;
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
fn inner_fn_attributes(f: &mut syn::ItemFn) -> syn::Result<ExportedFnParams> { fn inner_fn_attributes(f: &mut syn::ItemFn) -> syn::Result<ExportedFnParams> {
// #[cfg] attributes are not allowed on objects
if let Some(cfg_attr) = f.attrs.iter().find(|a| {
a.path
.get_ident()
.map(|i| i.to_string() == "cfg")
.unwrap_or(false)
}) {
return Err(syn::Error::new(cfg_attr.span(), "cfg attributes not allowed on this item"));
}
// Find the #[rhai_fn] attribute which will turn be read for the function parameters.
if let Some(rhai_fn_idx) = f.attrs.iter().position(|a| { if let Some(rhai_fn_idx) = f.attrs.iter().position(|a| {
a.path a.path
.get_ident() .get_ident()
@ -186,7 +199,7 @@ impl Parse for Module {
fn parse(input: ParseStream) -> syn::Result<Self> { fn parse(input: ParseStream) -> syn::Result<Self> {
let mut mod_all: syn::ItemMod = input.parse()?; let mut mod_all: syn::ItemMod = input.parse()?;
let fns: Vec<_>; let fns: Vec<_>;
let consts: Vec<_>; let mut consts: Vec<_> = new_vec![];
let mut submodules: Vec<_> = Vec::new(); let mut submodules: Vec<_> = Vec::new();
if let Some((_, ref mut content)) = mod_all.content { if let Some((_, ref mut content)) = mod_all.content {
// Gather and parse functions. // Gather and parse functions.
@ -210,24 +223,33 @@ impl Parse for Module {
.map(|_| vec) .map(|_| vec)
})?; })?;
// Gather and parse constants definitions. // Gather and parse constants definitions.
consts = content for item in content.iter() {
.iter() match item {
.filter_map(|item| match item {
syn::Item::Const(syn::ItemConst { syn::Item::Const(syn::ItemConst {
vis, vis,
ref expr, ref expr,
ident, ident,
attrs,
.. ..
}) => { }) => {
// #[cfg] attributes are not allowed on const declarations
if let Some(cfg_attr) = attrs.iter().find(|a| {
a.path
.get_ident()
.map(|i| i.to_string() == "cfg")
.unwrap_or(false)
}) {
return Err(syn::Error::new(
cfg_attr.span(),
"cfg attributes not allowed on this item"));
}
if let syn::Visibility::Public(_) = vis { if let syn::Visibility::Public(_) = vis {
Some((ident.to_string(), expr.as_ref().clone())) consts.push((ident.to_string(), expr.as_ref().clone()));
} else {
None
} }
},
_ => {},
} }
_ => None, };
})
.collect();
// Gather and parse submodule definitions. // Gather and parse submodule definitions.
// //
// They are actually removed from the module's body, because they will need // They are actually removed from the module's body, because they will need
@ -257,7 +279,6 @@ impl Parse for Module {
} }
} }
} else { } else {
consts = new_vec![];
fns = new_vec![]; fns = new_vec![];
} }
Ok(Module { Ok(Module {
@ -271,6 +292,10 @@ impl Parse for Module {
} }
impl Module { impl Module {
pub fn attrs(&self) -> Option<&Vec<syn::Attribute>> {
self.mod_all.as_ref().map(|m| &m.attrs)
}
pub fn module_name(&self) -> Option<&syn::Ident> { pub fn module_name(&self) -> Option<&syn::Ident> {
self.mod_all.as_ref().map(|m| &m.ident) self.mod_all.as_ref().map(|m| &m.ident)
} }
@ -313,8 +338,10 @@ impl Module {
let mut mod_all = mod_all.unwrap(); let mut mod_all = mod_all.unwrap();
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::replace(&mut mod_all.attrs, Vec::with_capacity(0));
Ok(quote! { Ok(quote! {
#(#mod_attrs)*
pub mod #mod_name { pub mod #mod_name {
#(#orig_content)* #(#orig_content)*
#(#inner_modules)* #(#inner_modules)*
@ -1101,7 +1128,82 @@ mod generate_tests {
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
{ m.set_sub_module("it_is", self::it_is::rhai_module_generate()); }
m
}
}
};
let item_mod = syn::parse2::<Module>(input_tokens).unwrap();
assert_streams_eq(item_mod.generate(), expected_tokens);
}
#[test]
fn one_fn_with_cfg_module() {
let input_tokens: TokenStream = quote! {
pub mod one_fn {
#[cfg(not(feature = "no_float"))]
pub mod it_is {
pub fn increment(x: &mut FLOAT) {
*x += 1.0 as FLOAT;
}
}
}
};
let expected_tokens = quote! {
pub mod one_fn {
#[cfg(not(feature = "no_float"))]
pub mod it_is {
pub fn increment(x: &mut FLOAT) {
*x += 1.0 as FLOAT;
}
#[allow(unused_imports)]
use super::*;
#[allow(unused_mut)]
pub fn rhai_module_generate() -> Module {
let mut m = Module::new();
m.set_fn("increment", FnAccess::Public,
&[core::any::TypeId::of::<FLOAT>()],
CallableFunction::from_plugin(increment_token()));
m
}
#[allow(non_camel_case_types)]
struct increment_token();
impl PluginFunction for increment_token {
fn call(&self,
args: &mut [&mut Dynamic], pos: Position
) -> Result<Dynamic, Box<EvalAltResult>> {
debug_assert_eq!(args.len(), 1usize,
"wrong arg count: {} != {}", args.len(), 1usize);
let arg0: &mut _ = &mut args[0usize].write_lock::<FLOAT>().unwrap();
Ok(Dynamic::from(increment(arg0)))
}
fn is_method_call(&self) -> bool { true }
fn is_varadic(&self) -> bool { false }
fn clone_boxed(&self) -> Box<dyn PluginFunction> {
Box::new(increment_token())
}
fn input_types(&self) -> Box<[TypeId]> {
new_vec![TypeId::of::<FLOAT>()].into_boxed_slice()
}
}
pub fn increment_token_callable() -> CallableFunction {
CallableFunction::from_plugin(increment_token())
}
pub fn increment_token_input_types() -> Box<[TypeId]> {
increment_token().input_types()
}
}
#[allow(unused_imports)]
use super::*;
#[allow(unused_mut)]
pub fn rhai_module_generate() -> Module {
let mut m = Module::new();
#[cfg(not(feature = "no_float"))] {
m.set_sub_module("it_is", self::it_is::rhai_module_generate()); m.set_sub_module("it_is", self::it_is::rhai_module_generate());
}
m m
} }
} }
@ -1139,7 +1241,7 @@ mod generate_tests {
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
m.set_sub_module("it_is", self::it_is::rhai_module_generate()); { m.set_sub_module("it_is", self::it_is::rhai_module_generate()); }
m m
} }
} }
@ -1191,8 +1293,8 @@ mod generate_tests {
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
m.set_sub_module("first_is", self::first_is::rhai_module_generate()); { m.set_sub_module("first_is", self::first_is::rhai_module_generate()); }
m.set_sub_module("second_is", self::second_is::rhai_module_generate()); { m.set_sub_module("second_is", self::second_is::rhai_module_generate()); }
m m
} }
} }
@ -1269,8 +1371,8 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
m.set_var("VALUE", 17); m.set_var("VALUE", 17);
m.set_sub_module("left", self::left::rhai_module_generate()); { m.set_sub_module("left", self::left::rhai_module_generate()); }
m.set_sub_module("right", self::right::rhai_module_generate()); { m.set_sub_module("right", self::right::rhai_module_generate()); }
m m
} }
} }
@ -1291,8 +1393,8 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
m.set_var("VALUE", 19); m.set_var("VALUE", 19);
m.set_sub_module("left", self::left::rhai_module_generate()); { m.set_sub_module("left", self::left::rhai_module_generate()); }
m.set_sub_module("right", self::right::rhai_module_generate()); { m.set_sub_module("right", self::right::rhai_module_generate()); }
m m
} }
} }
@ -1326,8 +1428,8 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
m.set_var("VALUE", 36); m.set_var("VALUE", 36);
m.set_sub_module("left", self::left::rhai_module_generate()); { m.set_sub_module("left", self::left::rhai_module_generate()); }
m.set_sub_module("right", self::right::rhai_module_generate()); { m.set_sub_module("right", self::right::rhai_module_generate()); }
m m
} }
} }
@ -1337,8 +1439,8 @@ mod generate_tests {
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
m.set_var("VALUE", 100); m.set_var("VALUE", 100);
m.set_sub_module("left", self::left::rhai_module_generate()); { m.set_sub_module("left", self::left::rhai_module_generate()); }
m.set_sub_module("right", self::right::rhai_module_generate()); { m.set_sub_module("right", self::right::rhai_module_generate()); }
m m
} }
} }

View File

@ -12,7 +12,7 @@ pub(crate) fn generate_body(
) -> proc_macro2::TokenStream { ) -> proc_macro2::TokenStream {
let mut set_fn_stmts: Vec<syn::Stmt> = Vec::new(); let mut set_fn_stmts: Vec<syn::Stmt> = Vec::new();
let mut set_const_stmts: Vec<syn::Stmt> = Vec::new(); let mut set_const_stmts: Vec<syn::Stmt> = Vec::new();
let mut add_mod_stmts: Vec<syn::Stmt> = Vec::new(); let mut add_mod_blocks: Vec<syn::ExprBlock> = Vec::new();
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap(); let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
for (const_name, const_expr) in consts { for (const_name, const_expr) in consts {
@ -32,9 +32,16 @@ pub(crate) fn generate_body(
} else { } else {
syn::LitStr::new(&module_name.to_string(), proc_macro2::Span::call_site()) syn::LitStr::new(&module_name.to_string(), proc_macro2::Span::call_site())
}; };
add_mod_stmts.push( let cfg_attrs: Vec<&syn::Attribute> = itemmod.attrs().unwrap().iter().filter(|&a| {
syn::parse2::<syn::Stmt>(quote! { a.path.get_ident()
.map(|i| i.to_string() == "cfg")
.unwrap_or(false)
}).collect();
add_mod_blocks.push(
syn::parse2::<syn::ExprBlock>(quote! {
#(#cfg_attrs)* {
m.set_sub_module(#exported_name, self::#module_name::rhai_module_generate()); m.set_sub_module(#exported_name, self::#module_name::rhai_module_generate());
}
}) })
.unwrap(), .unwrap(),
); );
@ -117,7 +124,7 @@ pub(crate) fn generate_body(
let mut m = Module::new(); let mut m = Module::new();
#(#set_fn_stmts)* #(#set_fn_stmts)*
#(#set_const_stmts)* #(#set_const_stmts)*
#(#add_mod_stmts)* #(#add_mod_blocks)*
m m
} }
} }

View File

@ -0,0 +1,8 @@
#![cfg(test)]
mod ui_tests {
#[test]
fn all() {
let t = trybuild::TestCases::new();
t.compile_fail("ui_tests/*.rs");
}
}

View File

@ -0,0 +1,25 @@
use rhai::plugin::*;
#[derive(Clone)]
pub struct Point {
x: f32,
y: f32,
}
#[cfg(not(feature = "foo"))]
#[export_fn]
pub fn test_fn(input: Point) -> bool {
input.x > input.y
}
fn main() {
let n = Point {
x: 0.0,
y: 10.0,
};
if test_fn(n) {
println!("yes");
} else {
println!("no");
}
}

View File

@ -0,0 +1,11 @@
error: cfg attributes not allowed on this item
--> $DIR/export_fn_cfg.rs:9:1
|
9 | #[cfg(not(feature = "foo"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0425]: cannot find function `test_fn` in this scope
--> $DIR/export_fn_cfg.rs:20:8
|
20 | if test_fn(n) {
| ^^^^^^^ not found in this scope

View File

@ -0,0 +1,31 @@
use rhai::plugin::*;
#[derive(Clone)]
pub struct Point {
x: f32,
y: f32,
}
#[export_module]
pub mod test_module {
use rhai::FLOAT;
#[cfg(feature = "foo")]
pub const MAGIC: FLOAT = 42.0 as FLOAT;
pub fn test_fn(input: Point) -> bool {
input.x > input.y
}
}
fn main() {
let n = Point {
x: 0.0,
y: 10.0,
};
if test_module::test_fn(n) {
println!("yes");
} else {
println!("no");
}
}

View File

@ -0,0 +1,11 @@
error: cfg attributes not allowed on this item
--> $DIR/module_cfg_const.rs:13:5
|
13 | #[cfg(feature = "foo")]
| ^^^^^^^^^^^^^^^^^^^^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/module_cfg_const.rs:26:8
|
26 | if test_module::test_fn(n) {
| ^^^^^^^^^^^ use of undeclared type or module `test_module`

View File

@ -0,0 +1,27 @@
use rhai::plugin::*;
#[derive(Clone)]
pub struct Point {
x: f32,
y: f32,
}
#[export_module]
pub mod test_module {
#[cfg(not(feature = "foo"))]
pub fn test_fn(input: Point) -> bool {
input.x > input.y
}
}
fn main() {
let n = Point {
x: 0.0,
y: 10.0,
};
if test_module::test_fn(n) {
println!("yes");
} else {
println!("no");
}
}

View File

@ -0,0 +1,11 @@
error: cfg attributes not allowed on this item
--> $DIR/module_cfg_fn.rs:11:5
|
11 | #[cfg(not(feature = "foo"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0433]: failed to resolve: use of undeclared type or module `test_module`
--> $DIR/module_cfg_fn.rs:22:8
|
22 | if test_module::test_fn(n) {
| ^^^^^^^^^^^ use of undeclared type or module `test_module`

View File

@ -0,0 +1,29 @@
use rhai::plugin::*;
#[derive(Clone)]
pub struct Point {
x: f32,
y: f32,
}
#[export_module]
pub mod test_module {
#[cfg(feature = "unset_feature")]
pub mod test_mod {
pub fn test_fn(input: Point) -> bool {
input.x > input.y
}
}
}
fn main() {
let n = Point {
x: 0.0,
y: 10.0,
};
if test_module::test_mod::test_fn(n) {
println!("yes");
} else {
println!("no");
}
}

View File

@ -0,0 +1,5 @@
error[E0433]: failed to resolve: could not find `test_mod` in `test_module`
--> $DIR/rhai_mod_inner_cfg_false.rs:24:21
|
24 | if test_module::test_mod::test_fn(n) {
| ^^^^^^^^ could not find `test_mod` in `test_module`

View File

@ -88,9 +88,9 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
{ {
set_exported_fn!(lib, KEYWORD_PRINT, format_map); set_exported_fn!(lib, KEYWORD_PRINT, format_map::format_map);
set_exported_fn!(lib, FN_TO_STRING, format_map); set_exported_fn!(lib, FN_TO_STRING, format_map::format_map);
set_exported_fn!(lib, KEYWORD_DEBUG, format_map); set_exported_fn!(lib, KEYWORD_DEBUG, format_map::format_map);
} }
}); });
@ -154,8 +154,11 @@ fn to_debug<T: Debug>(x: &mut T) -> ImmutableString {
format!("{:?}", x).into() format!("{:?}", x).into()
} }
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[export_fn] mod format_map {
#[inline] use super::*;
fn format_map(x: &mut Map) -> ImmutableString { #[inline]
#[export_fn]
pub fn format_map(x: &mut Map) -> ImmutableString {
format!("#{:?}", x).into() format!("#{:?}", x).into()
}
} }