Skip to content

Commit

Permalink
fix: allow ast when macro errors (#4005)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

LSP panics when compiling Contracts with macro #4004

## Summary\*

While processing macro, we want to populate definitions instead of
returning early in case of error encountered by macro.

## Additional Context

LSP was crashing because definitions for crate were not populated when
macro error was encountered. Normally, we collect definitions and errors
are returned if encountered.

When macro is used, macro processor was returning early from collection
process and not allowing for definitions to be populated, therefore when
other parts were attempting to access collected defs , they were not
present where expected.

This PR fixes the problem by not returning early in collection process
when macro error is encountered and allowing definitions to be collected
therefore we populate crate definitions while retaining errors
encountered on the way.

## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** 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: Tom French <[email protected]>
Co-authored-by: kevaundray <[email protected]>
  • Loading branch information
3 people authored Jan 12, 2024
1 parent 48fc6ab commit efccec3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 5 additions & 4 deletions compiler/noirc_frontend/src/hir/def_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,15 @@ impl CrateDefMap {
let mut ast = ast.into_sorted();

for macro_processor in &macro_processors {
ast = match macro_processor.process_untyped_ast(ast, &crate_id, context) {
Ok(ast) => ast,
match macro_processor.process_untyped_ast(ast.clone(), &crate_id, context) {
Ok(processed_ast) => {
ast = processed_ast;
}
Err((error, file_id)) => {
let def_error = DefCollectorErrorKind::MacroError(error);
errors.push((def_error.into(), file_id));
return errors;
}
};
}
}

// Allocate a default Module for the root, giving it a ModuleId
Expand Down
3 changes: 2 additions & 1 deletion compiler/noirc_frontend/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ fn force<'a, T: 'a>(parser: impl NoirParser<T> + 'a) -> impl NoirParser<Option<T
parser.map(Some).recover_via(empty().map(|_| None))
}

#[derive(Default)]
#[derive(Clone, Default)]
pub struct SortedModule {
pub imports: Vec<ImportStatement>,
pub functions: Vec<NoirFunction>,
Expand Down Expand Up @@ -344,6 +344,7 @@ impl std::fmt::Display for SortedSubModule {
}
}

#[derive(Clone)]
pub struct SortedSubModule {
pub name: Ident,
pub contents: SortedModule,
Expand Down

0 comments on commit efccec3

Please sign in to comment.