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: Traits methods - default and override #2574

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
9b818ca
Fix tests by removing trait implemetation checks from def_collector
ymadzhunkov Aug 29, 2023
1520160
fix(tests): simplify the trait-related compile_failure tests
nickysn Aug 30, 2023
d8d06cd
chore(tests): move tests that fail for a known reason to a subdirecto…
nickysn Aug 30, 2023
0190d68
test(traits): added more tests in KNOWN_FAILURES for the detection of…
nickysn Aug 30, 2023
7ec5c0d
These 2 test should not compile!
ymadzhunkov Aug 30, 2023
9d4cbe3
Move tests to KNOWN FAILURES
ymadzhunkov Aug 30, 2023
9be47e8
Setup work for checks on Types
alexvitkov Aug 31, 2023
7276c42
remove debug printf
alexvitkov Aug 31, 2023
a975c9d
Enable tests that implemetns trait twice
ymadzhunkov Aug 31, 2023
64811c8
Try to build it
ymadzhunkov Aug 31, 2023
379ab66
Fix error reporting when in case of duplicate trait implementation
ymadzhunkov Aug 31, 2023
5c3c689
Enable dup_trait_implementation_3
ymadzhunkov Sep 1, 2023
c6b7582
Code review comments
ymadzhunkov Sep 1, 2023
562cf9c
Fix build and tests
ymadzhunkov Sep 1, 2023
6358e35
fix: public
alexvitkov Sep 5, 2023
41b8e48
Enabling back tests that failed
ymadzhunkov Sep 6, 2023
c65884e
Obtain function name from resolver
ymadzhunkov Sep 5, 2023
ef46c6d
typevar for Trait::Self
alexvitkov Sep 5, 2023
e4f3f27
break up Trait::items into 3 arrays
alexvitkov Sep 5, 2023
ba7747d
use unify instead of == to compare types
alexvitkov Sep 5, 2023
fa61f44
remove clone
alexvitkov Sep 5, 2023
cbf5629
test: add test
alexvitkov Sep 5, 2023
3191234
New error when trait is implemented for non-struct
yordanmadzhunkov Sep 5, 2023
5b15d56
add check for trait method argc
alexvitkov Sep 5, 2023
580c1d7
consistent capitalization
alexvitkov Sep 5, 2023
ea7bbc9
better error struct field names
alexvitkov Sep 5, 2023
dac1fe2
emit MethodNotInTrait when needed
alexvitkov Sep 5, 2023
e33b8b4
remove outdated todos
alexvitkov Sep 5, 2023
77180f4
Fix clippy
ymadzhunkov Sep 5, 2023
000a8db
Rename test impl_trait_for_non_struct -> impl_trait_for_non_type
ymadzhunkov Sep 5, 2023
30a19ff
Fix cargo fmt
ymadzhunkov Sep 6, 2023
7e30706
These tests are expected to execute successfully
ymadzhunkov Sep 6, 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
Expand Up @@ -21,6 +21,4 @@ trait Default {
}

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
Expand Up @@ -25,6 +25,4 @@ impl Default for Foo {


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_2"
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 @@
trait Default {
}

struct Foo {
bar: Field,
}

// Duplicate trait implementations should not compile
impl Default for Foo {
}

// Duplicate trait implementations should not compile
impl Default for Foo {
}


fn main(x: Field, y: Field) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "dup_trait_implementation_3"
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,19 @@
trait Default {
}

struct MyStruct {
}

type MyType = MyStruct;

// Duplicate trait implementations should not compile
impl Default for MyStruct {
}

// Duplicate trait implementations should not compile
impl Default for MyType {
}


fn main(x: Field, y: Field) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ impl Default for Foo {
}

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_trait_for_non_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,15 @@
use dep::std;

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


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

fn main(x: Field, y: Field) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ impl Default for Foo {
}

fn main(x: Field) {
let first = Foo::method2(x);
assert(first == x);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ impl Default for Foo {
}

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
@@ -1,7 +1,6 @@
use dep::std;

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

struct Foo {
Expand All @@ -11,12 +10,10 @@ struct Foo {

// wrong trait name method should not compile
impl Default for Foo {
fn default_wrong_name(x: Field, y: Field) -> Self {
fn doesnt_exist(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
Expand Up @@ -16,6 +16,4 @@ impl Default for Foo {
}

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,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 == x);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ impl Default for Foo {
}

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,22 @@
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 foo = Foo { bar: x, array: [x, y] };
let first = Foo::default(x,foo);
assert(first.bar == x);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ impl Default for Foo {
}

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_self"
type = "bin"
authors = [""]
compiler_version = "0.10.5"

[dependencies]
18 changes: 18 additions & 0 deletions crates/nargo_cli/tests/execution_success/trait_self/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
struct Foo {
x: Field
}

trait Asd {
fn asd() -> Self;
}

impl Asd for Foo {
// the Self should typecheck properly
fn asd() -> Self {
Foo{x: 100}
}
}

fn main() {
assert(Foo::asd().x == 100);
}
8 changes: 5 additions & 3 deletions crates/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,10 +689,12 @@ impl Display for FunctionDefinition {
}

impl FunctionReturnType {
pub fn get_type(&self) -> &UnresolvedTypeData {
pub fn get_type(&self) -> UnresolvedType {
match self {
FunctionReturnType::Default(_span) => &UnresolvedTypeData::Unit,
FunctionReturnType::Ty(typ) => &typ.typ,
FunctionReturnType::Default(span) => {
UnresolvedType { typ: UnresolvedTypeData::Unit, span: Some(*span) }
}
FunctionReturnType::Ty(typ) => typ.clone(),
}
}
}
Expand Down
Loading
Loading