Skip to content

Commit

Permalink
Next
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Jul 26, 2024
1 parent 4baf36e commit 9f59957
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 39 deletions.
14 changes: 14 additions & 0 deletions crates/rue-typing/src/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ pub(crate) fn compare_type(
let mut any_incompatible = false;

for item in &items {
if matches!(db.get_recursive(*item), Type::Never) {
continue;
}

let cmp = compare_type(db, lhs, *item, ctx);
result = min(result, cmp);

Expand Down Expand Up @@ -321,6 +325,16 @@ pub(crate) fn compare_type(
}
}

(Type::Enum(ty), Type::Variant(variant)) => {
let comparison = compare_type(db, ty.type_id, rhs, ctx);

if variant.original_enum_type_id == ty.original_type_id {
max(comparison, Comparison::Assignable)
} else {
max(comparison, Comparison::Castable)
}
}

// Enums can be assigned if the structure is assignable and it's the same enum.
(Type::Enum(lhs), Type::Enum(rhs)) if lhs.original_type_id == rhs.original_type_id => {
compare_type(db, lhs.type_id, rhs.type_id, ctx)
Expand Down
42 changes: 4 additions & 38 deletions crates/rue-typing/src/difference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,7 @@ pub(crate) fn difference_type(
generic_types: ty.generic_types,
}))
}
(_, Type::Struct(ty)) => {
let ty = ty.clone();
let type_id = difference_type(types, lhs, ty.type_id, visited);

types.alloc(Type::Struct(Struct {
original_type_id: ty.original_type_id,
type_id,
field_names: ty.field_names,
rest: ty.rest,
generic_types: ty.generic_types,
}))
}
(_, Type::Struct(ty)) => difference_type(types, lhs, ty.type_id, visited),

(Type::Enum(ty), _) => {
let ty = ty.clone();
Expand All @@ -181,17 +170,7 @@ pub(crate) fn difference_type(
variants: ty.variants,
}))
}
(_, Type::Enum(ty)) => {
let ty = ty.clone();
let type_id = difference_type(types, lhs, ty.type_id, visited);

types.alloc(Type::Enum(Enum {
original_type_id: ty.original_type_id,
type_id,
has_fields: ty.has_fields,
variants: ty.variants,
}))
}
(_, Type::Enum(ty)) => difference_type(types, lhs, ty.type_id, visited),

(Type::Variant(variant), _) => {
let variant = variant.clone();
Expand All @@ -207,20 +186,7 @@ pub(crate) fn difference_type(
discriminant: variant.discriminant,
}))
}
(_, Type::Variant(variant)) => {
let variant = variant.clone();
let type_id = difference_type(types, lhs, variant.type_id, visited);

types.alloc(Type::Variant(Variant {
original_type_id: variant.original_type_id,
original_enum_type_id: variant.original_enum_type_id,
field_names: variant.field_names,
type_id,
rest: variant.rest,
generic_types: variant.generic_types,
discriminant: variant.discriminant,
}))
}
(_, Type::Variant(variant)) => difference_type(types, lhs, variant.type_id, visited),

(Type::Union(items), _) => {
let items = items.clone();
Expand All @@ -229,7 +195,7 @@ pub(crate) fn difference_type(

for item in &items {
let item = difference_type(types, *item, rhs, visited);
if matches!(types.get(item), Type::Never) {
if matches!(types.get_recursive(item), Type::Never) {
continue;
}
result.push(item);
Expand Down
2 changes: 1 addition & 1 deletion tests/enum/enum_type_guard.rue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fun main() -> Int {
raise "Unreachable";
}

assert color is Color::Red;
let red: Color::Red = color;

red as Int
}

0 comments on commit 9f59957

Please sign in to comment.