From a56361732e6632780170ba169988ea1195256905 Mon Sep 17 00:00:00 2001 From: Ashy Date: Sun, 23 Oct 2022 20:00:57 +0100 Subject: [PATCH] Improved macro check Reduced odds of making a mistake while reporting deactivated dynamic field features. Also added feature flags to tests. --- crates/rune-macros/Cargo.toml | 3 +++ crates/rune-macros/src/any.rs | 12 +---------- crates/rune-macros/src/context.rs | 36 ++++++++++++++++++++++++++++--- crates/rune/Cargo.toml | 2 +- tests/Cargo.toml | 3 +-- 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/crates/rune-macros/Cargo.toml b/crates/rune-macros/Cargo.toml index 1a340ad0b..1f2bc19f7 100644 --- a/crates/rune-macros/Cargo.toml +++ b/crates/rune-macros/Cargo.toml @@ -14,6 +14,9 @@ description = """ Helper macros for Rune. """ +[features] +dynamic_fields = [] + [dependencies] syn = { version = "1.0.82", features = ["full"] } quote = "1.0.10" diff --git a/crates/rune-macros/src/any.rs b/crates/rune-macros/src/any.rs index e971b558b..ca874e488 100644 --- a/crates/rune-macros/src/any.rs +++ b/crates/rune-macros/src/any.rs @@ -100,17 +100,7 @@ impl Derive { let meta_fields = match attrs.meta_fields { Some(meta_fields) => { - if cfg!(feature = "dynamic_fields") { - quote! {#meta_fields} - } else { - return Err(vec![syn::Error::new( - meta_fields.span(), - format!( - "attempted to set dynamic_fields to `{}` while the feature `dynamic_fields` is disabled.", - meta_fields - ), - )]); - } + quote! {#meta_fields} } None => quote! {Never}, }; diff --git a/crates/rune-macros/src/context.rs b/crates/rune-macros/src/context.rs index ca8a0832f..f4e9b504c 100644 --- a/crates/rune-macros/src/context.rs +++ b/crates/rune-macros/src/context.rs @@ -504,9 +504,39 @@ impl Context { })) if path == DYNAMIC_FIELDS => { attrs.meta_fields = Some(match s.value().as_str() { "never" => format_ident!("Never"), - "only" => format_ident!("Only"), - "first" => format_ident!("First"), - "last" => format_ident!("Last"), + "only" => { + if cfg!(feature = "dynamic_fields") { + format_ident!("Only") + } else { + self.errors.push(syn::Error::new_spanned( + s, + "attempted to set dynamic_fields to `only` while the feature `dynamic_fields` is disabled." + )); + return None; + } + } + "first" => { + if cfg!(feature = "dynamic_fields") { + format_ident!("First") + } else { + self.errors.push(syn::Error::new_spanned( + s, + "attempted to set dynamic_fields to `first` while the feature `dynamic_fields` is disabled." + )); + return None; + } + } + "last" => { + if cfg!(feature = "dynamic_fields") { + format_ident!("Last") + } else { + self.errors.push(syn::Error::new_spanned( + s, + "attempted to set dynamic_fields to `last` while the feature `dynamic_fields` is disabled." + )); + return None; + } + } _ => { self.errors.push(syn::Error::new_spanned( s, diff --git a/crates/rune/Cargo.toml b/crates/rune/Cargo.toml index e66551f93..9b202fed3 100644 --- a/crates/rune/Cargo.toml +++ b/crates/rune/Cargo.toml @@ -19,7 +19,7 @@ default = ["emit"] emit = ["codespan-reporting"] bench = [] workspace = ["toml", "toml-spanned-value", "semver", "relative-path", "serde-hashkey"] -dynamic_fields = [] +dynamic_fields = ["rune-macros/dynamic_fields"] [dependencies] thiserror = "1.0.30" diff --git a/tests/Cargo.toml b/tests/Cargo.toml index d7253e202..11f79e846 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -13,11 +13,10 @@ path = "test.rs" [features] default = ["full"] full = ["rune-modules/full"] -dynamic_fields = ["rune/dynamic_fields"] [dependencies] thiserror = "1.0.30" futures-executor = "0.3.0" -rune = { path = "../crates/rune" } +rune = { path = "../crates/rune", features = ["dynamic_fields"] } rune-modules = { path = "../crates/rune-modules", features = ["capture-io"] }