Kaynağa Gözat

Don't allow static casting of extern classes or enums (#10876)

Aidan Lee 2 yıl önce
ebeveyn
işleme
2bedacd8ee

+ 2 - 0
src/generators/gencpp.ml

@@ -2314,6 +2314,8 @@ let cpp_can_static_cast funcType inferredType =
    | TCppReference(_) | TCppStar(_) | TCppStruct(_) -> false
    | _ ->
       (match inferredType with
+      | TCppInst (cls, _) when is_extern_class cls -> false
+      | TCppEnum e when is_extern_enum e -> false
       | TCppInst _
       | TCppClass
       | TCppEnum _

+ 41 - 0
tests/unit/src/unit/issues/Issue10876.hx

@@ -0,0 +1,41 @@
+package unit.issues;
+
+import utest.Assert;
+
+#if (cpp && !cppia)
+@:native('MY_DEFINE')
+extern class MyDefine {}
+
+@:unreflective
+@:structAccess
+@:native('my_extern')
+extern class MyExtern<T> {
+	function new();
+
+	function create() : T;
+}
+
+@:headerCode('
+
+typedef int MY_DEFINE;
+
+template<class T>
+class my_extern {
+public:
+	T create() {
+		return T();
+	}
+};
+
+')
+#end
+class Issue10876 extends Test {
+    #if (cpp && !cppia)
+    function test() {
+        final vec = cpp.Pointer.fromStar(new MyExtern<MyDefine>());
+		final num = vec.ptr.create();
+
+        Assert.equals(0, num);
+    }
+    #end
+}