Skip to content

Commit

Permalink
Fixed required trait constraint not being checked (#6525)
Browse files Browse the repository at this point in the history
## 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>
  • Loading branch information
3 people authored Sep 18, 2024
1 parent 15c8298 commit 10a78a7
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 28 deletions.
28 changes: 0 additions & 28 deletions sway-core/src/semantic_analysis/namespace/trait_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1390,34 +1390,6 @@ impl TraitMap {
) -> Result<(), ErrorEmitted> {
let type_engine = engines.te();

// If the type is generic/placeholder, its definition needs to contains all
// constraints
match &*type_engine.get(type_id) {
TypeInfo::UnknownGeneric {
trait_constraints, ..
} => {
let all = constraints.iter().all(|required| {
trait_constraints.iter().any(|constraint| {
constraint.eq(required, &PartialEqWithEnginesContext::new(engines))
})
});
if all {
return Ok(());
}
}
TypeInfo::Placeholder(p) => {
let all = constraints.iter().all(|required| {
p.trait_constraints.iter().any(|constraint| {
constraint.eq(required, &PartialEqWithEnginesContext::new(engines))
})
});
if all {
return Ok(());
}
}
_ => {}
}

let _decl_engine = engines.de();
let unify_check = UnifyCheck::non_dynamic_equality(engines);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = "method_missing_constraint"
source = "member"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
license = "Apache-2.0"
name = "method_missing_constraint"
entry = "main.sw"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
script;

trait T1 {}
trait T2 {}
struct S<T> where T: T1 {
x: T,
}
impl<T> S<T> where T: T1{
fn only_t1(self) {
Self::also_t2(self);
}
fn also_t2(self) where T: T2{
}
}
impl T1 for u8 {}
impl T1 for u64 {}
impl T2 for u64 {}
fn main() {
let a = S::<u8>{x: 42};
a.only_t1(); //prints Hello but u8 doesn't implement T2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
category = "fail"

# check: $()Self::also_t2(self);
# nextln: $()Trait "T2" is not implemented for type "T".

0 comments on commit 10a78a7

Please sign in to comment.