From 4f08020bb7512231d2b302ce6aa80bf8da3207f7 Mon Sep 17 00:00:00 2001 From: Marcos Henrich Date: Fri, 23 Aug 2024 05:20:47 +0100 Subject: [PATCH] Fixes panic on unwrapping in type_check_trait_implementation. (#6434) ## Description This PR fixes panic on unwrapping by only performing the required behavior with the unwrapped value when it is available. Fixes #6336 ## 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: Sophie Dankel <47993817+sdankel@users.noreply.github.com> Co-authored-by: Joshua Batty --- .../ast_node/declaration/impl_trait.rs | 38 +++++++++++-------- .../associated_type_not_in_trait/Forc.lock | 3 ++ .../associated_type_not_in_trait/Forc.toml | 6 +++ .../associated_type_not_in_trait/src/main.sw | 4 ++ .../associated_type_not_in_trait/test.toml | 3 ++ 5 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/Forc.lock create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/Forc.toml create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/src/main.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/test.toml diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs b/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs index 35e03b4f75c..e1ab4aaf699 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs @@ -831,22 +831,28 @@ fn type_check_trait_implementation( None, ), }; - trait_type_mapping.extend(&TypeSubstMap::from_type_parameters_and_type_arguments( - vec![type_engine.insert( - engines, - old_type_decl_info1, - type_decl.name.span().source_id(), - )], - vec![type_decl.ty.clone().unwrap().type_id], - )); - trait_type_mapping.extend(&TypeSubstMap::from_type_parameters_and_type_arguments( - vec![type_engine.insert( - engines, - old_type_decl_info2, - type_decl.name.span().source_id(), - )], - vec![type_decl.ty.clone().unwrap().type_id], - )); + if let Some(type_arg) = type_decl.ty.clone() { + trait_type_mapping.extend( + &TypeSubstMap::from_type_parameters_and_type_arguments( + vec![type_engine.insert( + engines, + old_type_decl_info1, + type_decl.name.span().source_id(), + )], + vec![type_arg.type_id], + ), + ); + trait_type_mapping.extend( + &TypeSubstMap::from_type_parameters_and_type_arguments( + vec![type_engine.insert( + engines, + old_type_decl_info2, + type_decl.name.span().source_id(), + )], + vec![type_arg.type_id], + ), + ); + } } } } diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/Forc.lock new file mode 100644 index 00000000000..d6d5627a81d --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/Forc.lock @@ -0,0 +1,3 @@ +[[package]] +name = "associated_type_not_in_trait" +source = "member" diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/Forc.toml new file mode 100644 index 00000000000..800719114d2 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/Forc.toml @@ -0,0 +1,6 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "associated_type_not_in_trait" +implicit-std = false diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/src/main.sw new file mode 100644 index 00000000000..5a1f41599ba --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/src/main.sw @@ -0,0 +1,4 @@ +script; +trait Trait{} +struct Struct0{} +impl Trait for Struct0{type u;const u=0;} \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/test.toml b/test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/test.toml new file mode 100644 index 00000000000..74ffd8a5f62 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/associated_type_not_in_trait/test.toml @@ -0,0 +1,3 @@ +category = "fail" + +# check: $()Type "u" is not a part of trait "Trait"'s interface surface.