Skip to content

Commit dc48336

Browse files
authored
Merge d09c9ec into 7ca6d24
2 parents 7ca6d24 + d09c9ec commit dc48336

File tree

91 files changed

+1802
-500
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+1802
-500
lines changed

.noir-sync-commit

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fc74c55ffed892962413c6fe15af62e1d2e7b785
1+
5bbd9ba9a6d6494fd16813b44036b78c871f6613

noir-projects/aztec-nr/aztec/src/keys/getters/test.nr

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn setup() -> (TestEnvironment, PrivateContext, TestAccount) {
1818

1919
#[test(should_fail_with="Invalid public keys hint for address")]
2020
fn test_get_current_keys_unknown_unregistered() {
21-
let (_, context, account) = setup();
21+
let (_, mut context, account) = setup();
2222

2323
let _ = OracleMock::mock("getPublicKeysAndPartialAddress").returns([0; KEY_ORACLE_RESPONSE_LENGTH]).times(1);
2424
let _ = get_current_public_keys(&mut context, account.address);

noir-projects/noir-contracts/contracts/nft_contract/src/main.nr

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ contract NFT {
233233
let from_ovpk_m = get_current_public_keys(&mut context, from).ovpk_m;
234234
let to_keys = get_current_public_keys(&mut context, to);
235235

236-
let new_note = NFTNote::new(token_id, to_keys.npk_m.hash());
236+
let mut new_note = NFTNote::new(token_id, to_keys.npk_m.hash());
237237
nfts.at(to).insert(&mut new_note).emit(encode_and_encrypt_note_with_keys(&mut context, from_ovpk_m, to_keys.ivpk_m, to));
238238
}
239239

noir/noir-repo/aztec_macros/src/transforms/storage.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,11 @@ pub fn generate_storage_implementation(
222222
})
223223
.collect();
224224

225-
let storage_constructor_statement = make_statement(StatementKind::Expression(expression(
226-
ExpressionKind::constructor((chained_path!(storage_struct_name), field_constructors)),
227-
)));
225+
let storage_constructor_statement =
226+
make_statement(StatementKind::Expression(expression(ExpressionKind::constructor((
227+
UnresolvedType::from_path(chained_path!(storage_struct_name)),
228+
field_constructors,
229+
)))));
228230

229231
// This is the type over which the impl is generic.
230232
let generic_context_ident = ident("Context");

noir/noir-repo/aztec_macros/src/utils/parse_utils.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ fn empty_expression(expression: &mut Expression) {
299299
ExpressionKind::Quote(..)
300300
| ExpressionKind::Resolved(_)
301301
| ExpressionKind::Interned(_)
302+
| ExpressionKind::InternedStatement(_)
302303
| ExpressionKind::Error => (),
303304
}
304305
}
@@ -505,7 +506,7 @@ fn empty_method_call_expression(method_call_expression: &mut MethodCallExpressio
505506
}
506507

