summaryrefslogtreecommitdiff
path: root/tests/tbs
diff options
context:
space:
mode:
authoryury <yury@3ad0048d-3df7-0310-abae-a5850022a9f2>2018-11-04 15:37:52 +0000
committeryury <yury@3ad0048d-3df7-0310-abae-a5850022a9f2>2018-11-04 15:37:52 +0000
commit9387669cbafb87398130c553c1671c5933f13306 (patch)
tree057a447d2d727874b49c7cb282585679a23405ca /tests/tbs
parent36c6c1c9440de049e99d54bf4a6be6da0ee71fd8 (diff)
downloadfpc-9387669cbafb87398130c553c1671c5933f13306.tar.gz
* Further improvement for r40180:
An uninitialized function Result of a managed type needs special handling. When passing it as a var parameter a warning need to be emitted, since a user may expect Result to be empty (nil) by default as it happens with local vars of a managed type. But this is not true for Result and may lead to serious issues. The only exception is SetLength(Result, ?) for a string Result. A user always expects undefined contents of the string after calling SetLength(). In such case a hint need to be emitted. + Tests for this. git-svn-id: https://svn.freepascal.org/svn/fpc/trunk@40216 3ad0048d-3df7-0310-abae-a5850022a9f2
Diffstat (limited to 'tests/tbs')
-rw-r--r--tests/tbs/tb0653.pp64
1 files changed, 64 insertions, 0 deletions
diff --git a/tests/tbs/tb0653.pp b/tests/tbs/tb0653.pp
new file mode 100644
index 0000000000..d821065090
--- /dev/null
+++ b/tests/tbs/tb0653.pp
@@ -0,0 +1,64 @@
+{ %norun }
+{ %opt=-Sewn -vwn -O- }
+
+{
+ Test for correct emitting of warnings/hints for uninitialized variables of management types
+ See also tbf/tb0258.pp
+}
+
+// Only hints about uninitialized managed variables must be issued for this code
+
+{$mode objfpc}
+
+type
+ TLongArray = array of longint;
+
+procedure p;
+var
+ a : TLongArray;
+ s: ansistring;
+begin
+ setlength(a,100); // hint for local var
+ setlength(s,100); // hint for local var
+ a[1]:=1;
+ writeln(a[1]);
+ s[1]:='a';
+ writeln(s[1]);
+end;
+
+procedure svar(var s: ansistring; len: longint);
+begin
+ setlength(s,len);
+end;
+
+procedure avar(var a: TLongArray; len: longint);
+begin
+ setlength(a,len);
+end;
+
+procedure p2;
+var
+ a : TLongArray;
+ s: ansistring;
+begin
+ avar(a,100); // hint for local var
+ svar(s,100); // hint for local var
+ a[1]:=1;
+ writeln(a[1]);
+ s[1]:='a';
+ writeln(s[1]);
+end;
+
+function f2: ansistring;
+begin
+ // Hint for the ansistring Result, since all contents of the Result
+ // after calling SetLength() is expected to be undefined.
+ setlength(Result,1);
+ Result[1]:='a';
+end;
+
+begin
+ p;
+ p2;
+ f2;
+end.