From 12a1c6fa9068f7bd19d2214f7893bb7ad3d31ea8 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Mon, 30 Sep 2024 16:12:26 -0300 Subject: [PATCH 01/10] feat: visibility for impl functions --- compiler/noirc_frontend/src/elaborator/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/noirc_frontend/src/elaborator/mod.rs b/compiler/noirc_frontend/src/elaborator/mod.rs index c5e457c405b..b983527abb0 100644 --- a/compiler/noirc_frontend/src/elaborator/mod.rs +++ b/compiler/noirc_frontend/src/elaborator/mod.rs @@ -1134,7 +1134,7 @@ impl<'context> Elaborator<'context> { // object types in each method overlap or not. If they do, we issue an error. // If not, that is specialization which is allowed. let name = method.name_ident().clone(); - if module.declare_function(name, ItemVisibility::Public, *method_id).is_err() { + if module.declare_function(name, method.def.visibility, *method_id).is_err() { let existing = module.find_func_with_name(method.name_ident()).expect( "declare_function should only error if there is an existing function", ); From 3223b5bfecd423384089d873c8c1453286e67cab Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Tue, 1 Oct 2024 15:22:41 -0300 Subject: [PATCH 02/10] Don't error if calling struct private fn from same struct --- .../noirc_frontend/src/elaborator/scope.rs | 2 +- .../src/hir/def_collector/dc_crate.rs | 2 + .../src/hir/def_collector/dc_mod.rs | 1 + .../src/hir/resolution/import.rs | 47 +++++++++++-------- .../src/hir/resolution/path_resolver.rs | 18 +++++-- .../noirc_frontend/src/tests/visibility.rs | 24 +++++++++- noir_stdlib/src/bigint.nr | 12 ++--- noir_stdlib/src/embedded_curve_ops.nr | 4 +- noir_stdlib/src/hash/poseidon2.nr | 2 +- noir_stdlib/src/uint128.nr | 2 +- tooling/lsp/src/attribute_reference_finder.rs | 2 +- 11 files changed, 80 insertions(+), 36 deletions(-) diff --git a/compiler/noirc_frontend/src/elaborator/scope.rs b/compiler/noirc_frontend/src/elaborator/scope.rs index 0fb5a58035a..f13cb00f070 100644 --- a/compiler/noirc_frontend/src/elaborator/scope.rs +++ b/compiler/noirc_frontend/src/elaborator/scope.rs @@ -90,7 +90,7 @@ impl<'context> Elaborator<'context> { } fn resolve_path_in_module(&mut self, path: Path, module_id: ModuleId) -> PathResolutionResult { - let resolver = StandardPathResolver::new(module_id); + let resolver = StandardPathResolver::new(module_id, self.self_type.clone()); if !self.interner.lsp_mode { return resolver.resolve( diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs index 267c9bc84b8..5cd65462074 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs @@ -547,6 +547,7 @@ fn inject_prelude( if let Ok(PathResolution { module_def_id, error }) = path_resolver::resolve_path( &context.def_maps, ModuleId { krate: crate_id, local_id: crate_root }, + None, path, &mut context.def_interner.usage_tracker, &mut None, @@ -564,6 +565,7 @@ fn inject_prelude( ImportDirective { visibility: ItemVisibility::Private, module_id: crate_root, + self_type: None, path: Path { segments, kind: PathKind::Plain, span: Span::default() }, alias: None, is_prelude: true, diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs index dff63e045fe..cc1f2506879 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs @@ -81,6 +81,7 @@ pub fn collect_defs( collector.def_collector.imports.push(ImportDirective { visibility: import.visibility, module_id: collector.module_id, + self_type: None, path: import.path, alias: import.alias, is_prelude: false, diff --git a/compiler/noirc_frontend/src/hir/resolution/import.rs b/compiler/noirc_frontend/src/hir/resolution/import.rs index 73a1b1ccb7c..592f952f75a 100644 --- a/compiler/noirc_frontend/src/hir/resolution/import.rs +++ b/compiler/noirc_frontend/src/hir/resolution/import.rs @@ -5,6 +5,7 @@ use crate::graph::CrateId; use crate::hir::def_collector::dc_crate::CompilationError; use crate::node_interner::ReferenceId; use crate::usage_tracker::UsageTracker; +use crate::Type; use std::collections::BTreeMap; use crate::ast::{Ident, ItemVisibility, Path, PathKind, PathSegment}; @@ -16,6 +17,7 @@ use super::errors::ResolverError; pub struct ImportDirective { pub visibility: ItemVisibility, pub module_id: LocalModuleId, + pub self_type: Option, pub path: Path, pub alias: Option, pub is_prelude: bool, @@ -92,18 +94,15 @@ pub fn resolve_import( path_references: &mut Option<&mut Vec>, ) -> Result { let module_scope = import_directive.module_id; - let NamespaceResolution { - module_id: resolved_module, - namespace: resolved_namespace, - mut error, - } = resolve_path_to_ns( - import_directive, - crate_id, - crate_id, - def_maps, - usage_tracker, - path_references, - )?; + let NamespaceResolution { module_id: resolved_module, namespace: resolved_namespace, error } = + resolve_path_to_ns( + import_directive, + crate_id, + crate_id, + def_maps, + usage_tracker, + path_references, + )?; let name = resolve_path_name(import_directive); @@ -113,14 +112,21 @@ pub fn resolve_import( .map(|(_, visibility, _)| visibility) .expect("Found empty namespace"); - error = error.or_else(|| { - if can_reference_module_id( - def_maps, - crate_id, - import_directive.module_id, - resolved_module, - visibility, - ) { + let error = error.or_else(|| { + let self_type_module_id = match &import_directive.self_type { + Some(Type::Struct(struct_type, _)) => Some(struct_type.borrow().id.module_id()), + _ => None, + }; + + if self_type_module_id == Some(resolved_module) + || can_reference_module_id( + def_maps, + crate_id, + import_directive.module_id, + resolved_module, + visibility, + ) + { None } else { Some(PathResolutionError::Private(name.clone())) @@ -408,6 +414,7 @@ fn resolve_external_dep( let dep_directive = ImportDirective { visibility: ItemVisibility::Private, module_id: dep_module.local_id, + self_type: directive.self_type.clone(), path, alias: directive.alias.clone(), is_prelude: false, diff --git a/compiler/noirc_frontend/src/hir/resolution/path_resolver.rs b/compiler/noirc_frontend/src/hir/resolution/path_resolver.rs index 50089d849ae..ad3b98f73e4 100644 --- a/compiler/noirc_frontend/src/hir/resolution/path_resolver.rs +++ b/compiler/noirc_frontend/src/hir/resolution/path_resolver.rs @@ -2,6 +2,7 @@ use super::import::{resolve_import, ImportDirective, PathResolution, PathResolut use crate::ast::{ItemVisibility, Path}; use crate::node_interner::ReferenceId; use crate::usage_tracker::UsageTracker; +use crate::Type; use std::collections::BTreeMap; use crate::graph::CrateId; @@ -28,11 +29,13 @@ pub trait PathResolver { pub struct StandardPathResolver { // Module that we are resolving the path in module_id: ModuleId, + // The type we are in, + self_type: Option, } impl StandardPathResolver { - pub fn new(module_id: ModuleId) -> StandardPathResolver { - Self { module_id } + pub fn new(module_id: ModuleId, self_type: Option) -> StandardPathResolver { + Self { module_id, self_type } } } @@ -44,7 +47,14 @@ impl PathResolver for StandardPathResolver { usage_tracker: &mut UsageTracker, path_references: &mut Option<&mut Vec>, ) -> PathResolutionResult { - resolve_path(def_maps, self.module_id, path, usage_tracker, path_references) + resolve_path( + def_maps, + self.module_id, + self.self_type.clone(), + path, + usage_tracker, + path_references, + ) } fn local_module_id(&self) -> LocalModuleId { @@ -61,6 +71,7 @@ impl PathResolver for StandardPathResolver { pub fn resolve_path( def_maps: &BTreeMap, module_id: ModuleId, + self_type: Option, path: Path, usage_tracker: &mut UsageTracker, path_references: &mut Option<&mut Vec>, @@ -69,6 +80,7 @@ pub fn resolve_path( let import = ImportDirective { visibility: ItemVisibility::Private, module_id: module_id.local_id, + self_type, path, alias: None, is_prelude: false, diff --git a/compiler/noirc_frontend/src/tests/visibility.rs b/compiler/noirc_frontend/src/tests/visibility.rs index e6c2680ea19..ae2d2161269 100644 --- a/compiler/noirc_frontend/src/tests/visibility.rs +++ b/compiler/noirc_frontend/src/tests/visibility.rs @@ -3,7 +3,7 @@ use crate::{ def_collector::{dc_crate::CompilationError, errors::DefCollectorErrorKind}, resolution::{errors::ResolverError, import::PathResolutionError}, }, - tests::get_program_errors, + tests::{assert_no_errors, get_program_errors}, }; #[test] @@ -111,3 +111,25 @@ fn errors_if_trying_to_access_public_function_inside_private_module() { assert_eq!(ident.to_string(), "bar"); } + +#[test] +fn does_not_error_if_calling_private_struct_function_from_same_struct() { + let src = r#" + struct Foo { + + } + + impl Foo { + fn foo() { + Foo::bar() + } + + fn bar() {} + } + + fn main() { + let _ = Foo {}; + } + "#; + assert_no_errors(src); +} diff --git a/noir_stdlib/src/bigint.nr b/noir_stdlib/src/bigint.nr index 0015f480794..ad6f1fa2c1b 100644 --- a/noir_stdlib/src/bigint.nr +++ b/noir_stdlib/src/bigint.nr @@ -20,17 +20,17 @@ pub struct BigInt { impl BigInt { #[builtin(bigint_add)] - fn bigint_add(self, other: BigInt) -> BigInt {} + pub fn bigint_add(self, other: BigInt) -> BigInt {} #[builtin(bigint_sub)] - fn bigint_sub(self, other: BigInt) -> BigInt {} + pub fn bigint_sub(self, other: BigInt) -> BigInt {} #[builtin(bigint_mul)] - fn bigint_mul(self, other: BigInt) -> BigInt {} + pub fn bigint_mul(self, other: BigInt) -> BigInt {} #[builtin(bigint_div)] - fn bigint_div(self, other: BigInt) -> BigInt {} + pub fn bigint_div(self, other: BigInt) -> BigInt {} #[builtin(bigint_from_le_bytes)] - fn from_le_bytes(bytes: [u8], modulus: [u8]) -> BigInt {} + pub fn from_le_bytes(bytes: [u8], modulus: [u8]) -> BigInt {} #[builtin(bigint_to_le_bytes)] - fn to_le_bytes(self) -> [u8; 32] {} + pub fn to_le_bytes(self) -> [u8; 32] {} fn check_32_bytes(self: Self, other: BigInt) -> bool { let bytes = self.to_le_bytes(); diff --git a/noir_stdlib/src/embedded_curve_ops.nr b/noir_stdlib/src/embedded_curve_ops.nr index 445b6d96554..78d67bffeb4 100644 --- a/noir_stdlib/src/embedded_curve_ops.nr +++ b/noir_stdlib/src/embedded_curve_ops.nr @@ -67,14 +67,14 @@ impl EmbeddedCurveScalar { } #[field(bn254)] - fn from_field(scalar: Field) -> EmbeddedCurveScalar { + pub fn from_field(scalar: Field) -> EmbeddedCurveScalar { let (a,b) = crate::field::bn254::decompose(scalar); EmbeddedCurveScalar { lo: a, hi: b } } //Bytes to scalar: take the first (after the specified offset) 16 bytes of the input as the lo value, and the next 16 bytes as the hi value #[field(bn254)] - fn from_bytes(bytes: [u8; 64], offset: u32) -> EmbeddedCurveScalar { + pub fn from_bytes(bytes: [u8; 64], offset: u32) -> EmbeddedCurveScalar { let mut v = 1; let mut lo = 0 as Field; let mut hi = 0 as Field; diff --git a/noir_stdlib/src/hash/poseidon2.nr b/noir_stdlib/src/hash/poseidon2.nr index 6b61ca32302..fb813120fef 100644 --- a/noir_stdlib/src/hash/poseidon2.nr +++ b/noir_stdlib/src/hash/poseidon2.nr @@ -20,7 +20,7 @@ impl Poseidon2 { } } - fn new(iv: Field) -> Poseidon2 { + pub(crate) fn new(iv: Field) -> Poseidon2 { let mut result = Poseidon2 { cache: [0; 3], state: [0; 4], cache_size: 0, squeeze_mode: false }; result.state[RATE] = iv; result diff --git a/noir_stdlib/src/uint128.nr b/noir_stdlib/src/uint128.nr index a3d7491eaff..9cb94567d94 100644 --- a/noir_stdlib/src/uint128.nr +++ b/noir_stdlib/src/uint128.nr @@ -98,7 +98,7 @@ impl U128 { ((ascii >= 65) & (ascii <= 90)) // Between 'A' and 'Z' } - fn decode_ascii(ascii: u8) -> Field { + pub(crate) fn decode_ascii(ascii: u8) -> Field { (if ascii < 58 { ascii - 48 } else { diff --git a/tooling/lsp/src/attribute_reference_finder.rs b/tooling/lsp/src/attribute_reference_finder.rs index f08c8073a79..0387d35d41f 100644 --- a/tooling/lsp/src/attribute_reference_finder.rs +++ b/tooling/lsp/src/attribute_reference_finder.rs @@ -103,7 +103,7 @@ impl<'a> Visitor for AttributeReferenceFinder<'a> { return; }; - let resolver = StandardPathResolver::new(self.module_id); + let resolver = StandardPathResolver::new(self.module_id, None); let mut usage_tracker = UsageTracker::default(); let Ok(result) = resolver.resolve(self.def_maps, path, &mut usage_tracker, &mut None) else { From 50999da6aca27acfca30d2a0f085f14c115ec6df Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Tue, 1 Oct 2024 15:43:43 -0300 Subject: [PATCH 03/10] Fix tests --- compiler/noirc_frontend/src/tests/turbofish.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/noirc_frontend/src/tests/turbofish.rs b/compiler/noirc_frontend/src/tests/turbofish.rs index 43d536fd196..e0cac726089 100644 --- a/compiler/noirc_frontend/src/tests/turbofish.rs +++ b/compiler/noirc_frontend/src/tests/turbofish.rs @@ -29,11 +29,11 @@ fn turbofish_numeric_generic_nested_call() { } impl Foo { - fn static_method() -> [u8; N] { + pub fn static_method() -> [u8; N] { [0; N] } - fn impl_method(self) -> [T; N] { + pub fn impl_method(self) -> [T; N] { [self.a; N] } } @@ -108,7 +108,7 @@ fn turbofish_in_middle_of_variable_unsupported_yet() { } impl Foo { - fn new(x: T) -> Self { + pub fn new(x: T) -> Self { Foo { x } } } From d69e2242c116d63b0a2a688cfb8cad03dc326bda Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Tue, 1 Oct 2024 15:48:39 -0300 Subject: [PATCH 04/10] Pass module ID instead of having to clone type --- compiler/noirc_frontend/src/elaborator/scope.rs | 8 +++++++- .../src/hir/def_collector/dc_crate.rs | 2 +- .../src/hir/def_collector/dc_mod.rs | 2 +- .../noirc_frontend/src/hir/resolution/import.rs | 13 ++++--------- .../src/hir/resolution/path_resolver.rs | 16 ++++++++-------- 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/compiler/noirc_frontend/src/elaborator/scope.rs b/compiler/noirc_frontend/src/elaborator/scope.rs index f13cb00f070..b83a1494ab9 100644 --- a/compiler/noirc_frontend/src/elaborator/scope.rs +++ b/compiler/noirc_frontend/src/elaborator/scope.rs @@ -90,7 +90,13 @@ impl<'context> Elaborator<'context> { } fn resolve_path_in_module(&mut self, path: Path, module_id: ModuleId) -> PathResolutionResult { - let resolver = StandardPathResolver::new(module_id, self.self_type.clone()); + let self_type_module_id = if let Some(Type::Struct(struct_type, _)) = &self.self_type { + Some(struct_type.borrow().id.module_id()) + } else { + None + }; + + let resolver = StandardPathResolver::new(module_id, self_type_module_id); if !self.interner.lsp_mode { return resolver.resolve( diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs index 5cd65462074..9bd4484e804 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs @@ -565,7 +565,7 @@ fn inject_prelude( ImportDirective { visibility: ItemVisibility::Private, module_id: crate_root, - self_type: None, + self_type_module_id: None, path: Path { segments, kind: PathKind::Plain, span: Span::default() }, alias: None, is_prelude: true, diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs index cc1f2506879..e4300a2ac74 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs @@ -81,7 +81,7 @@ pub fn collect_defs( collector.def_collector.imports.push(ImportDirective { visibility: import.visibility, module_id: collector.module_id, - self_type: None, + self_type_module_id: None, path: import.path, alias: import.alias, is_prelude: false, diff --git a/compiler/noirc_frontend/src/hir/resolution/import.rs b/compiler/noirc_frontend/src/hir/resolution/import.rs index 592f952f75a..3bec800a83f 100644 --- a/compiler/noirc_frontend/src/hir/resolution/import.rs +++ b/compiler/noirc_frontend/src/hir/resolution/import.rs @@ -5,7 +5,7 @@ use crate::graph::CrateId; use crate::hir::def_collector::dc_crate::CompilationError; use crate::node_interner::ReferenceId; use crate::usage_tracker::UsageTracker; -use crate::Type; + use std::collections::BTreeMap; use crate::ast::{Ident, ItemVisibility, Path, PathKind, PathSegment}; @@ -17,7 +17,7 @@ use super::errors::ResolverError; pub struct ImportDirective { pub visibility: ItemVisibility, pub module_id: LocalModuleId, - pub self_type: Option, + pub self_type_module_id: Option, pub path: Path, pub alias: Option, pub is_prelude: bool, @@ -113,12 +113,7 @@ pub fn resolve_import( .expect("Found empty namespace"); let error = error.or_else(|| { - let self_type_module_id = match &import_directive.self_type { - Some(Type::Struct(struct_type, _)) => Some(struct_type.borrow().id.module_id()), - _ => None, - }; - - if self_type_module_id == Some(resolved_module) + if import_directive.self_type_module_id == Some(resolved_module) || can_reference_module_id( def_maps, crate_id, @@ -414,7 +409,7 @@ fn resolve_external_dep( let dep_directive = ImportDirective { visibility: ItemVisibility::Private, module_id: dep_module.local_id, - self_type: directive.self_type.clone(), + self_type_module_id: directive.self_type_module_id, path, alias: directive.alias.clone(), is_prelude: false, diff --git a/compiler/noirc_frontend/src/hir/resolution/path_resolver.rs b/compiler/noirc_frontend/src/hir/resolution/path_resolver.rs index ad3b98f73e4..562366fae77 100644 --- a/compiler/noirc_frontend/src/hir/resolution/path_resolver.rs +++ b/compiler/noirc_frontend/src/hir/resolution/path_resolver.rs @@ -2,7 +2,7 @@ use super::import::{resolve_import, ImportDirective, PathResolution, PathResolut use crate::ast::{ItemVisibility, Path}; use crate::node_interner::ReferenceId; use crate::usage_tracker::UsageTracker; -use crate::Type; + use std::collections::BTreeMap; use crate::graph::CrateId; @@ -29,13 +29,13 @@ pub trait PathResolver { pub struct StandardPathResolver { // Module that we are resolving the path in module_id: ModuleId, - // The type we are in, - self_type: Option, + // The module of the self type, if any (for example, the ModuleId of a struct) + self_type_module_id: Option, } impl StandardPathResolver { - pub fn new(module_id: ModuleId, self_type: Option) -> StandardPathResolver { - Self { module_id, self_type } + pub fn new(module_id: ModuleId, self_type_module_id: Option) -> StandardPathResolver { + Self { module_id, self_type_module_id } } } @@ -50,7 +50,7 @@ impl PathResolver for StandardPathResolver { resolve_path( def_maps, self.module_id, - self.self_type.clone(), + self.self_type_module_id, path, usage_tracker, path_references, @@ -71,7 +71,7 @@ impl PathResolver for StandardPathResolver { pub fn resolve_path( def_maps: &BTreeMap, module_id: ModuleId, - self_type: Option, + self_type_module_id: Option, path: Path, usage_tracker: &mut UsageTracker, path_references: &mut Option<&mut Vec>, @@ -80,7 +80,7 @@ pub fn resolve_path( let import = ImportDirective { visibility: ItemVisibility::Private, module_id: module_id.local_id, - self_type, + self_type_module_id, path, alias: None, is_prelude: false, From d576a2096a790f2a97760f168bc3f819c2b927dc Mon Sep 17 00:00:00 2001 From: Tom French Date: Wed, 2 Oct 2024 12:49:48 +0100 Subject: [PATCH 05/10] chore: add new test case --- .../noirc_frontend/src/tests/visibility.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/compiler/noirc_frontend/src/tests/visibility.rs b/compiler/noirc_frontend/src/tests/visibility.rs index ae2d2161269..98552a1745b 100644 --- a/compiler/noirc_frontend/src/tests/visibility.rs +++ b/compiler/noirc_frontend/src/tests/visibility.rs @@ -133,3 +133,21 @@ fn does_not_error_if_calling_private_struct_function_from_same_struct() { "#; assert_no_errors(src); } + +#[test] +fn does_not_error_if_calling_private_struct_function_from_same_module() { + let src = r#" + struct Foo; + + impl Foo { + fn bar() -> Field { + 0 + } + } + + fn main() { + assert_eq(Foo::bar(), 0); + } + "#; + assert_no_errors(src); +} From 385f5a615d8ae42a41cc93d89aabc8aa1e41cce2 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2024 10:51:28 -0300 Subject: [PATCH 06/10] Make sure a module can call private functions of its structs --- .../noirc_frontend/src/hir/comptime/tests.rs | 3 ++- .../src/hir/def_collector/dc_mod.rs | 25 ++++++++++++++----- .../noirc_frontend/src/hir/def_map/mod.rs | 3 ++- .../src/hir/def_map/module_data.rs | 5 ++++ .../src/hir/resolution/import.rs | 18 +++++++++++-- compiler/noirc_frontend/src/tests.rs | 3 ++- 6 files changed, 46 insertions(+), 11 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/tests.rs b/compiler/noirc_frontend/src/hir/comptime/tests.rs index 5b03b27e0b2..3e3bacb5ed3 100644 --- a/compiler/noirc_frontend/src/hir/comptime/tests.rs +++ b/compiler/noirc_frontend/src/hir/comptime/tests.rs @@ -28,7 +28,8 @@ fn interpret_helper(src: &str) -> Result { location, Vec::new(), Vec::new(), - false, + false, // is contract + false, // is struct ))); assert_eq!(root, module_id); diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs index e4300a2ac74..41eecabadc1 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs @@ -386,7 +386,8 @@ impl<'a> ModCollector<'a> { Vec::new(), Vec::new(), false, - false, + false, // is contract + false, // is struct ) { Ok(module_id) => TraitId(ModuleId { krate, local_id: module_id.local_id }), Err(error) => { @@ -619,6 +620,7 @@ impl<'a> ModCollector<'a> { submodule.contents.inner_attributes.clone(), true, submodule.is_contract, + false, // is struct ) { Ok(child) => { self.collect_attributes( @@ -718,7 +720,8 @@ impl<'a> ModCollector<'a> { mod_decl.outer_attributes.clone(), ast.inner_attributes.clone(), true, - false, + false, // is contract + false, // is struct ) { Ok(child_mod_id) => { self.collect_attributes( @@ -770,6 +773,7 @@ impl<'a> ModCollector<'a> { inner_attributes: Vec, add_to_parent_scope: bool, is_contract: bool, + is_struct: bool, ) -> Result { push_child_module( &mut context.def_interner, @@ -782,6 +786,7 @@ impl<'a> ModCollector<'a> { inner_attributes, add_to_parent_scope, is_contract, + is_struct, ) } @@ -817,6 +822,7 @@ fn push_child_module( inner_attributes: Vec, add_to_parent_scope: bool, is_contract: bool, + is_struct: bool, ) -> Result { // Note: the difference between `location` and `mod_location` is: // - `mod_location` will point to either the token "foo" in `mod foo { ... }` @@ -826,8 +832,14 @@ fn push_child_module( // Eventually the location put in `ModuleData` is used for codelenses about `contract`s, // 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 = - ModuleData::new(Some(parent), location, outer_attributes, inner_attributes, is_contract); + let new_module = ModuleData::new( + Some(parent), + location, + outer_attributes, + inner_attributes, + is_contract, + is_struct, + ); let module_id = def_map.modules.insert(new_module); let modules = &mut def_map.modules; @@ -962,8 +974,9 @@ pub fn collect_struct( location, Vec::new(), Vec::new(), - false, - false, + false, // add to parent scope + false, // is contract + true, // is struct ) { Ok(module_id) => { interner.new_struct(&unresolved, resolved_generics, krate, module_id.local_id, file_id) diff --git a/compiler/noirc_frontend/src/hir/def_map/mod.rs b/compiler/noirc_frontend/src/hir/def_map/mod.rs index 9afe897a167..e9dfc5ac067 100644 --- a/compiler/noirc_frontend/src/hir/def_map/mod.rs +++ b/compiler/noirc_frontend/src/hir/def_map/mod.rs @@ -102,7 +102,8 @@ impl CrateDefMap { location, Vec::new(), ast.inner_attributes.clone(), - false, + false, // is contract + false, // is struct )); let def_map = CrateDefMap { diff --git a/compiler/noirc_frontend/src/hir/def_map/module_data.rs b/compiler/noirc_frontend/src/hir/def_map/module_data.rs index ff36bcf27d6..fe6fe8285d3 100644 --- a/compiler/noirc_frontend/src/hir/def_map/module_data.rs +++ b/compiler/noirc_frontend/src/hir/def_map/module_data.rs @@ -26,6 +26,9 @@ pub struct ModuleData { /// True if this module is a `contract Foo { ... }` module containing contract functions pub is_contract: bool, + /// True if this module is actually a struct + pub is_struct: bool, + pub attributes: Vec, } @@ -36,6 +39,7 @@ impl ModuleData { outer_attributes: Vec, inner_attributes: Vec, is_contract: bool, + is_struct: bool, ) -> ModuleData { let mut attributes = outer_attributes; attributes.extend(inner_attributes); @@ -47,6 +51,7 @@ impl ModuleData { definitions: ItemScope::default(), location, is_contract, + is_struct, attributes, } } diff --git a/compiler/noirc_frontend/src/hir/resolution/import.rs b/compiler/noirc_frontend/src/hir/resolution/import.rs index 3bec800a83f..f0b7f941ef8 100644 --- a/compiler/noirc_frontend/src/hir/resolution/import.rs +++ b/compiler/noirc_frontend/src/hir/resolution/import.rs @@ -444,11 +444,15 @@ pub fn can_reference_module_id( ItemVisibility::PublicCrate => same_crate, ItemVisibility::Private => { same_crate - && module_descendent_of_target( + && (module_descendent_of_target( target_crate_def_map, target_module.local_id, current_module, - ) + ) || module_is_parent_of_struct_module( + target_crate_def_map, + current_module, + target_module.local_id, + )) } } } @@ -468,3 +472,13 @@ fn module_descendent_of_target( .parent .map_or(false, |parent| module_descendent_of_target(def_map, target, parent)) } + +/// Returns true if `target` is a struct and its parent is `current`. +fn module_is_parent_of_struct_module( + def_map: &CrateDefMap, + current: LocalModuleId, + target: LocalModuleId, +) -> bool { + let module_data = &def_map.modules[target.0]; + module_data.is_struct && module_data.parent == Some(current) +} diff --git a/compiler/noirc_frontend/src/tests.rs b/compiler/noirc_frontend/src/tests.rs index 63013036ca9..adfb6f5b5a4 100644 --- a/compiler/noirc_frontend/src/tests.rs +++ b/compiler/noirc_frontend/src/tests.rs @@ -90,7 +90,8 @@ pub(crate) fn get_program(src: &str) -> (ParsedModule, Context, Vec<(Compilation location, Vec::new(), inner_attributes.clone(), - false, + false, // is contract + false, // is struct )); let def_map = CrateDefMap { From 6eeee568ce03661db7a37d9e3eb0f334b7d3ac61 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2024 11:04:04 -0300 Subject: [PATCH 07/10] Fix test --- compiler/noirc_frontend/src/tests/visibility.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/noirc_frontend/src/tests/visibility.rs b/compiler/noirc_frontend/src/tests/visibility.rs index 98552a1745b..c734b86265c 100644 --- a/compiler/noirc_frontend/src/tests/visibility.rs +++ b/compiler/noirc_frontend/src/tests/visibility.rs @@ -146,6 +146,7 @@ fn does_not_error_if_calling_private_struct_function_from_same_module() { } fn main() { + let _ = Foo {}; assert_eq(Foo::bar(), 0); } "#; From 29ffae443591016337ae42d7f5f4422dcf60ddc9 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2024 15:37:15 -0300 Subject: [PATCH 08/10] Add/remove `pub` --- noir_stdlib/src/bigint.nr | 12 ++--- noir_stdlib/src/embedded_curve_ops.nr | 4 +- noir_stdlib/src/meta/ctstring.nr | 8 ++-- noir_stdlib/src/meta/expr.nr | 64 ++++++++++++------------- noir_stdlib/src/meta/format_string.nr | 2 +- noir_stdlib/src/meta/function_def.nr | 26 +++++----- noir_stdlib/src/meta/module.nr | 12 ++--- noir_stdlib/src/meta/quoted.nr | 10 ++-- noir_stdlib/src/meta/struct_def.nr | 18 +++---- noir_stdlib/src/meta/trait_def.nr | 2 +- noir_stdlib/src/meta/trait_impl.nr | 8 ++-- noir_stdlib/src/meta/typ.nr | 42 ++++++++-------- noir_stdlib/src/meta/typed_expr.nr | 4 +- noir_stdlib/src/meta/unresolved_type.nr | 2 +- 14 files changed, 107 insertions(+), 107 deletions(-) diff --git a/noir_stdlib/src/bigint.nr b/noir_stdlib/src/bigint.nr index ad6f1fa2c1b..0015f480794 100644 --- a/noir_stdlib/src/bigint.nr +++ b/noir_stdlib/src/bigint.nr @@ -20,17 +20,17 @@ pub struct BigInt { impl BigInt { #[builtin(bigint_add)] - pub fn bigint_add(self, other: BigInt) -> BigInt {} + fn bigint_add(self, other: BigInt) -> BigInt {} #[builtin(bigint_sub)] - pub fn bigint_sub(self, other: BigInt) -> BigInt {} + fn bigint_sub(self, other: BigInt) -> BigInt {} #[builtin(bigint_mul)] - pub fn bigint_mul(self, other: BigInt) -> BigInt {} + fn bigint_mul(self, other: BigInt) -> BigInt {} #[builtin(bigint_div)] - pub fn bigint_div(self, other: BigInt) -> BigInt {} + fn bigint_div(self, other: BigInt) -> BigInt {} #[builtin(bigint_from_le_bytes)] - pub fn from_le_bytes(bytes: [u8], modulus: [u8]) -> BigInt {} + fn from_le_bytes(bytes: [u8], modulus: [u8]) -> BigInt {} #[builtin(bigint_to_le_bytes)] - pub fn to_le_bytes(self) -> [u8; 32] {} + fn to_le_bytes(self) -> [u8; 32] {} fn check_32_bytes(self: Self, other: BigInt) -> bool { let bytes = self.to_le_bytes(); diff --git a/noir_stdlib/src/embedded_curve_ops.nr b/noir_stdlib/src/embedded_curve_ops.nr index 78d67bffeb4..7941da94699 100644 --- a/noir_stdlib/src/embedded_curve_ops.nr +++ b/noir_stdlib/src/embedded_curve_ops.nr @@ -13,12 +13,12 @@ pub struct EmbeddedCurvePoint { impl EmbeddedCurvePoint { /// Elliptic curve point doubling operation /// returns the doubled point of a point P, i.e P+P - fn double(self) -> EmbeddedCurvePoint { + pub fn double(self) -> EmbeddedCurvePoint { embedded_curve_add(self, self) } /// Returns the null element of the curve; 'the point at infinity' - fn point_at_infinity() -> EmbeddedCurvePoint { + pub fn point_at_infinity() -> EmbeddedCurvePoint { EmbeddedCurvePoint { x: 0, y: 0, is_infinite: true } } } diff --git a/noir_stdlib/src/meta/ctstring.nr b/noir_stdlib/src/meta/ctstring.nr index ec205fa6f88..4b4854682db 100644 --- a/noir_stdlib/src/meta/ctstring.nr +++ b/noir_stdlib/src/meta/ctstring.nr @@ -2,20 +2,20 @@ use crate::append::Append; impl CtString { // docs:start:new - comptime fn new() -> Self { + pub comptime fn new() -> Self { // docs:end:new "".as_ctstring() } // Bug: using &mut self as the object results in this method not being found // docs:start:append_str - comptime fn append_str(self, s: str) -> Self { + pub comptime fn append_str(self, s: str) -> Self { // docs:end:append_str f"{self}{s}".as_ctstring() } // docs:start:append_fmtstr - comptime fn append_fmtstr(self, s: fmtstr) -> Self { + pub comptime fn append_fmtstr(self, s: fmtstr) -> Self { // docs:end:append_fmtstr f"{self}{s}".as_ctstring() } @@ -24,7 +24,7 @@ impl CtString { /// To get around this, we return a quoted str and the underlying str can /// be accessed using macro insertion `foo.as_quoted_str!()`. // docs:start:as_quoted_str - comptime fn as_quoted_str(self) -> Quoted { + pub comptime fn as_quoted_str(self) -> Quoted { // docs:end:as_quoted_str quote { $self } } diff --git a/noir_stdlib/src/meta/expr.nr b/noir_stdlib/src/meta/expr.nr index c96f7d27442..83a165fc533 100644 --- a/noir_stdlib/src/meta/expr.nr +++ b/noir_stdlib/src/meta/expr.nr @@ -8,89 +8,89 @@ impl Expr { /// If this expression is an array literal `[elem1, ..., elemN]`, this returns a slice of each element in the array. #[builtin(expr_as_array)] // docs:start:as_array - comptime fn as_array(self) -> Option<[Expr]> {} + pub comptime fn as_array(self) -> Option<[Expr]> {} // docs:end:as_array /// If this expression is an assert, this returns the assert expression and the optional message. #[builtin(expr_as_assert)] // docs:start:as_assert - comptime fn as_assert(self) -> Option<(Expr, Option)> {} + pub comptime fn as_assert(self) -> Option<(Expr, Option)> {} // docs:end:as_assert /// If this expression is an assert_eq, this returns the left-hand-side and right-hand-side /// expressions, together with the optional message. #[builtin(expr_as_assert_eq)] // docs:start:as_assert_eq - comptime fn as_assert_eq(self) -> Option<(Expr, Expr, Option)> {} + pub comptime fn as_assert_eq(self) -> Option<(Expr, Expr, Option)> {} // docs:end:as_assert_eq /// If this expression is an assignment, this returns a tuple with the left hand side /// and right hand side in order. #[builtin(expr_as_assign)] // docs:start:as_assign - comptime fn as_assign(self) -> Option<(Expr, Expr)> {} + pub comptime fn as_assign(self) -> Option<(Expr, Expr)> {} // docs:end:as_assign /// If this expression is a binary operator operation ` `, /// return the left-hand side, operator, and the right-hand side of the operation. #[builtin(expr_as_binary_op)] // docs:start:as_binary_op - comptime fn as_binary_op(self) -> Option<(Expr, BinaryOp, Expr)> {} + pub comptime fn as_binary_op(self) -> Option<(Expr, BinaryOp, Expr)> {} // docs:end:as_binary_op /// If this expression is a block `{ stmt1; stmt2; ...; stmtN }`, return /// a slice containing each statement. #[builtin(expr_as_block)] // docs:start:as_block - comptime fn as_block(self) -> Option<[Expr]> {} + pub comptime fn as_block(self) -> Option<[Expr]> {} // docs:end:as_block /// If this expression is a boolean literal, return that literal. #[builtin(expr_as_bool)] // docs:start:as_bool - comptime fn as_bool(self) -> Option {} + pub comptime fn as_bool(self) -> Option {} // docs:end:as_bool /// If this expression is a cast expression `expr as type`, returns the casted /// expression and the type to cast to. // docs:start:as_cast #[builtin(expr_as_cast)] - comptime fn as_cast(self) -> Option<(Expr, UnresolvedType)> {} + pub comptime fn as_cast(self) -> Option<(Expr, UnresolvedType)> {} // docs:end:as_cast /// If this expression is a `comptime { stmt1; stmt2; ...; stmtN }` block, /// return each statement in the block. #[builtin(expr_as_comptime)] // docs:start:as_comptime - comptime fn as_comptime(self) -> Option<[Expr]> {} + pub comptime fn as_comptime(self) -> Option<[Expr]> {} // docs:end:as_comptime /// If this expression is a constructor `Type { field1: expr1, ..., fieldN: exprN }`, /// return the type and the fields. #[builtin(expr_as_constructor)] // docs:start:as_constructor - comptime fn as_constructor(self) -> Option<(UnresolvedType, [(Quoted, Expr)])> {} + pub comptime fn as_constructor(self) -> Option<(UnresolvedType, [(Quoted, Expr)])> {} // docs:end:as_constructor /// If this expression is a for statement over a single expression, return the identifier, /// the expression and the for loop body. #[builtin(expr_as_for)] // docs:start:as_for - comptime fn as_for(self) -> Option<(Quoted, Expr, Expr)> {} + pub comptime fn as_for(self) -> Option<(Quoted, Expr, Expr)> {} // docs:end:as_for /// If this expression is a for statement over a range, return the identifier, /// the range start, the range end and the for loop body. #[builtin(expr_as_for_range)] // docs:start:as_for_range - comptime fn as_for_range(self) -> Option<(Quoted, Expr, Expr, Expr)> {} + pub comptime fn as_for_range(self) -> Option<(Quoted, Expr, Expr, Expr)> {} // docs:end:as_for_range /// If this expression is a function call `foo(arg1, ..., argN)`, return /// the function and a slice of each argument. #[builtin(expr_as_function_call)] // docs:start:as_function_call - comptime fn as_function_call(self) -> Option<(Expr, [Expr])> {} + pub comptime fn as_function_call(self) -> Option<(Expr, [Expr])> {} // docs:end:as_function_call /// If this expression is an `if condition { then_branch } else { else_branch }`, @@ -98,90 +98,90 @@ impl Expr { /// `None` is returned for that branch instead. #[builtin(expr_as_if)] // docs:start:as_if - comptime fn as_if(self) -> Option<(Expr, Expr, Option)> {} + pub comptime fn as_if(self) -> Option<(Expr, Expr, Option)> {} // docs:end:as_if /// If this expression is an index into an array `array[index]`, return the /// array and the index. #[builtin(expr_as_index)] // docs:start:as_index - comptime fn as_index(self) -> Option<(Expr, Expr)> {} + pub comptime fn as_index(self) -> Option<(Expr, Expr)> {} // docs:end:as_index /// If this expression is an integer literal, return the integer as a field /// as well as whether the integer is negative (true) or not (false). #[builtin(expr_as_integer)] // docs:start:as_integer - comptime fn as_integer(self) -> Option<(Field, bool)> {} + pub comptime fn as_integer(self) -> Option<(Field, bool)> {} // docs:end:as_integer /// If this expression is a lambda, returns the parameters, return type and body. #[builtin(expr_as_lambda)] // docs:start:as_lambda - comptime fn as_lambda(self) -> Option<([(Expr, Option)], Option, Expr)> {} + pub comptime fn as_lambda(self) -> Option<([(Expr, Option)], Option, Expr)> {} // docs:end:as_lambda /// If this expression is a let statement, returns the let pattern as an `Expr`, /// the optional type annotation, and the assigned expression. #[builtin(expr_as_let)] // docs:start:as_let - comptime fn as_let(self) -> Option<(Expr, Option, Expr)> {} + pub comptime fn as_let(self) -> Option<(Expr, Option, Expr)> {} // docs:end:as_let /// If this expression is a member access `foo.bar`, return the struct/tuple /// expression and the field. The field will be represented as a quoted value. #[builtin(expr_as_member_access)] // docs:start:as_member_access - comptime fn as_member_access(self) -> Option<(Expr, Quoted)> {} + pub comptime fn as_member_access(self) -> Option<(Expr, Quoted)> {} // docs:end:as_member_access /// If this expression is a method call `foo.bar::(arg1, ..., argN)`, return /// the receiver, method name, a slice of each generic argument, and a slice of each argument. #[builtin(expr_as_method_call)] // docs:start:as_method_call - comptime fn as_method_call(self) -> Option<(Expr, Quoted, [UnresolvedType], [Expr])> {} + pub comptime fn as_method_call(self) -> Option<(Expr, Quoted, [UnresolvedType], [Expr])> {} // docs:end:as_method_call /// If this expression is a repeated element array `[elem; length]`, return /// the repeated element and the length expressions. #[builtin(expr_as_repeated_element_array)] // docs:start:as_repeated_element_array - comptime fn as_repeated_element_array(self) -> Option<(Expr, Expr)> {} + pub comptime fn as_repeated_element_array(self) -> Option<(Expr, Expr)> {} // docs:end:as_repeated_element_array /// If this expression is a repeated element slice `[elem; length]`, return /// the repeated element and the length expressions. #[builtin(expr_as_repeated_element_slice)] // docs:start:as_repeated_element_slice - comptime fn as_repeated_element_slice(self) -> Option<(Expr, Expr)> {} + pub comptime fn as_repeated_element_slice(self) -> Option<(Expr, Expr)> {} // docs:end:as_repeated_element_slice /// If this expression is a slice literal `&[elem1, ..., elemN]`, /// return each element of the slice. #[builtin(expr_as_slice)] // docs:start:as_slice - comptime fn as_slice(self) -> Option<[Expr]> {} + pub comptime fn as_slice(self) -> Option<[Expr]> {} // docs:end:as_slice /// If this expression is a tuple `(field1, ..., fieldN)`, /// return each element of the tuple. #[builtin(expr_as_tuple)] // docs:start:as_tuple - comptime fn as_tuple(self) -> Option<[Expr]> {} + pub comptime fn as_tuple(self) -> Option<[Expr]> {} // docs:end:as_tuple /// If this expression is a unary operation ` `, /// return the unary operator as well as the right-hand side expression. #[builtin(expr_as_unary_op)] // docs:start:as_unary_op - comptime fn as_unary_op(self) -> Option<(UnaryOp, Expr)> {} + pub comptime fn as_unary_op(self) -> Option<(UnaryOp, Expr)> {} // docs:end:as_unary_op /// If this expression is an `unsafe { stmt1; ...; stmtN }` block, /// return each statement inside in a slice. #[builtin(expr_as_unsafe)] // docs:start:as_unsafe - comptime fn as_unsafe(self) -> Option<[Expr]> {} + pub comptime fn as_unsafe(self) -> Option<[Expr]> {} // docs:end:as_unsafe /// Returns `true` if this expression is trailed by a semicolon. @@ -202,19 +202,19 @@ impl Expr { /// ``` #[builtin(expr_has_semicolon)] // docs:start:has_semicolon - comptime fn has_semicolon(self) -> bool {} + pub comptime fn has_semicolon(self) -> bool {} // docs:end:has_semicolon /// Returns `true` if this expression is `break`. #[builtin(expr_is_break)] // docs:start:is_break - comptime fn is_break(self) -> bool {} + pub comptime fn is_break(self) -> bool {} // docs:end:is_break /// Returns `true` if this expression is `continue`. #[builtin(expr_is_continue)] // docs:start:is_continue - comptime fn is_continue(self) -> bool {} + pub comptime fn is_continue(self) -> bool {} // docs:end:is_continue /// Applies a mapping function to this expression and to all of its sub-expressions. @@ -225,7 +225,7 @@ impl Expr { /// For example, calling `modify` on `(&[1], &[2, 3])` with an `f` that returns `Option::some` /// for expressions that are integers, doubling them, would return `(&[2], &[4, 6])`. // docs:start:modify - comptime fn modify(self, f: fn[Env](Expr) -> Option) -> Expr { + pub comptime fn modify(self, f: fn[Env](Expr) -> Option) -> Expr { // docs:end:modify let result = modify_array(self, f); let result = result.or_else(|| modify_assert(self, f)); @@ -262,7 +262,7 @@ impl Expr { /// Returns this expression as a `Quoted` value. It's the same as `quote { $self }`. // docs:start:quoted - comptime fn quoted(self) -> Quoted { + pub comptime fn quoted(self) -> Quoted { // docs:end:quoted quote { $self } } @@ -278,7 +278,7 @@ impl Expr { /// the current `comptime` function. #[builtin(expr_resolve)] // docs:start:resolve - comptime fn resolve(self, in_function: Option) -> TypedExpr {} + pub comptime fn resolve(self, in_function: Option) -> TypedExpr {} // docs:end:resolve } diff --git a/noir_stdlib/src/meta/format_string.nr b/noir_stdlib/src/meta/format_string.nr index 075a69fdafb..f3c18212599 100644 --- a/noir_stdlib/src/meta/format_string.nr +++ b/noir_stdlib/src/meta/format_string.nr @@ -1,6 +1,6 @@ impl fmtstr { #[builtin(fmtstr_quoted_contents)] // docs:start:quoted_contents - comptime fn quoted_contents(self) -> Quoted {} + pub comptime fn quoted_contents(self) -> Quoted {} // docs:end:quoted_contents } diff --git a/noir_stdlib/src/meta/function_def.nr b/noir_stdlib/src/meta/function_def.nr index cd5f9cc79fb..3c29d57e20c 100644 --- a/noir_stdlib/src/meta/function_def.nr +++ b/noir_stdlib/src/meta/function_def.nr @@ -1,67 +1,67 @@ impl FunctionDefinition { #[builtin(function_def_add_attribute)] // docs:start:add_attribute - comptime fn add_attribute(self, attribute: str) {} + pub comptime fn add_attribute(self, attribute: str) {} // docs:end:add_attribute #[builtin(function_def_body)] // docs:start:body - comptime fn body(self) -> Expr {} + pub comptime fn body(self) -> Expr {} // docs:end:body #[builtin(function_def_has_named_attribute)] // docs:start:has_named_attribute - comptime fn has_named_attribute(self, name: str) -> bool {} + pub comptime fn has_named_attribute(self, name: str) -> bool {} // docs:end:has_named_attribute #[builtin(function_def_is_unconstrained)] // docs:start:is_unconstrained - comptime fn is_unconstrained(self) -> bool {} + pub comptime fn is_unconstrained(self) -> bool {} // docs:end:is_unconstrained #[builtin(function_def_module)] // docs:start:module - comptime fn module(self) -> Module {} + pub comptime fn module(self) -> Module {} // docs:end:module #[builtin(function_def_name)] // docs:start:name - comptime fn name(self) -> Quoted {} + pub comptime fn name(self) -> Quoted {} // docs:end:name #[builtin(function_def_parameters)] // docs:start:parameters - comptime fn parameters(self) -> [(Quoted, Type)] {} + pub comptime fn parameters(self) -> [(Quoted, Type)] {} // docs:end:parameters #[builtin(function_def_return_type)] // docs:start:return_type - comptime fn return_type(self) -> Type {} + pub comptime fn return_type(self) -> Type {} // docs:end:return_type #[builtin(function_def_set_body)] // docs:start:set_body - comptime fn set_body(self, body: Expr) {} + pub comptime fn set_body(self, body: Expr) {} // docs:end:set_body #[builtin(function_def_set_parameters)] // docs:start:set_parameters - comptime fn set_parameters(self, parameters: [(Quoted, Type)]) {} + pub comptime fn set_parameters(self, parameters: [(Quoted, Type)]) {} // docs:end:set_parameters #[builtin(function_def_set_return_type)] // docs:start:set_return_type - comptime fn set_return_type(self, return_type: Type) {} + pub comptime fn set_return_type(self, return_type: Type) {} // docs:end:set_return_type #[builtin(function_def_set_return_public)] // docs:start:set_return_public - comptime fn set_return_public(self, public: bool) {} + pub comptime fn set_return_public(self, public: bool) {} // docs:end:set_return_public #[builtin(function_def_set_unconstrained)] // docs:start:set_unconstrained - comptime fn set_unconstrained(self, value: bool) {} + pub comptime fn set_unconstrained(self, value: bool) {} // docs:end:set_unconstrained } diff --git a/noir_stdlib/src/meta/module.nr b/noir_stdlib/src/meta/module.nr index 1b29301c4ef..e9fac5ded0a 100644 --- a/noir_stdlib/src/meta/module.nr +++ b/noir_stdlib/src/meta/module.nr @@ -1,32 +1,32 @@ impl Module { #[builtin(module_add_item)] // docs:start:add_item - comptime fn add_item(self, item: Quoted) {} + pub comptime fn add_item(self, item: Quoted) {} // docs:end:add_item #[builtin(module_has_named_attribute)] // docs:start:has_named_attribute - comptime fn has_named_attribute(self, name: str) -> bool {} + pub comptime fn has_named_attribute(self, name: str) -> bool {} // docs:end:has_named_attribute #[builtin(module_is_contract)] // docs:start:is_contract - comptime fn is_contract(self) -> bool {} + pub comptime fn is_contract(self) -> bool {} // docs:end:is_contract #[builtin(module_functions)] // docs:start:functions - comptime fn functions(self) -> [FunctionDefinition] {} + pub comptime fn functions(self) -> [FunctionDefinition] {} // docs:end:functions #[builtin(module_structs)] // docs:start:structs - comptime fn structs(self) -> [StructDefinition] {} + pub comptime fn structs(self) -> [StructDefinition] {} // docs:end:structs #[builtin(module_name)] // docs:start:name - comptime fn name(self) -> Quoted {} + pub comptime fn name(self) -> Quoted {} // docs:end:name } diff --git a/noir_stdlib/src/meta/quoted.nr b/noir_stdlib/src/meta/quoted.nr index 6e8d001c57c..cf97107ed68 100644 --- a/noir_stdlib/src/meta/quoted.nr +++ b/noir_stdlib/src/meta/quoted.nr @@ -4,27 +4,27 @@ use crate::option::Option; impl Quoted { #[builtin(quoted_as_expr)] // docs:start:as_expr - comptime fn as_expr(self) -> Option {} + pub comptime fn as_expr(self) -> Option {} // docs:end:as_expr #[builtin(quoted_as_module)] // docs:start:as_module - comptime fn as_module(self) -> Option {} + pub comptime fn as_module(self) -> Option {} // docs:end:as_module #[builtin(quoted_as_trait_constraint)] // docs:start:as_trait_constraint - comptime fn as_trait_constraint(self) -> TraitConstraint {} + pub comptime fn as_trait_constraint(self) -> TraitConstraint {} // docs:end:as_trait_constraint #[builtin(quoted_as_type)] // docs:start:as_type - comptime fn as_type(self) -> Type {} + pub comptime fn as_type(self) -> Type {} // docs:end:as_type #[builtin(quoted_tokens)] // docs:start:tokens - comptime fn tokens(self) -> [Quoted] {} + pub comptime fn tokens(self) -> [Quoted] {} // docs:end:tokens } diff --git a/noir_stdlib/src/meta/struct_def.nr b/noir_stdlib/src/meta/struct_def.nr index b17c0d37613..fe7eabc7007 100644 --- a/noir_stdlib/src/meta/struct_def.nr +++ b/noir_stdlib/src/meta/struct_def.nr @@ -1,47 +1,47 @@ impl StructDefinition { #[builtin(struct_def_add_attribute)] // docs:start:add_attribute - comptime fn add_attribute(self, attribute: str) {} + pub comptime fn add_attribute(self, attribute: str) {} // docs:end:add_attribute #[builtin(struct_def_add_generic)] // docs:start:add_generic - comptime fn add_generic(self, generic_name: str) -> Type {} + pub comptime fn add_generic(self, generic_name: str) -> Type {} // docs:end:add_generic /// Return a syntactic version of this struct definition as a type. /// For example, `as_type(quote { type Foo { ... } })` would return `Foo` #[builtin(struct_def_as_type)] // docs:start:as_type - comptime fn as_type(self) -> Type {} + pub comptime fn as_type(self) -> Type {} // docs:end:as_type #[builtin(struct_def_has_named_attribute)] // docs:start:has_named_attribute - comptime fn has_named_attribute(self, name: str) -> bool {} + pub comptime fn has_named_attribute(self, name: str) -> bool {} // docs:end:has_named_attribute /// Return each generic on this struct. #[builtin(struct_def_generics)] // docs:start:generics - comptime fn generics(self) -> [Type] {} + pub comptime fn generics(self) -> [Type] {} // docs:end:generics /// Returns (name, type) pairs of each field in this struct. Each type is as-is /// with any generic arguments unchanged. #[builtin(struct_def_fields)] // docs:start:fields - comptime fn fields(self) -> [(Quoted, Type)] {} + pub comptime fn fields(self) -> [(Quoted, Type)] {} // docs:end:fields #[builtin(struct_def_module)] // docs:start:module - comptime fn module(self) -> Module {} + pub comptime fn module(self) -> Module {} // docs:end:module #[builtin(struct_def_name)] // docs:start:name - comptime fn name(self) -> Quoted {} + pub comptime fn name(self) -> Quoted {} // docs:end:name /// Sets the fields of this struct to the given fields list. @@ -50,7 +50,7 @@ impl StructDefinition { /// Each name is expected to be a single identifier. #[builtin(struct_def_set_fields)] // docs:start:set_fields - comptime fn set_fields(self, new_fields: [(Quoted, Type)]) {} + pub comptime fn set_fields(self, new_fields: [(Quoted, Type)]) {} // docs:end:set_fields } diff --git a/noir_stdlib/src/meta/trait_def.nr b/noir_stdlib/src/meta/trait_def.nr index 51676efbc34..0c2e7cfa5c1 100644 --- a/noir_stdlib/src/meta/trait_def.nr +++ b/noir_stdlib/src/meta/trait_def.nr @@ -4,7 +4,7 @@ use crate::cmp::Eq; impl TraitDefinition { #[builtin(trait_def_as_trait_constraint)] // docs:start:as_trait_constraint - comptime fn as_trait_constraint(_self: Self) -> TraitConstraint {} + pub comptime fn as_trait_constraint(_self: Self) -> TraitConstraint {} // docs:end:as_trait_constraint } diff --git a/noir_stdlib/src/meta/trait_impl.nr b/noir_stdlib/src/meta/trait_impl.nr index 6755a5c2031..9db859bf8a0 100644 --- a/noir_stdlib/src/meta/trait_impl.nr +++ b/noir_stdlib/src/meta/trait_impl.nr @@ -1,11 +1,11 @@ impl TraitImpl { #[builtin(trait_impl_trait_generic_args)] -// docs:start:trait_generic_args - comptime fn trait_generic_args(self) -> [Type] {} + // docs:start:trait_generic_args + pub comptime fn trait_generic_args(self) -> [Type] {} // docs:end:trait_generic_args #[builtin(trait_impl_methods)] -// docs:start:methods - comptime fn methods(self) -> [FunctionDefinition] {} + // docs:start:methods + pub comptime fn methods(self) -> [FunctionDefinition] {} // docs:end:methods } diff --git a/noir_stdlib/src/meta/typ.nr b/noir_stdlib/src/meta/typ.nr index 5a748c2c823..50f8fa60fb9 100644 --- a/noir_stdlib/src/meta/typ.nr +++ b/noir_stdlib/src/meta/typ.nr @@ -8,58 +8,58 @@ pub comptime fn fresh_type_variable() -> Type {} impl Type { #[builtin(type_as_array)] -// docs:start:as_array - comptime fn as_array(self) -> Option<(Type, Type)> {} + // docs:start:as_array + pub comptime fn as_array(self) -> Option<(Type, Type)> {} // docs:end:as_array #[builtin(type_as_constant)] -// docs:start:as_constant - comptime fn as_constant(self) -> Option {} + // docs:start:as_constant + pub comptime fn as_constant(self) -> Option {} // docs:end:as_constant #[builtin(type_as_integer)] -// docs:start:as_integer - comptime fn as_integer(self) -> Option<(bool, u8)> {} + // docs:start:as_integer + pub comptime fn as_integer(self) -> Option<(bool, u8)> {} // docs:end:as_integer #[builtin(type_as_slice)] -// docs:start:as_slice - comptime fn as_slice(self) -> Option {} + // docs:start:as_slice + pub comptime fn as_slice(self) -> Option {} // docs:end:as_slice #[builtin(type_as_str)] -// docs:start:as_str - comptime fn as_str(self) -> Option {} + // docs:start:as_str + pub comptime fn as_str(self) -> Option {} // docs:end:as_str #[builtin(type_as_struct)] -// docs:start:as_struct - comptime fn as_struct(self) -> Option<(StructDefinition, [Type])> {} + // docs:start:as_struct + pub comptime fn as_struct(self) -> Option<(StructDefinition, [Type])> {} // docs:end:as_struct #[builtin(type_as_tuple)] -// docs:start:as_tuple - comptime fn as_tuple(self) -> Option<[Type]> {} + // docs:start:as_tuple + pub comptime fn as_tuple(self) -> Option<[Type]> {} // docs:end:as_tuple #[builtin(type_get_trait_impl)] -// docs:start:get_trait_impl - comptime fn get_trait_impl(self, constraint: TraitConstraint) -> Option {} + // docs:start:get_trait_impl + pub comptime fn get_trait_impl(self, constraint: TraitConstraint) -> Option {} // docs:end:get_trait_impl #[builtin(type_implements)] -// docs:start:implements - comptime fn implements(self, constraint: TraitConstraint) -> bool {} + // docs:start:implements + pub comptime fn implements(self, constraint: TraitConstraint) -> bool {} // docs:end:implements #[builtin(type_is_bool)] -// docs:start:is_bool - comptime fn is_bool(self) -> bool {} + // docs:start:is_bool + pub comptime fn is_bool(self) -> bool {} // docs:end:is_bool #[builtin(type_is_field)] // docs:start:is_field - comptime fn is_field(self) -> bool {} + pub comptime fn is_field(self) -> bool {} // docs:end:is_field } diff --git a/noir_stdlib/src/meta/typed_expr.nr b/noir_stdlib/src/meta/typed_expr.nr index 1d3b073b6da..c69139ea233 100644 --- a/noir_stdlib/src/meta/typed_expr.nr +++ b/noir_stdlib/src/meta/typed_expr.nr @@ -5,12 +5,12 @@ impl TypedExpr { /// If this expression refers to a function definitions, returns it. Otherwise returns `Option::none()`. #[builtin(typed_expr_as_function_definition)] // docs:start:as_function_definition - comptime fn as_function_definition(self) -> Option {} + pub comptime fn as_function_definition(self) -> Option {} // docs:end:as_function_definition /// Returns the type of the expression, if the expression could be resolved without errors. #[builtin(typed_expr_get_type)] // docs:start:get_type - comptime fn get_type(self) -> Option {} + pub comptime fn get_type(self) -> Option {} // docs:end:get_type } diff --git a/noir_stdlib/src/meta/unresolved_type.nr b/noir_stdlib/src/meta/unresolved_type.nr index f53635414cc..c6e6d396546 100644 --- a/noir_stdlib/src/meta/unresolved_type.nr +++ b/noir_stdlib/src/meta/unresolved_type.nr @@ -1,6 +1,6 @@ impl UnresolvedType { #[builtin(unresolved_type_is_field)] // docs:start:is_field - comptime fn is_field(self) -> bool {} + pub comptime fn is_field(self) -> bool {} // docs:end:is_field } From bdd3447f6e7f67780531cd49017a8d7c83770430 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 3 Oct 2024 09:06:54 -0300 Subject: [PATCH 09/10] Remove one pub --- noir_stdlib/src/embedded_curve_ops.nr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noir_stdlib/src/embedded_curve_ops.nr b/noir_stdlib/src/embedded_curve_ops.nr index 7941da94699..b13c3637d01 100644 --- a/noir_stdlib/src/embedded_curve_ops.nr +++ b/noir_stdlib/src/embedded_curve_ops.nr @@ -74,7 +74,7 @@ impl EmbeddedCurveScalar { //Bytes to scalar: take the first (after the specified offset) 16 bytes of the input as the lo value, and the next 16 bytes as the hi value #[field(bn254)] - pub fn from_bytes(bytes: [u8; 64], offset: u32) -> EmbeddedCurveScalar { + fn from_bytes(bytes: [u8; 64], offset: u32) -> EmbeddedCurveScalar { let mut v = 1; let mut lo = 0 as Field; let mut hi = 0 as Field; From 2b6aec2741e325f8bb15ec6a13963d8cb98fe2e3 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 3 Oct 2024 09:55:47 -0300 Subject: [PATCH 10/10] Make it pub(crate) --- noir_stdlib/src/embedded_curve_ops.nr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noir_stdlib/src/embedded_curve_ops.nr b/noir_stdlib/src/embedded_curve_ops.nr index b13c3637d01..f5e7c7e8528 100644 --- a/noir_stdlib/src/embedded_curve_ops.nr +++ b/noir_stdlib/src/embedded_curve_ops.nr @@ -74,7 +74,7 @@ impl EmbeddedCurveScalar { //Bytes to scalar: take the first (after the specified offset) 16 bytes of the input as the lo value, and the next 16 bytes as the hi value #[field(bn254)] - fn from_bytes(bytes: [u8; 64], offset: u32) -> EmbeddedCurveScalar { + pub(crate) fn from_bytes(bytes: [u8; 64], offset: u32) -> EmbeddedCurveScalar { let mut v = 1; let mut lo = 0 as Field; let mut hi = 0 as Field;