-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Basic implementation of traits (#2368)
Co-authored-by: Yordan Madzhunkov <ymadzhunkov@gmail.com> Co-authored-by: Nikolay Nikolov <nickysn@gmail.com> Co-authored-by: jfecher <jfecher11@gmail.com>
- Loading branch information
1 parent
5361ebd
commit df9f09e
Showing
45 changed files
with
984 additions
and
15 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
crates/nargo_cli/tests/compile_failure/dup_trait_declaration/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/compile_failure/dup_trait_declaration/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = 1 | ||
y = 2 |
26 changes: 26 additions & 0 deletions
26
crates/nargo_cli/tests/compile_failure/dup_trait_declaration/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 7 additions & 0 deletions
7
crates/nargo_cli/tests/compile_failure/dup_trait_implementation/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/compile_failure/dup_trait_implementation/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = 1 | ||
y = 2 |
30 changes: 30 additions & 0 deletions
30
crates/nargo_cli/tests/compile_failure/dup_trait_implementation/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 7 additions & 0 deletions
7
crates/nargo_cli/tests/compile_failure/impl_struct_not_trait/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/compile_failure/impl_struct_not_trait/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = 1 | ||
y = 2 |
23 changes: 23 additions & 0 deletions
23
crates/nargo_cli/tests/compile_failure/impl_struct_not_trait/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 7 additions & 0 deletions
7
crates/nargo_cli/tests/compile_failure/trait_missing_implementation/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
24 changes: 24 additions & 0 deletions
24
crates/nargo_cli/tests/compile_failure/trait_missing_implementation/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 7 additions & 0 deletions
7
crates/nargo_cli/tests/compile_failure/trait_not_in_scope/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/compile_failure/trait_not_in_scope/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = 1 | ||
y = 2 |
18 changes: 18 additions & 0 deletions
18
crates/nargo_cli/tests/compile_failure/trait_not_in_scope/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 7 additions & 0 deletions
7
crates/nargo_cli/tests/compile_failure/trait_wrong_method_name/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/compile_failure/trait_wrong_method_name/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = 1 | ||
y = 2 |
22 changes: 22 additions & 0 deletions
22
crates/nargo_cli/tests/compile_failure/trait_wrong_method_name/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 7 additions & 0 deletions
7
crates/nargo_cli/tests/compile_failure/trait_wrong_method_return_type/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/compile_failure/trait_wrong_method_return_type/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = 1 | ||
y = 2 |
21 changes: 21 additions & 0 deletions
21
crates/nargo_cli/tests/compile_failure/trait_wrong_method_return_type/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 7 additions & 0 deletions
7
crates/nargo_cli/tests/compile_failure/trait_wrong_parameter/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/compile_failure/trait_wrong_parameter/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = 1 | ||
y = 2 |
21 changes: 21 additions & 0 deletions
21
crates/nargo_cli/tests/compile_failure/trait_wrong_parameter/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 7 additions & 0 deletions
7
crates/nargo_cli/tests/compile_failure/trait_wrong_parameter_type/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/compile_failure/trait_wrong_parameter_type/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = 1 | ||
y = 2 |
7 changes: 7 additions & 0 deletions
7
crates/nargo_cli/tests/compile_failure/trait_wrong_parameter_type/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 7 additions & 0 deletions
7
crates/nargo_cli/tests/compile_failure/trait_wrong_parameters_count/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/compile_failure/trait_wrong_parameters_count/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = 1 | ||
y = 2 |
21 changes: 21 additions & 0 deletions
21
crates/nargo_cli/tests/compile_failure/trait_wrong_parameters_count/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 7 additions & 0 deletions
7
crates/nargo_cli/tests/execution_success/trait_default_implementation/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/execution_success/trait_default_implementation/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = "5" | ||
y = "1" |
26 changes: 26 additions & 0 deletions
26
crates/nargo_cli/tests/execution_success/trait_default_implementation/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 7 additions & 0 deletions
7
crates/nargo_cli/tests/execution_success/trait_override_implementation/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/execution_success/trait_override_implementation/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = "5" | ||
y = "1" |
Oops, something went wrong.