Browse Source

Allow polymorphic cast on fields with `_` #302

gingerBill 6 years ago
parent
commit
0b6fc19fb0
2 changed files with 10 additions and 10 deletions
  1. 2 2
      src/ir.cpp
  2. 8 8
      src/types.cpp

+ 2 - 2
src/ir.cpp

@@ -4603,7 +4603,7 @@ irValue *ir_emit_conv(irProcedure *proc, irValue *value, Type *t) {
 		String field_name = ir_lookup_subtype_polymorphic_field(proc->module->info, t, src_type);
 		if (field_name.len > 0) {
 			// NOTE(bill): It can be casted
-			Selection sel = lookup_field(st, field_name, false);
+			Selection sel = lookup_field(st, field_name, false, true);
 			if (sel.entity != nullptr) {
 				ir_emit_comment(proc, str_lit("cast - polymorphism"));
 				if (st_is_ptr) {
@@ -4628,7 +4628,7 @@ irValue *ir_emit_conv(irProcedure *proc, irValue *value, Type *t) {
 
 				}
 			} else {
-				GB_PANIC("invalid subtype cast");
+				GB_PANIC("invalid subtype cast  %s.%.*s", type_to_string(src_type), LIT(field_name));
 			}
 		}
 	}

+ 8 - 8
src/types.cpp

@@ -1870,10 +1870,10 @@ ProcTypeOverloadKind are_proc_types_overload_safe(Type *x, Type *y) {
 
 
 
-Selection lookup_field_with_selection(Type *type_, String field_name, bool is_type, Selection sel);
+Selection lookup_field_with_selection(Type *type_, String field_name, bool is_type, Selection sel, bool allow_blank_ident=false);
 
-Selection lookup_field(Type *type_, String field_name, bool is_type) {
-	return lookup_field_with_selection(type_, field_name, is_type, empty_selection);
+Selection lookup_field(Type *type_, String field_name, bool is_type, bool allow_blank_ident=false) {
+	return lookup_field_with_selection(type_, field_name, is_type, empty_selection, allow_blank_ident);
 }
 
 Selection lookup_field_from_index(Type *type, i64 index) {
@@ -1931,10 +1931,10 @@ Selection lookup_field_from_index(Type *type, i64 index) {
 
 Entity *scope_lookup_current(Scope *s, String name);
 
-Selection lookup_field_with_selection(Type *type_, String field_name, bool is_type, Selection sel) {
+Selection lookup_field_with_selection(Type *type_, String field_name, bool is_type, Selection sel, bool allow_blank_ident) {
 	GB_ASSERT(type_ != nullptr);
 
-	if (is_blank_ident(field_name)) {
+	if (!allow_blank_ident && is_blank_ident(field_name)) {
 		return empty_selection;
 	}
 
@@ -1989,13 +1989,13 @@ Selection lookup_field_with_selection(Type *type_, String field_name, bool is_ty
 				}
 			}
 		} else if (type->kind == Type_BitSet) {
-			return lookup_field_with_selection(type->BitSet.elem, field_name, true, sel);
+			return lookup_field_with_selection(type->BitSet.elem, field_name, true, sel, allow_blank_ident);
 		}
 
 
 		if (type->kind == Type_Generic && type->Generic.specialized != nullptr) {
 			Type *specialized = type->Generic.specialized;
-			return lookup_field_with_selection(specialized, field_name, is_type, sel);
+			return lookup_field_with_selection(specialized, field_name, is_type, sel, allow_blank_ident);
 		}
 
 	} else if (type->kind == Type_Union) {
@@ -2017,7 +2017,7 @@ Selection lookup_field_with_selection(Type *type_, String field_name, bool is_ty
 				isize prev_count = sel.index.count;
 				selection_add_index(&sel, i); // HACK(bill): Leaky memory
 
-				sel = lookup_field_with_selection(f->type, field_name, is_type, sel);
+				sel = lookup_field_with_selection(f->type, field_name, is_type, sel, allow_blank_ident);
 
 				if (sel.entity != nullptr) {
 					if (is_type_pointer(f->type)) {