Browse Source

Disallow default struct values for `any`; `new_clone`

Ginger Bill 8 years ago
parent
commit
5af0acc4af
2 changed files with 10 additions and 3 deletions
  1. 5 0
      core/_preload.odin
  2. 5 3
      src/check_expr.cpp

+ 5 - 0
core/_preload.odin

@@ -474,6 +474,11 @@ new  :: proc(T: type) -> ^T #inline {
 	ptr^ = T{};
 	return ptr;
 }
+new_clone :: proc(data: $T) -> ^T #inline {
+	ptr := cast(^T)alloc(size_of(T), align_of(T));
+	ptr^ = data;
+	return ptr;
+}
 
 free :: proc(ptr:   rawptr)      do free_ptr(ptr);
 free :: proc(str:   string)      do free_ptr((cast(^raw.String)&str).data);

+ 5 - 3
src/check_expr.cpp

@@ -887,7 +887,11 @@ void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields,
 			} else if (is_operand_undef(a)) {
 				e->Variable.default_is_undef = true;
 			} else if (b.mode != Addressing_Constant) {
-				error(b.expr, "Default field parameter must be a constant");
+				error(b.expr, "Default field value must be a constant");
+			} else if (is_type_any(e->type)) {
+				gbString str = type_to_string(e->type);
+				error(b.expr, "A struct field of type `%s` cannot have a default value", str);
+				gb_string_free(str);
 			} else {
 				e->Variable.default_value = b.value;
 			}
@@ -900,8 +904,6 @@ void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields,
 
 		if (is_blank_ident(name_token)) {
 			array_add(fields, e);
-		} else if (name_token.string == "__tag") {
-			error(name, "`__tag` is a reserved identifier for fields");
 		} else {
 			HashKey key = hash_string(name_token.string);
 			Entity **found = map_get(entity_map, key);