Skip to content

Commit

Permalink
feat: Basic implementation of traits (#2368)
Browse files Browse the repository at this point in the history
Co-authored-by: Yordan Madzhunkov <[email protected]>
Co-authored-by: Nikolay Nikolov <[email protected]>
Co-authored-by: jfecher <[email protected]>
  • Loading branch information
4 people authored Aug 30, 2023
1 parent 5361ebd commit df9f09e
Show file tree
Hide file tree
Showing 45 changed files with 984 additions and 15 deletions.
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;

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

0 comments on commit df9f09e

Please sign in to comment.