Skip to content

Commit 9eb61de

Browse files
Using FileInput and VritualFileInput of FileId in inputs to support salsa upgrade
commit-id:b2dbe3c9
1 parent 405b815 commit 9eb61de

File tree

3 files changed

+108
-19
lines changed

3 files changed

+108
-19
lines changed

crates/cairo-lang-defs/src/cache/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1774,7 +1774,7 @@ impl VirtualFileCached {
17741774
name: virtual_file.name.clone(),
17751775
content: String::from(&*(virtual_file.content)),
17761776
code_mappings: virtual_file.code_mappings.to_vec(),
1777-
kind: virtual_file.kind.clone(),
1777+
kind: virtual_file.kind,
17781778
original_item_removed: virtual_file.original_item_removed,
17791779
}
17801780
}

crates/cairo-lang-filesystem/src/db.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::cfg::CfgSet;
1414
use crate::flag::Flag;
1515
use crate::ids::{
1616
BlobId, BlobLongId, CodeMapping, CodeOrigin, CrateId, CrateLongId, Directory, FileId,
17-
FileLongId, FlagId, FlagLongId, VirtualFile,
17+
FileInput, FileLongId, FlagId, FlagLongId, VirtualFile,
1818
};
1919
use crate::span::{FileSummary, TextOffset, TextSpan, TextWidth};
2020

