Skip to content

Commit

Permalink
Added corelib version validation when loading db.
Browse files Browse the repository at this point in the history
commit-id:9cabb842
  • Loading branch information
orizi committed Aug 13, 2024
1 parent 9bbb4d1 commit 9823c3a
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ rayon = "1.10.0"
rstest = "0.19.0"
salsa = "0.16.1"
schemars = { version = "0.8.19", features = ["preserve_order"] }
semver = "1.0.23"
serde = { version = "1.0.200", default-features = false, features = ["derive"] }
serde_json = "1.0.116"
sha2 = "0.10.8"
Expand Down
1 change: 1 addition & 0 deletions corelib/cairo_project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
core = "src"

[config.global]
version = "2.7.1"
edition = "2024_07"

[config.global.experimental_features]
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lang-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cairo-lang-syntax = { path = "../cairo-lang-syntax", version = "~2.7.1" }
cairo-lang-utils = { path = "../cairo-lang-utils", version = "~2.7.1" }
indoc.workspace = true
salsa.workspace = true
semver.workspace = true
smol_str.workspace = true
thiserror.workspace = true

Expand Down
33 changes: 29 additions & 4 deletions crates/cairo-lang-compiler/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::sync::Arc;

use anyhow::{anyhow, Result};
use anyhow::{anyhow, bail, Result};
use cairo_lang_defs::db::{DefsDatabase, DefsGroup};
use cairo_lang_defs::plugin::{InlineMacroExprPlugin, MacroPlugin};
use cairo_lang_filesystem::cfg::CfgSet;
use cairo_lang_filesystem::db::{
init_dev_corelib, init_files_group, AsFilesGroupMut, FilesDatabase, FilesGroup, FilesGroupEx,
CORELIB_CRATE_NAME,
CORELIB_CRATE_NAME, CORELIB_VERSION,
};
use cairo_lang_filesystem::detect::detect_corelib;
use cairo_lang_filesystem::flag::Flag;
use cairo_lang_filesystem::ids::FlagId;
use cairo_lang_filesystem::ids::{CrateLongId, FlagId};
use cairo_lang_lowering::db::{init_lowering_group, LoweringDatabase, LoweringGroup};
use cairo_lang_parser::db::{ParserDatabase, ParserGroup};
use cairo_lang_project::ProjectConfig;
Expand All @@ -20,7 +20,7 @@ use cairo_lang_semantic::plugin::{AnalyzerPlugin, PluginSuite};
use cairo_lang_sierra_generator::db::SierraGenDatabase;
use cairo_lang_syntax::node::db::{SyntaxDatabase, SyntaxGroup};
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::Upcast;
use cairo_lang_utils::{Intern, Upcast};

use crate::project::{update_crate_root, update_crate_roots_from_project_config};
use crate::InliningStrategy;
Expand Down Expand Up @@ -170,11 +170,36 @@ impl RootDatabaseBuilder {
update_crate_root(&mut db, config, CORELIB_CRATE_NAME.into(), corelib.clone());
}
}
validate_corelib(&db)?;

Ok(db)
}
}

/// Validates that the corelib version matches the expected one.
fn validate_corelib(db: &RootDatabase) -> Result<()> {
let Some(config) = db.crate_config(CrateLongId::Real(CORELIB_CRATE_NAME.into()).intern(db))
else {
return Ok(());
};
let Some(found) = config.settings.version else {
return Ok(());
};
let Ok(expected) = semver::Version::parse(CORELIB_VERSION) else {
return Ok(());
};
if found == expected {
return Ok(());
}
let path_part = match config.root {
cairo_lang_filesystem::ids::Directory::Real(path) => {
format!(" for `{}`", path.to_string_lossy())
}
cairo_lang_filesystem::ids::Directory::Virtual { .. } => "".to_string(),
};
bail!("Corelib version mismatch: expected `{expected}`, found `{found}`{path_part}.");
}

