浏览代码

GDScript: Add missing type conversions in `for range`

Danil Alexeev 3 月之前
父节点
当前提交
e2d4469dc2

+ 15 - 3
modules/gdscript/gdscript_byte_codegen.cpp

@@ -1585,9 +1585,21 @@ void GDScriptByteCodeGenerator::write_for_range_assignment(const Address &p_from
 	const Address &range_step = for_range_step_variables.back()->get();
 
 	// Assign range args.
-	write_assign(range_from, p_from);
-	write_assign(range_to, p_to);
-	write_assign(range_step, p_step);
+	if (range_from.type == p_from.type) {
+		write_assign(range_from, p_from);
+	} else {
+		write_assign_with_conversion(range_from, p_from);
+	}
+	if (range_to.type == p_to.type) {
+		write_assign(range_to, p_to);
+	} else {
+		write_assign_with_conversion(range_to, p_to);
+	}
+	if (range_step.type == p_step.type) {
+		write_assign(range_step, p_step);
+	} else {
+		write_assign_with_conversion(range_step, p_step);
+	}
 }
 
 void GDScriptByteCodeGenerator::write_for(const Address &p_variable, bool p_use_conversion, bool p_is_range) {

+ 13 - 0
modules/gdscript/gdscript_function.h

@@ -234,6 +234,19 @@ public:
 
 	GDScriptDataType() = default;
 
+	bool operator==(const GDScriptDataType &p_other) const {
+		return kind == p_other.kind &&
+				has_type == p_other.has_type &&
+				builtin_type == p_other.builtin_type &&
+				native_type == p_other.native_type &&
+				(script_type == p_other.script_type || script_type_ref == p_other.script_type_ref) &&
+				container_element_types == p_other.container_element_types;
+	}
+
+	bool operator!=(const GDScriptDataType &p_other) const {
+		return !(*this == p_other);
+	}
+
 	void operator=(const GDScriptDataType &p_other) {
 		kind = p_other.kind;
 		has_type = p_other.has_type;

+ 6 - 2
modules/gdscript/tests/scripts/runtime/features/for_range_large_ints.gd → modules/gdscript/tests/scripts/runtime/features/for_range.gd

@@ -1,7 +1,11 @@
-# GH-83293
-
 func test():
+	# GH-83293
 	for x in range(1 << 31, (1 << 31) + 3):
 		print(x)
 	for x in range(1 << 62, (1 << 62) + 3):
 		print(x)
+
+	# GH-107392
+	var n = 1.0
+	for x in range(n):
+		print(x)

+ 1 - 0
modules/gdscript/tests/scripts/runtime/features/for_range_large_ints.out → modules/gdscript/tests/scripts/runtime/features/for_range.out

@@ -5,3 +5,4 @@ GDTEST_OK
 4611686018427387904
 4611686018427387905
 4611686018427387906
+0