From 554003cf029b832046043cc41bc1a605014a3d56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sm=C3=B3=C5=82ka?= Date: Wed, 18 Dec 2024 12:43:33 +0100 Subject: [PATCH] Introduced the new per-crate plugin logic --- extensions/scarb-doc/src/db.rs | 17 ++++++++--------- scarb/src/compiler/compilers/lib.rs | 18 ++++++++++++++---- scarb/src/compiler/db.rs | 15 ++++++++++----- scarb/src/ops/compile.rs | 2 +- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/extensions/scarb-doc/src/db.rs b/extensions/scarb-doc/src/db.rs index 468951bfb..99534cc35 100644 --- a/extensions/scarb-doc/src/db.rs +++ b/extensions/scarb-doc/src/db.rs @@ -1,5 +1,5 @@ use cairo_lang_compiler::project::{update_crate_roots_from_project_config, ProjectConfig}; -use cairo_lang_defs::db::{try_ext_as_virtual_impl, DefsDatabase, DefsGroup}; +use cairo_lang_defs::db::{init_defs_group, try_ext_as_virtual_impl, DefsDatabase, DefsGroup}; use cairo_lang_doc::db::{DocDatabase, DocGroup}; use cairo_lang_filesystem::cfg::{Cfg, CfgSet}; use cairo_lang_filesystem::db::{ @@ -8,7 +8,9 @@ use cairo_lang_filesystem::db::{ use cairo_lang_filesystem::ids::VirtualFile; use cairo_lang_lowering::db::{LoweringDatabase, LoweringGroup}; use cairo_lang_parser::db::{ParserDatabase, ParserGroup}; -use cairo_lang_semantic::db::{SemanticDatabase, SemanticGroup}; +use cairo_lang_semantic::db::{ + init_semantic_group, PluginSuiteInput, SemanticDatabase, SemanticGroup, +}; use cairo_lang_semantic::inline_macros::get_default_plugin_suite; use cairo_lang_semantic::plugin::PluginSuite; use cairo_lang_starknet::starknet_plugin_suite; @@ -44,10 +46,13 @@ impl ScarbDocDatabase { }; init_files_group(&mut db); + init_defs_group(&mut db); + init_semantic_group(&mut db); db.set_cfg_set(Self::initial_cfg_set().into()); - db.apply_plugin_suite(plugin_suite); + let interned_plugin_suite = db.intern_plugin_suite(plugin_suite); + db.set_default_plugins_from_suite(interned_plugin_suite); if let Some(config) = project_config { db.apply_project_config(config); @@ -60,12 +65,6 @@ impl ScarbDocDatabase { CfgSet::from_iter([Cfg::name("doc")]) } - fn apply_plugin_suite(&mut self, plugin_suite: PluginSuite) { - self.set_macro_plugins(plugin_suite.plugins); - self.set_inline_macro_plugins(plugin_suite.inline_macro_plugins.into()); - self.set_analyzer_plugins(plugin_suite.analyzer_plugins); - } - fn apply_project_config(&mut self, config: ProjectConfig) { update_crate_roots_from_project_config(self, &config); } diff --git a/scarb/src/compiler/compilers/lib.rs b/scarb/src/compiler/compilers/lib.rs index 458c5f5f9..814c41e50 100644 --- a/scarb/src/compiler/compilers/lib.rs +++ b/scarb/src/compiler/compilers/lib.rs @@ -2,6 +2,9 @@ use anyhow::{Context, Result}; use cairo_lang_compiler::db::RootDatabase; use cairo_lang_compiler::CompilerConfig; use cairo_lang_defs::db::DefsGroup; +use cairo_lang_defs::plugin::MacroPlugin; +use cairo_lang_filesystem::db::FilesGroup; +use cairo_lang_filesystem::ids::CrateLongId; use cairo_lang_sierra::program::VersionedProgram; use cairo_lang_sierra_to_casm::compiler::SierraToCasmConfig; use cairo_lang_sierra_to_casm::metadata::{calc_metadata, calc_metadata_ap_change_only}; @@ -142,15 +145,22 @@ fn validate_compiler_config( unit: &CairoCompilationUnit, ws: &Workspace<'_>, ) { + let main_component = unit.main_component(); + let main_crate_id = db.intern_crate(CrateLongId::Real { + name: main_component.target_name(), + discriminator: main_component.id.to_discriminator(), + }); + // Generally, lib target compilation should be driven by a certain objective (e.g. cairo-run, // test framework, etc.), expressed by the plugin set with executables definition. // This does not apply to debug build (expressed by `replace_ids` flag), // which is a goal by itself. // See starkware-libs/cairo#5440 for more context. - let executable_plugin = db - .macro_plugins() - .iter() - .any(|plugin| !plugin.executable_attributes().is_empty()); + let executable_plugin = db.crate_macro_plugins(main_crate_id).iter().any(|&plugin| { + !db.lookup_intern_macro_plugin(plugin) + .executable_attributes() + .is_empty() + }); if !executable_plugin && !compiler_config.replace_ids { ws.config().ui().warn(formatdoc! {r#" artefacts produced by this build may be hard to utilize due to the build configuration diff --git a/scarb/src/compiler/db.rs b/scarb/src/compiler/db.rs index 429dd5813..6525ff423 100644 --- a/scarb/src/compiler/db.rs +++ b/scarb/src/compiler/db.rs @@ -58,13 +58,13 @@ fn load_plugins( let package_id = plugin_info.package.id; let plugin = ws.config().cairo_plugins().fetch(package_id)?; let instance = plugin.instantiate()?; - builder.with_plugin_suite(instance.plugin_suite()); + builder.with_default_plugin_suite(instance.plugin_suite()); } else { proc_macros.register(plugin_info.package.clone(), ws.config())?; } } let macro_host = Arc::new(proc_macros.into_plugin()?); - builder.with_plugin_suite(ProcMacroHostPlugin::build_plugin_suite(macro_host.clone())); + builder.with_default_plugin_suite(ProcMacroHostPlugin::build_plugin_suite(macro_host.clone())); Ok(macro_host) } @@ -198,10 +198,15 @@ fn build_project_config(unit: &CairoCompilationUnit) -> Result { Ok(project_config) } -pub(crate) fn has_starknet_plugin(db: &RootDatabase) -> bool { - db.macro_plugins() +pub(crate) fn has_starknet_plugin(db: &RootDatabase, component: &CompilationUnitComponent) -> bool { + let crate_id = db.intern_crate(CrateLongId::Real { + name: component.target_name(), + discriminator: component.id.to_discriminator(), + }); + + db.crate_macro_plugins(crate_id) .iter() - .any(|plugin| is_starknet_plugin(&**plugin)) + .any(|&plugin| is_starknet_plugin(&db.lookup_intern_macro_plugin(plugin))) } fn is_starknet_plugin(plugin: &dyn MacroPlugin) -> bool { diff --git a/scarb/src/ops/compile.rs b/scarb/src/ops/compile.rs index 3e63617e8..e41c18528 100644 --- a/scarb/src/ops/compile.rs +++ b/scarb/src/ops/compile.rs @@ -302,7 +302,7 @@ fn check_starknet_dependency( // I think we can get away with emitting false positives for users who write raw contracts // without using Starknet code generators. Such people shouldn't do what they do 😁 if unit.main_component().target_kind() == TargetKind::STARKNET_CONTRACT - && !has_starknet_plugin(db) + && !has_starknet_plugin(db, unit.main_component()) { ws.config().ui().warn(formatdoc! { r#"