Skip to content

Commit

Permalink
Add parameter checks and reporting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ymadzhunkov committed Jul 31, 2023
1 parent d3c4f54 commit 3cf1109
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use dep::std;

trait Default {
fn default(x: Field, y: Field) -> Self;
}

struct Foo {
bar: Field,
array: [Field; 2],
}

// wrong trait name method should not compile
impl Default for Foo {
fn default_wrong_name(x: Field, y: Field) -> Self {
Self { bar: x, array: [x,y] }
}
}

fn main(x: Field, y: Field) {
let first = Foo::default_wrong_name(x,y);
assert(first.bar == x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use dep::std;

trait Default {
fn default(x: Field, y: Field) -> Self;
}

struct Foo {
bar: Field,
array: [Field; 2],
}

impl Default for Foo {
fn default(x: Field, y: Field) -> Field {
x
}
}

fn main(x: Field, y: Field) {
let first = Foo::default(x,y);
assert(first.bar == x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use dep::std;

trait Default {
fn default(x: Field, y: Field) -> Self;
}

struct Foo {
bar: Field,
array: [Field; 2],
}

impl Default for Foo {
fn default(x: Field, y: Foo) -> Self {
Self { bar: x, array: [x, y.bar] }
}
}

fn main(x: Field, y: Field) {
let first = Foo::default(x,y);
assert(first.bar == x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use dep::std;

trait Default {
fn default(x: Field, y: Field) -> Self;
}

struct Foo {
bar: Field,
array: [Field; 2],
}

impl Default for Foo {
fn default(x: Field) -> Self {
Self { bar: x, array: [x, x] }
}
}

fn main(x: Field, y: Field) {
let first = Foo::default(x,y);
assert(first.bar == x);
}
52 changes: 45 additions & 7 deletions crates/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::format;

use fm::FileId;
use noirc_errors::FileDiagnostic;

Expand Down Expand Up @@ -75,16 +77,45 @@ fn check_trait_method_implementation_generics(
_noir_function: &NoirFunction,
_trait_name: &String,
) -> Result<(), DefCollectorErrorKind> {
// TODO
// TODO
Ok(())
}

fn check_trait_method_implementation_parameters(
_parameters: &Vec<(Ident, UnresolvedType)>,
_noir_function: &NoirFunction,
_trait_name: &String,
parameters: &Vec<(Ident, UnresolvedType)>,
noir_function: &NoirFunction,
trait_name: &String,
) -> Result<(), DefCollectorErrorKind> {
// TODO
if noir_function.def.parameters.len() != parameters.len() {
return Err(DefCollectorErrorKind::SimpleError {
primary_message: format!("Mismatch signature [Number of parameters] of method with name `{}` that implemetns trait `{}`", noir_function.name(), trait_name),
secondary_message: "".to_string(),
span: noir_function.name_ident().span(),
});
}
let mut count = 0;
for (pattern, typ, _abi_vis) in &noir_function.def.parameters {
let (expected_name, expected_type) = &parameters[count];
if pattern.name_ident().0.contents != expected_name.0.contents {
// we allow different namings of parameters
}
if typ != expected_type {
return Err(DefCollectorErrorKind::SimpleError {
primary_message: format!(
"Mismatch signature of method {} that implemtns trait {}",
noir_function.name(),
trait_name,
),
secondary_message: format!(
"`{}: {}` expected",
pattern.name_ident().0.contents,
expected_type.to_string(),
),
span: pattern.name_ident().span(),
});
}
count = count + 1;
}
Ok(())
}

Expand All @@ -93,7 +124,7 @@ fn check_trait_method_implementation_trait_constains(
_noir_function: &NoirFunction,
_trait_name: &String,
) -> Result<(), DefCollectorErrorKind> {
// TODO
// TODO
Ok(())
}

Expand Down Expand Up @@ -123,7 +154,14 @@ fn check_trait_method_implementation(
) -> Result<(), DefCollectorErrorKind> {
for item in &r#trait.items {
match item {
TraitItem::Function { name, generics, parameters, return_type, where_clause, body:_} => {
TraitItem::Function {
name,
generics,
parameters,
return_type,
where_clause,
body: _,
} => {
if name.0.contents == noir_function.def.name.0.contents {
// name matches, check for parameters, return type and where clause
check_trait_method_implementation_generics(
Expand Down

0 comments on commit 3cf1109

Please sign in to comment.