Implement constants via calling the constant.

This commit is contained in:
Stephen Chung 2020-09-17 11:56:10 +08:00 committed by J Henry Waugh
parent c07c4561eb
commit 92be487e80
4 changed files with 23 additions and 63 deletions

View File

@ -282,7 +282,6 @@ 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)*
#[allow(dead_code)]
pub mod #mod_name { pub mod #mod_name {
#(#orig_content)* #(#orig_content)*
#(#inner_modules)* #(#inner_modules)*

View File

@ -20,11 +20,12 @@ pub(crate) fn generate_body(
let mut set_flattened_mod_blocks: Vec<syn::ExprBlock> = Vec::new(); let mut set_flattened_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_type, const_expr) in consts { for (const_name, _, _) in consts {
let const_literal = syn::LitStr::new(&const_name, proc_macro2::Span::call_site()); let const_literal = syn::LitStr::new(&const_name, proc_macro2::Span::call_site());
let const_ref = syn::Ident::new(&const_name, proc_macro2::Span::call_site());
set_const_stmts.push( set_const_stmts.push(
syn::parse2::<syn::Stmt>(quote! { syn::parse2::<syn::Stmt>(quote! {
m.set_var(#const_literal, (#const_expr) as #const_type); m.set_var(#const_literal, #const_ref);
}) })
.unwrap(), .unwrap(),
); );

View File

@ -237,7 +237,7 @@ mod generate_tests {
let expected = expected.to_string(); let expected = expected.to_string();
if &actual != &expected { if &actual != &expected {
let mut counter = 0; let mut counter = 0;
let iter = actual.chars().zip(expected.chars()).skip_while(|(a, e)| { let _iter = actual.chars().zip(expected.chars()).skip_while(|(a, e)| {
if *a == *e { if *a == *e {
counter += 1; counter += 1;
true true
@ -250,7 +250,7 @@ mod generate_tests {
let (actual_diff, expected_diff) = { let (actual_diff, expected_diff) = {
let mut actual_diff = String::new(); let mut actual_diff = String::new();
let mut expected_diff = String::new(); let mut expected_diff = String::new();
for (a, e) in iter.take(50) { for (a, e) in _iter.take(50) {
actual_diff.push(a); actual_diff.push(a);
expected_diff.push(e); expected_diff.push(e);
} }

View File

@ -218,7 +218,7 @@ mod generate_tests {
let expected = expected.to_string(); let expected = expected.to_string();
if &actual != &expected { if &actual != &expected {
let mut counter = 0; let mut counter = 0;
let iter = actual.chars().zip(expected.chars()).skip_while(|(a, e)| { let _iter = actual.chars().zip(expected.chars()).skip_while(|(a, e)| {
if *a == *e { if *a == *e {
counter += 1; counter += 1;
true true
@ -231,7 +231,7 @@ mod generate_tests {
let (actual_diff, expected_diff) = { let (actual_diff, expected_diff) = {
let mut actual_diff = String::new(); let mut actual_diff = String::new();
let mut expected_diff = String::new(); let mut expected_diff = String::new();
for (a, e) in iter.take(50) { for (a, e) in _iter.take(50) {
actual_diff.push(a); actual_diff.push(a);
expected_diff.push(e); expected_diff.push(e);
} }
@ -252,7 +252,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod empty { pub mod empty {
#[allow(unused_imports)] #[allow(unused_imports)]
use super::*; use super::*;
@ -284,7 +283,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_fn { pub mod one_fn {
pub fn get_mystic_number() -> INT { pub fn get_mystic_number() -> INT {
42 42
@ -347,7 +345,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_fn { pub mod one_fn {
pub fn add_one_to(x: INT) -> INT { pub fn add_one_to(x: INT) -> INT {
x + 1 x + 1
@ -417,7 +414,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod two_fns { pub mod two_fns {
pub fn add_one_to(x: INT) -> INT { pub fn add_one_to(x: INT) -> INT {
x + 1 x + 1
@ -519,7 +515,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_fn { pub mod one_fn {
pub fn add_together(x: INT, y: INT) -> INT { pub fn add_together(x: INT, y: INT) -> INT {
x + y x + y
@ -587,7 +582,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_fn { pub mod one_fn {
pub fn add_together(x: INT, y: INT) -> INT { pub fn add_together(x: INT, y: INT) -> INT {
x + y x + y
@ -661,7 +655,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_constant { pub mod one_constant {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Foo(pub INT); pub struct Foo(pub INT);
@ -677,7 +670,7 @@ mod generate_tests {
} }
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_var("MYSTIC_NUMBER", (Foo(42)) as Foo); m.set_var("MYSTIC_NUMBER", MYSTIC_NUMBER);
if flatten {} else {} if flatten {} else {}
} }
} }
@ -696,7 +689,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_constant { pub mod one_constant {
pub const MYSTIC_NUMBER: INT = 42; pub const MYSTIC_NUMBER: INT = 42;
#[allow(unused_imports)] #[allow(unused_imports)]
@ -709,7 +701,7 @@ mod generate_tests {
} }
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_var("MYSTIC_NUMBER", (42) as INT); m.set_var("MYSTIC_NUMBER", MYSTIC_NUMBER);
if flatten {} else {} if flatten {} else {}
} }
} }
@ -729,7 +721,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_constant { pub mod one_constant {
pub use rhai::INT; pub use rhai::INT;
pub const MYSTIC_NUMBER: INT = 42; pub const MYSTIC_NUMBER: INT = 42;
@ -743,7 +734,7 @@ mod generate_tests {
} }
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_var("MYSTIC_NUMBER", (42) as INT); m.set_var("MYSTIC_NUMBER", MYSTIC_NUMBER);
if flatten {} else {} if flatten {} else {}
} }
} }
@ -764,7 +755,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_fn { pub mod one_fn {
fn get_mystic_number() -> INT { fn get_mystic_number() -> INT {
42 42
@ -800,7 +790,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_fn { pub mod one_fn {
pub fn get_mystic_number() -> INT { pub fn get_mystic_number() -> INT {
42 42
@ -839,7 +828,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_fn { pub mod one_fn {
pub fn get_mystic_number() -> INT { pub fn get_mystic_number() -> INT {
42 42
@ -903,7 +891,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_constant { pub mod one_constant {
const MYSTIC_NUMBER: INT = 42; const MYSTIC_NUMBER: INT = 42;
#[allow(unused_imports)] #[allow(unused_imports)]
@ -936,7 +923,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod str_fn { pub mod str_fn {
pub fn print_out_to(x: &str) { pub fn print_out_to(x: &str) {
x + 1 x + 1
@ -1001,7 +987,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod ref_fn { pub mod ref_fn {
pub fn increment(x: &mut FLOAT) { pub fn increment(x: &mut FLOAT) {
*x += 1.0 as FLOAT; *x += 1.0 as FLOAT;
@ -1068,9 +1053,7 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_fn { pub mod one_fn {
#[allow(dead_code)]
pub mod it_is { pub mod it_is {
pub fn increment(x: &mut FLOAT) { pub fn increment(x: &mut FLOAT) {
*x += 1.0 as FLOAT; *x += 1.0 as FLOAT;
@ -1155,10 +1138,9 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_fn { pub mod one_fn {
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
#[allow(dead_code)]
pub mod it_is { pub mod it_is {
pub fn increment(x: &mut FLOAT) { pub fn increment(x: &mut FLOAT) {
*x += 1.0 as FLOAT; *x += 1.0 as FLOAT;
@ -1245,7 +1227,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_fn { pub mod one_fn {
pub fn int_foo(x: &mut u64) -> u64 { pub fn int_foo(x: &mut u64) -> u64 {
(*x) * (*x) (*x) * (*x)
@ -1310,7 +1291,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_fn { pub mod one_fn {
pub fn int_foo(x: &mut u64) -> u64 { pub fn int_foo(x: &mut u64) -> u64 {
(*x) * (*x) (*x) * (*x)
@ -1377,7 +1357,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_fn { pub mod one_fn {
pub fn int_foo(x: &mut u64, y: u64) { pub fn int_foo(x: &mut u64, y: u64) {
*x = y * y *x = y * y
@ -1445,7 +1424,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_fn { pub mod one_fn {
pub fn int_foo(x: &mut u64, y: u64) { pub fn int_foo(x: &mut u64, y: u64) {
*x = y * y *x = y * y
@ -1517,7 +1495,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_index_fn { pub mod one_index_fn {
pub fn get_by_index(x: &mut MyCollection, i: u64) -> FLOAT { pub fn get_by_index(x: &mut MyCollection, i: u64) -> FLOAT {
x.get(i) x.get(i)
@ -1586,7 +1563,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_index_fn { pub mod one_index_fn {
pub fn get_by_index(x: &mut MyCollection, i: u64) -> FLOAT { pub fn get_by_index(x: &mut MyCollection, i: u64) -> FLOAT {
x.get(i) x.get(i)
@ -1659,7 +1635,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_index_fn { pub mod one_index_fn {
pub fn set_by_index(x: &mut MyCollection, i: u64, item: FLOAT) { pub fn set_by_index(x: &mut MyCollection, i: u64, item: FLOAT) {
x.entry(i).set(item) x.entry(i).set(item)
@ -1731,7 +1706,6 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_index_fn { pub mod one_index_fn {
pub fn set_by_index(x: &mut MyCollection, i: u64, item: FLOAT) { pub fn set_by_index(x: &mut MyCollection, i: u64, item: FLOAT) {
x.entry(i).set(item) x.entry(i).set(item)
@ -1807,9 +1781,7 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod one_constant { pub mod one_constant {
#[allow(dead_code)]
pub mod it_is { pub mod it_is {
pub const MYSTIC_NUMBER: INT = 42; pub const MYSTIC_NUMBER: INT = 42;
#[allow(unused_imports)] #[allow(unused_imports)]
@ -1822,7 +1794,7 @@ mod generate_tests {
} }
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_var("MYSTIC_NUMBER", (42) as INT); m.set_var("MYSTIC_NUMBER", MYSTIC_NUMBER);
if flatten {} else {} if flatten {} else {}
} }
} }
@ -1863,9 +1835,7 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod two_constants { pub mod two_constants {
#[allow(dead_code)]
pub mod first_is { pub mod first_is {
pub const MYSTIC_NUMBER: INT = 42; pub const MYSTIC_NUMBER: INT = 42;
#[allow(unused_imports)] #[allow(unused_imports)]
@ -1878,11 +1848,10 @@ mod generate_tests {
} }
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_var("MYSTIC_NUMBER", (42) as INT); m.set_var("MYSTIC_NUMBER", MYSTIC_NUMBER);
if flatten {} else {} if flatten {} else {}
} }
} }
#[allow(dead_code)]
pub mod second_is { pub mod second_is {
pub const SPECIAL_CPU_NUMBER: INT = 68000; pub const SPECIAL_CPU_NUMBER: INT = 68000;
#[allow(unused_imports)] #[allow(unused_imports)]
@ -1895,7 +1864,7 @@ mod generate_tests {
} }
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_var("SPECIAL_CPU_NUMBER", (68000) as INT); m.set_var("SPECIAL_CPU_NUMBER", SPECIAL_CPU_NUMBER);
if flatten {} else {} if flatten {} else {}
} }
} }
@ -1957,16 +1926,12 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
#[allow(dead_code)]
pub mod heap_root { pub mod heap_root {
pub const VALUE: INT = 100; pub const VALUE: INT = 100;
#[allow(dead_code)]
pub mod left { pub mod left {
pub const VALUE: INT = 19; pub const VALUE: INT = 19;
#[allow(dead_code)]
pub mod left { pub mod left {
pub const VALUE: INT = 17; pub const VALUE: INT = 17;
#[allow(dead_code)]
pub mod left { pub mod left {
pub const VALUE: INT = 2; pub const VALUE: INT = 2;
#[allow(unused_imports)] #[allow(unused_imports)]
@ -1979,11 +1944,10 @@ mod generate_tests {
} }
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_var("VALUE", (2) as INT); m.set_var("VALUE", VALUE);
if flatten {} else {} if flatten {} else {}
} }
} }
#[allow(dead_code)]
pub mod right { pub mod right {
pub const VALUE: INT = 7; pub const VALUE: INT = 7;
#[allow(unused_imports)] #[allow(unused_imports)]
@ -1996,7 +1960,7 @@ mod generate_tests {
} }
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_var("VALUE", (7) as INT); m.set_var("VALUE", VALUE);
if flatten {} else {} if flatten {} else {}
} }
} }
@ -2010,7 +1974,7 @@ mod generate_tests {
} }
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_var("VALUE", (17) as INT); m.set_var("VALUE", VALUE);
if flatten { if flatten {
{ self::left::rhai_generate_into_module(m, flatten); } { self::left::rhai_generate_into_module(m, flatten); }
@ -2021,7 +1985,6 @@ mod generate_tests {
} }
} }
} }
#[allow(dead_code)]
pub mod right { pub mod right {
pub const VALUE: INT = 3; pub const VALUE: INT = 3;
#[allow(unused_imports)] #[allow(unused_imports)]
@ -2034,7 +1997,7 @@ mod generate_tests {
} }
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_var("VALUE", (3) as INT); m.set_var("VALUE", VALUE);
if flatten {} else {} if flatten {} else {}
} }
} }
@ -2048,7 +2011,7 @@ mod generate_tests {
} }
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_var("VALUE", (19) as INT); m.set_var("VALUE", VALUE);
if flatten { if flatten {
{ self::left::rhai_generate_into_module(m, flatten); } { self::left::rhai_generate_into_module(m, flatten); }
@ -2059,10 +2022,8 @@ mod generate_tests {
} }
} }
} }
#[allow(dead_code)]
pub mod right { pub mod right {
pub const VALUE: INT = 36; pub const VALUE: INT = 36;
#[allow(dead_code)]
pub mod left { pub mod left {
pub const VALUE: INT = 25; pub const VALUE: INT = 25;
#[allow(unused_imports)] #[allow(unused_imports)]
@ -2075,11 +2036,10 @@ mod generate_tests {
} }
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_var("VALUE", (25) as INT); m.set_var("VALUE", VALUE);
if flatten {} else {} if flatten {} else {}
} }
} }
#[allow(dead_code)]
pub mod right { pub mod right {
pub const VALUE: INT = 1; pub const VALUE: INT = 1;
#[allow(unused_imports)] #[allow(unused_imports)]
@ -2092,7 +2052,7 @@ mod generate_tests {
} }
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_var("VALUE", (1) as INT); m.set_var("VALUE", VALUE);
if flatten {} else {} if flatten {} else {}
} }
} }
@ -2106,7 +2066,7 @@ mod generate_tests {
} }
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_var("VALUE", (36) as INT); m.set_var("VALUE", VALUE);
if flatten { if flatten {
{ self::left::rhai_generate_into_module(m, flatten); } { self::left::rhai_generate_into_module(m, flatten); }
@ -2127,7 +2087,7 @@ mod generate_tests {
} }
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_var("VALUE", (100) as INT); m.set_var("VALUE", VALUE);
if flatten { if flatten {
{ self::left::rhai_generate_into_module(m, flatten); } { self::left::rhai_generate_into_module(m, flatten); }