Browse Source

* no longer allow assigning values to typecasted properties, because in
case the getter is a function, the result is that first the getter is
called, and subsequently the temp holding the function result is
overwritten (thus not changing anything). This is Delphi-compatible,
and fixes tests/tbf/tb0214*

git-svn-id: trunk@13320 -

Jonas Maebe 16 years ago
parent
commit
b7235b21c9
3 changed files with 39 additions and 1 deletions
  1. 1 0
      .gitattributes
  2. 15 1
      compiler/htypechk.pas
  3. 23 0
      tests/tbf/tb0214a.pp

+ 1 - 0
.gitattributes

@@ -6977,6 +6977,7 @@ tests/tbf/tb0211a.pp svneol=native#text/plain
 tests/tbf/tb0212.pp svneol=native#text/plain
 tests/tbf/tb0213.pp svneol=native#text/plain
 tests/tbf/tb0214.pp svneol=native#text/plain
+tests/tbf/tb0214a.pp svneol=native#text/plain
 tests/tbf/tb0215.pp svneol=native#text/plain
 tests/tbf/tb0215a.pp svneol=native#text/plain
 tests/tbf/tb0215b.pp svneol=native#text/plain

+ 15 - 1
compiler/htypechk.pas

@@ -955,7 +955,8 @@ implementation
         gotvec,
         gotclass,
         gotdynarray,
-        gotderef : boolean;
+        gotderef,
+        gottypeconv : boolean;
         fromdef,
         todef    : tdef;
         errmsg,
@@ -976,6 +977,7 @@ implementation
         gotpointer:=false;
         gotdynarray:=false;
         gotstring:=false;
+        gottypeconv:=false;
         hp:=p;
         if not(valid_void in opts) and
            is_void(hp.resultdef) then
@@ -1013,6 +1015,17 @@ implementation
                       { same when we got a class and subscript (= deref) }
                       (gotclass and gotsubscript) or
                       (
+                       { allowing assignments to typecasted properties
+                           a) is Delphi-incompatible
+                           b) causes problems in case the getter is a function
+                              (because then the result of the getter is
+                               typecasted to this type, and then we "assign" to
+                               this typecasted function result) -> always
+                               disallow, since property accessors should be
+                               transparantly changeable to functions at all
+                               times
+                       }
+                       not(gottypeconv) and
                        not(gotsubscript and gotrecord) and
                        not(gotstring and gotvec)
                       ) then
@@ -1059,6 +1072,7 @@ implementation
                end;
              typeconvn :
                begin
+                 gottypeconv:=true;
                  { typecast sizes must match, exceptions:
                    - implicit typecast made by absolute
                    - from formaldef

+ 23 - 0
tests/tbf/tb0214a.pp

@@ -0,0 +1,23 @@
+{ %fail }
+
+{$ifdef fpc}
+{$mode delphi}
+{$endif}
+
+type
+  tc = class
+   private
+    fl: longint;
+   public
+    property l: longint read fl write fl;
+  end;
+
+var
+  c: tc;
+begin
+  { should give an error stating that you cannot assign to left hand side }
+  { (generated code also does not result in an assignment)                }
+  cardinal(c.l):=cardinal(5);
+end.
+
+