Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Basic implementation of traits #2368

Merged
merged 31 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
49984f7
Maybe it's better this way
ymadzhunkov Aug 17, 2023
4a007aa
Collect traits from AST to def_map
ymadzhunkov Aug 9, 2023
29094f0
Collect trait implementations in def collector
ymadzhunkov Aug 9, 2023
5d00169
Resolve traits
ymadzhunkov Aug 9, 2023
a6615f2
Separate collected trait implementations
ymadzhunkov Aug 17, 2023
568119f
fix(testsuite): converted traits tests to proper nargo packages
nickysn Aug 15, 2023
788ef07
fix(testsuite): added Prover.toml files to the traits compile_failure…
nickysn Aug 15, 2023
5032e7d
fix(testsuite): Move trait test into correct place so it can run auto…
ymadzhunkov Aug 17, 2023
ed1790a
Rename trait_not_exists -> trait_not_in_scope
ymadzhunkov Aug 21, 2023
1c63592
Resolve some of PR comments
ymadzhunkov Aug 21, 2023
4f5d1df
better naming as requsted in PR
ymadzhunkov Aug 21, 2023
a7f7188
Remove empty else and rename noir_function -> impl_method
ymadzhunkov Aug 21, 2023
e46c437
Check for matching of trait implementation method, when unspecified r…
ymadzhunkov Aug 21, 2023
44d9ee4
Apply cargo clippy suggestions
ymadzhunkov Aug 21, 2023
b41bab5
Delete duplicated test
ymadzhunkov Aug 22, 2023
7046cf1
Partial trait resolution
ymadzhunkov Aug 23, 2023
3f98928
Extract comparison of return type into stuct assosiated method
ymadzhunkov Aug 24, 2023
68f0a15
Add test of a trait with default implemetation that is used
ymadzhunkov Aug 25, 2023
d5caec4
Allow usage of default trait implementation
ymadzhunkov Aug 25, 2023
295f057
RP remark: fix typo
ymadzhunkov Aug 25, 2023
36096d4
Maybem it's better this way
ymadzhunkov Aug 25, 2023
87ed6e2
Fix clippy warnings
ymadzhunkov Aug 25, 2023
28edd77
Add test of override ot Trait default method
ymadzhunkov Aug 25, 2023
d2873ef
Try to fix 'nix flake check -L'
ymadzhunkov Aug 25, 2023
dd88b96
Try to fix CI dependency failed build
ymadzhunkov Aug 26, 2023
961550b
An other try
ymadzhunkov Aug 26, 2023
bbe62b6
Update crates/noirc_frontend/src/ast/mod.rs
yordanmadzhunkov Aug 29, 2023
28a3aaa
Update crates/noirc_frontend/src/hir/def_collector/dc_crate.rs
yordanmadzhunkov Aug 29, 2023
2ef48e2
Build fix and cargo fmt
ymadzhunkov Aug 29, 2023
05b5208
Fix build after rebase of master
ymadzhunkov Aug 29, 2023
43ec02d
Fix trait tests
ymadzhunkov Aug 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "dup_trait_declaration"
type = "bin"
authors = [""]
compiler_version = "0.9.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = 1
y = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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) -> Self {
Self { bar: x, array: [x,y] }
}
}

// Duplicate trait declarations should not compile
trait Default {
fn default(x: Field) -> Self;
}

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,7 @@
[package]
name = "dup_trait_implementation"
type = "bin"
authors = [""]
compiler_version = "0.9.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = 1
y = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use dep::std;

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

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

// Duplicate trait implementations should not compile
impl Default for Foo {
fn default(x: Field,y: Field) -> Self {
Self { bar: x, array: [x,y] }
}
}

// Duplicate trait implementations should not compile
impl Default for Foo {
fn default(x: Field, y: Field) -> Self {
Self { bar: y, array: [y,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,7 @@
[package]
name = "impl_struct_not_trait"
type = "bin"
authors = [""]
compiler_version = "0.9.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = 1
y = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use dep::std;

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

struct Default {
x: Field,
z: Field,
}

// Default is struct not a trait
impl Default for Foo {
fn default(x: Field, y: Field) -> Self {
Self { bar: x, array: [x,y] }
}
}

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,7 @@
[package]
name = "traits"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use dep::std;

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

fn method2(x: Field) -> Field;

}

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

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

fn main(x: Field) {
let first = Foo::method2(x);
assert(first == x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "trait_not_in_scope"
type = "bin"
authors = [""]
compiler_version = "0.9.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = 1
y = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use dep::std;

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

// Default trait does not exist
impl Default for Foo {
fn default(x: Field, y: Field) -> Self {
Self { bar: x, array: [x,y] }
}
}

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,7 @@
[package]
name = "trait_wrong_method_name"
type = "bin"
authors = [""]
compiler_version = "0.9.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = 1
y = 2
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,7 @@
[package]
name = "trait_wrong_method_return_type"
type = "bin"
authors = [""]
compiler_version = "0.9.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = 1
y = 2
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,7 @@
[package]
name = "trait_wrong_parameter"
type = "bin"
authors = [""]
compiler_version = "0.9.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = 1
y = 2
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,7 @@
[package]
name = "trait_wrong_method_return_type"
type = "bin"
authors = [""]
compiler_version = "0.9.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = 1
y = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
trait Default {
fn default(x: Field, y: NotAType) -> Field;
}

fn main(x: Field, y: Field) {
assert(y == x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "trait_wrong_parameters_count"
type = "bin"
authors = [""]
compiler_version = "0.9.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = 1
y = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use dep::std;
jfecher marked this conversation as resolved.
Show resolved Hide resolved

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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "trait_default_implementation"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = "5"
y = "1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use dep::std;

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

fn method2(x: Field) -> Field {
x
}

}

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

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

fn main(x: Field) {
let first = Foo::method2(x);
assert(first == x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "trait_override_implementation"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = "5"
y = "1"
Loading