From 2b7ca2bdd6be41abeb8ab5c5fa91f24657996e88 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 1 Nov 2022 13:14:20 +0000 Subject: Fix #2160 (deep subtyping through `using` of `_`) --- src/types.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/types.cpp') diff --git a/src/types.cpp b/src/types.cpp index fec324bf4..b9f2b375f 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -2526,6 +2526,44 @@ String lookup_subtype_polymorphic_field(Type *dst, Type *src) { return str_lit(""); } +bool lookup_subtype_polymorphic_selection(Type *dst, Type *src, Selection *sel) { + Type *prev_src = src; + // Type *prev_dst = dst; + src = base_type(type_deref(src)); + // dst = base_type(type_deref(dst)); + bool src_is_ptr = src != prev_src; + // bool dst_is_ptr = dst != prev_dst; + + GB_ASSERT(is_type_struct(src) || is_type_union(src)); + for_array(i, src->Struct.fields) { + Entity *f = src->Struct.fields[i]; + if (f->kind == Entity_Variable && f->flags & EntityFlags_IsSubtype) { + if (are_types_identical(dst, f->type)) { + array_add(&sel->index, cast(i32)i); + sel->entity = f; + return true; + } + if (src_is_ptr && is_type_pointer(dst)) { + if (are_types_identical(type_deref(dst), f->type)) { + array_add(&sel->index, cast(i32)i); + sel->indirect = true; + sel->entity = f; + return true; + } + } + if ((f->flags & EntityFlag_Using) != 0 && is_type_struct(f->type)) { + String name = lookup_subtype_polymorphic_field(dst, f->type); + if (name.len > 0) { + array_add(&sel->index, cast(i32)i); + return lookup_subtype_polymorphic_selection(dst, f->type, sel); + } + } + } + } + return false; +} + + Type *strip_type_aliasing(Type *x) { -- cgit v1.2.3