diff --git a/codegen/src/module.rs b/codegen/src/module.rs index d89bfbb9..85b1278c 100644 --- a/codegen/src/module.rs +++ b/codegen/src/module.rs @@ -140,12 +140,13 @@ impl Parse for Module { ref expr, ident, attrs, + ty, .. }) => { // #[cfg] attributes are not allowed on const declarations crate::attrs::deny_cfg_attr(&attrs)?; if let syn::Visibility::Public(_) = vis { - consts.push((ident.to_string(), expr.as_ref().clone())); + consts.push((ident.to_string(), ty.clone(), expr.as_ref().clone())); } } _ => {} diff --git a/codegen/src/rhai_module.rs b/codegen/src/rhai_module.rs index 911c7edc..165f7438 100644 --- a/codegen/src/rhai_module.rs +++ b/codegen/src/rhai_module.rs @@ -6,7 +6,7 @@ use crate::attrs::ExportScope; use crate::function::ExportedFn; use crate::module::Module; -pub(crate) type ExportedConst = (String, syn::Expr); +pub(crate) type ExportedConst = (String, Box, syn::Expr); pub(crate) fn generate_body( fns: &mut [ExportedFn], @@ -20,11 +20,11 @@ pub(crate) fn generate_body( let mut set_mod_blocks: Vec = Vec::new(); let str_type_path = syn::parse2::(quote! { str }).unwrap(); - for (const_name, const_expr) in consts { + for (const_name, const_type, const_expr) in consts { let const_literal = syn::LitStr::new(&const_name, proc_macro2::Span::call_site()); set_const_stmts.push( syn::parse2::(quote! { - m.set_var(#const_literal, #const_expr); + m.set_var(#const_literal, (#const_expr) as #const_type); }) .unwrap(), ); diff --git a/tests/plugins.rs b/tests/plugins.rs index 928f0381..2bee84e4 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -11,7 +11,7 @@ mod test { pub mod special_array_package { use rhai::{Array, INT}; - pub const MYSTIC_NUMBER: INT = 42 as INT; + pub const MYSTIC_NUMBER: INT = 42; #[cfg(not(feature = "no_object"))] pub mod feature {