Skip to content

Commit d48e54d

Browse files
committed
fix(options): Wrap derive output in const _: () = {} to scope __OPTIONS
Two structs deriving SentryOptions in the same module previously produced a duplicate symbol error because __OPTIONS was emitted at module scope with a fixed name. Wrapping all generated items in const _: () = { … } gives each derived struct its own anonymous scope, matching the standard serde-style hygiene idiom.
1 parent 2c4aa24 commit d48e54d

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

  • objectstore-typed-options-derive/src

objectstore-typed-options-derive/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//! Derive macro for [`objectstore_typed_options::SentryOptions`].
1+
//! Derive macro for `SentryOptions`.
22
//!
3-
//! See the [`objectstore-typed-options`] crate for full documentation and usage examples.
3+
//! See the `objectstore-typed-options` crate for full documentation and usage examples.
44
55
use proc_macro::TokenStream;
66
use quote::quote;
@@ -16,13 +16,15 @@ use syn::{DeriveInput, Fields, LitStr, parse_macro_input};
1616
///
1717
/// # Generated code
1818
///
19-
/// For each struct field, `deserialize` calls `Deserialize::deserialize(options.get(NAMESPACE, "<field>")?)`.
19+
/// For each struct field, `deserialize` calls `Deserialize::deserialize(options.get(NAMESPACE,
20+
/// "<field>")?)`.
2021
///
2122
/// Additionally generates:
2223
/// - `SentryOptions` trait impl with `NAMESPACE`, `SCHEMA`, and `deserialize`
2324
/// - Inherent `get() -> Arc<Self>`, `init() -> Result<(), Error>`, and (under `testing` feature)
2425
/// `override_with()`
25-
/// - A module-scoped `OnceLock<ArcSwap<T>>` static for the singleton instance
26+
/// - A `OnceLock<ArcSwap<T>>` static for the singleton instance, wrapped in `const _: () = { … }`
27+
/// to avoid symbol conflicts when multiple structs derive `SentryOptions` in the same module
2628
#[proc_macro_derive(SentryOptions, attributes(sentry_options))]
2729
pub fn derive_sentry_options(input: TokenStream) -> TokenStream {
2830
let input = parse_macro_input!(input as DeriveInput);
@@ -109,6 +111,7 @@ fn expand(input: DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
109111
.collect();
110112

111113
Ok(quote! {
114+
const _: () = {
112115
static __OPTIONS: ::std::sync::OnceLock<
113116
::objectstore_typed_options::arc_swap::ArcSwap<#name>
114117
> = ::std::sync::OnceLock::new();
@@ -224,5 +227,6 @@ fn expand(input: DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
224227
.expect("failed to override options")
225228
}
226229
}
230+
}; // end const _: () = { ... }
227231
})
228232
}

0 commit comments

Comments
 (0)