impl AsFilesGroupMut for RootDatabase {
fn as_files_group_mut(&mut self) -> &mut (dyn FilesGroup + 'static) {
self
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lang-filesystem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ cairo-lang-debug = { path = "../cairo-lang-debug", version = "~2.7.1" }
cairo-lang-utils = { path = "../cairo-lang-utils", version = "~2.7.1", features = ["serde"] }
path-clean.workspace = true
salsa.workspace = true
semver.workspace = true
serde = { workspace = true, default-features = true }
smol_str.workspace = true

Expand Down
5 changes: 5 additions & 0 deletions crates/cairo-lang-filesystem/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::Arc;

use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::{Intern, LookupIntern, Upcast};
use semver::Version;
use serde::{Deserialize, Serialize};

use crate::cfg::CfgSet;
Expand All @@ -18,6 +19,7 @@ use crate::span::{FileSummary, TextOffset, TextSpan, TextWidth};
mod test;

pub const CORELIB_CRATE_NAME: &str = "core";
pub const CORELIB_VERSION: &str = env!("CARGO_PKG_VERSION");

/// A configuration per crate.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
Expand All @@ -38,6 +40,8 @@ impl CrateConfiguration {
pub struct CrateSettings {
/// The crate's Cairo edition.
pub edition: Edition,
/// The crate's version.
pub version: Option<Version>,

pub cfg_set: Option<CfgSet>,

Expand Down Expand Up @@ -164,6 +168,7 @@ pub fn init_dev_corelib(db: &mut (dyn FilesGroup + 'static), core_lib_dir: PathB
root: Directory::Real(core_lib_dir),
settings: CrateSettings {
edition: Edition::V2024_07,
version: Version::parse(CORELIB_VERSION).ok(),
cfg_set: Default::default(),
experimental_features: ExperimentalFeaturesConfig {
negative_impls: true,
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lang-language-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ cairo-lang-utils = { path = "../cairo-lang-utils", version = "~2.7.1" }
itertools.workspace = true
salsa.workspace = true
scarb-metadata = "1.12"
semver.workspace = true
serde = { workspace = true, default-features = true }
serde_json.workspace = true
smol_str.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions crates/cairo-lang-language-server/src/project/scarb/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use cairo_lang_filesystem::db::{
use cairo_lang_filesystem::ids::{CrateId, CrateLongId, Directory};
use cairo_lang_utils::Intern;
use scarb_metadata::{Metadata, PackageMetadata};
use semver::Version;
use tracing::{debug, warn};

use crate::lang::db::AnalysisDatabase;
Expand Down Expand Up @@ -65,6 +66,7 @@ pub fn update_crate_roots(metadata: &Metadata, db: &mut AnalysisDatabase) {

let settings = CrateSettings {
edition: scarb_package_edition(&package, crate_name),
version: package.map(|p| p.version.clone()),
cfg_set: scarb_cfg_set_to_cairo(
component.cfg.as_ref().unwrap_or(&compilation_unit.cfg),
crate_name,
Expand Down
3 changes: 3 additions & 0 deletions crates/cairo-lang-project/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fn test_serde() {
crates_config: AllCratesConfig {
global: CrateSettings {
edition: Default::default(),
version: Default::default(),
experimental_features: ExperimentalFeaturesConfig::default(),
cfg_set: Default::default(),
},
Expand All @@ -25,6 +26,7 @@ fn test_serde() {
"crate1".into(),
CrateSettings {
edition: Edition::V2023_10,
version: Default::default(),
experimental_features: ExperimentalFeaturesConfig::default(),
cfg_set: Default::default(),
},
Expand All @@ -33,6 +35,7 @@ fn test_serde() {
"crate3".into(),
CrateSettings {
edition: Default::default(),
version: Default::default(),
experimental_features: ExperimentalFeaturesConfig {
negative_impls: true,
coupons: false,
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lang-semantic/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub fn setup_test_crate_ex(
} else {
CrateSettings {
edition: Edition::default(),
version: None,
experimental_features: ExperimentalFeaturesConfig {
negative_impls: true,
coupons: true,
Expand Down

0 comments on commit 9823c3a

Please sign in to comment.