Skip to content

Commit 9ca1a60

Browse files
authored
chore: allow setting namespace visibility on functions (#4510)
# Description ## Problem\* Resolves <!-- Link to GitHub Issue --> ## Summary\* This PR allows setting non-public visibilities when adding functions to namespaces. This doesn't currently cause any changes as we never actually read this visibility but we start to do so in a later PR. ## 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.
1 parent 773cf19 commit 9ca1a60

6 files changed

Lines changed: 54 additions & 26 deletions

File tree

aztec_macros/src/transforms/compute_note_hash_and_nullifier.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use noirc_frontend::{
77
},
88
macros_api::{FileId, HirContext, MacroError},
99
node_interner::FuncId,
10-
parse_program, FunctionReturnType, NoirFunction, UnresolvedTypeData,
10+
parse_program, FunctionReturnType, ItemVisibility, NoirFunction, UnresolvedTypeData,
1111
};
1212

1313
use crate::utils::hir_utils::fetch_struct_trait_impls;
@@ -113,7 +113,7 @@ pub fn inject_compute_note_hash_and_nullifier(
113113
context.def_map_mut(crate_id).unwrap()
114114
.modules_mut()[module_id.0]
115115
.declare_function(
116-
func.name_ident().clone(), func_id
116+
func.name_ident().clone(), ItemVisibility::Public, func_id
117117
).expect(
118118
"Failed to declare the autogenerated compute_note_hash_and_nullifier function, likely due to a duplicate definition. See https://github.com/AztecProtocol/aztec-packages/issues/4647."
119119
);

compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::{
1010
macros_api::MacroProcessor,
1111
node_interner::{FunctionModifiers, TraitId, TypeAliasId},
1212
parser::{SortedModule, SortedSubModule},
13-
FunctionDefinition, Ident, LetStatement, ModuleDeclaration, NoirFunction, NoirStruct,
14-
NoirTrait, NoirTraitImpl, NoirTypeAlias, TraitImplItem, TraitItem, TypeImpl,
13+
FunctionDefinition, Ident, ItemVisibility, LetStatement, ModuleDeclaration, NoirFunction,
14+
NoirStruct, NoirTrait, NoirTraitImpl, NoirTypeAlias, TraitImplItem, TraitItem, TypeImpl,
1515
};
1616

1717
use super::{
@@ -232,6 +232,7 @@ impl<'a> ModCollector<'a> {
232232

233233
let name = function.name_ident().clone();
234234
let func_id = context.def_interner.push_empty_fn();
235+
let visibility = function.def.visibility;
235236

236237
// First create dummy function in the DefInterner
237238
// So that we can get a FuncId
@@ -248,7 +249,7 @@ impl<'a> ModCollector<'a> {
248249

249250
// Add function to scope/ns of the module
250251
let result = self.def_collector.def_map.modules[self.module_id.0]
251-
.declare_function(name, func_id);
252+
.declare_function(name, visibility, func_id);
252253

253254
if let Err((first_def, second_def)) = result {
254255
let error = DefCollectorErrorKind::Duplicate {
@@ -407,7 +408,7 @@ impl<'a> ModCollector<'a> {
407408

408409
let modifiers = FunctionModifiers {
409410
name: name.to_string(),
410-
visibility: crate::ItemVisibility::Public,
411+
visibility: ItemVisibility::Public,
411412
// TODO(Maddiaa): Investigate trait implementations with attributes see: https://github.com/noir-lang/noir/issues/2629
412413
attributes: crate::token::Attributes::empty(),
413414
is_unconstrained: false,
@@ -419,7 +420,7 @@ impl<'a> ModCollector<'a> {
419420
.push_function_definition(func_id, modifiers, trait_id.0, location);
420421

421422
match self.def_collector.def_map.modules[trait_id.0.local_id.0]
422-
.declare_function(name.clone(), func_id)
423+
.declare_function(name.clone(), ItemVisibility::Public, func_id)
423424
{
424425
Ok(()) => {
425426
if let Some(body) = body {

compiler/noirc_frontend/src/hir/def_map/item_scope.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ impl ItemScope {
1919
pub fn add_definition(
2020
&mut self,
2121
name: Ident,
22+
visibility: ItemVisibility,
2223
mod_def: ModuleDefId,
2324
trait_id: Option<TraitId>,
2425
) -> Result<(), (Ident, Ident)> {
25-
self.add_item_to_namespace(name, mod_def, trait_id, false)?;
26+
self.add_item_to_namespace(name, visibility, mod_def, trait_id, false)?;
2627
self.defs.push(mod_def);
2728
Ok(())
2829
}
@@ -33,6 +34,7 @@ impl ItemScope {
3334
pub fn add_item_to_namespace(
3435
&mut self,
3536
name: Ident,
37+
visibility: ItemVisibility,
3638
mod_def: ModuleDefId,
3739
trait_id: Option<TraitId>,
3840
is_prelude: bool,
@@ -41,6 +43,11 @@ impl ItemScope {
4143
if let Entry::Occupied(mut o) = map.entry(name.clone()) {
4244
let trait_hashmap = o.get_mut();
4345
if let Entry::Occupied(mut n) = trait_hashmap.entry(trait_id) {
46+
// Generally we want to reject having two of the same ident in the same namespace.
47+
// The exception to this is when we're explicitly importing something
48+
// which exists in the Noir stdlib prelude.
49+
//
50+
// In this case we ignore the prelude and favour the explicit import.
4451
let is_prelude = std::mem::replace(&mut n.get_mut().2, is_prelude);
4552
let old_ident = o.key();
4653

@@ -50,12 +57,12 @@ impl ItemScope {
5057
Err((old_ident.clone(), name))
5158
}
5259
} else {
53-
trait_hashmap.insert(trait_id, (mod_def, ItemVisibility::Public, is_prelude));
60+
trait_hashmap.insert(trait_id, (mod_def, visibility, is_prelude));
5461
Ok(())
5562
}
5663
} else {
5764
let mut trait_hashmap = HashMap::new();
58-
trait_hashmap.insert(trait_id, (mod_def, ItemVisibility::Public, is_prelude));
65+
trait_hashmap.insert(trait_id, (mod_def, visibility, is_prelude));
5966
map.insert(name, trait_hashmap);
6067
Ok(())
6168
}

compiler/noirc_frontend/src/hir/def_map/module_data.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use noirc_errors::Location;
44

55
use crate::{
66
node_interner::{FuncId, GlobalId, StructId, TraitId, TypeAliasId},
7-
Ident,
7+
Ident, ItemVisibility,
88
};
99

1010
use super::{ItemScope, LocalModuleId, ModuleDefId, ModuleId, PerNs};
@@ -48,18 +48,24 @@ impl ModuleData {
4848
fn declare(
4949
&mut self,
5050
name: Ident,
51+
visibility: ItemVisibility,
5152
item_id: ModuleDefId,
5253
trait_id: Option<TraitId>,
5354
) -> Result<(), (Ident, Ident)> {
54-
self.scope.add_definition(name.clone(), item_id, trait_id)?;
55+
self.scope.add_definition(name.clone(), visibility, item_id, trait_id)?;
5556

5657
// definitions is a subset of self.scope so it is expected if self.scope.define_func_def
5758
// returns without error, so will self.definitions.define_func_def.
58-
self.definitions.add_definition(name, item_id, trait_id)
59+
self.definitions.add_definition(name, visibility, item_id, trait_id)
5960
}
6061

61-
pub fn declare_function(&mut self, name: Ident, id: FuncId) -> Result<(), (Ident, Ident)> {
62-
self.declare(name, id.into(), None)
62+
pub fn declare_function(
63+
&mut self,
64+
name: Ident,
65+
visibility: ItemVisibility,
66+
id: FuncId,
67+
) -> Result<(), (Ident, Ident)> {
68+
self.declare(name, visibility, id.into(), None)
6369
}
6470

6571
pub fn declare_trait_function(
@@ -68,7 +74,7 @@ impl ModuleData {
6874
id: FuncId,
6975
trait_id: TraitId,
7076
) -> Result<(), (Ident, Ident)> {
71-
self.declare(name, id.into(), Some(trait_id))
77+
self.declare(name, ItemVisibility::Public, id.into(), Some(trait_id))
7278
}
7379

7480
pub fn remove_function(&mut self, name: &Ident) {
@@ -77,31 +83,31 @@ impl ModuleData {
7783
}
7884

7985
pub fn declare_global(&mut self, name: Ident, id: GlobalId) -> Result<(), (Ident, Ident)> {
80-
self.declare(name, id.into(), None)
86+
self.declare(name, ItemVisibility::Public, id.into(), None)
8187
}
8288

8389
pub fn declare_struct(&mut self, name: Ident, id: StructId) -> Result<(), (Ident, Ident)> {
84-
self.declare(name, ModuleDefId::TypeId(id), None)
90+
self.declare(name, ItemVisibility::Public, ModuleDefId::TypeId(id), None)
8591
}
8692

8793
pub fn declare_type_alias(
8894
&mut self,
8995
name: Ident,
9096
id: TypeAliasId,
9197
) -> Result<(), (Ident, Ident)> {
92-
self.declare(name, id.into(), None)
98+
self.declare(name, ItemVisibility::Public, id.into(), None)
9399
}
94100

95101
pub fn declare_trait(&mut self, name: Ident, id: TraitId) -> Result<(), (Ident, Ident)> {
96-
self.declare(name, ModuleDefId::TraitId(id), None)
102+
self.declare(name, ItemVisibility::Public, ModuleDefId::TraitId(id), None)
97103
}
98104

99105
pub fn declare_child_module(
100106
&mut self,
101107
name: Ident,
102108
child_id: ModuleId,
103109
) -> Result<(), (Ident, Ident)> {
104-
self.declare(name, child_id.into(), None)
110+
self.declare(name, ItemVisibility::Public, child_id.into(), None)
105111
}
106112

107113
pub fn find_func_with_name(&self, name: &Ident) -> Option<FuncId> {
@@ -114,7 +120,7 @@ impl ModuleData {
114120
id: ModuleDefId,
115121
is_prelude: bool,
116122
) -> Result<(), (Ident, Ident)> {
117-
self.scope.add_item_to_namespace(name, id, None, is_prelude)
123+
self.scope.add_item_to_namespace(name, ItemVisibility::Public, id, None, is_prelude)
118124
}
119125

120126
pub fn find_name(&self, name: &Ident) -> PerNs {

compiler/noirc_frontend/src/hir/resolution/impls.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
Context,
1414
},
1515
node_interner::{FuncId, NodeInterner},
16-
Type,
16+
ItemVisibility, Type,
1717
};
1818

1919
use super::{
@@ -67,7 +67,14 @@ pub(crate) fn collect_impls(
6767
// be accessed with the `TypeName::method` syntax. We'll check later whether the
6868
// object types in each method overlap or not. If they do, we issue an error.
6969
// If not, that is specialization which is allowed.
70-
if module.declare_function(method.name_ident().clone(), *method_id).is_err() {
70+
if module
71+
.declare_function(
72+
method.name_ident().clone(),
73+
ItemVisibility::Public,
74+
*method_id,
75+
)
76+
.is_err()
77+
{
7178
module.remove_function(method.name_ident());
7279
}
7380
}

compiler/noirc_frontend/src/hir/resolution/traits.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
},
1717
hir_def::traits::{TraitConstant, TraitFunction, TraitImpl, TraitType},
1818
node_interner::{FuncId, NodeInterner, TraitId},
19-
Generics, Path, Shared, TraitItem, Type, TypeVariable, TypeVariableKind,
19+
Generics, ItemVisibility, Path, Shared, TraitItem, Type, TypeVariable, TypeVariableKind,
2020
};
2121

2222
use super::{
@@ -301,7 +301,14 @@ fn collect_trait_impl(
301301
// be accessed with the `TypeName::method` syntax. We'll check later whether the
302302
// object types in each method overlap or not. If they do, we issue an error.
303303
// If not, that is specialization which is allowed.
304-
if module.declare_function(method.name_ident().clone(), *method_id).is_err() {
304+
if module
305+
.declare_function(
306+
method.name_ident().clone(),
307+
ItemVisibility::Public,
308+
*method_id,
309+
)
310+
.is_err()
311+
{
305312
module.remove_function(method.name_ident());
306313
}
307314
}

0 commit comments

Comments
 (0)