Skip to content

Commit

Permalink
Fixes blanket trait conflicts with impl trait for struct. (#5247)
Browse files Browse the repository at this point in the history
## Description

When the same trait was implemented for a blanket implementation and for
a struct a conflict occurred.

With this commit, we use `UnifyCheck::non_generic_constraint_subset`
instead of `UnifyCheck::constraint_subset` this no longer unifies the
generic type from the blanket impl with the struct impl and the conflict
no longer occurs.

Closes #5244

## 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).
- [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 <igi-111@protonmail.com>
  • Loading branch information
esdrubal and IGI-111 authored Nov 6, 2023
1 parent 3426530 commit fe5085a
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sway-core/src/semantic_analysis/namespace/trait_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl TraitMap {
},
);

let unify_checker = UnifyCheck::constraint_subset(engines);
let unify_checker = UnifyCheck::non_generic_constraint_subset(engines);
let types_are_subset = unify_checker.check(type_id, *map_type_id);
let traits_are_subset = unify_checker.check(trait_type_id, map_trait_type_id);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[package]]
name = "blanket_trait"
source = "member"
dependencies = ["core"]

[[package]]
name = "core"
source = "path+from-root-764E7430BADFBDF1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
license = "Apache-2.0"
name = "blanket_trait"
entry = "main.sw"

[dependencies]
core = { path = "../../../../../../../sway-lib-core" }
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": "bool",
"typeId": 0,
"typeParameters": null
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
script;

trait MyFrom<T> {
fn from(t: T) -> Self;
}

impl<T> MyFrom<T> for T {
fn from(t: T) -> T {
t
}
}


struct A {}

struct B {}

impl MyFrom<A> for B {
fn from(_t: A) -> B {
B {}
}
}

pub fn main() -> bool {
let a = A {};
let _b: B = B::from(a);

true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
category = "run"
expected_result = { action = "return", value = 1 }
validate_abi = true
expected_warnings = 1

0 comments on commit fe5085a

Please sign in to comment.