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
…627)

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 authored Nov 2, 2023
1 parent 113dfc9 commit 29ee2e9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
4 changes: 0 additions & 4 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ rustflags = [
"-A",
"clippy::bool_assert_comparison",
"-A",
"clippy::borrow_interior_mutable_const",
"-A",
"clippy::cmp_owned",
"-A",
"clippy::collapsible_if",
"-A",
"clippy::comparison_chain",
"-A",
"clippy::declare_interior_mutable_const",
"-A",
"clippy::enum_variant_names",
"-A",
"clippy::expect_fun_call",
Expand Down
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 29ee2e9

Please sign in to comment.