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: catch imports of private definitions in resolution #4491

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5aaa6fe
feat: catch imports of private definitions in def collection
TomAFrench Feb 25, 2024
166dc3f
feat: fully resolve private items while returning warning
TomAFrench Mar 14, 2024
04c7203
chore: fix warning for unused result
TomAFrench Mar 14, 2024
6285007
chore: cleanup
TomAFrench Mar 14, 2024
557f9e3
Merge branch 'master' into tf/def-collection-visiblity
TomAFrench Mar 14, 2024
e6bb6b7
chore: replace placeholder error messages
TomAFrench Mar 15, 2024
bb95692
chore: remove private modules test
TomAFrench Mar 15, 2024
a08090c
chore: formatting
TomAFrench Mar 15, 2024
bf3a540
chore: nit
TomAFrench Mar 15, 2024
16048c9
chore: refactoring
TomAFrench Mar 15, 2024
424fbee
chore: refactor display of `ItemVisibility`
TomAFrench Mar 15, 2024
8633239
Merge branch 'master' into tf/def-collection-visiblity
TomAFrench Mar 15, 2024
0e21701
Update compiler/noirc_frontend/src/parser/parser.rs
TomAFrench Mar 19, 2024
352cfa6
Update compiler/noirc_frontend/src/parser/parser.rs
TomAFrench Mar 19, 2024
564a067
Update compiler/noirc_frontend/src/lexer/token.rs
TomAFrench Mar 19, 2024
5deda19
Update compiler/noirc_frontend/src/ast/statement.rs
TomAFrench Mar 23, 2024
72151b8
chore: remove duplicated code
TomAFrench Mar 23, 2024
83198ac
chore: simplify return types
TomAFrench Mar 23, 2024
b7d57bf
chore: make fn more private
TomAFrench Mar 23, 2024
c389a55
chore: make `PathResolution` actually contain a resolved path rather …
TomAFrench Mar 23, 2024
90ee97a
chore: cargo fmt
TomAFrench Mar 23, 2024
32f5246
Merge branch 'master' into tf/def-collection-visiblity
TomAFrench Apr 2, 2024
b2d1f77
Merge branch 'master' into tf/def-collection-visiblity
TomAFrench Jun 19, 2024
bd04dc4
Merge branch 'master' into tf/def-collection-visiblity
TomAFrench Jul 11, 2024
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/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ pub enum ItemVisibility {
Public,
Private,
PublicCrate,
PublicSuper,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand Down
12 changes: 9 additions & 3 deletions compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::lexer::token::SpannedToken;
use crate::parser::{ParserError, ParserErrorReason};
use crate::token::Token;
use crate::{
BlockExpression, Expression, ExpressionKind, IndexExpression, MemberAccessExpression,
MethodCallExpression, UnresolvedType,
BlockExpression, Expression, ExpressionKind, IndexExpression, ItemVisibility,
MemberAccessExpression, MethodCallExpression, UnresolvedType,
};
use acvm::FieldElement;
use iter_extended::vecmap;
Expand Down Expand Up @@ -243,11 +243,17 @@ pub trait Recoverable {
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ModuleDeclaration {
pub ident: Ident,
pub visibility: ItemVisibility,
}

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

Expand Down
17 changes: 14 additions & 3 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ impl UnresolvedFunctions {
for bound in &mut func.def.where_clause {
match resolve_trait_by_path(def_maps, module, bound.trait_bound.trait_path.clone())
{
Ok(trait_id) => {
Ok((trait_id, warning)) => {
bound.trait_bound.trait_id = Some(trait_id);
if let Some(warning) = warning {
errors.push(DefCollectorErrorKind::PathResolutionError(warning));
}
}
Err(err) => {
errors.push(err);
Expand Down Expand Up @@ -280,7 +283,14 @@ impl DefCollector {
// Resolve unresolved imports collected from the crate, one by one.
for collected_import in def_collector.collected_imports {
match resolve_import(crate_id, &collected_import, &context.def_maps) {
Ok(resolved_import) => {
Ok((resolved_import, warning)) => {
if let Some(warning) = warning {
errors.push((
DefCollectorErrorKind::PathResolutionError(warning).into(),
root_file_id,
));
}

// Populate module namespaces according to the imports used
let current_def_map = context.def_maps.get_mut(&crate_id).unwrap();

Expand Down Expand Up @@ -409,11 +419,12 @@ fn inject_prelude(
Path { segments: segments.clone(), kind: crate::PathKind::Dep, span: Span::default() };

if !crate_id.is_stdlib() {
if let Ok(module_def) = path_resolver::resolve_path(
if let Ok((module_def, ident)) = path_resolver::resolve_path(
&context.def_maps,
ModuleId { krate: crate_id, local_id: crate_root },
path,
) {
assert!(ident.is_none(), "Tried to add private item to prelude");
let module_id = module_def.as_module().expect("std::prelude should be a module");
let prelude = context.module(module_id).scope().names();

Expand Down
41 changes: 34 additions & 7 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,13 @@
};

// Create the corresponding module for the struct namespace
let id = match self.push_child_module(&name, self.file_id, false, false) {
let id = match self.push_child_module(
&name,
ItemVisibility::Public,
self.file_id,
false,
false,
) {
Ok(local_id) => {
context.def_interner.new_struct(&unresolved, krate, local_id, self.file_id)
}
Expand Down Expand Up @@ -364,7 +370,13 @@
let name = trait_definition.name.clone();

// Create the corresponding module for the trait namespace
let trait_id = match self.push_child_module(&name, self.file_id, false, false) {
let trait_id = match self.push_child_module(
&name,
ItemVisibility::Public,
self.file_id,
false,
false,
) {
Ok(local_id) => TraitId(ModuleId { krate, local_id }),
Err(error) => {
errors.push((error.into(), self.file_id));
Expand Down Expand Up @@ -470,7 +482,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 485 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 485 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 @@ -510,7 +522,13 @@
) -> Vec<(CompilationError, FileId)> {
let mut errors: Vec<(CompilationError, FileId)> = vec![];
for submodule in submodules {
match self.push_child_module(&submodule.name, file_id, true, submodule.is_contract) {
match self.push_child_module(
&submodule.name,
ItemVisibility::Public,
file_id,
true,
submodule.is_contract,
) {
Ok(child) => {
errors.extend(collect_defs(
self.def_collector,
Expand Down Expand Up @@ -593,7 +611,13 @@
);

// Add module into def collector and get a ModuleId
match self.push_child_module(&mod_decl.ident, child_file_id, true, false) {
match self.push_child_module(
&mod_decl.ident,
mod_decl.visibility,
child_file_id,
true,
false,
) {
Ok(child_mod_id) => {
errors.extend(collect_defs(
self.def_collector,
Expand All @@ -617,6 +641,7 @@
fn push_child_module(
&mut self,
mod_name: &Ident,
visibility: ItemVisibility,
file_id: FileId,
add_to_parent_scope: bool,
is_contract: bool,
Expand Down Expand Up @@ -644,9 +669,11 @@
local_id: LocalModuleId(module_id),
};

if let Err((first_def, second_def)) =
modules[self.module_id.0].declare_child_module(mod_name.to_owned(), mod_id)
{
if let Err((first_def, second_def)) = modules[self.module_id.0].declare_child_module(
mod_name.to_owned(),
visibility,
mod_id,
) {
let err = DefCollectorErrorKind::Duplicate {
typ: DuplicateType::Module,
first_def,
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 @@ -105,9 +105,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
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/hir/resolution/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum ResolverError {
#[error("path is not an identifier")]
PathIsNotIdent { span: Span },
#[error("could not resolve path")]
PathResolutionError(PathResolutionError),
PathResolutionError(#[from] PathResolutionError),
#[error("Expected")]
Expected { span: Span, expected: String, got: String },
#[error("Duplicate field in constructor")]
Expand Down
Loading
Loading