Преглед на файлове

Add error when trying to typecast class type to class instance via explicit typecasting

Pierre Muller преди 3 години
родител
ревизия
235a68232e
променени са 1 файла, в които са добавени 11 реда и са изтрити 2 реда
  1. 11 2
      compiler/defcmp.pas

+ 11 - 2
compiler/defcmp.pas

@@ -280,8 +280,17 @@ implementation
          if def_from.typ=objectdef then
            def_from:=find_real_class_definition(tobjectdef(def_from),false);
          if def_to.typ=objectdef then
-           def_to:=find_real_class_definition(tobjectdef(def_to),false);
-
+           begin
+             { Emit error when trying to typecast a object/class type to
+               another object type, because this treats a VMT
+               like an ordinary object/class instance, allowing
+               to access instance fields, which is wrong PM }
+             def_to:=find_real_class_definition(tobjectdef(def_to),false);
+             if (def_from.typ=classrefdef) and (fromtreetype=loadvmtaddrn) and (cdo_explicit in cdoptions)
+                { for jvm everything is a java class ... }
+                and (tobjectdef(def_to).objecttype <> odt_javaclass) then
+               comment(V_Error,'Explicit typecast object/class definition to objectdef/class definition');
+           end;
          { same def? then we've an exact match }
          if def_from=def_to then
           begin