diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/enum_instantiation.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/enum_instantiation.rs index 608d62884f6..d9374d17bb1 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/enum_instantiation.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/enum_instantiation.rs @@ -90,7 +90,8 @@ pub(crate) fn instantiate_enum( .with_help_text("Enum instantiator must match its declared variant type.") .with_type_annotation(start_type); - let typed_expr = ty::TyExpression::type_check(handler, enum_ctx, single_expr.clone())?; + let mut typed_expr = + ty::TyExpression::type_check(handler, enum_ctx, single_expr.clone())?; // unify the value of the argument with the variant handler.scope(|handler| { @@ -106,6 +107,9 @@ pub(crate) fn instantiate_enum( Ok(()) })?; + // Reuse the TypeId so further unification in typed_expr.return_type also affect enum_variant.type_argument.type_id + typed_expr.return_type = enum_variant.type_argument.type_id; + // we now know that the instantiator type matches the declared type, via the above tpe // check diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/enum_variant_unification/Forc.lock b/test/src/e2e_vm_tests/test_programs/should_pass/language/enum_variant_unification/Forc.lock new file mode 100644 index 00000000000..6742225f200 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/enum_variant_unification/Forc.lock @@ -0,0 +1,16 @@ +[[package]] +name = "core" +source = "path+from-root-F6BA7257B3F9949F" + +[[package]] +name = "enum_variant_unification" +source = "member" +dependencies = [ + "core", + "std", +] + +[[package]] +name = "std" +source = "path+from-root-F6BA7257B3F9949F" +dependencies = ["core"] diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/enum_variant_unification/Forc.toml b/test/src/e2e_vm_tests/test_programs/should_pass/language/enum_variant_unification/Forc.toml new file mode 100644 index 00000000000..de56d4525f5 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/enum_variant_unification/Forc.toml @@ -0,0 +1,9 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "enum_variant_unification" + +[dependencies] +core = { path = "../../../../../../../sway-lib-core" } +std = { path = "../../../../../../../sway-lib-std" } diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/enum_variant_unification/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/enum_variant_unification/src/main.sw new file mode 100644 index 00000000000..4b3b689d709 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/enum_variant_unification/src/main.sw @@ -0,0 +1,33 @@ +script; + +struct S { + x: T, +} + +fn main() -> u64 { + // https://github.com/FuelLabs/sway/issues/5492 + let _ = foo(); + let _ = bar(true); + + // https://github.com/FuelLabs/sway/issues/5581 + let _: S> = S { x: Option::Some(1) }; + + 0 +} + +#[inline(never)] +fn foo() -> Option { + match Some(true) { + Option::Some(_b) => Option::Some(17), + Option::None => Option::None, + } +} + +#[inline(never)] +fn bar(b: bool) -> Option { + if(b) { + Option::Some(19) + } else { + Option::None + } +} \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/enum_variant_unification/test.toml b/test/src/e2e_vm_tests/test_programs/should_pass/language/enum_variant_unification/test.toml new file mode 100644 index 00000000000..8bce66414f9 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/enum_variant_unification/test.toml @@ -0,0 +1,4 @@ +category = "run" +expected_result = { action = "return", value = 0 } +validate_abi = false +expected_warnings = 3