Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced DynEq + DynHash bounds for MacroPlugin, InlineMacroExprPlugin and AnalyzerPlugin traits #6839

Closed
wants to merge 1 commit into from
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
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ const_format = "0.2.32"
convert_case = "0.6.0"
derivative = "2.2.0"
diffy = "0.3.0"
dyn-eq = "0.1.3"
dyn-hash = "0.2.0"
env_logger = "0.11.3"
genco = "0.17.8"
good_lp = { version = "1.8.1", features = ["minilp"], default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-compiler/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use smol_str::SmolStr;
use crate::db::RootDatabase;
use crate::{CompilerConfig, compile_prepared_db_program_artifact};

#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq, Hash)]
pub struct MockExecutablePlugin {}

impl MacroPlugin for MockExecutablePlugin {
Expand Down
6 changes: 5 additions & 1 deletion crates/cairo-lang-defs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ cairo-lang-filesystem = { path = "../cairo-lang-filesystem", version = "~2.9.1"
cairo-lang-parser = { path = "../cairo-lang-parser", version = "~2.9.1" }
cairo-lang-syntax = { path = "../cairo-lang-syntax", version = "~2.9.1" }
cairo-lang-utils = { path = "../cairo-lang-utils", version = "~2.9.1" }
dyn-eq.workspace = true
dyn-hash.workspace = true
itertools = { workspace = true, default-features = true }
salsa.workspace = true
smol_str.workspace = true

[dev-dependencies]
cairo-lang-test-utils = { path = "../cairo-lang-test-utils", features = ["testing"] }
cairo-lang-test-utils = { path = "../cairo-lang-test-utils", features = [
"testing",
] }
env_logger.workspace = true
indoc.workspace = true
pretty_assertions.workspace = true
Expand Down
12 changes: 10 additions & 2 deletions crates/cairo-lang-defs/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use cairo_lang_syntax::node::ast;
use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::ids::SyntaxStablePtrId;
use cairo_lang_utils::ordered_hash_set::OrderedHashSet;
use dyn_eq::DynEq;
use dyn_hash::DynHash;
use smol_str::SmolStr;

/// A trait for arbitrary data that a macro generates along with a generated file.
Expand Down Expand Up @@ -95,9 +97,12 @@ pub struct MacroPluginMetadata<'a> {
pub edition: Edition,
}

dyn_eq::eq_trait_object!(MacroPlugin);
dyn_hash::hash_trait_object!(MacroPlugin);

// TOD(spapini): Move to another place.
/// A trait for a macro plugin: external plugin that generates additional code for items.
pub trait MacroPlugin: std::fmt::Debug + Sync + Send {
pub trait MacroPlugin: std::fmt::Debug + Sync + Send + DynEq + DynHash {
/// Generates code for an item. If no code should be generated returns None.
/// Otherwise, returns (virtual_module_name, module_content), and a virtual submodule
/// with that name and content should be created.
Expand Down Expand Up @@ -149,7 +154,10 @@ pub struct InlinePluginResult {
pub diagnostics: Vec<PluginDiagnostic>,
}

pub trait InlineMacroExprPlugin: std::fmt::Debug + Sync + Send {
dyn_eq::eq_trait_object!(InlineMacroExprPlugin);
dyn_hash::hash_trait_object!(InlineMacroExprPlugin);

pub trait InlineMacroExprPlugin: std::fmt::Debug + Sync + Send + DynEq + DynHash {
/// Generates code for an item. If no code should be generated returns None.
/// Otherwise, returns (virtual_module_name, module_content), and a virtual submodule
/// with that name and content should be created.
Expand Down
6 changes: 3 additions & 3 deletions crates/cairo-lang-defs/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ fn test_submodules() {
]);
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq, Hash)]
struct DummyPlugin;
impl MacroPlugin for DummyPlugin {
fn generate_code(
Expand Down Expand Up @@ -298,7 +298,7 @@ fn test_plugin_remove_original() {

/// If the original item is a function that is marked with #[remove_orig], only removes it, without
/// generating any new code.
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq, Hash)]
struct RemoveOrigPlugin;
impl MacroPlugin for RemoveOrigPlugin {
fn generate_code(
Expand All @@ -324,7 +324,7 @@ impl MacroPlugin for RemoveOrigPlugin {

/// Changes a function 'foo' to 'bar' if annotated with #[foo_to_bar]. Doesn't remove the original
/// item.
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq, Hash)]
struct FooToBarPlugin;
impl MacroPlugin for FooToBarPlugin {
fn generate_code(
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-executable/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const IMPLICIT_PRECEDENCE: &[&str] = &[
"core::circuit::MulMod",
];

#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
struct ExecutablePlugin;

Expand Down Expand Up @@ -137,7 +137,7 @@ impl MacroPlugin for ExecutablePlugin {
}

/// Plugin to add diagnostics on bad `#[executable_raw]` annotations.
#[derive(Default, Debug)]
#[derive(Default, Debug, PartialEq, Eq, Hash)]
struct RawExecutableAnalyzer;

impl AnalyzerPlugin for RawExecutableAnalyzer {
Expand Down
6 changes: 5 additions & 1 deletion crates/cairo-lang-plugins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ cairo-lang-filesystem = { path = "../cairo-lang-filesystem", version = "~2.9.1"
cairo-lang-parser = { path = "../cairo-lang-parser", version = "~2.9.1" }
cairo-lang-syntax = { path = "../cairo-lang-syntax", version = "~2.9.1" }
cairo-lang-utils = { path = "../cairo-lang-utils", version = "~2.9.1" }
dyn-eq.workspace = true
dyn-hash.workspace = true
indent.workspace = true
indoc.workspace = true
itertools = { workspace = true, default-features = true }
Expand All @@ -25,7 +27,9 @@ smol_str.workspace = true
[dev-dependencies]
cairo-lang-debug = { path = "../cairo-lang-debug" }
cairo-lang-parser = { path = "../cairo-lang-parser" }
cairo-lang-test-utils = { path = "../cairo-lang-test-utils", features = ["testing"] }
cairo-lang-test-utils = { path = "../cairo-lang-test-utils", features = [
"testing",
] }
env_logger.workspace = true
indoc.workspace = true
serde_json.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-plugins/src/plugins/compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cairo_lang_syntax::node::{Terminal, TypedSyntaxNode, ast};

/// Plugin that allows writing item level `compile_error!` causing a diagnostic.
/// Useful for testing that `cfg` attributes are valid.
#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct CompileErrorPlugin;

Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-plugins/src/plugins/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use cairo_lang_utils::try_extract_matches;
///
/// Mostly useful for marking test modules to prevent usage of their functionality out of tests,
/// and reduce compilation time when the tests data isn't required.
#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct ConfigPlugin;

Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-plugins/src/plugins/derive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod panic_destruct;
mod partial_eq;
mod serde;

#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct DerivePlugin;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::helpers::QueryAttrs;
use cairo_lang_syntax::node::{Terminal, TypedSyntaxNode, ast};

#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct ExternalAttributesValidationPlugin;

Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-plugins/src/plugins/generate_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cairo_lang_syntax::node::helpers::{BodyItems, GenericParamEx, QueryAttrs};
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::{Terminal, TypedSyntaxNode, ast};

#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct GenerateTraitPlugin;

Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-plugins/src/plugins/panicable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use cairo_lang_utils::try_extract_matches;
use indoc::formatdoc;
use itertools::Itertools;

#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct PanicablePlugin;

Expand Down
10 changes: 8 additions & 2 deletions crates/cairo-lang-semantic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ cairo-lang-plugins = { path = "../cairo-lang-plugins", version = "~2.9.1" }
cairo-lang-proc-macros = { path = "../cairo-lang-proc-macros", version = "~2.9.1" }
cairo-lang-syntax = { path = "../cairo-lang-syntax", version = "~2.9.1" }
cairo-lang-utils = { path = "../cairo-lang-utils", version = "~2.9.1" }
cairo-lang-test-utils = { path = "../cairo-lang-test-utils", version = "~2.9.1", optional = true, features = ["testing"] }
cairo-lang-test-utils = { path = "../cairo-lang-test-utils", version = "~2.9.1", optional = true, features = [
"testing",
] }
dyn-eq.workspace = true
dyn-hash.workspace = true
id-arena.workspace = true
indoc.workspace = true
itertools = { workspace = true, default-features = true }
Expand All @@ -32,7 +36,9 @@ toml = { workspace = true, optional = true }

[dev-dependencies]
cairo-lang-plugins = { path = "../cairo-lang-plugins" }
cairo-lang-test-utils = { path = "../cairo-lang-test-utils", features = ["testing"] }
cairo-lang-test-utils = { path = "../cairo-lang-test-utils", features = [
"testing",
] }
env_logger.workspace = true
log.workspace = true
pretty_assertions.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-semantic/src/diagnostic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn test_missing_module_file() {
// A dummy plugin that adds an inline module with a semantic error (per function
// in the original module).
// Used to test error location inside plugin generated inline modules.
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq, Hash)]
struct AddInlineModuleDummyPlugin;

impl MacroPlugin for AddInlineModuleDummyPlugin {
Expand Down Expand Up @@ -206,7 +206,7 @@ fn test_inline_inline_module_diagnostics() {
);
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq, Hash)]
struct NoU128RenameAnalyzerPlugin;
impl AnalyzerPlugin for NoU128RenameAnalyzerPlugin {
fn diagnostics(&self, db: &dyn SemanticGroup, module_id: ModuleId) -> Vec<PluginDiagnostic> {
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-semantic/src/inline_macros/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::{TypedSyntaxNode, ast};
use indoc::indoc;

#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq, Hash)]
pub struct ArrayMacro;
impl NamedPlugin for ArrayMacro {
const NAME: &'static str = "array";
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-semantic/src/inline_macros/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use cairo_lang_syntax::node::{TypedStablePtr, TypedSyntaxNode, ast};
use indoc::{formatdoc, indoc};

/// Macro for assertion.
#[derive(Default, Debug)]
#[derive(Default, Debug, PartialEq, Eq, Hash)]
pub struct AssertMacro;
impl NamedPlugin for AssertMacro {
const NAME: &'static str = "assert";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cairo_lang_syntax::node::{TypedStablePtr, TypedSyntaxNode, ast};
use indoc::indoc;
use num_bigint::BigInt;

#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq, Hash)]
pub struct ConstevalIntMacro;
impl NamedPlugin for ConstevalIntMacro {
const NAME: &'static str = "consteval_int";
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-semantic/src/inline_macros/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cairo_lang_syntax::node::helpers::WrappedArgListHelper;
use indoc::{formatdoc, indoc};

/// Macro for formatting.
#[derive(Default, Debug)]
#[derive(Default, Debug, PartialEq, Eq, Hash)]
pub struct FormatMacro;
impl NamedPlugin for FormatMacro {
const NAME: &'static str = "format";
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-semantic/src/inline_macros/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn try_handle_simple_panic(
}

/// Macro for panicking with a format string.
#[derive(Default, Debug)]
#[derive(Default, Debug, PartialEq, Eq, Hash)]
pub struct PanicMacro;
impl NamedPlugin for PanicMacro {
const NAME: &'static str = "panic";
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-semantic/src/inline_macros/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use indoc::{formatdoc, indoc};
use super::write::{WriteMacro, WritelnMacro};

/// Macro for printing.
#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq, Hash)]
pub struct PrintMacro;
impl NamedPlugin for PrintMacro {
const NAME: &'static str = "print";
Expand Down Expand Up @@ -50,7 +50,7 @@ impl InlineMacroExprPlugin for PrintMacro {
}

/// Macro for printing with a new line.
#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq, Hash)]
pub struct PrintlnMacro;
impl NamedPlugin for PrintlnMacro {
const NAME: &'static str = "println";
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-semantic/src/inline_macros/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use num_bigint::{BigInt, Sign};
pub const FELT252_BYTES: usize = 31;

/// Macro for writing into a formatter.
#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq, Hash)]
pub struct WriteMacro;
impl NamedPlugin for WriteMacro {
const NAME: &'static str = "write";
Expand Down Expand Up @@ -61,7 +61,7 @@ impl InlineMacroExprPlugin for WriteMacro {
}

/// Macro for writing into a formatter with an additional new line.
#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq, Hash)]
pub struct WritelnMacro;
impl NamedPlugin for WritelnMacro {
const NAME: &'static str = "writeln";
Expand Down
4 changes: 3 additions & 1 deletion crates/cairo-lang-semantic/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ use std::sync::Arc;
use cairo_lang_defs::ids::ModuleId;
use cairo_lang_defs::plugin::{InlineMacroExprPlugin, MacroPlugin, NamedPlugin, PluginDiagnostic};
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use dyn_eq::DynEq;
use dyn_hash::DynHash;

use crate::db::SemanticGroup;

/// A trait for an analyzer plugin: external plugin that generates additional diagnostics for
/// modules.
pub trait AnalyzerPlugin: std::fmt::Debug + Sync + Send {
pub trait AnalyzerPlugin: std::fmt::Debug + Sync + Send + DynEq + DynHash {
/// Runs the plugin on a module.
fn diagnostics(&self, db: &dyn SemanticGroup, module_id: ModuleId) -> Vec<PluginDiagnostic>;
/// Allows this plugin supplies.
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-starknet/src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const ALLOW_NO_DEFAULT_VARIANT_ATTR: &str = "starknet::store_no_default_variant"
const ALLOW_COLLIDING_PATHS_ATTR: &str = "starknet::colliding_storage_paths";

/// Plugin to add diagnostics for contracts for bad ABI generation.
#[derive(Default, Debug)]
#[derive(Default, Debug, PartialEq, Eq, Hash)]
pub struct ABIAnalyzer;

impl AnalyzerPlugin for ABIAnalyzer {
Expand Down Expand Up @@ -88,7 +88,7 @@ fn add_abi_diagnostics(
}

/// Plugin to add diagnostics for contracts with multiple paths to the same location in storage.
#[derive(Default, Debug)]
#[derive(Default, Debug, PartialEq, Eq, Hash)]
pub struct StorageAnalyzer;

impl AnalyzerPlugin for StorageAnalyzer {
Expand Down
Loading
Loading