Skip to content

Commit

Permalink
fix: Use a static rather than const for DSL V1 Grammar node instance
Browse files Browse the repository at this point in the history
This previously used const for the `fn instance()` meaning it always
returned a fresh copy with a separate allocation. The fixed behaviour
should better match the intent of handing out `Rc`s to a single
instance.

Found with Clippy.
  • Loading branch information
Xanewok committed Oct 31, 2023
1 parent 7bb650b commit 52d65c0
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions crates/codegen/grammar/src/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,14 @@ macro_rules! slang_grammar_definition {
impl $name {
const SOURCE_LOCATION: $crate::SourceLocation = slang_location!();
const NAME: &str = stringify!($name);
const INSTANCE: ::std::cell::OnceCell<std::rc::Rc<Self>> = ::std::cell::OnceCell::new();
fn instance() -> $crate::$trait_ref {
Self::INSTANCE
.get_or_init(::std::default::Default::default)
.clone()
use ::std::rc::Rc;

thread_local! {
static INSTANCE: Rc<$name> = Rc::default();
}

INSTANCE.with(Rc::clone)
}
}

Expand Down

0 comments on commit 52d65c0

Please sign in to comment.