GDScript: Consolidate behavior for assigning enum types

This makes sure that assigning values to enum-typed variables are
consistent. Same enum is always valid, different enum is always
invalid (without casting) and assigning `int` creates a warning
if there is no casting.

There are new test cases to ensure this behavior doesn't break in
the future.
This commit is contained in:
George Marques
2022-01-27 11:34:33 -03:00
parent 82efb1d262
commit ad6e2e82a9
27 changed files with 185 additions and 29 deletions

View File

@@ -0,0 +1,10 @@
enum MyEnum { ENUM_VALUE_1, ENUM_VALUE_2 }
enum MyOtherEnum { OTHER_ENUM_VALUE_1, OTHER_ENUM_VALUE_2 }
# Different enum types can't be assigned without casting.
var class_var: MyEnum = MyEnum.ENUM_VALUE_1
func test():
print(class_var)
class_var = MyOtherEnum.OTHER_ENUM_VALUE_2
print(class_var)

View File

@@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Cannot assign a value of type "MyOtherEnum (enum)" to a target of type "MyEnum (enum)".

View File

@@ -0,0 +1,8 @@
enum MyEnum { ENUM_VALUE_1, ENUM_VALUE_2 }
enum MyOtherEnum { OTHER_ENUM_VALUE_1, OTHER_ENUM_VALUE_2 }
# Different enum types can't be assigned without casting.
var class_var: MyEnum = MyOtherEnum.OTHER_ENUM_VALUE_1
func test():
print(class_var)

View File

@@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Value of type "MyOtherEnum (enum)" cannot be assigned to a variable of type "MyEnum (enum)".

View File

@@ -0,0 +1,8 @@
enum MyEnum { ENUM_VALUE_1, ENUM_VALUE_2 }
enum MyOtherEnum { OTHER_ENUM_VALUE_1, OTHER_ENUM_VALUE_2 }
func test():
var local_var: MyEnum = MyEnum.ENUM_VALUE_1
print(local_var)
local_var = MyOtherEnum.OTHER_ENUM_VALUE_2
print(local_var)

View File

@@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Cannot assign a value of type "MyOtherEnum (enum)" to a target of type "MyEnum (enum)".

View File

@@ -0,0 +1,6 @@
enum MyEnum { ENUM_VALUE_1, ENUM_VALUE_2 }
enum MyOtherEnum { OTHER_ENUM_VALUE_1, OTHER_ENUM_VALUE_2 }
func test():
var local_var: MyEnum = MyOtherEnum.OTHER_ENUM_VALUE_1
print(local_var)

View File

@@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Value of type "MyOtherEnum (enum)" cannot be assigned to a variable of type "MyEnum (enum)".