Browse Source

resolver: treat array of const as open array

git-svn-id: trunk@41348 -
Mattias Gaertner 6 years ago
parent
commit
73e1846222
2 changed files with 35 additions and 6 deletions
  1. 2 4
      packages/fcl-passrc/src/pasresolver.pp
  2. 33 2
      packages/fcl-passrc/tests/tcresolver.pas

+ 2 - 4
packages/fcl-passrc/src/pasresolver.pp

@@ -22215,8 +22215,7 @@ begin
     exit(false);
   if length(TPasArrayType(TypeEl).Ranges)<>0 then
     exit(false);
-  if TPasArrayType(TypeEl).ElType=nil then
-    exit(true);// array of const is a dynamic array of TVarRec
+  // Note: Array of Const is an open array of TVarRec
   if OptionalOpenArray and (proOpenAsDynArrays in Options) then
     Result:=true
   else
@@ -22229,8 +22228,7 @@ begin
       and (TypeEl.ClassType=TPasArrayType)
       and (length(TPasArrayType(TypeEl).Ranges)=0)
       and (TypeEl.Parent<>nil)
-      and (TypeEl.Parent.ClassType=TPasArgument)
-      and (TPasArrayType(TypeEl).ElType<>nil);
+      and (TypeEl.Parent.ClassType=TPasArgument);
 end;
 
 function TPasResolver.IsDynOrOpenArray(TypeEl: TPasType): boolean;

+ 33 - 2
packages/fcl-passrc/tests/tcresolver.pas

@@ -807,6 +807,8 @@ type
     // array of const
     Procedure TestArrayOfConst;
     Procedure TestArrayOfConst_PassDynArrayOfIntFail;
+    Procedure TestArrayOfConst_AssignNilFail;
+    Procedure TestArrayOfConst_SetLengthFail;
 
     // static arrays
     Procedure TestArrayIntRange_OutOfRange;
@@ -14375,6 +14377,7 @@ begin
   '  i: longint;',
   '  v: TVarRec;',
   '  a: TArrOfVarRec;',
+  '  sa: array[1..2] of TVarRec;',
   'begin',
   '  DoIt(args);',
   '  DoIt(a);',
@@ -14389,8 +14392,7 @@ begin
   '    end;',
   '  end;',
   '  for v in Args do ;',
-  '  args:=nil;',
-  '  SetLength(args,2);',
+  '  args:=sa;',
   'end;',
   'begin']);
   ParseProgram;
@@ -14412,6 +14414,35 @@ begin
     nIncompatibleTypeArgNo);
 end;
 
+procedure TTestResolver.TestArrayOfConst_AssignNilFail;
+begin
+  StartProgram(true,[supTVarRec]);
+  Add([
+  'type',
+  '  TArr = array of word;',
+  'procedure DoIt(args: array of const);',
+  'begin',
+  '  args:=nil;',
+  'end;',
+  'begin']);
+  CheckResolverException('Incompatible types: got "Nil" expected "array of const"',nIncompatibleTypesGotExpected);
+end;
+
+procedure TTestResolver.TestArrayOfConst_SetLengthFail;
+begin
+  StartProgram(true,[supTVarRec]);
+  Add([
+  'type',
+  '  TArr = array of word;',
+  'procedure DoIt(args: array of const);',
+  'begin',
+  '  SetLength(args,2);',
+  'end;',
+  'begin']);
+  CheckResolverException('Incompatible type arg no. 1: Got "array of const", expected "string or dynamic array variable"',
+    nIncompatibleTypeArgNo);
+end;
+
 procedure TTestResolver.TestArrayIntRange_OutOfRange;
 begin
   StartProgram(false);