Skip to content

Commit

Permalink
Adds reuse of enum variant TypeId.
Browse files Browse the repository at this point in the history
We already do unification of expressions with type annotation when necessary,
but these were not affecting enum variants because we used separate TypeIds for
the expression and for the enum variant.

With this change the enum variant and the expressions will share the same TypeId,
making unification to the expression to also affect the enum variant.

Fixes #5492
Fixes #5581

Probably helpful for #5559
  • Loading branch information
esdrubal committed Feb 21, 2024
1 parent 6b4412c commit d8e4809
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "enum_variant_unification"

[dependencies]
core = { path = "../../../../../../../sway-lib-core" }
std = { path = "../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
script;

struct S<T> {
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<Option<u8>> = S { x: Option::Some(1) };

0
}

#[inline(never)]
fn foo() -> Option<u8> {
match Some(true) {
Option::Some(_b) => Option::Some(17),
Option::None => Option::None,
}
}

#[inline(never)]
fn bar(b: bool) -> Option<u8> {
if(b) {
Option::Some(19)
} else {
Option::None
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
category = "run"
expected_result = { action = "return", value = 0 }
validate_abi = false
expected_warnings = 3

0 comments on commit d8e4809

Please sign in to comment.