From 5081ea1a0c2469ed77531a6a6718a9de86f1d140 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 15 Jun 2018 19:59:35 +0100 Subject: [PATCH] Fix type aliasing comparison; Fix gb_utf8_decode --- src/check_decl.cpp | 10 ---------- src/check_stmt.cpp | 10 +++++++--- src/check_type.cpp | 4 ++++ src/gb/gb.h | 2 +- src/types.cpp | 16 ++++++++++++++++ 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/check_decl.cpp b/src/check_decl.cpp index b23ab2b74..9580af157 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -248,16 +248,6 @@ void check_type_decl(CheckerContext *ctx, Entity *e, AstNode *type_expr, Type *d named->Named.base = bt; e->TypeName.is_type_alias = true; } - // if (is_alias) { - // if (is_type_named(bt)) { - // e->type = bt; - // e->TypeName.is_type_alias = true; - // } else { - // gbString str = type_to_string(bt); - // error(type_expr, "Type alias declaration with a non-named type '%s'", str); - // gb_string_free(str); - // } - // } } diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 42495d682..c9a1ad4d8 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -946,8 +946,8 @@ void check_type_switch_stmt(CheckerContext *ctx, AstNode *node, u32 mod_flags) { if (switch_kind == TypeSwitch_Union) { GB_ASSERT(is_type_union(bt)); bool tag_type_found = false; - for_array(i, bt->Union.variants) { - Type *vt = bt->Union.variants[i]; + for_array(j, bt->Union.variants) { + Type *vt = bt->Union.variants[j]; if (are_types_identical(vt, y.type)) { tag_type_found = true; break; @@ -955,7 +955,11 @@ void check_type_switch_stmt(CheckerContext *ctx, AstNode *node, u32 mod_flags) { } if (!tag_type_found) { gbString type_str = type_to_string(y.type); - error(y.expr, "Unknown tag type, got '%s'", type_str); + error(y.expr, "Unknown variant type, got '%s'", type_str); + for_array(j, bt->Union.variants) { + Type *vt = base_type(bt->Union.variants[j]); + gb_printf_err("\t%s\n", type_to_string(vt)); + } gb_string_free(type_str); continue; } diff --git a/src/check_type.cpp b/src/check_type.cpp index 7b0ff8488..231f69aa2 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -603,6 +603,10 @@ void check_enum_type(CheckerContext *ctx, Type *enum_type, Type *named_type, Ast enum_type->Enum.is_export = et->is_export; if (et->is_export) { Scope *parent = ctx->scope->parent; + if (parent->is_file) { + // NOTE(bhall): Use package scope + parent = parent->parent; + } for_array(i, fields) { Entity *f = fields[i]; if (f->kind != Entity_Constant) { diff --git a/src/gb/gb.h b/src/gb/gb.h index b2997eccd..0d8267ea3 100644 --- a/src/gb/gb.h +++ b/src/gb/gb.h @@ -6894,7 +6894,7 @@ isize gb_utf8_decode(u8 const *str, isize str_len, Rune *codepoint_out) { u8 x = gb__utf8_first[s0], sz; u8 b1, b2, b3; gbUtf8AcceptRange accept; - if (x > 0xf0) { + if (x >= 0xf0) { Rune mask = (cast(Rune)x >> 31) << 31; codepoint = (cast(Rune)s0 & (~mask)) | (GB_RUNE_INVALID & mask); width = 1; diff --git a/src/types.cpp b/src/types.cpp index a28bbf608..a57058dbf 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -1157,6 +1157,19 @@ bool is_type_comparable(Type *t) { return false; } +Type *strip_type_aliasing(Type *x) { + if (x == nullptr) { + return x; + } + if (x->kind == Type_Named) { + Entity *e = x->Named.type_name; + if (e != nullptr && e->kind == Entity_TypeName && e->TypeName.is_type_alias) { + return x->Named.base; + } + } + return x; +} + bool are_types_identical(Type *x, Type *y) { if (x == y) { return true; @@ -1167,6 +1180,9 @@ bool are_types_identical(Type *x, Type *y) { return false; } + x = strip_type_aliasing(x); + y = strip_type_aliasing(y); + switch (x->kind) { case Type_Generic: if (y->kind == Type_Generic) {