Skip to content

Commit 534075e

Browse files
Add helpers to obtain CrateId of the CompilationUnit (#1970)
**Stack**: - #1921 - #1913 - #1970⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do not merge manually using the UI - doing so may have unexpected results.*
1 parent d4e3dfd commit 534075e

File tree

5 files changed

+19
-29
lines changed

5 files changed

+19
-29
lines changed

scarb/src/compiler/compilation_unit.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use anyhow::{ensure, Result};
22
use cairo_lang_filesystem::cfg::CfgSet;
3-
use cairo_lang_filesystem::db::CrateIdentifier;
3+
use cairo_lang_filesystem::db::{CrateIdentifier, FilesGroup};
4+
use cairo_lang_filesystem::ids::{CrateId, CrateLongId};
45
use itertools::Itertools;
56
use serde::{Deserialize, Serialize};
67
use smol_str::SmolStr;
@@ -95,6 +96,16 @@ pub struct CompilationUnitComponent {
9596
pub dependencies: Vec<CompilationUnitDependency>,
9697
}
9798

99+
impl CompilationUnitComponent {
100+
/// Returns a [`CrateId`] of a crate associated with the [`CompilationUnitComponent`].
101+
pub fn crate_id(&self, db: &dyn FilesGroup) -> CrateId {
102+
db.intern_crate(CrateLongId::Real {
103+
name: self.cairo_package_name(),
104+
discriminator: self.id.to_discriminator(),
105+
})
106+
}
107+
}
108+
98109
/// The kind of the compilation unit dependency.
99110
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
100111
pub enum CompilationUnitDependency {

scarb/src/compiler/db.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ use cairo_lang_defs::db::DefsGroup;
1212
use cairo_lang_defs::ids::ModuleId;
1313
use cairo_lang_defs::plugin::MacroPlugin;
1414
use cairo_lang_filesystem::db::{
15-
AsFilesGroupMut, CrateIdentifier, CrateSettings, DependencySettings, FilesGroup, FilesGroupEx,
15+
AsFilesGroupMut, CrateIdentifier, CrateSettings, DependencySettings, FilesGroupEx,
1616
};
17-
use cairo_lang_filesystem::ids::CrateLongId;
1817
use cairo_lang_semantic::plugin::PluginSuite;
1918
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
2019
use smol_str::SmolStr;
@@ -102,11 +101,8 @@ fn inject_virtual_wrapper_lib(db: &mut RootDatabase, unit: &CairoCompilationUnit
102101
.collect();
103102

104103
for component in components {
105-
let name = component.cairo_package_name();
106-
let crate_id = db.intern_crate(CrateLongId::Real {
107-
name,
108-
discriminator: component.id.to_discriminator(),
109-
});
104+
let crate_id = component.crate_id(db);
105+
110106
let file_stems = component
111107
.targets
112108
.iter()

scarb/src/compiler/helpers.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use cairo_lang_compiler::diagnostics::DiagnosticsReporter;
99
use cairo_lang_compiler::CompilerConfig;
1010
use cairo_lang_diagnostics::{FormattedDiagnosticEntry, Severity};
1111
use cairo_lang_filesystem::db::FilesGroup;
12-
use cairo_lang_filesystem::ids::{CrateId, CrateLongId};
12+
use cairo_lang_filesystem::ids::CrateId;
1313
use itertools::Itertools;
1414
use serde::Serialize;
1515
use std::io::{BufWriter, Write};
@@ -117,11 +117,7 @@ impl From<cairo_lang_lowering::utils::InliningStrategy> for InliningStrategy {
117117

118118
pub fn collect_main_crate_ids(unit: &CairoCompilationUnit, db: &RootDatabase) -> Vec<CrateId> {
119119
let main_component = unit.main_component();
120-
let name = main_component.cairo_package_name();
121-
vec![db.intern_crate(CrateLongId::Real {
122-
discriminator: main_component.id.to_discriminator(),
123-
name,
124-
})]
120+
vec![main_component.crate_id(db)]
125121
}
126122

127123
pub fn write_json(

scarb/src/ops/expand.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ use cairo_lang_defs::db::DefsGroup;
99
use cairo_lang_defs::ids::{LanguageElementId, ModuleId, ModuleItemId};
1010
use cairo_lang_defs::patcher::PatchBuilder;
1111
use cairo_lang_diagnostics::ToOption;
12-
use cairo_lang_filesystem::db::FilesGroup;
13-
use cairo_lang_filesystem::ids::CrateLongId;
1412
use cairo_lang_formatter::{CairoFormatter, FormatOutcome, FormatterConfig};
1513
use cairo_lang_parser::db::ParserGroup;
1614
use cairo_lang_syntax::node::helpers::UsePathEx;
@@ -170,11 +168,7 @@ fn do_expand(
170168
) -> Result<()> {
171169
let ScarbDatabase { db, .. } =
172170
build_scarb_root_database(compilation_unit, ws, Default::default())?;
173-
let name = compilation_unit.main_component().cairo_package_name();
174-
let main_crate_id = db.intern_crate(CrateLongId::Real {
175-
name,
176-
discriminator: compilation_unit.main_component().id.to_discriminator(),
177-
});
171+
let main_crate_id = compilation_unit.main_component().crate_id(&db);
178172
let mut compiler_config = build_compiler_config(&db, compilation_unit, &[main_crate_id], ws);
179173
// Report diagnostics, but do not fail.
180174
let _ = compiler_config.diagnostics_reporter.check(&db);

scarb/src/ops/lint.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use anyhow::anyhow;
1212
use anyhow::Result;
1313
use cairo_lang_defs::db::DefsGroup;
1414
use cairo_lang_diagnostics::Diagnostics;
15-
use cairo_lang_filesystem::db::FilesGroup;
16-
use cairo_lang_filesystem::ids::CrateLongId;
1715
use cairo_lang_semantic::diagnostic::SemanticDiagnosticKind;
1816
use cairo_lang_semantic::{db::SemanticGroup, SemanticDiagnostic};
1917
use cairo_lang_utils::Upcast;
@@ -25,7 +23,6 @@ use cairo_lint_core::{
2523
use itertools::Itertools;
2624
use scarb_ui::components::Status;
2725
use serde::Deserialize;
28-
use smol_str::SmolStr;
2926

3027
use crate::core::{Package, Workspace};
3128

@@ -150,11 +147,7 @@ pub fn lint(opts: LintOptions, ws: &Workspace<'_>) -> Result<()> {
150147
build_scarb_root_database(compilation_unit, ws, additional_plugins)?;
151148

152149
let main_component = compilation_unit.main_component();
153-
154-
let crate_id = db.intern_crate(CrateLongId::Real {
155-
name: SmolStr::new(main_component.target_name()),
156-
discriminator: main_component.id.to_discriminator(),
157-
});
150+
let crate_id = main_component.crate_id(&db);
158151

159152
let diags: Vec<Diagnostics<SemanticDiagnostic>> = db
160153
.crate_modules(crate_id)

0 commit comments

Comments
 (0)