Skip to content

Commit

Permalink
fix: Fix panic in comptime code (noir-lang/noir#6361)
Browse files Browse the repository at this point in the history
feat: let the LSP import code action insert into existing use statements (noir-lang/noir#6358)
chore: minor tweaks to comptime doc (noir-lang/noir#6357)
feat: LSP auto-import will try to add to existing use statements (noir-lang/noir#6354)
fix: allow type aliases in let patterns (noir-lang/noir#6356)
feat(test): Run test matrix on stdlib tests (noir-lang/noir#6352)
fix(ssa): Do not mark an array from a parameter mutable (noir-lang/noir#6355)
fix: always inline `derive_generators` (noir-lang/noir#6350)
chore: add test to check that duplicate definitions generated from macros throws error (noir-lang/noir#6351)
chore: switch to btreeset for deterministic ordering (noir-lang/noir#6348)
feat(perf): Use [u32;16] for message block in sha256 (noir-lang/noir#6324)
chore: add some tests for type aliases
feat: remove 'single use' intermediate variables (noir-lang/noir#6268)
feat: Sync from aztec-packages (noir-lang/noir#6345)
feat(profiler)!: New flamegraph command that profiles the opcodes executed (noir-lang/noir#6327)
feat: check trait where clause (noir-lang/noir#6325)
fix: better formatting of leading/trailing line/block comments in expression lists (noir-lang/noir#6338)
feat: let the formatter remove lambda block braces for single-statement blocks (noir-lang/noir#6335)
chore: run tests in metaprogramming.rs (noir-lang/noir#6339)
  • Loading branch information
AztecBot committed Oct 26, 2024
2 parents b97a526 + 94ebdeb commit 18d0694
Show file tree
Hide file tree
Showing 36 changed files with 1,096 additions and 344 deletions.
2 changes: 1 addition & 1 deletion .noir-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d8e549aad6bae0f96621c57b58a301693463f732
2f376100d3ee7ab519d6ea30153395bb3e7af7b1
34 changes: 34 additions & 0 deletions noir/noir-repo/Cargo.lock

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

1 change: 1 addition & 0 deletions noir/noir-repo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ num-bigint = "0.4"
num-traits = "0.2"
similar-asserts = "1.5.0"
tempfile = "3.6.0"
test-case = "3.3.1"
jsonrpc = { version = "0.16.0", features = ["minreq_http"] }
flate2 = "1.0.24"
color-eyre = "0.6.2"
Expand Down
22 changes: 15 additions & 7 deletions noir/noir-repo/compiler/noirc_evaluator/src/ssa/opt/array_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ struct Context<'f> {
is_brillig_runtime: bool,
array_to_last_use: HashMap<ValueId, InstructionId>,
instructions_that_can_be_made_mutable: HashSet<InstructionId>,
arrays_from_load: HashSet<ValueId>,
// Mapping of an array that comes from a load and whether the address
// it was loaded from is a reference parameter.
arrays_from_load: HashMap<ValueId, bool>,
inner_nested_arrays: HashMap<ValueId, InstructionId>,
}

Expand All @@ -64,7 +66,7 @@ impl<'f> Context<'f> {
is_brillig_runtime,
array_to_last_use: HashMap::default(),
instructions_that_can_be_made_mutable: HashSet::default(),
arrays_from_load: HashSet::default(),
arrays_from_load: HashMap::default(),
inner_nested_arrays: HashMap::default(),
}
}
Expand Down Expand Up @@ -113,9 +115,13 @@ impl<'f> Context<'f> {
array_in_terminator = true;
}
});
if (!self.arrays_from_load.contains(&array) || is_return_block)
&& !array_in_terminator
{
if let Some(is_from_param) = self.arrays_from_load.get(&array) {
// If the array was loaded from a reference parameter, we cannot
// safely mark that array mutable as it may be shared by another value.
if !is_from_param && is_return_block {
self.instructions_that_can_be_made_mutable.insert(*instruction_id);
}
} else if !array_in_terminator {
self.instructions_that_can_be_made_mutable.insert(*instruction_id);
}
}
Expand All @@ -133,10 +139,12 @@ impl<'f> Context<'f> {
}
}
}
Instruction::Load { .. } => {
Instruction::Load { address } => {
let result = self.dfg.instruction_results(*instruction_id)[0];
if matches!(self.dfg.type_of_value(result), Array { .. } | Slice { .. }) {
self.arrays_from_load.insert(result);
let is_reference_param =
self.dfg.block_parameters(block_id).contains(address);
self.arrays_from_load.insert(result, is_reference_param);
}
}
_ => (),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ pub enum PathKind {
pub struct UseTree {
pub prefix: Path,
pub kind: UseTreeKind,
pub span: Span,
}

impl Display for UseTree {
Expand Down
18 changes: 16 additions & 2 deletions noir/noir-repo/compiler/noirc_frontend/src/elaborator/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,26 @@ impl<'context> Elaborator<'context> {
}
}

match self.lookup(path) {
Ok(struct_id) => {
let span = path.span;
match self.resolve_path_or_error(path) {
Ok(ModuleDefId::TypeId(struct_id)) => {
let struct_type = self.get_struct(struct_id);
let generics = struct_type.borrow().instantiate(self.interner);
Some(Type::Struct(struct_type, generics))
}
Ok(ModuleDefId::TypeAliasId(alias_id)) => {
let alias = self.interner.get_type_alias(alias_id);
let alias = alias.borrow();
Some(alias.instantiate(self.interner))
}
Ok(other) => {
self.push_err(ResolverError::Expected {
expected: StructId::description(),
got: other.as_str().to_owned(),
span,
});
None
}
Err(error) => {
self.push_err(error);
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,8 @@ impl<'local, 'interner> Interpreter<'local, 'interner> {
match &definition.kind {
DefinitionKind::Function(function_id) => {
let typ = self.elaborator.interner.id_type(id).follow_bindings();
let bindings =
Rc::new(self.elaborator.interner.get_instantiation_bindings(id).clone());
let bindings = self.elaborator.interner.try_get_instantiation_bindings(id);
let bindings = Rc::new(bindings.map_or(TypeBindings::default(), Clone::clone));
Ok(Value::Function(*function_id, typ, bindings))
}
DefinitionKind::Local(_) => self.lookup(&ident),
Expand Down
5 changes: 5 additions & 0 deletions noir/noir-repo/compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,11 @@ impl TypeAlias {

self.typ.substitute(&substitutions)
}

pub fn instantiate(&self, interner: &NodeInterner) -> Type {
let args = vecmap(&self.generics, |_| interner.next_type_variable());
self.get_type(&args)
}
}

/// A shared, mutable reference to some T.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ impl<'a> Parser<'a> {
Self::parse_use_tree_in_list,
);

UseTree { prefix, kind: UseTreeKind::List(use_trees) }
UseTree {
prefix,
kind: UseTreeKind::List(use_trees),
span: self.span_since(start_span),
}
} else {
self.expected_token(Token::LeftBrace);
self.parse_path_use_tree_end(prefix, nested)
self.parse_path_use_tree_end(prefix, nested, start_span)
}
} else {
self.parse_path_use_tree_end(prefix, nested)
self.parse_path_use_tree_end(prefix, nested, start_span)
}
}

Expand All @@ -71,6 +75,7 @@ impl<'a> Parser<'a> {
return Some(UseTree {
prefix: Path { segments: Vec::new(), kind: PathKind::Plain, span: start_span },
kind: UseTreeKind::Path(Ident::new("self".to_string(), start_span), None),
span: start_span,
});
}

Expand All @@ -89,25 +94,46 @@ impl<'a> Parser<'a> {
}
}

pub(super) fn parse_path_use_tree_end(&mut self, mut prefix: Path, nested: bool) -> UseTree {
pub(super) fn parse_path_use_tree_end(
&mut self,
mut prefix: Path,
nested: bool,
start_span: Span,
) -> UseTree {
if prefix.segments.is_empty() {
if nested {
self.expected_identifier();
} else {
self.expected_label(ParsingRuleLabel::UseSegment);
}
UseTree { prefix, kind: UseTreeKind::Path(Ident::default(), None) }
UseTree {
prefix,
kind: UseTreeKind::Path(Ident::default(), None),
span: self.span_since(start_span),
}
} else {
let ident = prefix.segments.pop().unwrap().ident;
if self.eat_keyword(Keyword::As) {
if let Some(alias) = self.eat_ident() {
UseTree { prefix, kind: UseTreeKind::Path(ident, Some(alias)) }
UseTree {
prefix,
kind: UseTreeKind::Path(ident, Some(alias)),
span: self.span_since(start_span),
}
} else {
self.expected_identifier();
UseTree { prefix, kind: UseTreeKind::Path(ident, None) }
UseTree {
prefix,
kind: UseTreeKind::Path(ident, None),
span: self.span_since(start_span),
}
}
} else {
UseTree { prefix, kind: UseTreeKind::Path(ident, None) }
UseTree {
prefix,
kind: UseTreeKind::Path(ident, None),
span: self.span_since(start_span),
}
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions noir/noir-repo/compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3546,3 +3546,19 @@ fn uses_self_in_import() {
"#;
assert_no_errors(src);
}

#[test]
fn alias_in_let_pattern() {
let src = r#"
struct Foo<T> { x: T }
type Bar<U> = Foo<U>;
fn main() {
let Bar { x } = Foo { x: [0] };
// This is just to show the compiler knows this is an array.
let _: [Field; 1] = x;
}
"#;
assert_no_errors(src);
}
19 changes: 0 additions & 19 deletions noir/noir-repo/compiler/noirc_frontend/src/tests/aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,3 @@ fn allows_usage_of_type_alias_as_return_type() {
"#;
assert_no_errors(src);
}

// This is a regression test for https://github.com/noir-lang/noir/issues/6347
#[test]
#[should_panic = r#"ResolverError(Expected { span: Span(Span { start: ByteIndex(95), end: ByteIndex(98) }), expected: "type", got: "type alias" }"#]
fn allows_destructuring_a_type_alias_of_a_struct() {
let src = r#"
struct Foo {
inner: Field
}
type Bar = Foo;
fn main() {
let Bar { inner } = Foo { inner: 42 };
assert_eq(inner, 42);
}
"#;
assert_no_errors(src);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
use crate::hir::{
def_collector::dc_crate::CompilationError, resolution::errors::ResolverError,
type_check::TypeCheckError,
use noirc_errors::Spanned;

use crate::{
ast::Ident,
hir::{
def_collector::{
dc_crate::CompilationError,
errors::{DefCollectorErrorKind, DuplicateType},
},
resolution::errors::ResolverError,
type_check::TypeCheckError,
},
};

use super::{assert_no_errors, get_program_errors};
Expand Down Expand Up @@ -96,3 +105,39 @@ fn allows_references_to_structs_generated_by_macros() {

assert_no_errors(src);
}

#[test]
fn errors_if_macros_inject_functions_with_name_collisions() {
let src = r#"
comptime fn make_colliding_functions(_s: StructDefinition) -> Quoted {
quote {
fn foo() {}
}
}
#[make_colliding_functions]
struct Foo {}
#[make_colliding_functions]
struct Bar {}
fn main() {
let _ = Foo {};
let _ = Bar {};
foo();
}
"#;

let errors = get_program_errors(src);
assert_eq!(errors.len(), 1);
assert!(matches!(
&errors[0].0,
CompilationError::DefinitionError(
DefCollectorErrorKind::Duplicate {
typ: DuplicateType::Function,
first_def: Ident(Spanned { contents, .. }),
..
},
) if contents == "foo"
));
}
Loading

0 comments on commit 18d0694

Please sign in to comment.