-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Where clauses on
impl
blocks (#3731)
Closes #1162 --------- Co-authored-by: emilyaherbert <[email protected]> Co-authored-by: Marcos Henrich <[email protected]> Co-authored-by: IGI-111 <[email protected]>
- Loading branch information
1 parent
6a3fba1
commit e95e052
Showing
8 changed files
with
215 additions
and
46 deletions.
There are no files selected for viewing
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
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
10 changes: 7 additions & 3 deletions
10
test/src/e2e_vm_tests/test_programs/should_fail/where_clause_impls/test.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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
category = "fail" | ||
|
||
# check: $()impl<T> MyAdd for MyPoint<T> where T: MyAdd { | ||
# check: $()"where" clauses are not yet supported | ||
# check: $()x: a.x.my_add(b.x), | ||
# nextln: $()No method named "my_add" found for type "T". | ||
# check: $()y: a.y.my_add(b.y), | ||
|
||
# check: $()y: a.y.my_add(b.y), | ||
# nextln: $()No method named "my_add" found for type "T". | ||
|
||
# check: $()let baz = foo.my_add(bar); | ||
# check: $()No method named "my_add" found for type "MyPoint<u32>". | ||
# nextln: $()No method named "my_add" found for type "MyPoint<u32>". |
13 changes: 13 additions & 0 deletions
13
test/src/e2e_vm_tests/test_programs/should_pass/language/where_clause_impls/Forc.lock
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,13 @@ | ||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-7FCA9B4231E4DA1F' | ||
|
||
[[package]] | ||
name = 'std' | ||
source = 'path+from-root-7FCA9B4231E4DA1F' | ||
dependencies = ['core'] | ||
|
||
[[package]] | ||
name = 'where_clause_impls' | ||
source = 'member' | ||
dependencies = ['std'] |
8 changes: 8 additions & 0 deletions
8
test/src/e2e_vm_tests/test_programs/should_pass/language/where_clause_impls/Forc.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,8 @@ | ||
[project] | ||
name = "where_clause_impls" | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
|
||
[dependencies] | ||
std = { path = "../../../../../../../sway-lib-std" } |
25 changes: 25 additions & 0 deletions
25
...c/e2e_vm_tests/test_programs/should_pass/language/where_clause_impls/json_abi_oracle.json
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,25 @@ | ||
{ | ||
"configurables": [], | ||
"functions": [ | ||
{ | ||
"attributes": null, | ||
"inputs": [], | ||
"name": "main", | ||
"output": { | ||
"name": "", | ||
"type": 0, | ||
"typeArguments": null | ||
} | ||
} | ||
], | ||
"loggedTypes": [], | ||
"messagesTypes": [], | ||
"types": [ | ||
{ | ||
"components": null, | ||
"type": "u64", | ||
"typeId": 0, | ||
"typeParameters": null | ||
} | ||
] | ||
} |
139 changes: 139 additions & 0 deletions
139
test/src/e2e_vm_tests/test_programs/should_pass/language/where_clause_impls/src/main.sw
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,139 @@ | ||
script; | ||
|
||
use std::{ | ||
assert::assert, | ||
logging::log, | ||
}; | ||
|
||
pub trait MyAdd { | ||
fn my_add(self, other: Self) -> Self; | ||
} | ||
|
||
impl MyAdd for u8 { | ||
fn my_add(self, other: Self) -> Self { | ||
self + other | ||
} | ||
} | ||
|
||
impl MyAdd for u16 { | ||
fn my_add(self, other: Self) -> Self { | ||
self + other | ||
} | ||
} | ||
|
||
impl MyAdd for u32 { | ||
fn my_add(self, other: Self) -> Self { | ||
self + other | ||
} | ||
} | ||
|
||
impl MyAdd for u64 { | ||
fn my_add(self, other: Self) -> Self { | ||
self + other | ||
} | ||
} | ||
|
||
pub trait MyMul { | ||
fn my_mul(self, other: Self) -> Self; | ||
} | ||
|
||
impl MyMul for u8 { | ||
fn my_mul(self, other: Self) -> Self { | ||
self * other | ||
} | ||
} | ||
|
||
impl MyMul for u16 { | ||
fn my_mul(self, other: Self) -> Self { | ||
self * other | ||
} | ||
} | ||
|
||
impl MyMul for u32 { | ||
fn my_mul(self, other: Self) -> Self { | ||
self * other | ||
} | ||
} | ||
|
||
impl MyMul for u64 { | ||
fn my_mul(self, other: Self) -> Self { | ||
self * other | ||
} | ||
} | ||
|
||
pub trait MyMath: MyAdd + MyMul { | ||
|
||
} { | ||
fn my_double(self) -> Self { | ||
self.my_add(self) | ||
} | ||
|
||
fn my_pow_2(self) -> Self { | ||
self.my_mul(self) | ||
} | ||
} | ||
|
||
impl MyMath for u8 {} | ||
|
||
impl MyMath for u16 {} | ||
|
||
impl MyMath for u32 {} | ||
|
||
impl MyMath for u64 {} | ||
|
||
pub struct MyPoint<T> { | ||
x: T, | ||
y: T, | ||
} | ||
|
||
impl<T> MyPoint<T> { | ||
pub fn new(x: T, y: T) -> MyPoint<T> { | ||
MyPoint { | ||
x, | ||
y, | ||
} | ||
} | ||
} | ||
|
||
impl<T> MyAdd for MyPoint<T> where T: MyAdd { | ||
fn my_add(self, other: Self) -> Self { | ||
MyPoint { | ||
x: self.x.my_add(other.x), | ||
y: self.y.my_add(other.y), | ||
} | ||
} | ||
} | ||
|
||
impl<T> MyMul for MyPoint<T> where T: MyMul { | ||
fn my_mul(self, other: Self) -> Self { | ||
MyPoint { | ||
x: self.x.my_mul(other.x), | ||
y: self.y.my_mul(other.y), | ||
} | ||
} | ||
} | ||
|
||
impl<T> MyMath for MyPoint<T> where T: MyMath { } | ||
|
||
pub fn basic_unit_tests() { | ||
assert(100.my_add(99) == 199); | ||
assert(3.my_mul(4) == 12); | ||
assert(5.my_double() == 10); | ||
assert(5.my_pow_2() == 25); | ||
} | ||
|
||
pub fn basic_point_tests() { | ||
let a = MyPoint::new(1, 2); | ||
let b = MyPoint::new(3, 4); | ||
|
||
let c = a.my_add(b); | ||
assert(c.x == 4); | ||
assert(c.y == 6); | ||
} | ||
|
||
fn main() -> u64 { | ||
basic_unit_tests(); | ||
basic_point_tests(); | ||
|
||
42 | ||
} |
5 changes: 5 additions & 0 deletions
5
test/src/e2e_vm_tests/test_programs/should_pass/language/where_clause_impls/test.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,5 @@ | ||
category = "run" | ||
expected_result = { action = "return", value = 42 } | ||
validate_abi = true | ||
|
||
expected_warnings = 8 |