507508
fn empty_constructor_expression(constructor_expression: &mut ConstructorExpression) {
508-
empty_path(&mut constructor_expression.type_name);
509+
empty_unresolved_type(&mut constructor_expression.typ);
509510
for (name, expression) in constructor_expression.fields.iter_mut() {
510511
empty_ident(name);
511512
empty_expression(expression);

noir/noir-repo/compiler/noirc_frontend/src/ast/expression.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::ast::{
77
};
88
use crate::hir::def_collector::errors::DefCollectorErrorKind;
99
use crate::macros_api::StructId;
10-
use crate::node_interner::{ExprId, InternedExpressionKind, QuotedTypeId};
10+
use crate::node_interner::{ExprId, InternedExpressionKind, InternedStatementKind, QuotedTypeId};
1111
use crate::token::{Attributes, FunctionAttribute, Token, Tokens};
1212
use crate::{Kind, Type};
1313
use acvm::{acir::AcirField, FieldElement};
@@ -48,6 +48,10 @@ pub enum ExpressionKind {
4848
// The actual ExpressionKind can be retrieved with a NodeInterner.
4949
Interned(InternedExpressionKind),
5050

51+
/// Interned statements are allowed to be parsed as expressions in case they resolve
52+
/// to an StatementKind::Expression or StatementKind::Semi.
53+
InternedStatement(InternedStatementKind),
54+
5155
Error,
5256
}
5357

@@ -200,9 +204,11 @@ impl ExpressionKind {
200204
ExpressionKind::Literal(Literal::FmtStr(contents))
201205
}
202206

203-
pub fn constructor((type_name, fields): (Path, Vec<(Ident, Expression)>)) -> ExpressionKind {
207+
pub fn constructor(
208+
(typ, fields): (UnresolvedType, Vec<(Ident, Expression)>),
209+
) -> ExpressionKind {
204210
ExpressionKind::Constructor(Box::new(ConstructorExpression {
205-
type_name,
211+
typ,
206212
fields,
207213
struct_type: None,
208214
}))
@@ -536,7 +542,7 @@ pub struct MethodCallExpression {
536542

537543
#[derive(Debug, PartialEq, Eq, Clone)]
538544
pub struct ConstructorExpression {
539-
pub type_name: Path,
545+
pub typ: UnresolvedType,
540546
pub fields: Vec<(Ident, Expression)>,
541547

542548
/// This may be filled out during macro expansion
@@ -615,6 +621,7 @@ impl Display for ExpressionKind {
615621
write!(f, "quote {{ {} }}", tokens.join(" "))
616622
}
617623
AsTraitPath(path) => write!(f, "{path}"),
624+
InternedStatement(_) => write!(f, "?InternedStatement"),
618625
}
619626
}
620627
}
@@ -717,7 +724,7 @@ impl Display for ConstructorExpression {
717724
let fields =
718725
self.fields.iter().map(|(ident, expr)| format!("{ident}: {expr}")).collect::<Vec<_>>();
719726

720-
write!(f, "({} {{ {} }})", self.type_name, fields.join(", "))
727+
write!(f, "({} {{ {} }})", self.typ, fields.join(", "))
721728
}
722729
}
723730

noir/noir-repo/compiler/noirc_frontend/src/ast/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,19 @@ impl UnresolvedType {
344344
pub(crate) fn is_type_expression(&self) -> bool {
345345
matches!(&self.typ, UnresolvedTypeData::Expression(_))
346346
}
347+
348+
pub fn from_path(mut path: Path) -> Self {
349+
let span = path.span;
350+
let last_segment = path.segments.last_mut().unwrap();
351+
let generics = last_segment.generics.take();
352+
let generic_type_args = if let Some(generics) = generics {
353+
GenericTypeArgs { ordered_args: generics, named_args: Vec::new() }
354+
} else {
355+
GenericTypeArgs::default()
356+
};
357+
let typ = UnresolvedTypeData::Named(path, generic_type_args, true);
358+
UnresolvedType { typ, span }
359+
}
347360
}
348361

349362
impl UnresolvedTypeData {

noir/noir-repo/compiler/noirc_frontend/src/ast/statement.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ impl Pattern {
620620
}
621621
Some(Expression {
622622
kind: ExpressionKind::Constructor(Box::new(ConstructorExpression {
623-
type_name: path.clone(),
623+
typ: UnresolvedType::from_path(path.clone()),
624624
fields,
625625
struct_type: None,
626626
})),

noir/noir-repo/compiler/noirc_frontend/src/ast/structure.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ use crate::token::SecondaryAttribute;
66
use iter_extended::vecmap;
77
use noirc_errors::Span;
88

9-
use super::Documented;
9+
use super::{Documented, ItemVisibility};
1010

1111
/// Ast node for a struct
1212
#[derive(Clone, Debug, PartialEq, Eq)]
1313
pub struct NoirStruct {
1414
pub name: Ident,
1515
pub attributes: Vec<SecondaryAttribute>,
16+
pub visibility: ItemVisibility,
1617
pub generics: UnresolvedGenerics,
1718
pub fields: Vec<Documented<StructField>>,
1819
pub span: Span,

noir/noir-repo/compiler/noirc_frontend/src/ast/traits.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub struct NoirTrait {
2222
pub span: Span,
2323
pub items: Vec<Documented<TraitItem>>,
2424
pub attributes: Vec<SecondaryAttribute>,
25+
pub visibility: ItemVisibility,
2526
}
2627

2728
/// Any declaration inside the body of a trait that a user is required to

noir/noir-repo/compiler/noirc_frontend/src/ast/visitor.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
InternedUnresolvedTypeData, QuotedTypeId,
1717
},
1818
parser::{Item, ItemKind, ParsedSubModule},
19-
token::{CustomAtrribute, SecondaryAttribute, Tokens},
19+
token::{CustomAttribute, SecondaryAttribute, Tokens},
2020
ParsedModule, QuotedType,
2121
};
2222

@@ -461,7 +461,7 @@ pub trait Visitor {
461461
true
462462
}
463463

464-
fn visit_custom_attribute(&mut self, _: &CustomAtrribute, _target: AttributeTarget) {}
464+
fn visit_custom_attribute(&mut self, _: &CustomAttribute, _target: AttributeTarget) {}
465465
}
466466

467467
impl ParsedModule {
@@ -841,6 +841,7 @@ impl Expression {
841841
ExpressionKind::Quote(tokens) => visitor.visit_quote(tokens),
842842
ExpressionKind::Resolved(expr_id) => visitor.visit_resolved_expression(*expr_id),
843843
ExpressionKind::Interned(id) => visitor.visit_interned_expression(*id),
844+
ExpressionKind::InternedStatement(id) => visitor.visit_interned_statement(*id),
844845
ExpressionKind::Error => visitor.visit_error_expression(),
845846
}
846847
}
@@ -948,7 +949,7 @@ impl ConstructorExpression {
948949
}
949950

950951
pub fn accept_children(&self, visitor: &mut impl Visitor) {
951-
self.type_name.accept(visitor);
952+
self.typ.accept(visitor);
952953

953954
for (_field_name, expression) in &self.fields {
954955
expression.accept(visitor);
@@ -1377,7 +1378,7 @@ impl SecondaryAttribute {
13771378
}
13781379
}
13791380

1380-
impl CustomAtrribute {
1381+
impl CustomAttribute {
13811382
pub fn accept(&self, target: AttributeTarget, visitor: &mut impl Visitor) {
13821383
visitor.visit_custom_attribute(self, target);
13831384
}

noir/noir-repo/compiler/noirc_frontend/src/elaborator/comptime.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ impl<'context> Elaborator<'context> {
102102
elaborator.function_context.push(FunctionContext::default());
103103
elaborator.scopes.start_function();
104104

105+
elaborator.local_module = self.local_module;
106+
elaborator.file = self.file;
107+
105108
setup(&mut elaborator);
106109

107110
elaborator.populate_scope_from_comptime_scopes();
@@ -316,7 +319,7 @@ impl<'context> Elaborator<'context> {
316319
// If the function is varargs, push the type of the last slice element N times
317320
// to account for N extra arguments.
318321
let modifiers = interpreter.elaborator.interner.function_modifiers(&function);
319-
let is_varargs = modifiers.attributes.is_varargs();
322+
let is_varargs = modifiers.attributes.has_varargs();
320323
let varargs_type = if is_varargs { parameters.pop() } else { None };
321324

322325
let varargs_elem_type = varargs_type.as_ref().and_then(|t| t.slice_element_type());

noir/noir-repo/compiler/noirc_frontend/src/elaborator/expressions.rs

+83-9
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use rustc_hash::FxHashSet as HashSet;
55

66
use crate::{
77
ast::{
8-
ArrayLiteral, ConstructorExpression, IfExpression, InfixExpression, Lambda,
9-
UnresolvedTypeExpression,
8+
ArrayLiteral, ConstructorExpression, IfExpression, InfixExpression, Lambda, UnaryOp,
9+
UnresolvedTypeData, UnresolvedTypeExpression,
1010
},
1111
hir::{
1212
comptime::{self, InterpreterError},
@@ -25,9 +25,9 @@ use crate::{
2525
macros_api::{
2626
BlockExpression, CallExpression, CastExpression, Expression, ExpressionKind, HirLiteral,
2727
HirStatement, Ident, IndexExpression, Literal, MemberAccessExpression,
28-
MethodCallExpression, PrefixExpression,
28+
MethodCallExpression, PrefixExpression, StatementKind,
2929
},
30-
node_interner::{DefinitionKind, ExprId, FuncId, TraitMethodId},
30+
node_interner::{DefinitionKind, ExprId, FuncId, InternedStatementKind, TraitMethodId},
3131
token::Tokens,
3232
QuotedType, Shared, StructType, Type,
3333
};
@@ -67,6 +67,9 @@ impl<'context> Elaborator<'context> {
6767
let expr = Expression::new(expr_kind.clone(), expr.span);
6868
return self.elaborate_expression(expr);
6969
}
70+
ExpressionKind::InternedStatement(id) => {
71+
return self.elaborate_interned_statement_as_expr(id, expr.span);
72+
}
7073
ExpressionKind::Error => (HirExpression::Error, Type::Error),
7174
ExpressionKind::Unquote(_) => {
7275
self.push_err(ResolverError::UnquoteUsedOutsideQuote { span: expr.span });
@@ -80,6 +83,29 @@ impl<'context> Elaborator<'context> {
8083
(id, typ)
8184
}
8285

86+
fn elaborate_interned_statement_as_expr(
87+
&mut self,
88+
id: InternedStatementKind,
89+
span: Span,
90+
) -> (ExprId, Type) {
91+
match self.interner.get_statement_kind(id) {
92+
StatementKind::Expression(expr) | StatementKind::Semi(expr) => {
93+
self.elaborate_expression(expr.clone())
94+
}
95+
StatementKind::Interned(id) => self.elaborate_interned_statement_as_expr(*id, span),
96+
StatementKind::Error => {
97+
let expr = Expression::new(ExpressionKind::Error, span);
98+
self.elaborate_expression(expr)
99+
}
100+
other => {
101+
let statement = other.to_string();
102+
self.push_err(ResolverError::InvalidInternedStatementInExpr { statement, span });
103+
let expr = Expression::new(ExpressionKind::Error, span);
104+
self.elaborate_expression(expr)
105+
}
106+
}
107+
}
108+
83109
pub(super) fn elaborate_block(&mut self, block: BlockExpression) -> (HirExpression, Type) {
84110
let (block, typ) = self.elaborate_block_expression(block);
85111
(HirExpression::Block(block), typ)
@@ -248,10 +274,17 @@ impl<'context> Elaborator<'context> {
248274
}
249275

250276
fn elaborate_prefix(&mut self, prefix: PrefixExpression, span: Span) -> (ExprId, Type) {
277+
let rhs_span = prefix.rhs.span;
278+
251279
let (rhs, rhs_type) = self.elaborate_expression(prefix.rhs);
252280
let trait_id = self.interner.get_prefix_operator_trait_method(&prefix.operator);
253281

254282
let operator = prefix.operator;
283+
284+
if let UnaryOp::MutableReference = operator {
285+
self.check_can_mutate(rhs, rhs_span);
286+
}
287+
255288
let expr =
256289
HirExpression::Prefix(HirPrefixExpression { operator, rhs, trait_method_id: trait_id });
257290
let expr_id = self.interner.push_expr(expr);
@@ -264,6 +297,26 @@ impl<'context> Elaborator<'context> {
264297
(expr_id, typ)
265298
}
266299

300+
fn check_can_mutate(&mut self, expr_id: ExprId, span: Span) {
301+
let expr = self.interner.expression(&expr_id);
302+
match expr {
303+
HirExpression::Ident(hir_ident, _) => {
304+
if let Some(definition) = self.interner.try_definition(hir_ident.id) {
305+
if !definition.mutable {
306+
self.push_err(TypeCheckError::CannotMutateImmutableVariable {
307+
name: definition.name.clone(),
308+
span,
309+
});
310+
}
311+
}
312+
}
313+
HirExpression::MemberAccess(member_access) => {
314+
self.check_can_mutate(member_access.lhs, span);
315+
}
316+
_ => (),
317+
}
318+
}
319+
267320
fn elaborate_index(&mut self, index_expr: IndexExpression) -> (HirExpression, Type) {
268321
let span = index_expr.index.span;
269322
let (index, index_type) = self.elaborate_expression(index_expr.index);
@@ -436,22 +489,43 @@ impl<'context> Elaborator<'context> {
436489
&mut self,
437490
constructor: ConstructorExpression,
438491
) -> (HirExpression, Type) {
492+
let span = constructor.typ.span;
493+
494+
// A constructor type can either be a Path or an interned UnresolvedType.
495+
// We represent both as UnresolvedType (with Path being a Named UnresolvedType)
496+
// and error if we don't get a Named path.
497+
let mut typ = constructor.typ.typ;
498+
if let UnresolvedTypeData::Interned(id) = typ {
499+
typ = self.interner.get_unresolved_type_data(id).clone();
500+
}
501+
let UnresolvedTypeData::Named(mut path, generics, _) = typ else {
502+
self.push_err(ResolverError::NonStructUsedInConstructor { typ: typ.to_string(), span });
503+
return (HirExpression::Error, Type::Error);
504+
};
505+
506+
let last_segment = path.segments.last_mut().unwrap();
507+
if !generics.ordered_args.is_empty() {
508+
last_segment.generics = Some(generics.ordered_args);
509+
}
510+
439511
let exclude_last_segment = true;
440-
self.check_unsupported_turbofish_usage(&constructor.type_name, exclude_last_segment);
512+
self.check_unsupported_turbofish_usage(&path, exclude_last_segment);
441513

442-
let span = constructor.type_name.span();
443-
let last_segment = constructor.type_name.last_segment();
514+
let last_segment = path.last_segment();
444515
let is_self_type = last_segment.ident.is_self_type_name();
445516

446517
let (r#type, struct_generics) = if let Some(struct_id) = constructor.struct_type {
447518
let typ = self.interner.get_struct(struct_id);
448519
let generics = typ.borrow().instantiate(self.interner);
449520
(typ, generics)
450521
} else {
451-
match self.lookup_type_or_error(constructor.type_name) {
522+
match self.lookup_type_or_error(path) {
452523
Some(Type::Struct(r#type, struct_generics)) => (r#type, struct_generics),
453524
Some(typ) => {
454-
self.push_err(ResolverError::NonStructUsedInConstructor { typ, span });
525+
self.push_err(ResolverError::NonStructUsedInConstructor {
526+
typ: typ.to_string(),
527+
span,
528+
});
455529
return (HirExpression::Error, Type::Error);
456530
}
457531
None => return (HirExpression::Error, Type::Error),

0 commit comments

Comments
 (0)