Skip to content

Commit

Permalink
feat: mod.nr entrypoint (noir-lang#5039)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves noir-lang#4860

## Summary\*

Allow using `mod.nr` as the entrypoint to a module.

- [x] Add test for conflicting `foo.nr` and `foo/mod.nr`
- [x] Add handling for `foo/lib/foo.nr` and other disallowed name-based
cases
- [x] Add tests for `foo/lib/foo.nr`, `main/foo.nr`, and `foo/mod/bar`

## Additional Context

I expected `find_module` to have the logic for this, but after a bunch
of debugging, it seems somehow unrelated. I.e. it's not even called for
the `mod.nr` case.

## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: TomAFrench <[email protected]>
Co-authored-by: Tom French <[email protected]>
Co-authored-by: Ary Borenszweig <[email protected]>
  • Loading branch information
4 people committed Jul 1, 2024
1 parent 0c4fffa commit 076fe0a
Show file tree
Hide file tree
Showing 23 changed files with 370 additions and 113 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion compiler/fm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ codespan-reporting.workspace = true
serde.workspace = true

[dev-dependencies]
tempfile.workspace = true
iter-extended.workspace = true
47 changes: 18 additions & 29 deletions compiler/fm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand All @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion compiler/noirc_frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading

0 comments on commit 076fe0a

Please sign in to comment.