Skip to content

Commit

Permalink
feat: Sync from noir (#7945)
Browse files Browse the repository at this point in the history
Automated pull of development from the
[noir](https://github.com/noir-lang/noir) programming language, a
dependency of Aztec.
BEGIN_COMMIT_OVERRIDE
chore: allow passing custom executors to fuzzer
(noir-lang/noir#5710)
feat: LSP path completion (noir-lang/noir#5712)
fix(debugger): Update the debugger to handle the new Brillig debug
metadata format (noir-lang/noir#5706)
feat: add `Quoted::as_expr` and `Expr::as_function_call`
(noir-lang/noir#5708)
feat: LSP autocompletion for use statement
(noir-lang/noir#5704)
feat: add `Type::implements`
(noir-lang/noir#5701)
END_COMMIT_OVERRIDE

---------

Co-authored-by: Maxim Vezenov <[email protected]>
  • Loading branch information
AztecBot and vezenovm authored Aug 13, 2024
1 parent d5e48aa commit 91042c7
Show file tree
Hide file tree
Showing 40 changed files with 3,019 additions and 317 deletions.
2 changes: 1 addition & 1 deletion .noir-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e4f7dbe63b55807b3ff0b4d6f47a8b7f847299fb
0ebf1fee471641db0bffcc8307d20327613c78c1
6 changes: 3 additions & 3 deletions noir/noir-repo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion noir/noir-repo/acvm-repo/acvm/src/pwg/brillig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct BrilligSolver<'b, F, B: BlackBoxFunctionSolver<F>> {
/// This id references which Brillig function within the main ACIR program we are solving.
/// This is used for appropriately resolving errors as the ACIR program artifacts
/// set up their Brillig debug metadata by function id.
function_id: BrilligFunctionId,
pub function_id: BrilligFunctionId,
}

impl<'b, B: BlackBoxFunctionSolver<F>, F: AcirField> BrilligSolver<'b, F, B> {
Expand Down
17 changes: 16 additions & 1 deletion noir/noir-repo/compiler/noirc_driver/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(crate) fn filter_relevant_files(
debug_symbols: &[DebugInfo],
file_manager: &FileManager,
) -> BTreeMap<FileId, DebugFile> {
let files_with_debug_symbols: BTreeSet<FileId> = debug_symbols
let mut files_with_debug_symbols: BTreeSet<FileId> = debug_symbols
.iter()
.flat_map(|function_symbols| {
function_symbols
Expand All @@ -28,6 +28,21 @@ pub(crate) fn filter_relevant_files(
})
.collect();

let files_with_brillig_debug_symbols: BTreeSet<FileId> = debug_symbols
.iter()
.flat_map(|function_symbols| {
let brillig_location_maps =
function_symbols.brillig_locations.values().flat_map(|brillig_location_map| {
brillig_location_map
.values()
.flat_map(|call_stack| call_stack.iter().map(|location| location.file))
});
brillig_location_maps
})
.collect();

files_with_debug_symbols.extend(files_with_brillig_debug_symbols);

let mut file_map = BTreeMap::new();

for file_id in files_with_debug_symbols {
Expand Down
4 changes: 2 additions & 2 deletions noir/noir-repo/compiler/noirc_frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ tracing.workspace = true
petgraph = "0.6"
rangemap = "1.4.0"
lalrpop-util = { version = "0.20.2", features = ["lexer"] }
strum = "0.24"
strum_macros = "0.24"


[dev-dependencies]
base64.workspace = true
strum = "0.24"
strum_macros = "0.24"

[build-dependencies]
lalrpop = "0.20.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use acvm::{AcirField, FieldElement};
use builtin_helpers::{
check_argument_count, check_function_not_yet_resolved, check_one_argument,
check_three_arguments, check_two_arguments, get_function_def, get_module, get_quoted,
check_three_arguments, check_two_arguments, get_expr, get_function_def, get_module, get_quoted,
get_slice, get_struct, get_trait_constraint, get_trait_def, get_tuple, get_type, get_u32,
hir_pattern_to_tokens, mutate_func_meta_type, parse, parse_tokens,
replace_func_meta_parameters, replace_func_meta_return_type,
Expand All @@ -17,8 +17,8 @@ use rustc_hash::FxHashMap as HashMap;

use crate::{
ast::{
FunctionKind, FunctionReturnType, IntegerBitSize, UnresolvedType, UnresolvedTypeData,
Visibility,
ExpressionKind, FunctionKind, FunctionReturnType, IntegerBitSize, UnresolvedType,
UnresolvedTypeData, Visibility,
},
hir::comptime::{errors::IResult, value::add_token_spans, InterpreterError, Value},
hir_def::function::FunctionBody,
Expand Down Expand Up @@ -47,6 +47,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
"array_as_str_unchecked" => array_as_str_unchecked(interner, arguments, location),
"array_len" => array_len(interner, arguments, location),
"as_slice" => as_slice(interner, arguments, location),
"expr_as_function_call" => expr_as_function_call(arguments, return_type, location),
"is_unconstrained" => Ok(Value::Bool(true)),
"function_def_name" => function_def_name(interner, arguments, location),
"function_def_parameters" => function_def_parameters(interner, arguments, location),
Expand All @@ -64,6 +65,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
"modulus_le_bits" => modulus_le_bits(interner, arguments, location),
"modulus_le_bytes" => modulus_le_bytes(interner, arguments, location),
"modulus_num_bits" => modulus_num_bits(interner, arguments, location),
"quoted_as_expr" => quoted_as_expr(arguments, return_type, location),
"quoted_as_module" => quoted_as_module(self, arguments, return_type, location),
"quoted_as_trait_constraint" => quoted_as_trait_constraint(self, arguments, location),
"quoted_as_type" => quoted_as_type(self, arguments, location),
Expand Down Expand Up @@ -91,6 +93,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
"type_as_struct" => type_as_struct(arguments, return_type, location),
"type_as_tuple" => type_as_tuple(arguments, return_type, location),
"type_eq" => type_eq(arguments, location),
"type_implements" => type_implements(interner, arguments, location),
"type_is_bool" => type_is_bool(arguments, location),
"type_is_field" => type_is_field(arguments, location),
"type_of" => type_of(arguments, location),
Expand Down Expand Up @@ -311,6 +314,20 @@ fn slice_insert(
Ok(Value::Slice(values, typ))
}

// fn as_expr(quoted: Quoted) -> Option<Expr>
fn quoted_as_expr(
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
) -> IResult<Value> {
let argument = check_one_argument(arguments, location)?;

let expr = parse(argument, parser::expression(), "an expression").ok();
let value = expr.map(|expr| Value::Expr(expr.kind));

option(return_type, value)
}

// fn as_module(quoted: Quoted) -> Option<Module>
fn quoted_as_module(
interpreter: &mut Interpreter,
Expand Down Expand Up @@ -490,6 +507,21 @@ fn type_eq(arguments: Vec<(Value, Location)>, location: Location) -> IResult<Val
Ok(Value::Bool(self_type == other_type))
}

// fn implements(self, constraint: TraitConstraint) -> bool
fn type_implements(
interner: &NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
let (typ, constraint) = check_two_arguments(arguments, location)?;

let typ = get_type(typ)?;
let (trait_id, generics) = get_trait_constraint(constraint)?;

let implements = interner.try_lookup_trait_implementation(&typ, trait_id, &generics).is_ok();
Ok(Value::Bool(implements))
}

// fn is_bool(self) -> bool
fn type_is_bool(arguments: Vec<(Value, Location)>, location: Location) -> IResult<Value> {
let value = check_one_argument(arguments, location)?;
Expand Down Expand Up @@ -656,6 +688,42 @@ fn zeroed(return_type: Type) -> IResult<Value> {
}
}

// fn as_function_call(self) -> Option<(Expr, [Expr])>
fn expr_as_function_call(
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
) -> IResult<Value> {
expr_as(arguments, return_type, location, |expr| {
if let ExpressionKind::Call(call_expression) = expr {
let function = Value::Expr(call_expression.func.kind);
let arguments = call_expression.arguments.into_iter();
let arguments = arguments.map(|argument| Value::Expr(argument.kind)).collect();
let arguments =
Value::Slice(arguments, Type::Slice(Box::new(Type::Quoted(QuotedType::Expr))));
Some(Value::Tuple(vec![function, arguments]))
} else {
None
}
})
}

// Helper function for implementing the `expr_as_...` functions.
fn expr_as<F>(
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
f: F,
) -> IResult<Value>
where
F: FnOnce(ExpressionKind) -> Option<Value>,
{
let self_argument = check_one_argument(arguments, location)?;
let expr = get_expr(self_argument)?;
let option_value = f(expr);
option(return_type, option_value)
}

// fn name(self) -> Quoted
fn function_def_name(
interner: &NodeInterner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use acvm::FieldElement;
use noirc_errors::Location;

use crate::{
ast::{IntegerBitSize, Signedness},
ast::{ExpressionKind, IntegerBitSize, Signedness},
hir::{
comptime::{errors::IResult, value::add_token_spans, Interpreter, InterpreterError, Value},
def_map::ModuleId,
Expand Down Expand Up @@ -145,6 +145,17 @@ pub(crate) fn get_u32((value, location): (Value, Location)) -> IResult<u32> {
}
}

pub(crate) fn get_expr((value, location): (Value, Location)) -> IResult<ExpressionKind> {
match value {
Value::Expr(expr) => Ok(expr),
value => {
let expected = Type::Quoted(QuotedType::Expr);
let actual = value.get_type().into_owned();
Err(InterpreterError::TypeMismatch { expected, actual, location })
}
}
}

pub(crate) fn get_function_def((value, location): (Value, Location)) -> IResult<FuncId> {
match value {
Value::FunctionDefinition(id) => Ok(id),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub enum Value {
ModuleDefinition(ModuleId),
Type(Type),
Zeroed(Type),
Expr(ExpressionKind),
}

impl Value {
Expand Down Expand Up @@ -103,6 +104,7 @@ impl Value {
Value::ModuleDefinition(_) => Type::Quoted(QuotedType::Module),
Value::Type(_) => Type::Quoted(QuotedType::Type),
Value::Zeroed(typ) => return Cow::Borrowed(typ),
Value::Expr(_) => Type::Quoted(QuotedType::Expr),
})
}

Expand Down Expand Up @@ -223,6 +225,7 @@ impl Value {
}
};
}
Value::Expr(expr) => expr,
Value::Pointer(..)
| Value::StructDefinition(_)
| Value::TraitConstraint(..)
Expand Down Expand Up @@ -345,7 +348,8 @@ impl Value {
HirExpression::Literal(HirLiteral::Slice(HirArrayLiteral::Standard(elements)))
}
Value::Quoted(tokens) => HirExpression::Unquote(add_token_spans(tokens, location.span)),
Value::Pointer(..)
Value::Expr(..)
| Value::Pointer(..)
| Value::StructDefinition(_)
| Value::TraitConstraint(..)
| Value::TraitDefinition(_)
Expand Down Expand Up @@ -530,6 +534,7 @@ impl<'value, 'interner> Display for ValuePrinter<'value, 'interner> {
Value::ModuleDefinition(_) => write!(f, "(module)"),
Value::Zeroed(typ) => write!(f, "(zeroed {typ})"),
Value::Type(typ) => write!(f, "{}", typ),
Value::Expr(expr) => write!(f, "{}", expr),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ impl ModuleData {
}
}

pub(crate) fn scope(&self) -> &ItemScope {
pub fn scope(&self) -> &ItemScope {
&self.scope
}

pub fn definitions(&self) -> &ItemScope {
&self.definitions
}

fn declare(
&mut self,
name: Ident,
Expand Down
3 changes: 1 addition & 2 deletions noir/noir-repo/compiler/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 +883,7 @@ impl AsRef<str> for SecondaryAttribute {

/// Note that `self` is not present - it is a contextual keyword rather than a true one as it is
/// only special within `impl`s. Otherwise `self` functions as a normal identifier.
#[derive(PartialEq, Eq, Hash, Debug, Copy, Clone, PartialOrd, Ord)]
#[cfg_attr(test, derive(strum_macros::EnumIter))]
#[derive(PartialEq, Eq, Hash, Debug, Copy, Clone, PartialOrd, Ord, strum_macros::EnumIter)]
pub enum Keyword {
As,
Assert,
Expand Down
2 changes: 2 additions & 0 deletions noir/noir-repo/compiler/noirc_frontend/src/parser/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub enum ParserErrorReason {
ExpectedFieldName(Token),
#[error("expected a pattern but found a type - {0}")]
ExpectedPatternButFoundType(Token),
#[error("expected an identifier after ::")]
ExpectedIdentifierAfterColons,
#[error("Expected a ; separating these two statements")]
MissingSeparatingSemi,
#[error("constrain keyword is deprecated")]
Expand Down
30 changes: 28 additions & 2 deletions noir/noir-repo/compiler/noirc_frontend/src/parser/parser/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,19 @@ pub fn path_no_turbofish() -> impl NoirParser<Path> {
}

fn path_inner<'a>(segment: impl NoirParser<PathSegment> + 'a) -> impl NoirParser<Path> + 'a {
let segments = segment.separated_by(just(Token::DoubleColon)).at_least(1);
let segments = segment
.separated_by(just(Token::DoubleColon))
.at_least(1)
.then(just(Token::DoubleColon).then_ignore(none_of(Token::LeftBrace).rewind()).or_not())
.validate(|(path_segments, trailing_colons), span, emit_error| {
if trailing_colons.is_some() {
emit_error(ParserError::with_reason(
ParserErrorReason::ExpectedIdentifierAfterColons,
span,
));
}
path_segments
});
let make_path = |kind| move |segments, span| Path { segments, kind, span };

let prefix = |key| keyword(key).ignore_then(just(Token::DoubleColon));
Expand Down Expand Up @@ -69,7 +81,7 @@ mod test {
use super::*;
use crate::parser::{
parse_type,
parser::test_helpers::{parse_all_failing, parse_with},
parser::test_helpers::{parse_all_failing, parse_recover, parse_with},
};

#[test]
Expand Down Expand Up @@ -111,4 +123,18 @@ mod test {
vec!["crate", "crate::std::crate", "foo::bar::crate", "foo::dep"],
);
}

#[test]
fn parse_path_with_trailing_colons() {
let src = "foo::bar::";

let (path, errors) = parse_recover(path_no_turbofish(), src);
let path = path.unwrap();
assert_eq!(path.segments.len(), 2);
assert_eq!(path.segments[0].ident.0.contents, "foo");
assert_eq!(path.segments[1].ident.0.contents, "bar");

assert_eq!(errors.len(), 1);
assert_eq!(errors[0].message, "expected an identifier after ::");
}
}
1 change: 1 addition & 0 deletions noir/noir-repo/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"foralls",
"formatcp",
"frontends",
"fuzzer",
"fxhash",
"getrandom",
"gloo",
Expand Down
Loading

0 comments on commit 91042c7

Please sign in to comment.