Skip to content

Commit

Permalink
Remove InputIngot field from InputFile
Browse files Browse the repository at this point in the history
Fixes infinite loop in salsa debug printing
  • Loading branch information
sbillig committed Jan 15, 2025
1 parent 47b7e82 commit 6af6147
Show file tree
Hide file tree
Showing 27 changed files with 215 additions and 249 deletions.
7 changes: 2 additions & 5 deletions crates/common2/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ impl InputIngot {

#[salsa::input]
pub struct InputFile {
/// A ingot id which the file belongs to.
pub ingot: InputIngot,

/// A path to the file from the ingot root directory.
#[return_ref]
pub path: Utf8PathBuf,
Expand All @@ -87,8 +84,8 @@ pub struct InputFile {
}

impl InputFile {
pub fn abs_path(&self, db: &dyn InputDb) -> Utf8PathBuf {
self.ingot(db).path(db).join(self.path(db))
pub fn abs_path(&self, db: &dyn InputDb, ingot: InputIngot) -> Utf8PathBuf {
ingot.path(db).join(self.path(db))
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/driver2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl DriverDataBase {
DiagnosticsCollection(pass_manager.run_on_module(top_mod))
}

pub fn standalone(&mut self, file_path: &path::Path, source: &str) -> InputFile {
pub fn standalone(&mut self, file_path: &path::Path, source: &str) -> (InputIngot, InputFile) {
let kind = IngotKind::StandAlone;

// We set the ingot version to 0.0.0 for stand-alone file.
Expand All @@ -84,14 +84,14 @@ impl DriverDataBase {
);

let file_name = root_file.file_name().unwrap().to_str().unwrap();
let input_file = InputFile::new(self, ingot, file_name.into(), source.to_string());
let input_file = InputFile::new(self, file_name.into(), source.to_string());
ingot.set_root_file(self, input_file);
ingot.set_files(self, [input_file].into_iter().collect());
input_file
(ingot, input_file)
}

pub fn top_mod(&self, input: InputFile) -> TopLevelMod {
map_file_to_mod(self, input)
pub fn top_mod(&self, ingot: InputIngot, input: InputFile) -> TopLevelMod {
map_file_to_mod(self, ingot, input)
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/driver2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub fn main() {
let source = std::fs::read_to_string(&args.file_path).unwrap();

let mut db = DriverDataBase::default();
let input_file = db.standalone(path, &source);
let top_mod = db.top_mod(input_file);
let (ingot, file) = db.standalone(path, &source);
let top_mod = db.top_mod(ingot, file);
let diags = db.run_on_top_mod(top_mod);
diags.emit(&db);

Expand Down
4 changes: 2 additions & 2 deletions crates/hir-analysis/tests/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn constraints_standalone(fixture: Fixture<&str>) {
let mut db = HirAnalysisTestDb::default();
let path = Path::new(fixture.path());
let file_name = path.file_name().and_then(|file| file.to_str()).unwrap();
let input = db.new_stand_alone(file_name, fixture.content());
let (top_mod, _) = db.top_mod(input);
let (ingot, file) = db.new_stand_alone(file_name, fixture.content());
let (top_mod, _) = db.top_mod(ingot, file);
db.assert_no_diags(top_mod);
}
4 changes: 2 additions & 2 deletions crates/hir-analysis/tests/def_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn def_analysis_standalone(fixture: Fixture<&str>) {
let mut db = HirAnalysisTestDb::default();
let path = Path::new(fixture.path());
let file_name = path.file_name().and_then(|file| file.to_str()).unwrap();
let input = db.new_stand_alone(file_name, fixture.content());
let (top_mod, _) = db.top_mod(input);
let (ingot, file) = db.new_stand_alone(file_name, fixture.content());
let (top_mod, _) = db.top_mod(ingot, file);
db.assert_no_diags(top_mod);
}
4 changes: 2 additions & 2 deletions crates/hir-analysis/tests/early_path_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ fn early_path_resolution_standalone(fixture: Fixture<&str>) {
let mut db = HirAnalysisTestDb::default();
let path = Path::new(fixture.path());
let file_name = path.file_name().and_then(|file| file.to_str()).unwrap();
let input = db.new_stand_alone(file_name, fixture.content());
let (top_mod, mut prop_formatter) = db.top_mod(input);
let (ingot, file) = db.new_stand_alone(file_name, fixture.content());
let (top_mod, mut prop_formatter) = db.top_mod(ingot, file);
db.assert_no_diags(top_mod);

let mut ctxt = VisitorCtxt::with_top_mod(db.as_hir_db(), top_mod);
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-analysis/tests/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ fn import_standalone(fixture: Fixture<&str>) {
let mut db = HirAnalysisTestDb::default();
let path = Path::new(fixture.path());
let file_name = path.file_name().and_then(|file| file.to_str()).unwrap();
let input = db.new_stand_alone(file_name, fixture.content());
let (top_mod, mut prop_formatter) = db.top_mod(input);
let (ingot, file) = db.new_stand_alone(file_name, fixture.content());
let (top_mod, mut prop_formatter) = db.top_mod(ingot, file);

db.assert_no_diags(top_mod);

Expand Down
6 changes: 3 additions & 3 deletions crates/hir-analysis/tests/repeated_updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ fn test_updated() {
fn foo() {}"#,
];

let input = db.new_stand_alone(file_name, versions[0]);
let (ingot, file) = db.new_stand_alone(file_name, versions[0]);

for version in versions {
{
let top_mod = map_file_to_mod(db.as_lower_hir_db(), input);
let top_mod = map_file_to_mod(db.as_lower_hir_db(), ingot, file);
let mut pass_manager = initialize_pass_manager(&db);
let _ = pass_manager.run_on_module(top_mod);
}

{
input.set_text(&mut db).to(version.into());
file.set_text(&mut db).to(version.into());
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions crates/hir-analysis/tests/test_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,23 @@ impl_db_traits!(
// https://github.com/rust-lang/rust/issues/46379
#[allow(dead_code)]
impl HirAnalysisTestDb {
pub fn new_stand_alone(&mut self, file_name: &str, text: &str) -> InputFile {
pub fn new_stand_alone(&mut self, file_name: &str, text: &str) -> (InputIngot, InputFile) {
let kind = IngotKind::StandAlone;
let version = Version::new(0, 0, 1);
let ingot = InputIngot::new(self, file_name, kind, version, IndexSet::default());
let root = InputFile::new(self, ingot, file_name.into(), text.to_string());
let root = InputFile::new(self, file_name.into(), text.to_string());
ingot.set_root_file(self, root);
ingot.set_files(self, [root].into_iter().collect());
root
(ingot, root)
}

pub fn top_mod(&self, input: InputFile) -> (TopLevelMod, HirPropertyFormatter) {
pub fn top_mod(
&self,
ingot: InputIngot,
input: InputFile,
) -> (TopLevelMod, HirPropertyFormatter) {
let mut prop_formatter = HirPropertyFormatter::default();
let top_mod = self.register_file(&mut prop_formatter, input);
let top_mod = self.register_file(&mut prop_formatter, ingot, input);
(top_mod, prop_formatter)
}

Expand Down Expand Up @@ -104,9 +108,10 @@ impl HirAnalysisTestDb {
fn register_file<'db>(
&'db self,
prop_formatter: &mut HirPropertyFormatter<'db>,
ingot: InputIngot,
input_file: InputFile,
) -> TopLevelMod<'db> {
let top_mod = lower::map_file_to_mod(self, input_file);
let top_mod = lower::map_file_to_mod(self, ingot, input_file);
let path = input_file.path(self);
let text = input_file.text(self);
prop_formatter.register_top_mod(path.as_str(), text, top_mod);
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-analysis/tests/ty_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ fn ty_check_standalone(fixture: Fixture<&str>) {
let mut db = HirAnalysisTestDb::default();
let path = Path::new(fixture.path());
let file_name = path.file_name().and_then(|file| file.to_str()).unwrap();
let input = db.new_stand_alone(file_name, fixture.content());
let (top_mod, mut prop_formatter) = db.top_mod(input);
let (ingot, file) = db.new_stand_alone(file_name, fixture.content());
let (top_mod, mut prop_formatter) = db.top_mod(ingot, file);

db.assert_no_diags(top_mod);

Expand Down
26 changes: 13 additions & 13 deletions crates/hir/src/hir_def/module_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,13 @@ mod tests {
Version::new(0, 0, 1),
Default::default(),
);
let local_root = InputFile::new(&db, local_ingot, "src/lib.fe".into(), "".into());
let mod1 = InputFile::new(&db, local_ingot, "src/mod1.fe".into(), "".into());
let mod2 = InputFile::new(&db, local_ingot, "src/mod2.fe".into(), "".into());
let foo = InputFile::new(&db, local_ingot, "src/mod1/foo.fe".into(), "".into());
let bar = InputFile::new(&db, local_ingot, "src/mod2/bar.fe".into(), "".into());
let baz = InputFile::new(&db, local_ingot, "src/mod2/baz.fe".into(), "".into());
let floating = InputFile::new(&db, local_ingot, "src/mod3/floating.fe".into(), "".into());
let local_root = InputFile::new(&db, "src/lib.fe".into(), "".into());
let mod1 = InputFile::new(&db, "src/mod1.fe".into(), "".into());
let mod2 = InputFile::new(&db, "src/mod2.fe".into(), "".into());
let foo = InputFile::new(&db, "src/mod1/foo.fe".into(), "".into());
let bar = InputFile::new(&db, "src/mod2/bar.fe".into(), "".into());
let baz = InputFile::new(&db, "src/mod2/baz.fe".into(), "".into());
let floating = InputFile::new(&db, "src/mod3/floating.fe".into(), "".into());
local_ingot.set_root_file(&mut db, local_root);
local_ingot.set_files(
&mut db,
Expand All @@ -281,12 +281,12 @@ mod tests {
.collect(),
);

let local_root_mod = lower::map_file_to_mod(&db, local_root);
let mod1_mod = lower::map_file_to_mod(&db, mod1);
let mod2_mod = lower::map_file_to_mod(&db, mod2);
let foo_mod = lower::map_file_to_mod(&db, foo);
let bar_mod = lower::map_file_to_mod(&db, bar);
let baz_mod = lower::map_file_to_mod(&db, baz);
let local_root_mod = lower::map_file_to_mod(&db, local_ingot, local_root);
let mod1_mod = lower::map_file_to_mod(&db, local_ingot, mod1);
let mod2_mod = lower::map_file_to_mod(&db, local_ingot, mod2);
let foo_mod = lower::map_file_to_mod(&db, local_ingot, foo);
let bar_mod = lower::map_file_to_mod(&db, local_ingot, bar);
let baz_mod = lower::map_file_to_mod(&db, local_ingot, baz);

let local_tree = lower::module_tree(&db, local_ingot);
let root_node = local_tree.root_data();
Expand Down
8 changes: 4 additions & 4 deletions crates/hir/src/hir_def/scope_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,8 @@ mod tests {
}
"#;

let file = db.standalone_file(text);
let scope_graph = db.parse_source(file);
let (ingot, file) = db.standalone_file(text);
let scope_graph = db.parse_source(ingot, file);
assert_eq!(scope_graph.items_dfs(&db).count(), 8);

for (i, item) in scope_graph.items_dfs(&db).enumerate() {
Expand Down Expand Up @@ -653,8 +653,8 @@ mod tests {
}
"#;

let file = db.standalone_file(text);
let scope_graph = db.parse_source(file);
let (ingot, file) = db.standalone_file(text);
let scope_graph = db.parse_source(ingot, file);
let root = scope_graph.top_mod.scope();
let enum_ = scope_graph.children(root).next().unwrap();
assert!(matches!(enum_.item(), ItemKind::Enum(_)));
Expand Down
18 changes: 9 additions & 9 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,28 @@ mod test_db {
impl_db_traits!(TestDb, InputDb, HirDb, LowerHirDb, SpannedHirDb);

impl TestDb {
pub fn parse_source(&self, file: InputFile) -> &ScopeGraph {
let top_mod = map_file_to_mod(self, file);
pub fn parse_source(&self, ingot: InputIngot, file: InputFile) -> &ScopeGraph {
let top_mod = map_file_to_mod(self, ingot, file);
scope_graph(self, top_mod)
}

/// Parses the given source text and returns the first inner item in the
/// file.
pub fn expect_item<'db, T>(&'db self, input: InputFile) -> T
pub fn expect_item<'db, T>(&'db self, ingot: InputIngot, input: InputFile) -> T
where
ItemKind<'db>: TryInto<T, Error = &'static str>,
{
let tree = self.parse_source(input);
let tree = self.parse_source(ingot, input);
tree.items_dfs(self)
.find_map(|it| it.try_into().ok())
.unwrap()
}

pub fn expect_items<'db, T>(&'db self, input: InputFile) -> Vec<T>
pub fn expect_items<'db, T>(&'db self, ingot: InputIngot, input: InputFile) -> Vec<T>
where
ItemKind<'db>: TryInto<T, Error = &'static str>,
{
let tree = self.parse_source(input);
let tree = self.parse_source(ingot, input);
tree.items_dfs(self)
.filter_map(|it| it.try_into().ok())
.collect()
Expand All @@ -153,15 +153,15 @@ mod test_db {
&text[range.start().into()..range.end().into()]
}

pub fn standalone_file(&mut self, text: &str) -> InputFile {
pub fn standalone_file(&mut self, text: &str) -> (InputIngot, InputFile) {
let path = "hir_test";
let kind = IngotKind::StandAlone;
let version = Version::new(0, 0, 1);
let ingot = InputIngot::new(self, path, kind, version, IndexSet::default());
let file = InputFile::new(self, ingot, "test_file.fe".into(), text.to_string());
let file = InputFile::new(self, "test_file.fe".into(), text.to_string());
ingot.set_root_file(self, file);
ingot.set_files(self, [file].into_iter().collect());
file
(ingot, file)
}
}
}
4 changes: 2 additions & 2 deletions crates/hir/src/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ mod use_tree;
/// This function just maps the file to a top-level module, and doesn't perform
/// any parsing or lowering.
/// To perform the actual lowering, use [`scope_graph`] instead.
pub fn map_file_to_mod(db: &dyn LowerHirDb, file: InputFile) -> TopLevelMod {
let ingot = module_tree_impl(db.as_hir_db(), file.ingot(db.as_input_db())).ingot;
pub fn map_file_to_mod(db: &dyn LowerHirDb, ingot: InputIngot, file: InputFile) -> TopLevelMod {
let ingot = module_tree_impl(db.as_hir_db(), ingot).ingot;
map_file_to_mod_impl(db.as_hir_db(), ingot, file)
}

Expand Down
4 changes: 2 additions & 2 deletions crates/hir/src/span/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ mod tests {
}
}"#;

let input = db.standalone_file(text);
let body: Body = db.expect_item::<Body>(input);
let (ingot, file) = db.standalone_file(text);
let body: Body = db.expect_item::<Body>(ingot, file);
let bin_expr = match body.exprs(db.as_hir_db()).values().nth(2).unwrap().unwrap() {
Expr::AugAssign(lhs, rhs, bin_op) => (*lhs, *rhs, *bin_op),
_ => unreachable!(),
Expand Down
Loading

0 comments on commit 6af6147

Please sign in to comment.