Skip to content

Commit

Permalink
Merge branch 'master' into release-please--branches--master
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray authored Nov 17, 2023
2 parents 39cd7f2 + 9701f90 commit c0ffc13
Show file tree
Hide file tree
Showing 308 changed files with 1,919 additions and 1,471 deletions.
19 changes: 11 additions & 8 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ noirc_abi = { path = "tooling/noirc_abi" }
bb_abstraction_leaks = { path = "tooling/bb_abstraction_leaks" }

# LSP
async-lsp = { version = "0.0.5", default-features = false }
lsp-types = "0.94"
async-lsp = { version = "0.1.0", default-features = false }
lsp-types = "0.94.1"
tower = "0.4"

# Wasm
Expand Down
2 changes: 1 addition & 1 deletion compiler/integration-tests/circuits/main/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main(x : Field, y : pub Field) {
fn main(x: Field, y: pub Field) {
assert(x != y);
}
10 changes: 5 additions & 5 deletions compiler/integration-tests/circuits/recursion/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use dep::std;

fn main(
verification_key : [Field; 114],
proof : [Field; 94],
public_inputs : [Field; 1],
key_hash : Field,
input_aggregation_object : [Field; 16],
verification_key: [Field; 114],
proof: [Field; 94],
public_inputs: [Field; 1],
key_hash: Field,
input_aggregation_object: [Field; 16]
) -> pub [Field; 16] {
let vk : [Field] = verification_key;
let p : [Field] = proof;
Expand Down
7 changes: 4 additions & 3 deletions compiler/noirc_errors/src/debug_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct DebugInfo {

/// Holds OpCodes Counts for Acir and Brillig Opcodes
/// To be printed with `nargo info --profile-info`
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct OpCodesCount {
pub acir_size: usize,
pub brillig_size: usize,
Expand Down Expand Up @@ -51,12 +52,12 @@ impl DebugInfo {
self.locations.get(loc).cloned()
}

pub fn count_span_opcodes(&self) -> HashMap<&Location, OpCodesCount> {
let mut accumulator: HashMap<&Location, Vec<&OpcodeLocation>> = HashMap::new();
pub fn count_span_opcodes(&self) -> HashMap<Location, OpCodesCount> {
let mut accumulator: HashMap<Location, Vec<&OpcodeLocation>> = HashMap::new();

for (opcode_location, locations) in self.locations.iter() {
for location in locations.iter() {
let opcodes = accumulator.entry(location).or_insert(Vec::new());
let opcodes = accumulator.entry(*location).or_insert(Vec::new());
opcodes.push(opcode_location);
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/ssa/ir/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub(crate) fn display_instruction(
)
}
Instruction::RangeCheck { value, max_bit_size, .. } => {
write!(f, "range_check {} to {} bits", show(*value), *max_bit_size,)
writeln!(f, "range_check {} to {} bits", show(*value), *max_bit_size,)
}
}
}
16 changes: 16 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/flatten_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,22 @@ impl<'f> Context<'f> {
self.remember_store(address, value);
Instruction::Store { address, value }
}
Instruction::RangeCheck { value, max_bit_size, assert_message } => {
// Replace value with `value * predicate` to zero out value when predicate is inactive.

// Condition needs to be cast to argument type in order to multiply them together.
let argument_type = self.inserter.function.dfg.type_of_value(value);
let casted_condition = self.insert_instruction(
Instruction::Cast(condition, argument_type),
call_stack.clone(),
);

let value = self.insert_instruction(
Instruction::binary(BinaryOp::Mul, value, casted_condition),
call_stack.clone(),
);
Instruction::RangeCheck { value, max_bit_size, assert_message }
}
other => other,
}
} else {
Expand Down
21 changes: 16 additions & 5 deletions compiler/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ pub struct FunctionDefinition {
pub visibility: FunctionVisibility,

pub generics: UnresolvedGenerics,
pub parameters: Vec<(Pattern, UnresolvedType, Visibility)>,
pub parameters: Vec<Param>,
pub body: BlockExpression,
pub span: Span,
pub where_clause: Vec<UnresolvedTraitConstraint>,
Expand All @@ -379,6 +379,14 @@ pub struct FunctionDefinition {
pub return_distinctness: Distinctness,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Param {
pub visibility: Visibility,
pub pattern: Pattern,
pub typ: UnresolvedType,
pub span: Span,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum FunctionReturnType {
/// Returns type is not specified.
Expand Down Expand Up @@ -634,8 +642,11 @@ impl FunctionDefinition {
) -> FunctionDefinition {
let p = parameters
.iter()
.map(|(ident, unresolved_type)| {
(Pattern::Identifier(ident.clone()), unresolved_type.clone(), Visibility::Private)
.map(|(ident, unresolved_type)| Param {
visibility: Visibility::Private,
pattern: Pattern::Identifier(ident.clone()),
typ: unresolved_type.clone(),
span: ident.span().merge(unresolved_type.span.unwrap()),
})
.collect();
FunctionDefinition {
Expand All @@ -661,8 +672,8 @@ impl Display for FunctionDefinition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{:?}", self.attributes)?;

let parameters = vecmap(&self.parameters, |(name, r#type, visibility)| {
format!("{name}: {visibility} {type}")
let parameters = vecmap(&self.parameters, |Param { visibility, pattern, typ, span: _ }| {
format!("{pattern}: {visibility} {typ}")
});

let where_clause = vecmap(&self.where_clause, ToString::to_string);
Expand Down
8 changes: 6 additions & 2 deletions compiler/noirc_frontend/src/ast/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use noirc_errors::Span;

use crate::{
token::{Attributes, FunctionAttribute, SecondaryAttribute},
FunctionReturnType, Ident, Pattern, Visibility,
FunctionReturnType, Ident, Param, Visibility,
};

use super::{FunctionDefinition, UnresolvedType, UnresolvedTypeData};
Expand Down Expand Up @@ -45,6 +45,10 @@ impl NoirFunction {
NoirFunction { kind: FunctionKind::Oracle, def }
}

pub fn return_visibility(&self) -> Visibility {
self.def.return_visibility
}

pub fn return_type(&self) -> UnresolvedType {
match &self.def.return_type {
FunctionReturnType::Default(_) => {
Expand All @@ -59,7 +63,7 @@ impl NoirFunction {
pub fn name_ident(&self) -> &Ident {
&self.def.name
}
pub fn parameters(&self) -> &Vec<(Pattern, UnresolvedType, Visibility)> {
pub fn parameters(&self) -> &[Param] {
&self.def.parameters
}
pub fn attributes(&self) -> &Attributes {
Expand Down
8 changes: 8 additions & 0 deletions compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,14 @@ pub enum Pattern {
}

impl Pattern {
pub fn span(&self) -> Span {
match self {
Pattern::Identifier(ident) => ident.span(),
Pattern::Mutable(_, span) | Pattern::Tuple(_, span) | Pattern::Struct(_, _, span) => {
*span
}
}
}
pub fn name_ident(&self) -> &Ident {
match self {
Pattern::Identifier(name_ident) => name_ident,
Expand Down
19 changes: 10 additions & 9 deletions compiler/noirc_frontend/src/hir/aztec_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use crate::{
};
use crate::{
ForLoopStatement, ForRange, FunctionDefinition, FunctionVisibility, ImportStatement,
NoirStruct, PrefixExpression, Signedness, StatementKind, StructType, Type, TypeImpl, UnaryOp,
NoirStruct, Param, PrefixExpression, Signedness, StatementKind, StructType, Type, TypeImpl,
UnaryOp,
};
use fm::FileId;

Expand Down Expand Up @@ -226,12 +227,12 @@ fn check_for_compute_note_hash_and_nullifier_definition(module: &SortedModule) -
module.functions.iter().any(|func| {
func.def.name.0.contents == "compute_note_hash_and_nullifier"
&& func.def.parameters.len() == 4
&& func.def.parameters[0].1.typ == UnresolvedTypeData::FieldElement
&& func.def.parameters[1].1.typ == UnresolvedTypeData::FieldElement
&& func.def.parameters[2].1.typ == UnresolvedTypeData::FieldElement
&& func.def.parameters[0].typ.typ == UnresolvedTypeData::FieldElement
&& func.def.parameters[1].typ.typ == UnresolvedTypeData::FieldElement
&& func.def.parameters[2].typ.typ == UnresolvedTypeData::FieldElement
// checks if the 4th parameter is an array and the Box<UnresolvedType> in
// Array(Option<UnresolvedTypeExpression>, Box<UnresolvedType>) contains only fields
&& match &func.def.parameters[3].1.typ {
&& match &func.def.parameters[3].typ.typ {
UnresolvedTypeData::Array(_, inner_type) => {
match inner_type.typ {
UnresolvedTypeData::FieldElement => true,
Expand Down Expand Up @@ -513,14 +514,14 @@ fn generate_selector_impl(structure: &NoirStruct) -> TypeImpl {
/// fn foo() {
/// // ...
/// }
pub(crate) fn create_inputs(ty: &str) -> (Pattern, UnresolvedType, Visibility) {
pub(crate) fn create_inputs(ty: &str) -> Param {
let context_ident = ident("inputs");
let context_pattern = Pattern::Identifier(context_ident);
let type_path = chained_path!("aztec", "abi", ty);
let context_type = make_type(UnresolvedTypeData::Named(type_path, vec![]));
let visibility = Visibility::Private;

(context_pattern, context_type, visibility)
Param { pattern: context_pattern, typ: context_type, visibility, span: Span::default() }
}

/// Creates the private context object to be accessed within the function, the parameters need to be extracted to be
Expand Down Expand Up @@ -548,7 +549,7 @@ pub(crate) fn create_inputs(ty: &str) -> (Pattern, UnresolvedType, Visibility) {
/// let mut context = PrivateContext::new(inputs, hasher.hash());
/// }
/// ```
fn create_context(ty: &str, params: &[(Pattern, UnresolvedType, Visibility)]) -> Vec<Statement> {
fn create_context(ty: &str, params: &[Param]) -> Vec<Statement> {
let mut injected_expressions: Vec<Statement> = vec![];

// `let mut hasher = Hasher::new();`
Expand All @@ -564,7 +565,7 @@ fn create_context(ty: &str, params: &[(Pattern, UnresolvedType, Visibility)]) ->
injected_expressions.push(let_hasher);

// Iterate over each of the function parameters, adding to them to the hasher
params.iter().for_each(|(pattern, typ, _vis)| {
params.iter().for_each(|Param { pattern, typ, span: _, visibility: _ }| {
match pattern {
Pattern::Identifier(identifier) => {
// Match the type to determine the padding to do
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::{
};
use crate::{
ArrayLiteral, ContractFunctionType, Distinctness, ForRange, FunctionVisibility, Generics,
LValue, NoirStruct, NoirTypeAlias, Path, PathKind, Pattern, Shared, StructType, Type,
LValue, NoirStruct, NoirTypeAlias, Param, Path, PathKind, Pattern, Shared, StructType, Type,
TypeAliasType, TypeBinding, TypeVariable, UnaryOp, UnresolvedGenerics,
UnresolvedTraitConstraint, UnresolvedType, UnresolvedTypeData, UnresolvedTypeExpression,
Visibility, ERROR_IDENT,
Expand Down Expand Up @@ -760,7 +760,7 @@ impl<'a> Resolver<'a> {
let mut parameters = vec![];
let mut parameter_types = vec![];

for (pattern, typ, visibility) in func.parameters().iter().cloned() {
for Param { visibility, pattern, typ, span: _ } in func.parameters().iter().cloned() {
if visibility == Visibility::Public && !self.pub_allowed(func) {
self.push_err(ResolverError::UnnecessaryPub {
ident: func.name_ident().clone(),
Expand Down
29 changes: 25 additions & 4 deletions compiler/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ impl<'interner> TypeChecker<'interner> {
// variable to handle generic functions.
let t = self.interner.id_type_substitute_trait_as_type(ident.id);
let (typ, bindings) = t.instantiate(self.interner);

// Push any trait constraints required by this definition to the context
// to be checked later when the type of this variable is further constrained.
if let Some(definition) = self.interner.try_definition(ident.id) {
if let DefinitionKind::Function(function) = definition.kind {
let function = self.interner.function_meta(&function);
for mut constraint in function.trait_constraints.clone() {
constraint.typ = constraint.typ.substitute(&bindings);
self.trait_constraints.push((constraint, *expr_id));
}
}
}

self.interner.store_instantiation_bindings(*expr_id, bindings);
typ
}
Expand Down Expand Up @@ -294,7 +307,7 @@ impl<'interner> TypeChecker<'interner> {
typ
}

fn verify_trait_constraint(
pub fn verify_trait_constraint(
&mut self,
object_type: &Type,
trait_id: TraitId,
Expand Down Expand Up @@ -859,7 +872,7 @@ impl<'interner> TypeChecker<'interner> {
method_name: &str,
expr_id: &ExprId,
) -> Option<HirMethodReference> {
match object_type {
match object_type.follow_bindings() {
Type::Struct(typ, _args) => {
let id = typ.borrow().id;
match self.interner.lookup_method(object_type, id, method_name, false) {
Expand Down Expand Up @@ -914,12 +927,20 @@ impl<'interner> TypeChecker<'interner> {
.interner
.lookup_primitive_trait_method_mut(element.as_ref(), method_name)
.map(HirMethodReference::FuncId)
.or_else(|| self.lookup_method(element, method_name, expr_id)),
.or_else(|| self.lookup_method(&element, method_name, expr_id)),

// If we fail to resolve the object to a struct type, we have no way of type
// checking its arguments as we can't even resolve the name of the function
Type::Error => None,

other => match self.interner.lookup_primitive_method(other, method_name) {
// The type variable must be unbound at this point since follow_bindings was called
Type::TypeVariable(_, TypeVariableKind::Normal) => {
let span = self.interner.expr_span(expr_id);
self.errors.push(TypeCheckError::TypeAnnotationsNeeded { span });
None
}

other => match self.interner.lookup_primitive_method(&other, method_name) {
Some(method_id) => Some(HirMethodReference::FuncId(method_id)),
None => {
self.errors.push(TypeCheckError::UnresolvedMethodCall {
Expand Down
Loading

0 comments on commit c0ffc13

Please sign in to comment.