Skip to content

Commit

Permalink
feat: use visibility (noir-lang/noir#5856)
Browse files Browse the repository at this point in the history
chore: use `new_let` more widely (noir-lang/noir#5882)
feat: Sync from aztec-packages (noir-lang/noir#5883)
feat!: return arrays instead of slices from `to_be_radix` functions (noir-lang/noir#5851)
chore: add a span to track timing of brillig gen (noir-lang/noir#5835)
feat: add `Expr::as_assert_eq` (noir-lang/noir#5880)
feat: LSP diagnostics now have "unnecessary" and "deprecated" tags (noir-lang/noir#5878)
feat: LSP code actions to import or qualify unresolved paths (noir-lang/noir#5876)
  • Loading branch information
AztecBot committed Sep 3, 2024
2 parents e7c6570 + 15ddcaf commit 7c44816
Show file tree
Hide file tree
Showing 56 changed files with 777 additions and 424 deletions.
2 changes: 1 addition & 1 deletion .noir-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d59c7087495f8af0dfb387dc587ecc422888096b
e349f30b60a473e2068afafb6fae4a4ea50d185b
26 changes: 9 additions & 17 deletions noir/noir-repo/aztec_macros/src/utils/ast_utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use noirc_errors::{Span, Spanned};
use noirc_frontend::ast::{
BinaryOpKind, CallExpression, CastExpression, Expression, ExpressionKind, FunctionReturnType,
Ident, IndexExpression, InfixExpression, Lambda, LetStatement, MemberAccessExpression,
MethodCallExpression, NoirTraitImpl, Path, PathSegment, Pattern, PrefixExpression, Statement,
StatementKind, TraitImplItem, UnaryOp, UnresolvedType, UnresolvedTypeData,
Ident, IndexExpression, InfixExpression, Lambda, MemberAccessExpression, MethodCallExpression,
NoirTraitImpl, Path, PathSegment, Pattern, PrefixExpression, Statement, StatementKind,
TraitImplItem, UnaryOp, UnresolvedType, UnresolvedTypeData,
};
use noirc_frontend::token::SecondaryAttribute;

Expand Down Expand Up @@ -73,13 +73,11 @@ pub fn mutable(name: &str) -> Pattern {
}

pub fn mutable_assignment(name: &str, assigned_to: Expression) -> Statement {
make_statement(StatementKind::Let(LetStatement {
pattern: mutable(name),
r#type: make_type(UnresolvedTypeData::Unspecified),
expression: assigned_to,
comptime: false,
attributes: vec![],
}))
make_statement(StatementKind::new_let(
mutable(name),
make_type(UnresolvedTypeData::Unspecified),
assigned_to,
))
}

pub fn mutable_reference(variable_name: &str) -> Expression {
Expand All @@ -98,13 +96,7 @@ pub fn assignment_with_type(
typ: UnresolvedTypeData,
assigned_to: Expression,
) -> Statement {
make_statement(StatementKind::Let(LetStatement {
pattern: pattern(name),
r#type: make_type(typ),
expression: assigned_to,
comptime: false,
attributes: vec![],
}))
make_statement(StatementKind::new_let(pattern(name), make_type(typ), assigned_to))
}

pub fn return_type(path: Path) -> FunctionReturnType {
Expand Down
6 changes: 3 additions & 3 deletions noir/noir-repo/aztec_macros/src/utils/parse_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn empty_item(item: &mut Item) {
empty_parsed_submodule(parsed_submodule);
}
ItemKind::ModuleDecl(module_declaration) => empty_module_declaration(module_declaration),
ItemKind::Import(use_tree) => empty_use_tree(use_tree),
ItemKind::Import(use_tree, _) => empty_use_tree(use_tree),
ItemKind::Struct(noir_struct) => empty_noir_struct(noir_struct),
ItemKind::TypeAlias(noir_type_alias) => empty_noir_type_alias(noir_type_alias),
}
Expand Down Expand Up @@ -404,9 +404,9 @@ fn empty_pattern(pattern: &mut Pattern) {
}

fn empty_unresolved_trait_constraints(
unresolved_trait_constriants: &mut [UnresolvedTraitConstraint],
unresolved_trait_constraints: &mut [UnresolvedTraitConstraint],
) {
for trait_constraint in unresolved_trait_constriants.iter_mut() {
for trait_constraint in unresolved_trait_constraints.iter_mut() {
empty_unresolved_trait_constraint(trait_constraint);
}
}
Expand Down
28 changes: 5 additions & 23 deletions noir/noir-repo/compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,6 @@ pub struct CompileOptions {
pub skip_underconstrained_check: bool,
}

#[derive(Clone, Debug, Default)]
pub struct CheckOptions {
pub compile_options: CompileOptions,
pub error_on_unused_imports: bool,
}

impl CheckOptions {
pub fn new(compile_options: &CompileOptions, error_on_unused_imports: bool) -> Self {
Self { compile_options: compile_options.clone(), error_on_unused_imports }
}
}

pub fn parse_expression_width(input: &str) -> Result<ExpressionWidth, std::io::Error> {
use std::io::{Error, ErrorKind};
let width = input
Expand Down Expand Up @@ -290,20 +278,19 @@ pub fn add_dep(
pub fn check_crate(
context: &mut Context,
crate_id: CrateId,
check_options: &CheckOptions,
options: &CompileOptions,
) -> CompilationResult<()> {
let options = &check_options.compile_options;

let macros: &[&dyn MacroProcessor] =
if options.disable_macros { &[] } else { &[&aztec_macros::AztecMacro] };

let mut errors = vec![];
let error_on_unused_imports = true;
let diagnostics = CrateDefMap::collect_defs(
crate_id,
context,
options.debug_comptime_in_file.as_deref(),
options.arithmetic_generics,
check_options.error_on_unused_imports,
error_on_unused_imports,
macros,
);
errors.extend(diagnostics.into_iter().map(|(error, file_id)| {
Expand Down Expand Up @@ -337,10 +324,7 @@ pub fn compile_main(
options: &CompileOptions,
cached_program: Option<CompiledProgram>,
) -> CompilationResult<CompiledProgram> {
let error_on_unused_imports = true;
let check_options = CheckOptions::new(options, error_on_unused_imports);

let (_, mut warnings) = check_crate(context, crate_id, &check_options)?;
let (_, mut warnings) = check_crate(context, crate_id, options)?;

let main = context.get_main_function(&crate_id).ok_or_else(|| {
// TODO(#2155): This error might be a better to exist in Nargo
Expand Down Expand Up @@ -375,9 +359,7 @@ pub fn compile_contract(
crate_id: CrateId,
options: &CompileOptions,
) -> CompilationResult<CompiledContract> {
let error_on_unused_imports = true;
let check_options = CheckOptions::new(options, error_on_unused_imports);
let (_, warnings) = check_crate(context, crate_id, &check_options)?;
let (_, warnings) = check_crate(context, crate_id, options)?;

// TODO: We probably want to error if contracts is empty
let contracts = context.get_all_contracts(&crate_id);
Expand Down
14 changes: 12 additions & 2 deletions noir/noir-repo/compiler/noirc_frontend/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,22 @@ impl UnresolvedTypeExpression {
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
/// Represents whether the definition can be referenced outside its module/crate
pub enum ItemVisibility {
Public,
Private,
PublicCrate,
Public,
}

impl std::fmt::Display for ItemVisibility {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ItemVisibility::Public => write!(f, "pub"),
ItemVisibility::Private => Ok(()),
ItemVisibility::PublicCrate => write!(f, "pub(crate)"),
}
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand Down
38 changes: 19 additions & 19 deletions noir/noir-repo/compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use iter_extended::vecmap;
use noirc_errors::{Span, Spanned};

use super::{
BlockExpression, Expression, ExpressionKind, GenericTypeArgs, IndexExpression,
BlockExpression, Expression, ExpressionKind, GenericTypeArgs, IndexExpression, ItemVisibility,
MemberAccessExpression, MethodCallExpression, UnresolvedType,
};
use crate::elaborator::types::SELF_TYPE_NAME;
Expand Down Expand Up @@ -136,7 +136,9 @@ impl Recoverable for StatementKind {

impl StatementKind {
pub fn new_let(
((pattern, r#type), expression): ((Pattern, UnresolvedType), Expression),
pattern: Pattern,
r#type: UnresolvedType,
expression: Expression,
) -> StatementKind {
StatementKind::Let(LetStatement {
pattern,
Expand Down Expand Up @@ -300,6 +302,7 @@ impl std::fmt::Display for ModuleDeclaration {

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ImportStatement {
pub visibility: ItemVisibility,
pub path: Path,
pub alias: Option<Ident>,
}
Expand Down Expand Up @@ -348,7 +351,7 @@ pub enum UseTreeKind {
}

impl UseTree {
pub fn desugar(self, root: Option<Path>) -> Vec<ImportStatement> {
pub fn desugar(self, root: Option<Path>, visibility: ItemVisibility) -> Vec<ImportStatement> {
let prefix = if let Some(mut root) = root {
root.segments.extend(self.prefix.segments);
root
Expand All @@ -358,10 +361,11 @@ impl UseTree {

match self.kind {
UseTreeKind::Path(name, alias) => {
vec![ImportStatement { path: prefix.join(name), alias }]
vec![ImportStatement { visibility, path: prefix.join(name), alias }]
}
UseTreeKind::List(trees) => {
trees.into_iter().flat_map(|tree| tree.desugar(Some(prefix.clone()))).collect()
let trees = trees.into_iter();
trees.flat_map(|tree| tree.desugar(Some(prefix.clone()), visibility)).collect()
}
}
}
Expand Down Expand Up @@ -715,13 +719,11 @@ impl ForRange {

// let fresh1 = array;
let let_array = Statement {
kind: StatementKind::Let(LetStatement {
pattern: Pattern::Identifier(array_ident.clone()),
r#type: UnresolvedTypeData::Unspecified.with_span(Default::default()),
expression: array,
comptime: false,
attributes: vec![],
}),
kind: StatementKind::new_let(
Pattern::Identifier(array_ident.clone()),
UnresolvedTypeData::Unspecified.with_span(Default::default()),
array,
),
span: array_span,
};

Expand Down Expand Up @@ -761,13 +763,11 @@ impl ForRange {

// let elem = array[i];
let let_elem = Statement {
kind: StatementKind::Let(LetStatement {
pattern: Pattern::Identifier(identifier),
r#type: UnresolvedTypeData::Unspecified.with_span(Default::default()),
expression: Expression::new(loop_element, array_span),
comptime: false,
attributes: vec![],
}),
kind: StatementKind::new_let(
Pattern::Identifier(identifier),
UnresolvedTypeData::Unspecified.with_span(Default::default()),
Expression::new(loop_element, array_span),
),
span: array_span,
};

Expand Down
11 changes: 6 additions & 5 deletions noir/noir-repo/compiler/noirc_frontend/src/ast/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ use crate::{
};

use super::{
FunctionReturnType, GenericTypeArgs, IntegerBitSize, Pattern, Signedness, UnresolvedGenerics,
UnresolvedTraitConstraint, UnresolvedType, UnresolvedTypeData, UnresolvedTypeExpression,
FunctionReturnType, GenericTypeArgs, IntegerBitSize, ItemVisibility, Pattern, Signedness,
UnresolvedGenerics, UnresolvedTraitConstraint, UnresolvedType, UnresolvedTypeData,
UnresolvedTypeExpression,
};

/// Implements the [Visitor pattern](https://en.wikipedia.org/wiki/Visitor_pattern) for Noir's AST.
Expand Down Expand Up @@ -252,7 +253,7 @@ pub trait Visitor {
true
}

fn visit_import(&mut self, _: &UseTree) -> bool {
fn visit_import(&mut self, _: &UseTree, _visibility: ItemVisibility) -> bool {
true
}

Expand Down Expand Up @@ -470,8 +471,8 @@ impl Item {
}
}
ItemKind::Trait(noir_trait) => noir_trait.accept(self.span, visitor),
ItemKind::Import(use_tree) => {
if visitor.visit_import(use_tree) {
ItemKind::Import(use_tree, visibility) => {
if visitor.visit_import(use_tree, *visibility) {
use_tree.accept(visitor);
}
}
Expand Down
36 changes: 15 additions & 21 deletions noir/noir-repo/compiler/noirc_frontend/src/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,11 @@ impl DebugInstrumenter {
None => false,
Some(ast::Statement { kind: ast::StatementKind::Expression(ret_expr), .. }) => {
let save_ret_expr = ast::Statement {
kind: ast::StatementKind::Let(ast::LetStatement {
pattern: ast::Pattern::Identifier(ident("__debug_expr", ret_expr.span)),
r#type: ast::UnresolvedTypeData::Unspecified.with_span(Default::default()),
expression: ret_expr.clone(),
comptime: false,
attributes: vec![],
}),
kind: ast::StatementKind::new_let(
ast::Pattern::Identifier(ident("__debug_expr", ret_expr.span)),
ast::UnresolvedTypeData::Unspecified.with_span(Default::default()),
ret_expr.clone(),
),
span: ret_expr.span,
};
statements.push(save_ret_expr);
Expand Down Expand Up @@ -242,18 +240,16 @@ impl DebugInstrumenter {
});

ast::Statement {
kind: ast::StatementKind::Let(ast::LetStatement {
pattern: ast::Pattern::Tuple(vars_pattern, let_stmt.pattern.span()),
r#type: ast::UnresolvedTypeData::Unspecified.with_span(Default::default()),
comptime: false,
expression: ast::Expression {
kind: ast::StatementKind::new_let(
ast::Pattern::Tuple(vars_pattern, let_stmt.pattern.span()),
ast::UnresolvedTypeData::Unspecified.with_span(Default::default()),
ast::Expression {
kind: ast::ExpressionKind::Block(ast::BlockExpression {
statements: block_stmts,
}),
span: let_stmt.expression.span,
},
attributes: vec![],
}),
),
span: *span,
}
}
Expand All @@ -274,13 +270,11 @@ impl DebugInstrumenter {
// __debug_expr
// };

let let_kind = ast::StatementKind::Let(ast::LetStatement {
pattern: ast::Pattern::Identifier(ident("__debug_expr", assign_stmt.expression.span)),
r#type: ast::UnresolvedTypeData::Unspecified.with_span(Default::default()),
expression: assign_stmt.expression.clone(),
comptime: false,
attributes: vec![],
});
let let_kind = ast::StatementKind::new_let(
ast::Pattern::Identifier(ident("__debug_expr", assign_stmt.expression.span)),
ast::UnresolvedTypeData::Unspecified.with_span(Default::default()),
assign_stmt.expression.clone(),
);
let expression_span = assign_stmt.expression.span;
let new_assign_stmt = match &assign_stmt.lvalue {
ast::LValue::Ident(id) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl<'context> Elaborator<'context> {
TopLevelStatement::Error => (),

TopLevelStatement::Module(_)
| TopLevelStatement::Import(_)
| TopLevelStatement::Import(..)
| TopLevelStatement::Struct(_)
| TopLevelStatement::Trait(_)
| TopLevelStatement::Impl(_)
Expand Down
Loading

0 comments on commit 7c44816

Please sign in to comment.