summaryrefslogtreecommitdiff
path: root/tests/tbf
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/tbf
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/tbf')
-rw-r--r--tests/tbf/tb0258.pp23
-rw-r--r--tests/tbf/tb0259.pp30
-rw-r--r--tests/tbf/tb0260.pp27
3 files changed, 75 insertions, 5 deletions
diff --git a/tests/tbf/tb0258.pp b/tests/tbf/tb0258.pp
index b4eda048ca..17857b534e 100644
--- a/tests/tbf/tb0258.pp
+++ b/tests/tbf/tb0258.pp
@@ -1,13 +1,26 @@
{ %fail% }
{ %opt=-Sew -vw -O- }
-procedure p;
-var
- a : array of longint;
+{
+ Test for correct emitting of warnings/hints for uninitialized variables of management types
+ See also tbs/tb0653.pp, tbf/tb0259.pp, tbf/tb0260.pp
+}
+
+// This code must issue warnings "Function result variable of a managed type does not seem to be initialized".
+
+{$mode objfpc}
+
+type
+ TLongArray = array of longint;
+
+function f: TLongArray;
begin
- setlength(a,100);
+ // Warning for the dyn array Result, since contents of the Result after calling SetLength()
+ // is expected to be zeroed, but instead it is undefined.
+ setlength(Result,100);
+ Result[2]:=1;
end;
begin
+ f;
end.
-
diff --git a/tests/tbf/tb0259.pp b/tests/tbf/tb0259.pp
new file mode 100644
index 0000000000..5e3d7c31e3
--- /dev/null
+++ b/tests/tbf/tb0259.pp
@@ -0,0 +1,30 @@
+{ %fail% }
+{ %opt=-Sew -vw -O- }
+
+{
+ Test for correct emitting of warnings/hints for uninitialized variables of management types
+ See also tbf/tb0258.pp
+}
+
+// This code must issue warnings "Function result variable of a managed type does not seem to be initialized".
+
+{$mode objfpc}
+
+type
+ TLongArray = array of longint;
+
+procedure fvar(var a: TLongArray);
+begin
+ setlength(a,100);
+ a[2]:=1;
+end;
+
+function f: TLongArray;
+begin
+ // Warning for the dyn array Result, since initial contents of the Result is undefined.
+ fvar(Result);
+end;
+
+begin
+ f;
+end.
diff --git a/tests/tbf/tb0260.pp b/tests/tbf/tb0260.pp
new file mode 100644
index 0000000000..6379aa0e39
--- /dev/null
+++ b/tests/tbf/tb0260.pp
@@ -0,0 +1,27 @@
+{ %fail% }
+{ %opt=-Sew -vw -O- }
+
+{
+ Test for correct emitting of warnings/hints for uninitialized variables of management types
+ See also tbf/tb0258.pp
+}
+
+// This code must issue warnings "Function result variable of a managed type does not seem to be initialized".
+
+{$mode objfpc}
+
+procedure fvar(var a: ansistring);
+begin
+ setlength(a,100);
+ a[2]:='a';
+end;
+
+function f: ansistring;
+begin
+ // Warning for the ansistring Result, since initial contents of the Result is undefined.
+ fvar(Result);
+end;
+
+begin
+ f;
+end.