From 248a957b378154c30b63875b0e81dc178c8f69af Mon Sep 17 00:00:00 2001 From: Marcos Henrich Date: Mon, 16 Sep 2024 13:15:15 +0100 Subject: [PATCH] Fixes panic error on decl_to_type_info. Doing an unwrap without checking if the value was none was causing a panic. Fixed by throwing an error so we can also see the previous errors. Fixes #6330 --- sway-core/src/semantic_analysis/namespace/root.rs | 6 ++++++ .../parser_associated_type_error/Forc.lock | 13 +++++++++++++ .../parser_associated_type_error/Forc.toml | 8 ++++++++ .../parser_associated_type_error/src/main.sw | 3 +++ .../parser_associated_type_error/test.toml | 6 ++++++ 5 files changed, 36 insertions(+) create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/Forc.lock create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/Forc.toml create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/src/main.sw create mode 100644 test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/test.toml diff --git a/sway-core/src/semantic_analysis/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index a9da70d1881..b6770d6db8a 100644 --- a/sway-core/src/semantic_analysis/namespace/root.rs +++ b/sway-core/src/semantic_analysis/namespace/root.rs @@ -935,6 +935,12 @@ impl Root { ty::TyDecl::EnumDecl(enum_ty_decl) => TypeInfo::Enum(enum_ty_decl.decl_id), ty::TyDecl::TraitTypeDecl(type_decl) => { let type_decl = engines.de().get_type(&type_decl.decl_id); + if type_decl.ty.is_none() { + return Err(handler.emit_err(CompileError::Internal( + "Trait type declaration has no type", + symbol.span(), + ))); + } (*engines.te().get(type_decl.ty.clone().unwrap().type_id)).clone() } _ => { diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/Forc.lock new file mode 100644 index 00000000000..0284e5bd9e5 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/Forc.lock @@ -0,0 +1,13 @@ +[[package]] +name = "core" +source = "path+from-root-6C2FF70C66B98326" + +[[package]] +name = "parser_associated_type_error" +source = "member" +dependencies = ["std"] + +[[package]] +name = "std" +source = "path+from-root-6C2FF70C66B98326" +dependencies = ["core"] diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/Forc.toml new file mode 100644 index 00000000000..06817f2a91c --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "parser_associated_type_error" + +[dependencies] +std = { path = "../../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/src/main.sw new file mode 100644 index 00000000000..8f11dc585b4 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/src/main.sw @@ -0,0 +1,3 @@ +script; + +trait T{type E const C:Self::E::E} diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/test.toml b/test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/test.toml new file mode 100644 index 00000000000..df5b315a430 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_fail/parser_associated_type_error/test.toml @@ -0,0 +1,6 @@ +category = "fail" + +# check: $()trait T{type E const C:Self::E::E} +# nextln: $()Expected `;`. + +# check: $()Trait type declaration has no type