Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ pub trait Recoverable {
fn error(span: Span) -> Self;
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ModuleDeclaration {
pub ident: Ident,
}

impl std::fmt::Display for ModuleDeclaration {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "mod {}", self.ident)
}
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ImportStatement {
pub path: Path,
Expand Down
18 changes: 9 additions & 9 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::{
hir::def_collector::dc_crate::{UnresolvedStruct, UnresolvedTrait},
node_interner::{FunctionModifiers, TraitId, TypeAliasId},
parser::{SortedModule, SortedSubModule},
FunctionDefinition, Ident, LetStatement, NoirFunction, NoirStruct, NoirTrait, NoirTraitImpl,
NoirTypeAlias, TraitImplItem, TraitItem, TypeImpl,
FunctionDefinition, Ident, LetStatement, ModuleDeclaration, NoirFunction, NoirStruct,
NoirTrait, NoirTraitImpl, NoirTypeAlias, TraitImplItem, TraitItem, TypeImpl,
};

use super::{
Expand Down Expand Up @@ -522,33 +522,33 @@ impl<'a> ModCollector<'a> {
fn parse_module_declaration(
&mut self,
context: &mut Context,
mod_name: &Ident,
mod_decl: &ModuleDeclaration,
crate_id: CrateId,
) -> Vec<(CompilationError, FileId)> {
let mut errors: Vec<(CompilationError, FileId)> = vec![];
let child_file_id =
match find_module(&context.file_manager, self.file_id, &mod_name.0.contents) {
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_name.clone();
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 location = Location { file: self.file_id, span: mod_name.span() };
let location = Location { file: self.file_id, span: mod_decl.ident.span() };

if let Some(old_location) = context.visited_files.get(&child_file_id) {
let error = DefCollectorErrorKind::ModuleAlreadyPartOfCrate {
mod_name: mod_name.clone(),
mod_name: mod_decl.ident.clone(),
span: location.span,
};
errors.push((error.into(), location.file));

let error = DefCollectorErrorKind::ModuleOriginallyDefined {
mod_name: mod_name.clone(),
mod_name: mod_decl.ident.clone(),
span: old_location.span,
};
errors.push((error.into(), old_location.file));
Expand All @@ -566,7 +566,7 @@ impl<'a> ModCollector<'a> {
);

// Add module into def collector and get a ModuleId
match self.push_child_module(mod_name, child_file_id, true, false) {
match self.push_child_module(&mod_decl.ident, child_file_id, true, false) {
Ok(child_mod_id) => {
errors.extend(collect_defs(
self.def_collector,
Expand Down
18 changes: 9 additions & 9 deletions compiler/noirc_frontend/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ mod parser;
use crate::token::{Keyword, Token};
use crate::{ast::ImportStatement, Expression, NoirStruct};
use crate::{
Ident, LetStatement, NoirFunction, NoirTrait, NoirTraitImpl, NoirTypeAlias, Recoverable,
StatementKind, TypeImpl, UseTree,
Ident, LetStatement, ModuleDeclaration, NoirFunction, NoirTrait, NoirTraitImpl, NoirTypeAlias,
Recoverable, StatementKind, TypeImpl, UseTree,
};

use chumsky::prelude::*;
Expand All @@ -28,7 +28,7 @@ pub use parser::parse_program;
#[derive(Debug, Clone)]
pub(crate) enum TopLevelStatement {
Function(NoirFunction),
Module(Ident),
Module(ModuleDeclaration),
Import(UseTree),
Struct(NoirStruct),
Trait(NoirTrait),
Expand Down Expand Up @@ -220,7 +220,7 @@ pub struct SortedModule {
pub globals: Vec<LetStatement>,

/// Module declarations like `mod foo;`
pub module_decls: Vec<Ident>,
pub module_decls: Vec<ModuleDeclaration>,

/// Full submodules as in `mod foo { ... definitions ... }`
pub submodules: Vec<SortedSubModule>,
Expand All @@ -229,7 +229,7 @@ pub struct SortedModule {
impl std::fmt::Display for SortedModule {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for decl in &self.module_decls {
writeln!(f, "mod {decl};")?;
writeln!(f, "{decl};")?;
}

for import in &self.imports {
Expand Down Expand Up @@ -309,7 +309,7 @@ pub enum ItemKind {
Impl(TypeImpl),
TypeAlias(NoirTypeAlias),
Global(LetStatement),
ModuleDecl(Ident),
ModuleDecl(ModuleDeclaration),
Submodules(ParsedSubModule),
}

Expand Down Expand Up @@ -380,8 +380,8 @@ impl SortedModule {
self.imports.extend(import_stmt.desugar(None));
}

fn push_module_decl(&mut self, mod_name: Ident) {
self.module_decls.push(mod_name);
fn push_module_decl(&mut self, mod_decl: ModuleDeclaration) {
self.module_decls.push(mod_decl);
}

fn push_submodule(&mut self, submodule: SortedSubModule) {
Expand Down Expand Up @@ -474,7 +474,7 @@ impl std::fmt::Display for TopLevelStatement {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TopLevelStatement::Function(fun) => fun.fmt(f),
TopLevelStatement::Module(m) => write!(f, "mod {m}"),
TopLevelStatement::Module(m) => m.fmt(f),
TopLevelStatement::Import(tree) => write!(f, "use {tree}"),
TopLevelStatement::Trait(t) => t.fmt(f),
TopLevelStatement::TraitImpl(i) => i.fmt(f),
Expand Down
10 changes: 6 additions & 4 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ use crate::parser::{force, ignore_then_commit, statement_recovery};
use crate::token::{Keyword, Token, TokenKind};
use crate::{
BinaryOp, BinaryOpKind, BlockExpression, Distinctness, ForLoopStatement, ForRange,
FunctionReturnType, Ident, IfExpression, InfixExpression, LValue, Literal, NoirTypeAlias,
Param, Path, Pattern, Recoverable, Statement, TraitBound, TypeImpl, UnresolvedTraitConstraint,
UnresolvedTypeExpression, UseTree, UseTreeKind, Visibility,
FunctionReturnType, Ident, IfExpression, InfixExpression, LValue, Literal, ModuleDeclaration,
NoirTypeAlias, Param, Path, Pattern, Recoverable, Statement, TraitBound, TypeImpl,
UnresolvedTraitConstraint, UnresolvedTypeExpression, UseTree, UseTreeKind, Visibility,
};

use chumsky::prelude::*;
Expand Down Expand Up @@ -370,7 +370,9 @@ fn optional_type_annotation<'a>() -> impl NoirParser<UnresolvedType> + 'a {
}

fn module_declaration() -> impl NoirParser<TopLevelStatement> {
keyword(Keyword::Mod).ignore_then(ident()).map(TopLevelStatement::Module)
keyword(Keyword::Mod)
.ignore_then(ident())
.map(|ident| TopLevelStatement::Module(ModuleDeclaration { ident }))
}

fn use_statement() -> impl NoirParser<TopLevelStatement> {
Expand Down