From 5ff7c39ec15b5ef7cdc9e663d7d6e858dd73567b Mon Sep 17 00:00:00 2001 From: Daniel Frederico Lins Leite Date: Tue, 29 Oct 2024 11:21:02 +0000 Subject: [PATCH] return type checked trait constraints (#6604) ## Description This PR fixes https://github.com/FuelLabs/sway/issues/6382. The issue was that `TypeParameter::type_check_trait_constraints(...)` mutates the type inside the `TypeEngine`, but it was not returning the new type-checked trait constraints. ## Checklist - [ ] I have linked to any relevant issues. - [ ] 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) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [ ] 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). - [ ] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: IGI-111 --- .../type_system/ast_elements/type_parameter.rs | 6 ++++-- .../generic_where_in_impl_self/src/main.sw | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/sway-core/src/type_system/ast_elements/type_parameter.rs b/sway-core/src/type_system/ast_elements/type_parameter.rs index fb008336f4d..2b2ca658d23 100644 --- a/sway-core/src/type_system/ast_elements/type_parameter.rs +++ b/sway-core/src/type_system/ast_elements/type_parameter.rs @@ -213,7 +213,7 @@ impl TypeParameter { // Type check trait constraints only after type checking all type parameters. // This is required because a trait constraint may use other type parameters. // Ex: `struct Struct2 where A : MyAdd` - for type_param in &new_type_params { + for type_param in new_type_params.iter_mut() { TypeParameter::type_check_trait_constraints(handler, ctx.by_ref(), type_param)?; } @@ -327,7 +327,7 @@ impl TypeParameter { fn type_check_trait_constraints( handler: &Handler, mut ctx: TypeCheckContext, - type_parameter: &TypeParameter, + type_parameter: &mut TypeParameter, ) -> Result<(), ErrorEmitted> { let type_engine = ctx.engines.te(); @@ -372,6 +372,8 @@ impl TypeParameter { }, ); + type_parameter.trait_constraints = trait_constraints_with_supertraits; + // Insert the trait constraints into the namespace. type_parameter.insert_into_namespace_constraints(handler, ctx.by_ref())?; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self/src/main.sw index 4c3e852777f..474043af251 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_where_in_impl_self/src/main.sw @@ -27,9 +27,27 @@ impl CallTrait where K: Trait { } } +// https://github.com/FuelLabs/sway/issues/6382 +trait Devour { fn eat(t: T); } +struct Brain { } +struct Zombie { } + +impl Devour for Zombie { + fn eat(_b: Brain) { } +} + +fn feed(t: T) where U: Devour { + U::eat(t); +} + fn main() -> bool { let _ = call_trait(1); let ct = CallTrait:: {}; assert(ct.call_trait(1) == 42); + + // https://github.com/FuelLabs/sway/issues/6382 + let b = Brain{}; + feed::(b); + true }