Skip to content

Commit 10a78a7

Browse files
esdrubalJoshuaBattysdankel
authored
Fixed required trait constraint not being checked (#6525)
## Description An optimization on `check_if_trait_constraints_are_satisfied_for_type_inner` was making missing trait constraints not being detected. Fixes #6374. ## 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: Joshua Batty <joshpbatty@gmail.com> Co-authored-by: Sophie Dankel <47993817+sdankel@users.noreply.github.com>
1 parent 15c8298 commit 10a78a7

File tree

6 files changed

+35
-28
lines changed

6 files changed

+35
-28
lines changed

sway-core/src/semantic_analysis/namespace/trait_map.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,34 +1390,6 @@ impl TraitMap {
13901390
) -> Result<(), ErrorEmitted> {
13911391
let type_engine = engines.te();
13921392

1393-
// If the type is generic/placeholder, its definition needs to contains all
1394-
// constraints
1395-
match &*type_engine.get(type_id) {
1396-
TypeInfo::UnknownGeneric {
1397-
trait_constraints, ..
1398-
} => {
1399-
let all = constraints.iter().all(|required| {
1400-
trait_constraints.iter().any(|constraint| {
1401-
constraint.eq(required, &PartialEqWithEnginesContext::new(engines))
1402-
})
1403-
});
1404-
if all {
1405-
return Ok(());
1406-
}
1407-
}
1408-
TypeInfo::Placeholder(p) => {
1409-
let all = constraints.iter().all(|required| {
1410-
p.trait_constraints.iter().any(|constraint| {
1411-
constraint.eq(required, &PartialEqWithEnginesContext::new(engines))
1412-
})
1413-
});
1414-
if all {
1415-
return Ok(());
1416-
}
1417-
}
1418-
_ => {}
1419-
}
1420-
14211393
let _decl_engine = engines.de();
14221394
let unify_check = UnifyCheck::non_dynamic_equality(engines);
14231395

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[[package]]
2+
name = "method_missing_constraint"
3+
source = "member"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[project]
2+
authors = ["Fuel Labs <contact@fuel.sh>"]
3+
license = "Apache-2.0"
4+
name = "method_missing_constraint"
5+
entry = "main.sw"
6+
implicit-std = false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
script;
2+
3+
trait T1 {}
4+
trait T2 {}
5+
struct S<T> where T: T1 {
6+
x: T,
7+
}
8+
impl<T> S<T> where T: T1{
9+
fn only_t1(self) {
10+
Self::also_t2(self);
11+
}
12+
fn also_t2(self) where T: T2{
13+
}
14+
}
15+
impl T1 for u8 {}
16+
impl T1 for u64 {}
17+
impl T2 for u64 {}
18+
fn main() {
19+
let a = S::<u8>{x: 42};
20+
a.only_t1(); //prints Hello but u8 doesn't implement T2
21+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
category = "fail"
2+
3+
# check: $()Self::also_t2(self);
4+
# nextln: $()Trait "T2" is not implemented for type "T".

0 commit comments

Comments
 (0)