-
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.
Adds type inference from trait implementations. (#6590)
## Description When we have a type annotation with placeholders and we assign to it the result of a trait implementation call, if there is a single possible trait implementation we can use its type in the type annotation type. Fixes #5299 ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: IGI-111 <[email protected]> Co-authored-by: Joshua Batty <[email protected]>
- Loading branch information
1 parent
6b05844
commit 34b70f2
Showing
14 changed files
with
238 additions
and
54 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
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
13 changes: 0 additions & 13 deletions
13
..._vm_tests/test_programs/should_fail/generic_trait_tuple_without_type_ascription/Forc.lock
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
...m_tests/test_programs/should_fail/generic_trait_tuple_without_type_ascription/src/main.sw
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
..._vm_tests/test_programs/should_fail/generic_trait_tuple_without_type_ascription/test.toml
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
test/src/e2e_vm_tests/test_programs/should_fail/trait_inference_multiple_options/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-599A6910FBBA5980" | ||
|
||
[[package]] | ||
name = "std" | ||
source = "path+from-root-599A6910FBBA5980" | ||
dependencies = ["core"] | ||
|
||
[[package]] | ||
name = "trait_inference_multiple_options" | ||
source = "member" | ||
dependencies = ["std"] |
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 |
---|---|---|
|
@@ -2,7 +2,8 @@ | |
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "generic_trait_tuple_without_type_ascription" | ||
name = "trait_inference_multiple_options" | ||
implicit-std = false | ||
|
||
[dependencies] | ||
std = { path = "../../../reduced_std_libs/sway-lib-std-assert" } | ||
std = { path = "../../../reduced_std_libs/sway-lib-std-assert" } |
40 changes: 40 additions & 0 deletions
40
test/src/e2e_vm_tests/test_programs/should_fail/trait_inference_multiple_options/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,40 @@ | ||
script; | ||
|
||
pub trait MyFrom<T> { | ||
fn from(b: T) -> Self; | ||
} | ||
|
||
|
||
pub trait MyInto<T> { | ||
fn into(self) -> T; | ||
} | ||
|
||
|
||
impl<T, U> MyInto<U> for T | ||
where | ||
U: MyFrom<T>, | ||
{ | ||
fn into(self) -> U { | ||
U::from(self) | ||
} | ||
} | ||
|
||
impl MyFrom<u256> for (u64, u64, u64, u64) { | ||
fn from(val: u256) -> (u64, u64, u64, u64) { | ||
(42, 0, 0, 0) | ||
} | ||
} | ||
|
||
impl MyFrom<u256> for (u64, u64, u64, u32) { | ||
fn from(val: u256) -> (u64, u64, u64, u32) { | ||
(42, 0, 0, 0) | ||
} | ||
} | ||
|
||
fn main() -> bool { | ||
let (a, _b, _c, _d) = u256::min().into(); | ||
|
||
assert_eq(a, 42); | ||
|
||
true | ||
} |
6 changes: 6 additions & 0 deletions
6
test/src/e2e_vm_tests/test_programs/should_fail/trait_inference_multiple_options/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,6 @@ | ||
category = "fail" | ||
|
||
# check: $()Multiple impls satisfying MyFrom<u256> for (numeric, _, _, _) | ||
# check: $()Trait implemented for types: | ||
# nextln: $()#0 MyFrom<u256> for (u64, u64, u64, u64) | ||
# nextln: $()#1 MyFrom<u256> for (u64, u64, u64, u32) |
13 changes: 13 additions & 0 deletions
13
test/src/e2e_vm_tests/test_programs/should_pass/language/trait_inference/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-3903213068BE1E4C" | ||
|
||
[[package]] | ||
name = "std" | ||
source = "path+from-root-3903213068BE1E4C" | ||
dependencies = ["core"] | ||
|
||
[[package]] | ||
name = "trait_inference" | ||
source = "member" | ||
dependencies = ["std"] |
8 changes: 8 additions & 0 deletions
8
test/src/e2e_vm_tests/test_programs/should_pass/language/trait_inference/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] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "trait_inference" | ||
|
||
[dependencies] | ||
std = { path = "../../../../reduced_std_libs/sway-lib-std-assert" } |
41 changes: 41 additions & 0 deletions
41
test/src/e2e_vm_tests/test_programs/should_pass/language/trait_inference/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,41 @@ | ||
script; | ||
|
||
pub trait MyFrom<T> { | ||
fn from(b: T) -> Self; | ||
} | ||
|
||
|
||
pub trait MyInto<T> { | ||
fn into(self) -> T; | ||
} | ||
|
||
|
||
impl<T, U> MyInto<U> for T | ||
where | ||
U: MyFrom<T>, | ||
{ | ||
fn into(self) -> U { | ||
U::from(self) | ||
} | ||
} | ||
|
||
impl MyFrom<u256> for (u64, u64, u64, u64) { | ||
fn from(val: u256) -> (u64, u64, u64, u64) { | ||
(42, 0, 0, 0) | ||
} | ||
} | ||
|
||
// Should not interfere with trait inference | ||
impl MyFrom<u64> for (u64, u64, u64, u32) { | ||
fn from(val: u64) -> (u64, u64, u64, u32) { | ||
(42, 0, 0, 0) | ||
} | ||
} | ||
|
||
fn main() -> bool { | ||
let (a, _b, _c, _d) = u256::min().into(); | ||
|
||
assert_eq(a, 42); | ||
|
||
true | ||
} |
5 changes: 5 additions & 0 deletions
5
test/src/e2e_vm_tests/test_programs/should_pass/language/trait_inference/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 = 1 } | ||
expected_result_new_encoding = { action = "return_data", value = "01" } | ||
validate_abi = false | ||
expected_warnings = 3 |