1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-05 12:10:55 +00:00

GDScript: Allow use local constants as types

This commit is contained in:
Danil Alexeev
2023-08-24 19:01:31 +03:00
parent 6758a7f8c0
commit 68a567bd13
12 changed files with 236 additions and 122 deletions

View File

@@ -0,0 +1,5 @@
enum MyEnum {}
func test():
var e: E
const E = MyEnum

View File

@@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Local constant "E" is not resolved at this point.

View File

@@ -0,0 +1,5 @@
enum MyEnum {}
func test():
var E = MyEnum
var e: E

View File

@@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Local variable "E" cannot be used as a type.

View File

@@ -0,0 +1,5 @@
enum MyEnum {A}
func test():
const E = MyEnum.A
var e: E

View File

@@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Local constant "E" is not a valid type.

View File

@@ -0,0 +1,41 @@
class InnerClass:
enum InnerEnum {A = 2}
const INNER_CONST = "INNER_CONST"
enum Enum {A = 1}
const Other = preload("./local_const_as_type.notest.gd")
func test():
const IC = InnerClass
const IE = IC.InnerEnum
const E = Enum
# Doesn't work in CI, but works in the editor. Looks like an unrelated bug. TODO: Investigate it.
# Error: Invalid call. Nonexistent function 'new' in base 'GDScript'.
var a1: IC = null # IC.new()
var a2: IE = IE.A
var a3: IC.InnerEnum = IE.A
var a4: E = E.A
print(a1.INNER_CONST)
print(a2)
print(a3)
print(a4)
const O = Other
const OV: Variant = Other # Removes metatype.
const OIC = O.InnerClass
const OIE = OIC.InnerEnum
const OE = O.Enum
var b: O = O.new()
@warning_ignore("unsafe_method_access")
var bv: OV = OV.new()
var b1: OIC = OIC.new()
var b2: OIE = OIE.A
var b3: O.InnerClass.InnerEnum = OIE.A
var b4: OE = OE.A
print(b.CONST)
print(bv.CONST)
print(b1.INNER_CONST)
print(b2)
print(b3)
print(b4)

View File

@@ -0,0 +1,6 @@
class InnerClass:
enum InnerEnum {A = 20}
const INNER_CONST = "OTHER_INNER_CONST"
enum Enum {A = 10}
const CONST = "OTHER_CONST"

View File

@@ -0,0 +1,11 @@
GDTEST_OK
INNER_CONST
2
2
1
OTHER_CONST
OTHER_CONST
OTHER_INNER_CONST
20
20
10