From 6b636d953901778e054f4bdd78751f875d4512d4 Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Sun, 8 Dec 2024 11:12:18 -0800 Subject: [PATCH] Set up module ids properly --- crates/compiler/parse/src/ast.rs | 2 +- .../compiler/test_syntax/src/test_helpers.rs | 35 +++++++++++++++++-- .../test_syntax/tests/test_snapshots.rs | 5 +-- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/crates/compiler/parse/src/ast.rs b/crates/compiler/parse/src/ast.rs index 9ea52f8b2a..85032d0bfe 100644 --- a/crates/compiler/parse/src/ast.rs +++ b/crates/compiler/parse/src/ast.rs @@ -896,7 +896,7 @@ impl<'a, 'b> RecursiveValueDefIter<'a, 'b> { } } - fn push_pending_from_expr(&mut self, expr: &'b Expr<'a>) { + pub fn push_pending_from_expr(&mut self, expr: &'b Expr<'a>) { let mut expr_stack = vec![expr]; use Expr::*; diff --git a/crates/compiler/test_syntax/src/test_helpers.rs b/crates/compiler/test_syntax/src/test_helpers.rs index d9299de1f7..487b8502ef 100644 --- a/crates/compiler/test_syntax/src/test_helpers.rs +++ b/crates/compiler/test_syntax/src/test_helpers.rs @@ -7,7 +7,10 @@ use roc_can::expr::canonicalize_expr; use roc_can::scope::Scope; use roc_error_macros::set_panic_not_exit; use roc_fmt::{annotation::Formattable, header::fmt_header, MigrationFlags}; +use roc_module::ident::QualifiedModuleName; use roc_module::symbol::{IdentIds, Interns, ModuleIds, PackageModuleIds, Symbol}; +use roc_parse::ast::RecursiveValueDefIter; +use roc_parse::ast::ValueDef; use roc_parse::header::parse_module_defs; use roc_parse::parser::Parser; use roc_parse::parser::SyntaxError; @@ -146,8 +149,35 @@ impl<'a> Output<'a> { } Output::Expr(loc_expr) => { let mut var_store = VarStore::default(); - let qualified_module_ids = PackageModuleIds::default(); - let home = ModuleIds::default().get_or_insert(&"Test".into()); + let mut imported: Vec<(QualifiedModuleName, Region)> = vec![]; + + let empty_defs = Defs::default(); + let mut it = RecursiveValueDefIter::new(&empty_defs); + it.push_pending_from_expr(&loc_expr.value); + for (def, region) in it { + if let ValueDef::ModuleImport(import) = def { + imported.push((import.name.value.into(), *region)); + } + } + + let mut module_ids = ModuleIds::default(); + let mut qualified_module_ids = PackageModuleIds::default(); + let mut dep_idents = IdentIds::exposed_builtins(0); + + // Make sure the module_ids has ModuleIds for all our deps, + // then record those ModuleIds in can_module_ids for later. + // For each of our imports, add an entry to deps_by_name + // + // e.g. for `import pf.Foo exposing [bar]`, add `Foo` to deps_by_name + // + // Also build a list of imported_values_to_expose (like `bar` above.) + for (qualified_module_name, _region) in imported.into_iter() { + let pq_module_name = qualified_module_name.into_pq_module_name(None); + let module_id = qualified_module_ids.get_or_insert(&pq_module_name); + dep_idents.get_or_insert(module_id); + } + + let home = module_ids.get_or_insert(&"Test".into()); let mut scope = Scope::new( home, @@ -156,7 +186,6 @@ impl<'a> Output<'a> { Default::default(), ); - let dep_idents = IdentIds::exposed_builtins(0); let mut env = Env::new( arena, src, diff --git a/crates/compiler/test_syntax/tests/test_snapshots.rs b/crates/compiler/test_syntax/tests/test_snapshots.rs index b6b81d1a2f..6bb3fa3f6a 100644 --- a/crates/compiler/test_syntax/tests/test_snapshots.rs +++ b/crates/compiler/test_syntax/tests/test_snapshots.rs @@ -704,10 +704,7 @@ mod test_snapshots { // This is the current list as of writing. // We should be driving these down to zero over time. // Adding this protection in now to avoid accidentally adding more. - "all_the_bangs" - | "import_in_closure_with_curlies_after" - | "inline_import" - | "newline_before_import_curlies" => true, + "all_the_bangs" => true, // When adding new snapshot tests, strongly prefer fixing any canonicalization panics // they may run into rather than adding them to this list.