Skip to content

Commit

Permalink
chore: pass macro processors by reference (#4501)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

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

## Summary\*

We're cloning unnecessarily here as we can just pass the processors by
reference.

## Additional Context



## 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: Jake Fecher <jfecher11@gmail.com>
  • Loading branch information
TomAFrench and jfecher authored Mar 7, 2024
1 parent 2de8fbf commit 99c4d27
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 22 deletions.
7 changes: 2 additions & 5 deletions compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,8 @@ pub fn check_crate(
deny_warnings: bool,
disable_macros: bool,
) -> CompilationResult<()> {
let macros: Vec<&dyn MacroProcessor> = if disable_macros {
vec![]
} else {
vec![&aztec_macros::AztecMacro as &dyn MacroProcessor]
};
let macros: &[&dyn MacroProcessor] =
if disable_macros { &[] } else { &[&aztec_macros::AztecMacro as &dyn MacroProcessor] };

let mut errors = vec![];
let diagnostics = CrateDefMap::collect_defs(crate_id, context, macros);
Expand Down
10 changes: 3 additions & 7 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl DefCollector {
context: &mut Context,
ast: SortedModule,
root_file_id: FileId,
macro_processors: Vec<&dyn MacroProcessor>,
macro_processors: &[&dyn MacroProcessor],
) -> Vec<(CompilationError, FileId)> {
let mut errors: Vec<(CompilationError, FileId)> = vec![];
let crate_id = def_map.krate;
Expand All @@ -220,11 +220,7 @@ impl DefCollector {
let crate_graph = &context.crate_graph[crate_id];

for dep in crate_graph.dependencies.clone() {
errors.extend(CrateDefMap::collect_defs(
dep.crate_id,
context,
macro_processors.clone(),
));
errors.extend(CrateDefMap::collect_defs(dep.crate_id, context, macro_processors));

let dep_def_root =
context.def_map(&dep.crate_id).expect("ice: def map was just created").root;
Expand Down Expand Up @@ -257,7 +253,7 @@ impl DefCollector {
context.def_maps.insert(crate_id, def_collector.def_map);

// TODO(#4653): generalize this function
for macro_processor in &macro_processors {
for macro_processor in macro_processors {
macro_processor
.process_unresolved_traits_impls(
&crate_id,
Expand Down
12 changes: 3 additions & 9 deletions compiler/noirc_frontend/src/hir/def_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl CrateDefMap {
pub fn collect_defs(
crate_id: CrateId,
context: &mut Context,
macro_processors: Vec<&dyn MacroProcessor>,
macro_processors: &[&dyn MacroProcessor],
) -> Vec<(CompilationError, FileId)> {
// Check if this Crate has already been compiled
// XXX: There is probably a better alternative for this.
Expand All @@ -90,7 +90,7 @@ impl CrateDefMap {
let (ast, parsing_errors) = context.parsed_file_results(root_file_id);
let mut ast = ast.into_sorted();

for macro_processor in &macro_processors {
for macro_processor in macro_processors {
match macro_processor.process_untyped_ast(ast.clone(), &crate_id, context) {
Ok(processed_ast) => {
ast = processed_ast;
Expand All @@ -115,13 +115,7 @@ impl CrateDefMap {
};

// Now we want to populate the CrateDefMap using the DefCollector
errors.extend(DefCollector::collect(
def_map,
context,
ast,
root_file_id,
macro_processors.clone(),
));
errors.extend(DefCollector::collect(def_map, context, ast, root_file_id, macro_processors));

errors.extend(
parsing_errors.iter().map(|e| (e.clone().into(), root_file_id)).collect::<Vec<_>>(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ mod test {
&mut context,
program.clone().into_sorted(),
root_file_id,
Vec::new(), // No macro processors
&[], // No macro processors
));
}
(program, context, errors)
Expand Down

0 comments on commit 99c4d27

Please sign in to comment.