Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/lintspec-core/src/definitions/constructor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parsing and validation of constructors.
use crate::{
interner::{INTERNER, Symbol},
interner::{Symbol, get_or_intern_static},
lint::{CheckNoticeAndDev, CheckParams, ItemDiagnostics},
natspec::NatSpec,
};
Expand Down Expand Up @@ -34,7 +34,7 @@ impl SourceItem for ConstructorDefinition {
}

fn name(&self) -> Symbol {
INTERNER.get_or_intern_static("constructor")
get_or_intern_static("constructor")
}

fn span(&self) -> TextRange {
Expand All @@ -48,7 +48,7 @@ impl Validate for ConstructorDefinition {
let mut out = ItemDiagnostics {
parent: self.parent(),
item_type: self.item_type(),
name: self.name().resolve_with(&INTERNER),
name: self.name().resolve(),
span: self.span(),
diags: vec![],
};
Expand Down
4 changes: 2 additions & 2 deletions crates/lintspec-core/src/definitions/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parsing and validation of contract definitions.
use crate::{
interner::{INTERNER, Symbol},
interner::Symbol,
lint::{CheckAuthor, CheckNoticeAndDev, CheckTitle, ItemDiagnostics},
natspec::NatSpec,
};
Expand Down Expand Up @@ -46,7 +46,7 @@ impl Validate for ContractDefinition {
let mut out = ItemDiagnostics {
parent: self.parent(),
item_type: self.item_type(),
name: self.name().resolve_with(&INTERNER),
name: self.name().resolve(),
span: self.span(),
diags: vec![],
};
Expand Down
4 changes: 2 additions & 2 deletions crates/lintspec-core/src/definitions/enumeration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parsing and validation of enum definitions.
use crate::{
interner::{INTERNER, Symbol},
interner::Symbol,
lint::{CheckNoticeAndDev, CheckParams, ItemDiagnostics},
natspec::NatSpec,
};
Expand Down Expand Up @@ -52,7 +52,7 @@ impl Validate for EnumDefinition {
let mut out = ItemDiagnostics {
parent: self.parent(),
item_type: self.item_type(),
name: self.name().resolve_with(&INTERNER),
name: self.name().resolve(),
span: self.span(),
diags: vec![],
};
Expand Down
4 changes: 2 additions & 2 deletions crates/lintspec-core/src/definitions/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parsing and validation of error definitions.
use crate::{
interner::{INTERNER, Symbol},
interner::Symbol,
lint::{CheckNoticeAndDev, CheckParams, ItemDiagnostics},
natspec::NatSpec,
};
Expand Down Expand Up @@ -52,7 +52,7 @@ impl Validate for ErrorDefinition {
let mut out = ItemDiagnostics {
parent: self.parent(),
item_type: self.item_type(),
name: self.name().resolve_with(&INTERNER),
name: self.name().resolve(),
span: self.span(),
diags: vec![],
};
Expand Down
4 changes: 2 additions & 2 deletions crates/lintspec-core/src/definitions/event.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parsing and validation of event definitions.
use crate::{
interner::{INTERNER, Symbol},
interner::Symbol,
lint::{CheckNoticeAndDev, CheckParams, ItemDiagnostics},
natspec::NatSpec,
};
Expand Down Expand Up @@ -52,7 +52,7 @@ impl Validate for EventDefinition {
let mut out = ItemDiagnostics {
parent: self.parent(),
item_type: self.item_type(),
name: self.name().resolve_with(&INTERNER),
name: self.name().resolve(),
span: self.span(),
diags: vec![],
};
Expand Down
4 changes: 2 additions & 2 deletions crates/lintspec-core/src/definitions/function.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parsing and validation of function definitions.
use crate::{
interner::{INTERNER, Symbol},
interner::Symbol,
lint::{CheckNoticeAndDev, CheckParams, CheckReturns, Diagnostic, ItemDiagnostics},
natspec::{NatSpec, NatSpecKind},
};
Expand Down Expand Up @@ -80,7 +80,7 @@ impl SourceItem for FunctionDefinition {

impl Validate for FunctionDefinition {
fn validate(&self, options: &ValidationOptions) -> ItemDiagnostics {
let name = self.name.resolve_with(&INTERNER);
let name = self.name.resolve();
let mut out = ItemDiagnostics {
parent: self.parent(),
item_type: self.item_type(),
Expand Down
4 changes: 2 additions & 2 deletions crates/lintspec-core/src/definitions/interface.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parsing and validation of interface definitions.
use crate::{
interner::{INTERNER, Symbol},
interner::Symbol,
lint::{CheckAuthor, CheckNoticeAndDev, CheckTitle, ItemDiagnostics},
natspec::NatSpec,
};
Expand Down Expand Up @@ -46,7 +46,7 @@ impl Validate for InterfaceDefinition {
let mut out = ItemDiagnostics {
parent: self.parent(),
item_type: self.item_type(),
name: self.name().resolve_with(&INTERNER),
name: self.name().resolve(),
span: self.span(),
diags: vec![],
};
Expand Down
4 changes: 2 additions & 2 deletions crates/lintspec-core/src/definitions/library.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parsing and validation of library definitions.
use crate::{
interner::{INTERNER, Symbol},
interner::Symbol,
lint::{CheckAuthor, CheckNoticeAndDev, CheckTitle, ItemDiagnostics},
natspec::NatSpec,
};
Expand Down Expand Up @@ -46,7 +46,7 @@ impl Validate for LibraryDefinition {
let mut out = ItemDiagnostics {
parent: self.parent(),
item_type: self.item_type(),
name: self.name().resolve_with(&INTERNER),
name: self.name().resolve(),
span: self.span(),
diags: vec![],
};
Expand Down
4 changes: 2 additions & 2 deletions crates/lintspec-core/src/definitions/modifier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parsing and validation of modifier definitions.
use crate::{
interner::{INTERNER, Symbol},
interner::Symbol,
lint::{CheckNoticeAndDev, CheckParams, Diagnostic, ItemDiagnostics},
natspec::{NatSpec, NatSpecKind},
};
Expand Down Expand Up @@ -67,7 +67,7 @@ impl Validate for ModifierDefinition {
let mut out = ItemDiagnostics {
parent: self.parent(),
item_type: self.item_type(),
name: self.name().resolve_with(&INTERNER),
name: self.name().resolve(),
span: self.span(),
diags: vec![],
};
Expand Down
4 changes: 2 additions & 2 deletions crates/lintspec-core/src/definitions/structure.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parsing and validation of struct definitions.
use crate::{
interner::{INTERNER, Symbol},
interner::Symbol,
lint::{CheckNoticeAndDev, CheckParams, ItemDiagnostics},
natspec::NatSpec,
};
Expand Down Expand Up @@ -52,7 +52,7 @@ impl Validate for StructDefinition {
let mut out = ItemDiagnostics {
parent: self.parent(),
item_type: self.item_type(),
name: self.name().resolve_with(&INTERNER),
name: self.name().resolve(),
span: self.span(),
diags: vec![],
};
Expand Down
4 changes: 2 additions & 2 deletions crates/lintspec-core/src/definitions/variable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parsing and validation of state variable declarations.
use crate::{
interner::{INTERNER, Symbol},
interner::Symbol,
lint::{CheckNoticeAndDev, CheckReturns, Diagnostic, ItemDiagnostics},
natspec::{NatSpec, NatSpecKind},
};
Expand Down Expand Up @@ -88,7 +88,7 @@ impl Validate for VariableDeclaration {
let mut out = ItemDiagnostics {
parent: self.parent(),
item_type: self.item_type(),
name: self.name().resolve_with(&INTERNER),
name: self.name().resolve(),
span: self.span(),
diags: vec![],
};
Expand Down
37 changes: 31 additions & 6 deletions crates/lintspec-core/src/interner.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
//! String interner used by data structures.
use std::sync::LazyLock;

/// The global string interner used by the parsers and other data structures.
pub static INTERNER: LazyLock<Interner> = LazyLock::new(Interner::new);
thread_local! {
/// The thread-local string interner used by the parsers and other data structures.
pub static INTERNER: &'static Interner = Box::leak(Box::new(Interner::new()));
}

/// Intern a string reference if needed, and return the corresponding [`Symbol`].
///
/// If the argument has a `'static` lifetime, use [`get_or_intern_static`] instead.
pub fn get_or_intern(s: impl AsRef<str>) -> Symbol {
INTERNER.with(|i| i.get_or_intern(s))
}

/// Intern a static string slice if needed, and return the corresponding [`Symbol`].
#[expect(clippy::must_use_candidate)]
pub fn get_or_intern_static(s: &'static str) -> Symbol {
INTERNER.with(|i| i.get_or_intern_static(s))
}

/// Resolve a symbol to its interned string slice.
#[must_use]
pub fn resolve(sym: Symbol) -> &'static str {
INTERNER.with(|i| i.resolve(sym))
}

/// The reference to an interned string from the [`INTERNER`].
///
/// This type can be compared to rapidly check for string equality.
/// NOTE: This symbol can only be resolved in the same thread where it was created. This is because the interner is
/// thread-local. The resolved string slice has a static lifetime and can be used in other threads after resolution.
///
/// This type can be compared to rapidly check for string equality, within a single thread.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Symbol(inturn::Symbol);

impl Symbol {
/// Resolve the symbol to its interned string slice.
pub fn resolve_with(self, interner: &'static Interner) -> &'static str {
interner.resolve(self)
#[must_use]
pub fn resolve(self) -> &'static str {
INTERNER.with(|i| i.resolve(self))
}
}

Expand Down Expand Up @@ -41,6 +65,7 @@ impl Interner {
}

/// Resolve a symbol to its interned string slice.
#[must_use]
pub fn resolve(&'static self, sym: Symbol) -> &'static str {
self.0.resolve(sym.0)
}
Expand Down
12 changes: 6 additions & 6 deletions crates/lintspec-core/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
config::{Config, ContractRules, FunctionConfig, Req, VariableConfig, WithParamsRules},
definitions::{Identifier, ItemType, Parent},
error::{Error, Result},
interner::INTERNER,
interner::Symbol,
natspec::{NatSpec, NatSpecKind},
parser::{DocumentId, Parse, ParsedDocument},
textindex::TextRange,
Expand Down Expand Up @@ -340,7 +340,7 @@ impl CheckParams<'_> {
fn missing_diags(&self) -> impl Iterator<Item = Diagnostic> {
self.params.iter().filter_map(|p| {
p.name.map(|name| {
let name = INTERNER.resolve(name);
let name = name.resolve();
Diagnostic {
span: p.span.clone(),
message: format!("@param {name} is missing"),
Expand Down Expand Up @@ -369,7 +369,7 @@ impl CheckParams<'_> {
// the item's span is relative to the comment's start offset
let span_start = self.default_span.start + item.span.start;
let span_end = self.default_span.start + item.span.end;
let name = INTERNER.resolve(name);
let name = name.resolve();
Some(Diagnostic {
span: span_start..span_end,
message: format!("extra @param {name}"),
Expand All @@ -384,7 +384,7 @@ impl CheckParams<'_> {
/// Generate diagnostics for wrong number of `@param` comments (missing or more than one)
fn count_diags(&self, natspec: &NatSpec) -> impl Iterator<Item = Diagnostic> {
self.counts(natspec).filter_map(|(param, count)| {
let name = param.name.map_or("unnamed_param", |n| INTERNER.resolve(n));
let name = param.name.map_or("unnamed_param", Symbol::resolve);
match count {
0 => Some(Diagnostic {
span: param.span.clone(),
Expand Down Expand Up @@ -483,7 +483,7 @@ impl CheckReturns<'_> {
fn missing_diags(&self) -> impl Iterator<Item = Diagnostic> {
self.returns.iter().enumerate().map(|(idx, r)| {
let message = if let Some(name) = r.name {
let name = INTERNER.resolve(name);
let name = name.resolve();
format!("@return {name} is missing")
} else if self.is_var {
"@return is missing".to_string()
Expand Down Expand Up @@ -543,7 +543,7 @@ impl CheckReturns<'_> {
.filter_map(move |(idx, ret)| {
if let Some(name) = ret.name {
// Handle named returns
Self::named_count_diag(natspec, ret, INTERNER.resolve(name))
Self::named_count_diag(natspec, ret, name.resolve())
} else {
// Handle unnamed returns
self.unnamed_diag(returns_count, idx, ret)
Expand Down
Loading
Loading