Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add ModuleDeclaration struct #4512

Merged
merged 1 commit into from
Mar 7, 2024
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 @@
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 @@ -458,7 +458,7 @@
}
}
TraitItem::Type { name } => {
// TODO(nickysn or alexvitkov): implement context.def_interner.push_empty_type_alias and get an id, instead of using TypeAliasId::dummy_id()

Check warning on line 461 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (nickysn)

Check warning on line 461 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (alexvitkov)
if let Err((first_def, second_def)) = self.def_collector.def_map.modules
[trait_id.0.local_id.0]
.declare_type_alias(name.clone(), TypeAliasId::dummy_id())
Expand Down Expand Up @@ -522,33 +522,33 @@
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 @@
);

// 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
Loading