@@ -203,7 +203,7 @@ pub trait FilesGroup: ExternalFiles {
203203
/// Change this mechanism to hold file_overrides on the db struct outside salsa mechanism,
204204
/// and invalidate manually.
205205
#[salsa::input]
206-
fn file_overrides_input(&self) -> Arc<OrderedHashMap<FileLongId, Arc<str>>>;
206+
fn file_overrides_input(&self) -> Arc<OrderedHashMap<FileInput, Arc<str>>>;
207207

208208
/// Interned version of `file_overrides_input`.
209209
fn file_overrides(&self) -> Arc<OrderedHashMap<FileId, Arc<str>>>;
@@ -235,6 +235,9 @@ pub trait FilesGroup: ExternalFiles {
235235
fn blob_content(&self, blob_id: BlobId) -> Option<Arc<[u8]>>;
236236
/// Query to get a compilation flag by its ID.
237237
fn get_flag(&self, id: FlagId) -> Option<Arc<Flag>>;
238+
239+
/// Create an input file from an interned file id.
240+
fn file_input(&self, file_id: FileId) -> FileInput;
238241
}
239242

240243
pub fn init_files_group(db: &mut (dyn FilesGroup + 'static)) {
@@ -249,7 +252,9 @@ pub fn file_overrides(db: &dyn FilesGroup) -> Arc<OrderedHashMap<FileId, Arc<str
249252
let inp = db.file_overrides_input();
250253
Arc::new(
251254
inp.iter()
252-
.map(|(file_id, content)| (file_id.clone().intern(db), content.clone()))
255+
.map(|(file_id, content)| {
256+
(file_id.clone().into_file_long_id(db).intern(db), content.clone())
257+
})
253258
.collect(),
254259
)
255260
}
@@ -268,6 +273,10 @@ pub fn flags(db: &dyn FilesGroup) -> Arc<OrderedHashMap<FlagId, Arc<Flag>>> {
268273
Arc::new(inp.iter().map(|(flag_id, flag)| (flag_id.clone().intern(db), flag.clone())).collect())
269274
}
270275

276+
fn file_input(db: &dyn FilesGroup, file_id: FileId) -> FileInput {
277+
file_id.lookup_intern(db).into_file_input(db)
278+
}
279+
271280
pub fn init_dev_corelib(db: &mut (dyn FilesGroup + 'static), core_lib_dir: PathBuf) {
272281
db.set_crate_config(
273282
CrateId::core(db),
@@ -294,7 +303,7 @@ pub fn init_dev_corelib(db: &mut (dyn FilesGroup + 'static), core_lib_dir: PathB
294303
pub trait FilesGroupEx: FilesGroup {
295304
/// Overrides file content. None value removes the override.
296305
fn override_file_content(&mut self, file: FileId, content: Option<Arc<str>>) {
297-
let file = self.lookup_intern_file(file);
306+
let file = self.file_input(file);
298307
let mut overrides = self.file_overrides_input().as_ref().clone();
299308
match content {
300309
Some(content) => overrides.insert(file, content),

crates/cairo-lang-filesystem/src/ids.rs

Lines changed: 94 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ impl FlagId {
7070
}
7171
}
7272

73+
/// Same as `FileLongId`, but without the interning inside virtual files.
74+
/// This is used to avoid the need to intern the file id inside salsa database inputs.
75+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
76+
pub enum FileInput {
77+
OnDisk(PathBuf),
78+
Virtual(VirtualFileInput),
79+
External(salsa::InternId),
80+
}
81+
82+
impl FileInput {
83+
pub fn into_file_long_id(self, db: &dyn FilesGroup) -> FileLongId {
84+
match self {
85+
FileInput::OnDisk(path) => FileLongId::OnDisk(path),
86+
FileInput::Virtual(vf) => FileLongId::Virtual(vf.into_virtual_file(db)),
87+
FileInput::External(id) => FileLongId::External(id),
88+
}
89+
}
90+
}
91+
7392
/// We use a higher level FileId struct, because not all files are on disk. Some might be online.
7493
/// Some might be virtual/computed on demand.
7594
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
@@ -79,7 +98,7 @@ pub enum FileLongId {
7998
External(salsa::InternId),
8099
}
81100
/// Whether the file holds syntax for a module or for an expression.
82-
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
101+
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
83102
pub enum FileKind {
84103
Module,
85104
Expr,
@@ -139,6 +158,31 @@ impl CodeOrigin {
139158
}
140159
}
141160

161+
/// Same as `VirtualFile`, but without the interning inside virtual files.
162+
/// This is used to avoid the need to intern the file id inside salsa database inputs.
163+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
164+
pub struct VirtualFileInput {
165+
pub parent: Option<Arc<FileInput>>,
166+
pub name: SmolStr,
167+
pub content: Arc<str>,
168+
pub code_mappings: Arc<[CodeMapping]>,
169+
pub kind: FileKind,
170+
pub original_item_removed: bool,
171+
}
172+
173+
impl VirtualFileInput {
174+
fn into_virtual_file(self, db: &dyn FilesGroup) -> VirtualFile {
175+
VirtualFile {
176+
parent: self.parent.map(|id| id.as_ref().clone().into_file_long_id(db).intern(db)),
177+
name: self.name,
178+
content: self.content,
179+
code_mappings: self.code_mappings,
180+
kind: self.kind,
181+
original_item_removed: self.original_item_removed,
182+
}
183+
}
184+
}
185+
142186
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
143187
pub struct VirtualFile {
144188
pub parent: Option<FileId>,
@@ -160,36 +204,72 @@ impl VirtualFile {
160204
self.name.clone().into()
161205
}
162206
}
163-
}
164207

165-
define_short_id!(FileId, FileLongId, FilesGroup, lookup_intern_file, intern_file);
166-
impl FileId {
167-
pub fn new(db: &dyn FilesGroup, path: PathBuf) -> FileId {
168-
FileLongId::OnDisk(path.clean()).intern(db)
208+
fn into_virtual_file_input(self, db: &dyn FilesGroup) -> VirtualFileInput {
209+
VirtualFileInput {
210+
parent: self
211+
.parent
212+
.map(|id| Arc::new(id.clone().lookup_intern(db).into_file_input(db))),
213+
name: self.name,
214+
content: self.content,
215+
code_mappings: self.code_mappings,
216+
kind: self.kind,
217+
original_item_removed: self.original_item_removed,
218+
}
169219
}
170-
pub fn file_name(self, db: &dyn FilesGroup) -> String {
171-
match self.lookup_intern(db) {
220+
}
221+
222+
impl FileLongId {
223+
pub fn file_name(&self, db: &dyn FilesGroup) -> String {
224+
match self {
172225
FileLongId::OnDisk(path) => {
173226
path.file_name().and_then(|x| x.to_str()).unwrap_or("<unknown>").to_string()
174227
}
175228
FileLongId::Virtual(vf) => vf.name.to_string(),
176-
FileLongId::External(external_id) => db.ext_as_virtual(external_id).name.to_string(),
229+
FileLongId::External(external_id) => db.ext_as_virtual(*external_id).name.to_string(),
177230
}
178231
}
179-
pub fn full_path(self, db: &dyn FilesGroup) -> String {
180-
match self.lookup_intern(db) {
232+
pub fn full_path(&self, db: &dyn FilesGroup) -> String {
233+
match self {
181234
FileLongId::OnDisk(path) => path.to_str().unwrap_or("<unknown>").to_string(),
182235
FileLongId::Virtual(vf) => vf.full_path(db),
183-
FileLongId::External(external_id) => db.ext_as_virtual(external_id).full_path(db),
236+
FileLongId::External(external_id) => db.ext_as_virtual(*external_id).full_path(db),
184237
}
185238
}
186-
pub fn kind(self, db: &dyn FilesGroup) -> FileKind {
187-
match self.lookup_intern(db) {
239+
pub fn kind(&self) -> FileKind {
240+
match self {
188241
FileLongId::OnDisk(_) => FileKind::Module,
189242
FileLongId::Virtual(vf) => vf.kind,
190243
FileLongId::External(_) => FileKind::Module,
191244
}
192245
}
246+
247+
pub fn into_file_input(&self, db: &dyn FilesGroup) -> FileInput {
248+
match self {
249+
FileLongId::OnDisk(path) => FileInput::OnDisk(path.clone()),
250+
FileLongId::Virtual(vf) => FileInput::Virtual(vf.clone().into_virtual_file_input(db)),
251+
FileLongId::External(id) => FileInput::External(*id),
252+
}
253+
}
254+
}
255+
256+
define_short_id!(FileId, FileLongId, FilesGroup, lookup_intern_file, intern_file);
257+
impl FileId {
258+
pub fn new(db: &dyn FilesGroup, path: PathBuf) -> FileId {
259+
FileLongId::OnDisk(path.clean()).intern(db)
260+
}
261+
262+
pub fn file_name(self, db: &dyn FilesGroup) -> String {
263+
self.lookup_intern(db).file_name(db)
264+
}
265+
266+
pub fn full_path(self, db: &dyn FilesGroup) -> String {
267+
self.lookup_intern(db).full_path(db)
268+
}
269+
270+
pub fn kind(self, db: &dyn FilesGroup) -> FileKind {
271+
self.lookup_intern(db).kind()
272+
}
193273
}
194274

195275
#[derive(Clone, Debug, Hash, PartialEq, Eq)]

0 commit comments

Comments
 (0)