Ver Fonte

* accept overloaded operators that return any shortstring type when needing
a conversion to a shortstring type (so an operator := that returns a
string[255] can be used to assign this type to a string[80]) (mantis
#12109)
* do not allow overloading := with a string[x<>255] as result type,
because we want one such overload to satisfy all conversions (see
previous point)

git-svn-id: trunk@12590 -

Jonas Maebe há 16 anos atrás
pai
commit
2929624ffc
5 ficheiros alterados com 46 adições e 2 exclusões
  1. 2 0
      .gitattributes
  2. 10 1
      compiler/htypechk.pas
  3. 3 1
      compiler/symsym.pas
  4. 12 0
      tests/webtbf/tw12109a.pp
  5. 19 0
      tests/webtbs/tw12109.pp

+ 2 - 0
.gitattributes

@@ -8233,6 +8233,7 @@ tests/webtbf/tw11849a.pp svneol=native#text/plain
 tests/webtbf/tw11862a.pp svneol=native#text/plain
 tests/webtbf/tw11970.pp svneol=native#text/plain
 tests/webtbf/tw12075.pp svneol=native#text/plain
+tests/webtbf/tw12109a.pp svneol=native#text/plain
 tests/webtbf/tw12303.pp svneol=native#text/plain
 tests/webtbf/tw12329.pp svneol=native#text/plain
 tests/webtbf/tw12365a.cfg svneol=native#text/plain
@@ -8701,6 +8702,7 @@ tests/webtbs/tw12050b.pp svneol=native#text/plain
 tests/webtbs/tw12051.pp svneol=native#text/plain
 tests/webtbs/tw1207.pp svneol=native#text/plain
 tests/webtbs/tw12076.pp svneol=native#text/plain
+tests/webtbs/tw12109.pp svneol=native#text/plain
 tests/webtbs/tw12151.pp svneol=native#text/plain
 tests/webtbs/tw12186.pp svneol=native#text/plain
 tests/webtbs/tw12202.pp svneol=native#text/plain

+ 10 - 1
compiler/htypechk.pas

@@ -409,7 +409,16 @@ implementation
                 if optoken=_ASSIGNMENT then
                   begin
                     eq:=compare_defs_ext(ld,pf.returndef,nothingn,conv,pd,[cdo_explicit]);
-                    result:=(eq=te_incompatible);
+                    result:=
+                      (eq=te_incompatible) and
+                      { don't allow overloading assigning to custom shortstring
+                        types, because we also don't want to differentiate based
+                        on different shortstring types (e.g.,
+                        "operator :=(const v: variant) res: shorstring" also
+                        has to work for assigning a variant to a string[80])
+                      }
+                      (not is_shortstring(pf.returndef) or
+                       (tstringdef(pf.returndef).len=255));
                   end
                 else
                   begin

+ 3 - 1
compiler/symsym.pas

@@ -702,7 +702,9 @@ implementation
         for i:=0 to ProcdefList.Count-1 do
           begin
             pd:=tprocdef(ProcdefList[i]);
-            if equal_defs(todef,pd.returndef) and
+            if (equal_defs(todef,pd.returndef) or
+                { shortstrings of different lengths are ok as result }
+                (is_shortstring(todef) and is_shortstring(pd.returndef))) and
                { the result type must be always really equal and not an alias,
                  if you mess with this code, check tw4093 }
                ((todef=pd.returndef) or

+ 12 - 0
tests/webtbf/tw12109a.pp

@@ -0,0 +1,12 @@
+{ %fail }
+
+type
+  tr = record end;
+  ts2 = string[80];
+
+operator :=(const r: tr) res: ts2;
+begin
+end;
+
+begin
+end.

+ 19 - 0
tests/webtbs/tw12109.pp

@@ -0,0 +1,19 @@
+{$mode delphi}
+
+Procedure test;
+
+Var
+  V : Variant;
+  SS : String [80];
+
+Begin
+   V := 'Hello';
+   SS := V;
+   if (ss<>'Hello') then
+     halt(1);
+End;
+
+
+begin
+  test;
+end.