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

feat: visibility for modules #6165

Merged
merged 9 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ pub trait Recoverable {

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ModuleDeclaration {
pub visibility: ItemVisibility,
pub ident: Ident,
pub outer_attributes: Vec<SecondaryAttribute>,
}
Expand Down
9 changes: 8 additions & 1 deletion compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@
let trait_id = match self.push_child_module(
context,
&name,
ItemVisibility::Public,
Location::new(name.span(), self.file_id),
Vec::new(),
Vec::new(),
Expand Down Expand Up @@ -612,6 +613,7 @@
match self.push_child_module(
context,
&submodule.name,
submodule.visibility,
Location::new(submodule.name.span(), file_id),
submodule.outer_attributes.clone(),
submodule.contents.inner_attributes.clone(),
Expand Down Expand Up @@ -711,6 +713,7 @@
match self.push_child_module(
context,
&mod_decl.ident,
mod_decl.visibility,
Location::new(Span::empty(0), child_file_id),
mod_decl.outer_attributes.clone(),
ast.inner_attributes.clone(),
Expand Down Expand Up @@ -761,6 +764,7 @@
&mut self,
context: &mut Context,
mod_name: &Ident,
visibility: ItemVisibility,
mod_location: Location,
outer_attributes: Vec<SecondaryAttribute>,
inner_attributes: Vec<SecondaryAttribute>,
Expand All @@ -772,6 +776,7 @@
&mut self.def_collector.def_map,
self.module_id,
mod_name,
visibility,
mod_location,
outer_attributes,
inner_attributes,
Expand Down Expand Up @@ -806,6 +811,7 @@
def_map: &mut CrateDefMap,
parent: LocalModuleId,
mod_name: &Ident,
visibility: ItemVisibility,
mod_location: Location,
outer_attributes: Vec<SecondaryAttribute>,
inner_attributes: Vec<SecondaryAttribute>,
Expand All @@ -817,7 +823,7 @@
// if it's an inline module, or the first char of a the file if it's an external module.
// - `location` will always point to the token "foo" in `mod foo` regardless of whether
// it's inline or external.
// Eventually the location put in `ModuleData` is used for codelenses about `contract`s,

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (codelenses)
// so we keep using `location` so that it continues to work as usual.
let location = Location::new(mod_name.span(), mod_location.file);
let new_module =
Expand All @@ -840,7 +846,7 @@
// the struct name.
if add_to_parent_scope {
if let Err((first_def, second_def)) =
modules[parent.0].declare_child_module(mod_name.to_owned(), mod_id)
modules[parent.0].declare_child_module(mod_name.to_owned(), visibility, mod_id)
{
let err = DefCollectorErrorKind::Duplicate {
typ: DuplicateType::Module,
Expand Down Expand Up @@ -952,6 +958,7 @@
def_map,
module_id,
&name,
ItemVisibility::Public,
location,
Vec::new(),
Vec::new(),
Expand Down
3 changes: 2 additions & 1 deletion compiler/noirc_frontend/src/hir/def_map/module_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@ impl ModuleData {
pub fn declare_child_module(
&mut self,
name: Ident,
visibility: ItemVisibility,
child_id: ModuleId,
) -> Result<(), (Ident, Ident)> {
self.declare(name, ItemVisibility::Public, child_id.into(), None)
self.declare(name, visibility, child_id.into(), None)
}

pub fn find_func_with_name(&self, name: &Ident) -> Option<FuncId> {
Expand Down
3 changes: 3 additions & 0 deletions compiler/noirc_frontend/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ pub enum ItemKind {
/// These submodules always share the same file as some larger ParsedModule
#[derive(Clone, Debug)]
pub struct ParsedSubModule {
pub visibility: ItemVisibility,
pub name: Ident,
pub contents: ParsedModule,
pub outer_attributes: Vec<SecondaryAttribute>,
Expand All @@ -375,6 +376,7 @@ pub struct ParsedSubModule {
impl ParsedSubModule {
pub fn into_sorted(self) -> SortedSubModule {
SortedSubModule {
visibility: self.visibility,
name: self.name,
contents: self.contents.into_sorted(),
outer_attributes: self.outer_attributes,
Expand All @@ -398,6 +400,7 @@ impl std::fmt::Display for SortedSubModule {
#[derive(Clone)]
pub struct SortedSubModule {
pub name: Ident,
pub visibility: ItemVisibility,
pub contents: SortedModule,
pub outer_attributes: Vec<SecondaryAttribute>,
pub is_contract: bool,
Expand Down
18 changes: 13 additions & 5 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,16 @@ fn submodule(
module_parser: impl NoirParser<ParsedModule>,
) -> impl NoirParser<TopLevelStatementKind> {
attributes()
.then(item_visibility())
.then_ignore(keyword(Keyword::Mod))
.then(ident())
.then_ignore(just(Token::LeftBrace))
.then(module_parser)
.then_ignore(just(Token::RightBrace))
.validate(|((attributes, name), contents), span, emit| {
.validate(|(((attributes, visibility), name), contents), span, emit| {
let attributes = validate_secondary_attributes(attributes, span, emit);
TopLevelStatementKind::SubModule(ParsedSubModule {
visibility,
name,
contents,
outer_attributes: attributes,
Expand All @@ -283,14 +285,16 @@ fn contract(
module_parser: impl NoirParser<ParsedModule>,
) -> impl NoirParser<TopLevelStatementKind> {
attributes()
.then(item_visibility())
.then_ignore(keyword(Keyword::Contract))
.then(ident())
.then_ignore(just(Token::LeftBrace))
.then(module_parser)
.then_ignore(just(Token::RightBrace))
.validate(|((attributes, name), contents), span, emit| {
.validate(|(((attributes, visibility), name), contents), span, emit| {
let attributes = validate_secondary_attributes(attributes, span, emit);
TopLevelStatementKind::SubModule(ParsedSubModule {
visibility,
name,
contents,
outer_attributes: attributes,
Expand Down Expand Up @@ -431,10 +435,14 @@ fn optional_type_annotation<'a>() -> impl NoirParser<UnresolvedType> + 'a {
}

fn module_declaration() -> impl NoirParser<TopLevelStatementKind> {
attributes().then_ignore(keyword(Keyword::Mod)).then(ident()).validate(
|(attributes, ident), span, emit| {
attributes().then(item_visibility()).then_ignore(keyword(Keyword::Mod)).then(ident()).validate(
|((attributes, visibility), ident), span, emit| {
let attributes = validate_secondary_attributes(attributes, span, emit);
TopLevelStatementKind::Module(ModuleDeclaration { ident, outer_attributes: attributes })
TopLevelStatementKind::Module(ModuleDeclaration {
visibility,
ident,
outer_attributes: attributes,
})
},
)
}
Expand Down
12 changes: 11 additions & 1 deletion docs/docs/noir/modules_packages_crates/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,14 @@ fn main() {
}
```

In this example, the module `some_module` re-exports two public names defined in `foo`.
In this example, the module `some_module` re-exports two public names defined in `foo`.

### Visibility

By default, like functions, modules are private to the module (or crate) the exist in. You can use `pub`
to make the module public or `pub(crate)` to make it public to just its crate:

```rust
// This module is now public and can be seen by other crates.
pub mod foo;
```
8 changes: 4 additions & 4 deletions noir_stdlib/src/collections/mod.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod vec;
mod bounded_vec;
mod map;
mod umap;
pub mod vec;
pub mod bounded_vec;
pub mod map;
pub mod umap;
2 changes: 1 addition & 1 deletion noir_stdlib/src/ec/consts/mod.nr
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod te;
pub mod te;
8 changes: 4 additions & 4 deletions noir_stdlib/src/ec/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// Overview
// ========
// The following three elliptic curve representations are admissible:
mod tecurve; // Twisted Edwards curves
mod swcurve; // Elliptic curves in Short Weierstrass form
mod montcurve; // Montgomery curves
mod consts; // Commonly used curve presets
pub mod tecurve; // Twisted Edwards curves
pub mod swcurve; // Elliptic curves in Short Weierstrass form
pub mod montcurve; // Montgomery curves
pub mod consts; // Commonly used curve presets
//
// Note that Twisted Edwards and Montgomery curves are (birationally) equivalent, so that
// they may be freely converted between one another, whereas Short Weierstrass curves are
Expand Down
4 changes: 2 additions & 2 deletions noir_stdlib/src/ec/montcurve.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod affine {
pub mod affine {
// Affine representation of Montgomery curves
// Points are represented by two-dimensional Cartesian coordinates.
// All group operations are induced by those of the corresponding Twisted Edwards curve.
Expand Down Expand Up @@ -210,7 +210,7 @@ mod affine {
}
}
}
mod curvegroup {
pub mod curvegroup {
// Affine representation of Montgomery curves
// Points are represented by three-dimensional projective (homogeneous) coordinates.
// All group operations are induced by those of the corresponding Twisted Edwards curve.
Expand Down
4 changes: 2 additions & 2 deletions noir_stdlib/src/ec/swcurve.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod affine {
pub mod affine {
// Affine representation of Short Weierstrass curves
// Points are represented by two-dimensional Cartesian coordinates.
// Group operations are implemented in terms of those in CurveGroup (in this case, extended Twisted Edwards) coordinates
Expand Down Expand Up @@ -186,7 +186,7 @@ mod affine {
}
}

mod curvegroup {
pub mod curvegroup {
// CurveGroup representation of Weierstrass curves
// Points are represented by three-dimensional Jacobian coordinates.
// See <https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates> for details.
Expand Down
4 changes: 2 additions & 2 deletions noir_stdlib/src/ec/tecurve.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod affine {
pub mod affine {
// Affine coordinate representation of Twisted Edwards curves
// Points are represented by two-dimensional Cartesian coordinates.
// Group operations are implemented in terms of those in CurveGroup (in this case, extended Twisted Edwards) coordinates
Expand Down Expand Up @@ -193,7 +193,7 @@ mod affine {
}
}
}
mod curvegroup {
pub mod curvegroup {
// CurveGroup coordinate representation of Twisted Edwards curves
// Points are represented by four-dimensional projective coordinates, viz. extended Twisted Edwards coordinates.
// See section 3 of <https://eprint.iacr.org/2008/522.pdf> for details.
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/field/mod.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod bn254;
pub mod bn254;
use bn254::lt as bn254_lt;

impl Field {
Expand Down
17 changes: 7 additions & 10 deletions noir_stdlib/src/hash/mod.nr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod poseidon;
mod mimc;
mod poseidon2;
mod keccak;
mod sha256;
mod sha512;
pub mod poseidon;
pub mod mimc;
pub mod poseidon2;
pub mod keccak;
pub mod sha256;
pub mod sha512;

use crate::default::Default;
use crate::uint128::U128;
Expand Down Expand Up @@ -96,10 +96,7 @@ pub fn derive_generators<let N: u32, let M: u32>(domain_separator_bytes: [u8; M]

#[builtin(derive_pedersen_generators)]
#[field(bn254)]
fn __derive_generators<let N: u32, let M: u32>(
domain_separator_bytes: [u8; M],
starting_index: u32
) -> [EmbeddedCurvePoint; N] {}
fn __derive_generators<let N: u32, let M: u32>(domain_separator_bytes: [u8; M], starting_index: u32) -> [EmbeddedCurvePoint; N] {}

#[field(bn254)]
// Same as from_field but:
Expand Down
4 changes: 2 additions & 2 deletions noir_stdlib/src/hash/poseidon/bn254.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Instantiations of Poseidon constants, permutations and sponge for prime field of the same order as BN254
mod perm;
mod consts;
pub mod perm;
pub mod consts;

use crate::hash::poseidon::absorb;

Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/hash/poseidon/mod.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod bn254; // Instantiations of Poseidon for prime field of the same order as BN254
pub mod bn254; // Instantiations of Poseidon for prime field of the same order as BN254
use crate::hash::Hasher;
use crate::default::Default;

Expand Down
62 changes: 31 additions & 31 deletions noir_stdlib/src/lib.nr
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
mod hash;
mod aes128;
mod array;
mod slice;
mod merkle;
mod schnorr;
mod ecdsa_secp256k1;
mod ecdsa_secp256r1;
mod eddsa;
mod embedded_curve_ops;
mod sha256;
mod sha512;
mod field;
mod ec;
mod collections;
mod compat;
mod convert;
mod option;
mod string;
mod test;
mod cmp;
mod ops;
mod default;
mod prelude;
mod uint128;
mod bigint;
mod runtime;
mod meta;
mod append;
mod mem;
mod panic;
pub mod hash;
pub mod aes128;
pub mod array;
pub mod slice;
pub mod merkle;
pub mod schnorr;
pub mod ecdsa_secp256k1;
pub mod ecdsa_secp256r1;
pub mod eddsa;
pub mod embedded_curve_ops;
pub mod sha256;
pub mod sha512;
pub mod field;
pub mod ec;
pub mod collections;
pub mod compat;
pub mod convert;
pub mod option;
pub mod string;
pub mod test;
pub mod cmp;
pub mod ops;
pub mod default;
pub mod prelude;
pub mod uint128;
pub mod bigint;
pub mod runtime;
pub mod meta;
pub mod append;
pub mod mem;
pub mod panic;

// Oracle calls are required to be wrapped in an unconstrained function
// Thus, the only argument to the `println` oracle is expected to always be an ident
Expand Down
28 changes: 14 additions & 14 deletions noir_stdlib/src/meta/mod.nr
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
mod ctstring;
mod expr;
mod format_string;
mod function_def;
mod module;
mod op;
mod struct_def;
mod trait_constraint;
mod trait_def;
mod trait_impl;
mod typ;
mod typed_expr;
mod quoted;
mod unresolved_type;
pub mod ctstring;
pub mod expr;
pub mod format_string;
pub mod function_def;
pub mod module;
pub mod op;
pub mod struct_def;
pub mod trait_constraint;
pub mod trait_def;
pub mod trait_impl;
pub mod typ;
pub mod typed_expr;
pub mod quoted;
pub mod unresolved_type;

/// Calling unquote as a macro (via `unquote!(arg)`) will unquote
/// its argument. Since this is the effect `!` already does, `unquote`
Expand Down
Loading
Loading