Browse Source

Disable non-comparison operations for enum (use `bit_set` for flags)

gingerBill 7 years ago
parent
commit
1d0ac72e4a
3 changed files with 17 additions and 14 deletions
  1. 6 1
      examples/demo/demo.odin
  2. 1 1
      src/check_decl.cpp
  3. 10 12
      src/types.cpp

+ 6 - 1
examples/demo/demo.odin

@@ -614,6 +614,11 @@ using_enum :: proc() {
 	f2 := C;
 	f2 := C;
 	fmt.println(f0, f1, f2);
 	fmt.println(f0, f1, f2);
 	fmt.println(len(Foo));
 	fmt.println(len(Foo));
+
+	// Non-comparsion operations are not allowed with enum
+	// You must convert to an integer if you want to do this
+	// x := f0 + f1;
+	y := int(f0) + int(f1);
 }
 }
 
 
 explicit_procedure_overloading :: proc() {
 explicit_procedure_overloading :: proc() {
@@ -772,6 +777,6 @@ main :: proc() {
 		complete_switch();
 		complete_switch();
 		cstring_example();
 		cstring_example();
 		deprecated_attribute();
 		deprecated_attribute();
-	}
 		bit_set_type();
 		bit_set_type();
+	}
 }
 }

+ 1 - 1
src/check_decl.cpp

@@ -253,7 +253,7 @@ void check_type_decl(CheckerContext *ctx, Entity *e, Ast *type_expr, Type *def)
 	if (decl->is_using) {
 	if (decl->is_using) {
 		// NOTE(bill): Must be an enum declaration
 		// NOTE(bill): Must be an enum declaration
 		if (te->kind == Ast_EnumType) {
 		if (te->kind == Ast_EnumType) {
-			Scope *parent = ctx->scope->parent;
+			Scope *parent = e->scope;
 			if (parent->flags&ScopeFlag_File) {
 			if (parent->flags&ScopeFlag_File) {
 				// NOTE(bill): Use package scope
 				// NOTE(bill): Use package scope
 				parent = parent->parent;
 				parent = parent->parent;

+ 10 - 12
src/types.cpp

@@ -659,42 +659,40 @@ bool is_type_named_alias(Type *t) {
 }
 }
 
 
 bool is_type_boolean(Type *t) {
 bool is_type_boolean(Type *t) {
-	t = core_type(t);
+	// t = core_type(t);
+	t = base_type(t);
 	if (t->kind == Type_Basic) {
 	if (t->kind == Type_Basic) {
 		return (t->Basic.flags & BasicFlag_Boolean) != 0;
 		return (t->Basic.flags & BasicFlag_Boolean) != 0;
 	}
 	}
 	return false;
 	return false;
 }
 }
 bool is_type_integer(Type *t) {
 bool is_type_integer(Type *t) {
-	t = core_type(t);
+	// t = core_type(t);
+	t = base_type(t);
 	if (t->kind == Type_Basic) {
 	if (t->kind == Type_Basic) {
 		return (t->Basic.flags & BasicFlag_Integer) != 0;
 		return (t->Basic.flags & BasicFlag_Integer) != 0;
 	}
 	}
 	return false;
 	return false;
 }
 }
 bool is_type_unsigned(Type *t) {
 bool is_type_unsigned(Type *t) {
-	t = core_type(t);
+	t = base_type(t);
+	// t = core_type(t);
 	if (t->kind == Type_Basic) {
 	if (t->kind == Type_Basic) {
 		return (t->Basic.flags & BasicFlag_Unsigned) != 0;
 		return (t->Basic.flags & BasicFlag_Unsigned) != 0;
 	}
 	}
 	return false;
 	return false;
 }
 }
 bool is_type_rune(Type *t) {
 bool is_type_rune(Type *t) {
-	t = core_type(t);
+	// t = core_type(t);
+	t = base_type(t);
 	if (t->kind == Type_Basic) {
 	if (t->kind == Type_Basic) {
 		return (t->Basic.flags & BasicFlag_Rune) != 0;
 		return (t->Basic.flags & BasicFlag_Rune) != 0;
 	}
 	}
 	return false;
 	return false;
 }
 }
-bool is_type_number(Type *t) {
-	t = core_type(t);
-	if (t->kind == Type_Basic) {
-		return (t->Basic.flags & BasicFlag_Numeric) != 0;
-	}
-	return false;
-}
 bool is_type_numeric(Type *t) {
 bool is_type_numeric(Type *t) {
-	t = core_type(t);
+	// t = core_type(t);
+	t = base_type(t);
 	if (t->kind == Type_Basic) {
 	if (t->kind == Type_Basic) {
 		return (t->Basic.flags & BasicFlag_Numeric) != 0;
 		return (t->Basic.flags & BasicFlag_Numeric) != 0;
 	}
 	}