diff --git a/Cargo.lock b/Cargo.lock index d2afe025c69..37376ad7c80 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1542,7 +1542,6 @@ dependencies = [ "codespan-reporting", "iter-extended", "serde", - "tempfile", ] [[package]] @@ -2928,7 +2927,6 @@ dependencies = [ "smol_str", "strum", "strum_macros", - "tempfile", "thiserror", "tracing", ] diff --git a/compiler/fm/Cargo.toml b/compiler/fm/Cargo.toml index f556f305c78..1a356d93d89 100644 --- a/compiler/fm/Cargo.toml +++ b/compiler/fm/Cargo.toml @@ -13,5 +13,4 @@ codespan-reporting.workspace = true serde.workspace = true [dev-dependencies] -tempfile.workspace = true iter-extended.workspace = true diff --git a/compiler/fm/src/lib.rs b/compiler/fm/src/lib.rs index bb080c62c0e..2e52d802479 100644 --- a/compiler/fm/src/lib.rs +++ b/compiler/fm/src/lib.rs @@ -185,24 +185,17 @@ mod path_normalization { #[cfg(test)] mod tests { use super::*; - use tempfile::{tempdir, TempDir}; - // Returns the absolute path to the file - fn create_dummy_file(dir: &TempDir, file_name: &Path) -> PathBuf { - let file_path = dir.path().join(file_name); - let _file = std::fs::File::create(&file_path).unwrap(); - file_path + fn add_file(fm: &mut FileManager, file_name: &Path) -> FileId { + fm.add_file_with_source(file_name, "fn foo() {}".to_string()).unwrap() } #[test] fn path_resolve_file_module_other_ext() { - let dir = tempdir().unwrap(); - let file_name = Path::new("foo.nr"); - create_dummy_file(&dir, file_name); + let dir = PathBuf::new(); + let mut fm = FileManager::new(&dir); - let mut fm = FileManager::new(dir.path()); - - let file_id = fm.add_file_with_source(file_name, "fn foo() {}".to_string()).unwrap(); + let file_id = add_file(&mut fm, &dir.join("foo.nr")); assert!(fm.path(file_id).unwrap().ends_with("foo.nr")); } @@ -213,23 +206,19 @@ mod tests { /// they should both resolve to ../foo.nr #[test] fn path_resolve_modules_with_different_paths_as_same_file() { - let dir = tempdir().unwrap(); - let sub_dir = TempDir::new_in(&dir).unwrap(); - let sub_sub_dir = TempDir::new_in(&sub_dir).unwrap(); - - let mut fm = FileManager::new(dir.path()); - - // Create a lib.nr file at the root. - let file_name = Path::new("lib.nr"); - create_dummy_file(&dir, file_name); - - // Create another path with `./` and `../` inside it - let second_file_name = PathBuf::from(sub_sub_dir.path()).join("./../../lib.nr"); - - // Add both files to the file manager - let file_id = fm.add_file_with_source(file_name, "fn foo() {}".to_string()).unwrap(); - let second_file_id = - fm.add_file_with_source(&second_file_name, "fn foo() {}".to_string()).unwrap(); + let dir = PathBuf::new(); + let mut fm = FileManager::new(&dir); + + // Create a lib.nr file at the root and add it to the file manager. + let file_id = add_file(&mut fm, &dir.join("lib.nr")); + + // Create another path with `./` and `../` inside it, and add it to the file manager + let sub_dir = dir.join("sub_dir"); + let sub_sub_dir = sub_dir.join("sub_sub_dir"); + let second_file_id = add_file( + &mut fm, + PathBuf::from(sub_sub_dir.as_path()).join("./../../lib.nr").as_path(), + ); assert_eq!(file_id, second_file_id); } diff --git a/compiler/noirc_frontend/Cargo.toml b/compiler/noirc_frontend/Cargo.toml index 381eb4a9bb9..cc5a9b1e652 100644 --- a/compiler/noirc_frontend/Cargo.toml +++ b/compiler/noirc_frontend/Cargo.toml @@ -36,7 +36,6 @@ lalrpop-util = { version = "0.20.2", features = ["lexer"] } base64.workspace = true strum = "0.24" strum_macros = "0.24" -tempfile.workspace = true [build-dependencies] lalrpop = "0.20.2" diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs index ab9de6c25c4..c9198a1d04c 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs @@ -1,4 +1,5 @@ -use std::{collections::HashMap, path::Path, vec}; +use std::path::Path; +use std::{collections::HashMap, vec}; use acvm::{AcirField, FieldElement}; use fm::{FileId, FileManager, FILE_EXTENSION}; @@ -557,17 +558,14 @@ impl<'a> ModCollector<'a> { macro_processors: &[&dyn MacroProcessor], ) -> Vec<(CompilationError, FileId)> { let mut errors: Vec<(CompilationError, FileId)> = vec![]; - let child_file_id = - match find_module(&context.file_manager, self.file_id, &mod_decl.ident.0.contents) { - Ok(child_file_id) => child_file_id, - Err(expected_path) => { - let mod_name = mod_decl.ident.clone(); - let err = - DefCollectorErrorKind::UnresolvedModuleDecl { mod_name, expected_path }; - errors.push((err.into(), self.file_id)); - return errors; - } - }; + let child_file_id = match find_module(&context.file_manager, self.file_id, &mod_decl.ident) + { + Ok(child_file_id) => child_file_id, + Err(err) => { + errors.push((err.into(), self.file_id)); + return errors; + } + }; let location = Location { file: self.file_id, span: mod_decl.ident.span() }; @@ -684,26 +682,47 @@ impl<'a> ModCollector<'a> { fn find_module( file_manager: &FileManager, anchor: FileId, - mod_name: &str, -) -> Result { + mod_name: &Ident, +) -> Result { let anchor_path = file_manager .path(anchor) .expect("File must exist in file manager in order for us to be resolving its imports.") .with_extension(""); let anchor_dir = anchor_path.parent().unwrap(); - // if `anchor` is a `main.nr`, `lib.nr`, `mod.nr` or `{mod_name}.nr`, we check siblings of - // the anchor at `base/mod_name.nr`. - let candidate = if should_check_siblings_for_module(&anchor_path, anchor_dir) { - anchor_dir.join(format!("{mod_name}.{FILE_EXTENSION}")) + // Assuming anchor is called "anchor.nr" and we are looking up a module named "mod_name"... + // This is "mod_name" + let mod_name_str = &mod_name.0.contents; + + // If we are in a special name like "main.nr", "lib.nr", "mod.nr" or "{mod_name}.nr", + // the search starts at the same directory, otherwise it starts in a nested directory. + let start_dir = if should_check_siblings_for_module(&anchor_path, anchor_dir) { + anchor_dir } else { - // Otherwise, we check for children of the anchor at `base/anchor/mod_name.nr` - anchor_path.join(format!("{mod_name}.{FILE_EXTENSION}")) + anchor_path.as_path() }; - file_manager - .name_to_id(candidate.clone()) - .ok_or_else(|| candidate.as_os_str().to_string_lossy().to_string()) + // Check "mod_name.nr" + let mod_name_candidate = start_dir.join(format!("{mod_name_str}.{FILE_EXTENSION}")); + let mod_name_result = file_manager.name_to_id(mod_name_candidate.clone()); + + // Check "mod_name/mod.nr" + let mod_nr_candidate = start_dir.join(mod_name_str).join(format!("mod.{FILE_EXTENSION}")); + let mod_nr_result = file_manager.name_to_id(mod_nr_candidate.clone()); + + match (mod_nr_result, mod_name_result) { + (Some(_), Some(_)) => Err(DefCollectorErrorKind::OverlappingModuleDecls { + mod_name: mod_name.clone(), + expected_path: mod_name_candidate.as_os_str().to_string_lossy().to_string(), + alternative_path: mod_nr_candidate.as_os_str().to_string_lossy().to_string(), + }), + (Some(id), None) | (None, Some(id)) => Ok(id), + (None, None) => Err(DefCollectorErrorKind::UnresolvedModuleDecl { + mod_name: mod_name.clone(), + expected_path: mod_name_candidate.as_os_str().to_string_lossy().to_string(), + alternative_path: mod_nr_candidate.as_os_str().to_string_lossy().to_string(), + }), + } } /// Returns true if a module's child modules are expected to be in the same directory. @@ -801,73 +820,186 @@ pub(crate) fn collect_global( } #[cfg(test)] -mod tests { +mod find_module_tests { use super::*; - use std::path::PathBuf; - use tempfile::{tempdir, TempDir}; + use noirc_errors::Spanned; + use std::path::{Path, PathBuf}; + + fn add_file(file_manager: &mut FileManager, dir: &Path, file_name: &str) -> FileId { + let mut target_filename = PathBuf::from(&dir); + for path in file_name.split('/') { + target_filename = target_filename.join(path); + } - // Returns the absolute path to the file - fn create_dummy_file(dir: &TempDir, file_name: &Path) -> PathBuf { - let file_path = dir.path().join(file_name); - let _file = std::fs::File::create(&file_path).unwrap(); - file_path + file_manager + .add_file_with_source(&target_filename, "fn foo() {}".to_string()) + .expect("could not add file to file manager and obtain a FileId") + } + + fn find_module( + file_manager: &FileManager, + anchor: FileId, + mod_name: &str, + ) -> Result { + let mod_name = Ident(Spanned::from_position(0, 1, mod_name.to_string())); + super::find_module(file_manager, anchor, &mod_name) } #[test] - fn path_resolve_file_module() { - let dir = tempdir().unwrap(); + fn errors_if_cannot_find_file() { + let dir = PathBuf::new(); + let mut fm = FileManager::new(&PathBuf::new()); - let entry_file_name = Path::new("my_dummy_file.nr"); - create_dummy_file(&dir, entry_file_name); + let file_id = add_file(&mut fm, &dir, "my_dummy_file.nr"); + + let result = find_module(&fm, file_id, "foo"); + assert!(matches!(result, Err(DefCollectorErrorKind::UnresolvedModuleDecl { .. }))); + } - let mut fm = FileManager::new(dir.path()); + #[test] + fn errors_because_cannot_find_mod_relative_to_main() { + let dir = PathBuf::new(); + let mut fm = FileManager::new(&dir); - let file_id = fm.add_file_with_source(entry_file_name, "fn foo() {}".to_string()).unwrap(); + let main_file_id = add_file(&mut fm, &dir, "main.nr"); + add_file(&mut fm, &dir, "main/foo.nr"); - let dep_file_name = Path::new("foo.nr"); - create_dummy_file(&dir, dep_file_name); - find_module(&fm, file_id, "foo").unwrap_err(); + let result = find_module(&fm, main_file_id, "foo"); + assert!(matches!(result, Err(DefCollectorErrorKind::UnresolvedModuleDecl { .. }))); } #[test] - fn path_resolve_sub_module() { - let dir = tempdir().unwrap(); - let mut fm = FileManager::new(dir.path()); - - // Create a lib.nr file at the root. - // we now have dir/lib.nr - let lib_nr_path = create_dummy_file(&dir, Path::new("lib.nr")); - let file_id = fm - .add_file_with_source(lib_nr_path.as_path(), "fn foo() {}".to_string()) - .expect("could not add file to file manager and obtain a FileId"); - - // Create a sub directory - // we now have: - // - dir/lib.nr - // - dir/sub_dir - let sub_dir = TempDir::new_in(&dir).unwrap(); - let sub_dir_name = sub_dir.path().file_name().unwrap().to_str().unwrap(); - - // Add foo.nr to the subdirectory - // we no have: - // - dir/lib.nr - // - dir/sub_dir/foo.nr - let foo_nr_path = create_dummy_file(&sub_dir, Path::new("foo.nr")); - fm.add_file_with_source(foo_nr_path.as_path(), "fn foo() {}".to_string()); - - // Add a parent module for the sub_dir - // we no have: - // - dir/lib.nr - // - dir/sub_dir.nr - // - dir/sub_dir/foo.nr - let sub_dir_nr_path = create_dummy_file(&dir, Path::new(&format!("{sub_dir_name}.nr"))); - fm.add_file_with_source(sub_dir_nr_path.as_path(), "fn foo() {}".to_string()); - - // First check for the sub_dir.nr file and add it to the FileManager - let sub_dir_file_id = find_module(&fm, file_id, sub_dir_name).unwrap(); - - // Now check for files in it's subdirectory + fn errors_because_cannot_find_mod_relative_to_lib() { + let dir = PathBuf::new(); + let mut fm = FileManager::new(&dir); + + let lib_file_id = add_file(&mut fm, &dir, "lib.nr"); + add_file(&mut fm, &dir, "lib/foo.nr"); + + let result = find_module(&fm, lib_file_id, "foo"); + assert!(matches!(result, Err(DefCollectorErrorKind::UnresolvedModuleDecl { .. }))); + } + + #[test] + fn errors_because_cannot_find_sibling_mod_for_regular_name() { + let dir = PathBuf::new(); + let mut fm = FileManager::new(&dir); + + let foo_file_id = add_file(&mut fm, &dir, "foo.nr"); + add_file(&mut fm, &dir, "bar.nr"); + + let result = find_module(&fm, foo_file_id, "bar"); + assert!(matches!(result, Err(DefCollectorErrorKind::UnresolvedModuleDecl { .. }))); + } + + #[test] + fn cannot_find_module_in_the_same_directory_for_regular_name() { + let dir = PathBuf::new(); + let mut fm = FileManager::new(&dir); + + let lib_file_id = add_file(&mut fm, &dir, "lib.nr"); + add_file(&mut fm, &dir, "bar.nr"); + add_file(&mut fm, &dir, "foo.nr"); + + // `mod bar` from `lib.nr` should find `bar.nr` + let bar_file_id = find_module(&fm, lib_file_id, "bar").unwrap(); + + // `mod foo` from `bar.nr` should fail to find `foo.nr` + let result = find_module(&fm, bar_file_id, "foo"); + assert!(matches!(result, Err(DefCollectorErrorKind::UnresolvedModuleDecl { .. }))); + } + + #[test] + fn finds_module_in_sibling_dir_for_regular_name() { + let dir = PathBuf::new(); + let mut fm = FileManager::new(&dir); + + let sub_dir_file_id = add_file(&mut fm, &dir, "sub_dir.nr"); + add_file(&mut fm, &dir, "sub_dir/foo.nr"); + + // `mod foo` from `sub_dir.nr` should find `sub_dir/foo.nr` find_module(&fm, sub_dir_file_id, "foo").unwrap(); } + + #[test] + fn finds_module_in_sibling_dir_mod_nr_for_regular_name() { + let dir = PathBuf::new(); + let mut fm = FileManager::new(&dir); + + let sub_dir_file_id = add_file(&mut fm, &dir, "sub_dir.nr"); + add_file(&mut fm, &dir, "sub_dir/foo/mod.nr"); + + // `mod foo` from `sub_dir.nr` should find `sub_dir/foo.nr` + find_module(&fm, sub_dir_file_id, "foo").unwrap(); + } + + #[test] + fn finds_module_in_sibling_dir_for_special_name() { + let dir = PathBuf::new(); + let mut fm = FileManager::new(&dir); + + let lib_file_id = add_file(&mut fm, &dir, "lib.nr"); + add_file(&mut fm, &dir, "sub_dir.nr"); + add_file(&mut fm, &dir, "sub_dir/foo.nr"); + + // `mod sub_dir` from `lib.nr` should find `sub_dir.nr` + let sub_dir_file_id = find_module(&fm, lib_file_id, "sub_dir").unwrap(); + + // `mod foo` from `sub_dir.nr` should find `sub_dir/foo.nr` + find_module(&fm, sub_dir_file_id, "foo").unwrap(); + } + + #[test] + fn finds_mod_dot_nr_for_special_name() { + let dir = PathBuf::new(); + let mut fm = FileManager::new(&dir); + + let lib_file_id = add_file(&mut fm, &dir, "lib.nr"); + add_file(&mut fm, &dir, "foo/mod.nr"); + + // Check that searching "foo" finds the mod.nr file + find_module(&fm, lib_file_id, "foo").unwrap(); + } + + #[test] + fn errors_mod_dot_nr_in_same_directory() { + let dir = PathBuf::new(); + let mut fm = FileManager::new(&dir); + + let lib_file_id = add_file(&mut fm, &dir, "lib.nr"); + add_file(&mut fm, &dir, "mod.nr"); + + // Check that searching "foo" does not pick up the mod.nr file + let result = find_module(&fm, lib_file_id, "foo"); + assert!(matches!(result, Err(DefCollectorErrorKind::UnresolvedModuleDecl { .. }))); + } + + #[test] + fn errors_if_file_exists_at_both_potential_module_locations_for_regular_name() { + let dir = PathBuf::new(); + let mut fm = FileManager::new(&dir); + + let foo_file_id = add_file(&mut fm, &dir, "foo.nr"); + add_file(&mut fm, &dir, "foo/bar.nr"); + add_file(&mut fm, &dir, "foo/bar/mod.nr"); + + // Check that `mod bar` from `foo` gives an error + let result = find_module(&fm, foo_file_id, "bar"); + assert!(matches!(result, Err(DefCollectorErrorKind::OverlappingModuleDecls { .. }))); + } + + #[test] + fn errors_if_file_exists_at_both_potential_module_locations_for_special_name() { + let dir = PathBuf::new(); + let mut fm = FileManager::new(&dir); + + let lib_file_id = add_file(&mut fm, &dir, "lib.nr"); + add_file(&mut fm, &dir, "foo.nr"); + add_file(&mut fm, &dir, "foo/mod.nr"); + + // Check that searching "foo" gives an error + let result = find_module(&fm, lib_file_id, "foo"); + assert!(matches!(result, Err(DefCollectorErrorKind::OverlappingModuleDecls { .. }))); + } } diff --git a/compiler/noirc_frontend/src/hir/def_collector/errors.rs b/compiler/noirc_frontend/src/hir/def_collector/errors.rs index af2264c3843..37c5a460667 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/errors.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/errors.rs @@ -27,7 +27,9 @@ pub enum DefCollectorErrorKind { #[error("duplicate {typ} found in namespace")] Duplicate { typ: DuplicateType, first_def: Ident, second_def: Ident }, #[error("unresolved import")] - UnresolvedModuleDecl { mod_name: Ident, expected_path: String }, + UnresolvedModuleDecl { mod_name: Ident, expected_path: String, alternative_path: String }, + #[error("overlapping imports")] + OverlappingModuleDecls { mod_name: Ident, expected_path: String, alternative_path: String }, #[error("path resolution error")] PathResolutionError(PathResolutionError), #[error("Non-struct type used in impl")] @@ -121,12 +123,22 @@ impl<'a> From<&'a DefCollectorErrorKind> for Diagnostic { diag } } - DefCollectorErrorKind::UnresolvedModuleDecl { mod_name, expected_path } => { + DefCollectorErrorKind::UnresolvedModuleDecl { mod_name, expected_path, alternative_path } => { let span = mod_name.0.span(); let mod_name = &mod_name.0.contents; Diagnostic::simple_error( - format!("No module `{mod_name}` at path `{expected_path}`"), + format!("No module `{mod_name}` at path `{expected_path}` or `{alternative_path}`"), + String::new(), + span, + ) + } + DefCollectorErrorKind::OverlappingModuleDecls { mod_name, expected_path, alternative_path } => { + let span = mod_name.0.span(); + let mod_name = &mod_name.0.contents; + + Diagnostic::simple_error( + format!("Overlapping modules `{mod_name}` at path `{expected_path}` and `{alternative_path}`"), String::new(), span, ) diff --git a/docs/docs/noir/modules_packages_crates/modules.md b/docs/docs/noir/modules_packages_crates/modules.md index ae822a1cff4..9fffd925b7b 100644 --- a/docs/docs/noir/modules_packages_crates/modules.md +++ b/docs/docs/noir/modules_packages_crates/modules.md @@ -49,6 +49,27 @@ crate ``` +The module filename may also be the name of the module as a directory with the contents in a +file named `mod.nr` within that directory. The above example can alternatively be expressed like this: + +Filename : `src/main.nr` + +```rust +mod foo; + +fn main() { + foo::hello_world(); +} +``` + +Filename : `src/foo/mod.nr` + +```rust +fn from_foo() {} +``` + +Note that it's an error to have both files `src/foo.nr` and `src/foo/mod.nr` in the filesystem. + ### Importing a module throughout the tree All modules are accessible from the `crate::` namespace. @@ -103,3 +124,28 @@ crate └── bar └── from_bar ``` + +Similar to importing a module in the crate root, modules can be placed in a `mod.nr` file, like this: + +Filename : `src/main.nr` + +```rust +mod foo; + +fn main() { + foo::from_foo(); +} +``` + +Filename : `src/foo/mod.nr` + +```rust +mod bar; +fn from_foo() {} +``` + +Filename : `src/foo/bar/mod.nr` + +```rust +fn from_bar() {} +``` \ No newline at end of file diff --git a/test_programs/compile_failure/invalid_main_sub_lib/Nargo.toml b/test_programs/compile_failure/invalid_main_sub_lib/Nargo.toml new file mode 100644 index 00000000000..12776f47712 --- /dev/null +++ b/test_programs/compile_failure/invalid_main_sub_lib/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "invalid_main_sub_lib" +type = "bin" +authors = [""] +compiler_version = ">=0.30.0" + +[dependencies] \ No newline at end of file diff --git a/test_programs/compile_failure/invalid_main_sub_lib/src/main.nr b/test_programs/compile_failure/invalid_main_sub_lib/src/main.nr new file mode 100644 index 00000000000..4658900a47a --- /dev/null +++ b/test_programs/compile_failure/invalid_main_sub_lib/src/main.nr @@ -0,0 +1,5 @@ +mod lib; + +use crate::lib::foo; + +fn main() {} diff --git a/test_programs/compile_failure/invalid_main_sub_lib/src/main/lib.nr b/test_programs/compile_failure/invalid_main_sub_lib/src/main/lib.nr new file mode 100644 index 00000000000..0f89316b5bd --- /dev/null +++ b/test_programs/compile_failure/invalid_main_sub_lib/src/main/lib.nr @@ -0,0 +1,3 @@ +pub fn foo() -> bool { + true +} diff --git a/test_programs/compile_failure/invalid_mod_mod_path/Nargo.toml b/test_programs/compile_failure/invalid_mod_mod_path/Nargo.toml new file mode 100644 index 00000000000..7a93d385c1c --- /dev/null +++ b/test_programs/compile_failure/invalid_mod_mod_path/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "invalid_mod_mod_path" +type = "bin" +authors = [""] +compiler_version = ">=0.30.0" + +[dependencies] \ No newline at end of file diff --git a/test_programs/compile_failure/invalid_mod_mod_path/main/lib.nr b/test_programs/compile_failure/invalid_mod_mod_path/main/lib.nr new file mode 100644 index 00000000000..0f89316b5bd --- /dev/null +++ b/test_programs/compile_failure/invalid_mod_mod_path/main/lib.nr @@ -0,0 +1,3 @@ +pub fn foo() -> bool { + true +} diff --git a/test_programs/compile_failure/invalid_mod_mod_path/src/main.nr b/test_programs/compile_failure/invalid_mod_mod_path/src/main.nr new file mode 100644 index 00000000000..86fa197360f --- /dev/null +++ b/test_programs/compile_failure/invalid_mod_mod_path/src/main.nr @@ -0,0 +1,4 @@ +mod crate::mod; + +fn main() { +} diff --git a/test_programs/compile_failure/invalid_mod_mod_path/src/mod.nr b/test_programs/compile_failure/invalid_mod_mod_path/src/mod.nr new file mode 100644 index 00000000000..e2f24cb9226 --- /dev/null +++ b/test_programs/compile_failure/invalid_mod_mod_path/src/mod.nr @@ -0,0 +1,3 @@ +pub foo() -> bool { + true +} diff --git a/test_programs/compile_failure/overlapping_mod/Nargo.toml b/test_programs/compile_failure/overlapping_mod/Nargo.toml new file mode 100644 index 00000000000..c9d59b22a39 --- /dev/null +++ b/test_programs/compile_failure/overlapping_mod/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "overlapping_lib_and_mod" +type = "bin" +authors = [""] +compiler_version = ">=0.30.0" + +[dependencies] \ No newline at end of file diff --git a/test_programs/compile_failure/overlapping_mod/src/foo.nr b/test_programs/compile_failure/overlapping_mod/src/foo.nr new file mode 100644 index 00000000000..f17d7d7cec5 --- /dev/null +++ b/test_programs/compile_failure/overlapping_mod/src/foo.nr @@ -0,0 +1,4 @@ +pub fn bar() -> bool { + true +} + diff --git a/test_programs/compile_failure/overlapping_mod/src/foo/mod.nr b/test_programs/compile_failure/overlapping_mod/src/foo/mod.nr new file mode 100644 index 00000000000..261729b812b --- /dev/null +++ b/test_programs/compile_failure/overlapping_mod/src/foo/mod.nr @@ -0,0 +1,3 @@ +pub fn bar() -> bool { + true +} diff --git a/test_programs/compile_failure/overlapping_mod/src/main.nr b/test_programs/compile_failure/overlapping_mod/src/main.nr new file mode 100644 index 00000000000..12a3d91f941 --- /dev/null +++ b/test_programs/compile_failure/overlapping_mod/src/main.nr @@ -0,0 +1,7 @@ +mod foo; + +use foo::bar; + +fn main() { + assert(bar()); +} diff --git a/test_programs/compile_success_empty/mod_nr_entrypoint/Nargo.toml b/test_programs/compile_success_empty/mod_nr_entrypoint/Nargo.toml new file mode 100644 index 00000000000..b90b7326186 --- /dev/null +++ b/test_programs/compile_success_empty/mod_nr_entrypoint/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "mod_nr_entrypoint" +type = "bin" +authors = [""] +compiler_version = ">=0.29.0" + +[dependencies] \ No newline at end of file diff --git a/test_programs/compile_success_empty/mod_nr_entrypoint/src/baz.nr b/test_programs/compile_success_empty/mod_nr_entrypoint/src/baz.nr new file mode 100644 index 00000000000..0bcfe8b02ad --- /dev/null +++ b/test_programs/compile_success_empty/mod_nr_entrypoint/src/baz.nr @@ -0,0 +1,3 @@ +pub fn in_baz_mod() -> bool { + true +} diff --git a/test_programs/compile_success_empty/mod_nr_entrypoint/src/foo/bar.nr b/test_programs/compile_success_empty/mod_nr_entrypoint/src/foo/bar.nr new file mode 100644 index 00000000000..f2efe64906d --- /dev/null +++ b/test_programs/compile_success_empty/mod_nr_entrypoint/src/foo/bar.nr @@ -0,0 +1,3 @@ +pub fn in_bar_mod() -> Field { + 2 +} diff --git a/test_programs/compile_success_empty/mod_nr_entrypoint/src/foo/mod.nr b/test_programs/compile_success_empty/mod_nr_entrypoint/src/foo/mod.nr new file mode 100644 index 00000000000..4eac6cb8514 --- /dev/null +++ b/test_programs/compile_success_empty/mod_nr_entrypoint/src/foo/mod.nr @@ -0,0 +1,5 @@ +mod bar; + +pub fn in_foo_mod() -> Field { + 1 +} diff --git a/test_programs/compile_success_empty/mod_nr_entrypoint/src/main.nr b/test_programs/compile_success_empty/mod_nr_entrypoint/src/main.nr new file mode 100644 index 00000000000..620fd99f6ec --- /dev/null +++ b/test_programs/compile_success_empty/mod_nr_entrypoint/src/main.nr @@ -0,0 +1,11 @@ +use crate::foo::in_foo_mod; +use crate::foo::bar::in_bar_mod; +use crate::baz::in_baz_mod; + +mod foo; +mod baz; + +fn main() { + assert(in_foo_mod() != in_bar_mod()); + assert(in_baz_mod()); +}