Browse Source

fcl-passrc: resolver: check duplicate class forwards

git-svn-id: trunk@38224 -
Mattias Gaertner 7 years ago
parent
commit
2ae54c3c00
2 changed files with 30 additions and 1 deletions
  1. 16 1
      packages/fcl-passrc/src/pasresolver.pp
  2. 14 0
      packages/fcl-passrc/tests/tcresolver.pas

+ 16 - 1
packages/fcl-passrc/src/pasresolver.pp

@@ -5191,9 +5191,24 @@ var
   aModifier: String;
   aModifier: String;
   IsSealed: Boolean;
   IsSealed: Boolean;
   CanonicalSelf: TPasClassOfType;
   CanonicalSelf: TPasClassOfType;
+  ParentDecls: TPasDeclarations;
+  Decl: TPasElement;
 begin
 begin
   if aClass.IsForward then
   if aClass.IsForward then
+    begin
+    // check for duplicate forwards
+    ParentDecls:=aClass.Parent as TPasDeclarations;
+    for i:=0 to ParentDecls.Declarations.Count-1 do
+      begin
+      Decl:=TPasElement(ParentDecls.Declarations[i]);
+      if (CompareText(Decl.Name,aClass.Name)=0)
+          and (Decl<>aClass) then
+        RaiseMsg(20180212144132,nDuplicateIdentifier,sDuplicateIdentifier,
+          [Decl.Name,GetElementSourcePosStr(Decl)],aClass);
+      end;
     exit;
     exit;
+    end;
+
   if aClass.ObjKind<>okClass then
   if aClass.ObjKind<>okClass then
     begin
     begin
     if (aClass.ObjKind=okInterface)
     if (aClass.ObjKind=okInterface)
@@ -7103,7 +7118,7 @@ begin
   if not (TopScope is TPasSectionScope) then
   if not (TopScope is TPasSectionScope) then
     RaiseNotYetImplemented(20171225110934,El,'nested classes');
     RaiseNotYetImplemented(20171225110934,El,'nested classes');
 
 
-  Duplicate:=TPasIdentifierScope(TopScope).FindIdentifier(El.Name);
+  Duplicate:=TPasIdentifierScope(TopScope).FindLocalIdentifier(El.Name);
   //if Duplicate<>nil then
   //if Duplicate<>nil then
     //writeln('  Duplicate=',GetObjName(Duplicate.Element),' ',ord(Duplicate.Kind));
     //writeln('  Duplicate=',GetObjName(Duplicate.Element),' ',ord(Duplicate.Kind));
 
 

+ 14 - 0
packages/fcl-passrc/tests/tcresolver.pas

@@ -427,6 +427,7 @@ type
     Procedure TestClassForward;
     Procedure TestClassForward;
     Procedure TestClassForwardAsAncestorFail;
     Procedure TestClassForwardAsAncestorFail;
     Procedure TestClassForwardNotResolved;
     Procedure TestClassForwardNotResolved;
+    Procedure TestClassForwardDuplicateFail;
     Procedure TestClass_Method;
     Procedure TestClass_Method;
     Procedure TestClass_ConstructorMissingDotFail;
     Procedure TestClass_ConstructorMissingDotFail;
     Procedure TestClass_MethodImplDuplicateFail;
     Procedure TestClass_MethodImplDuplicateFail;
@@ -6518,6 +6519,19 @@ begin
     nForwardTypeNotResolved);
     nForwardTypeNotResolved);
 end;
 end;
 
 
+procedure TTestResolver.TestClassForwardDuplicateFail;
+begin
+  StartProgram(false);
+  Add([
+  'type',
+  '  TObject = class;',
+  '  TObject = class;',
+  '  TObject = class',
+  '  end;',
+  'begin']);
+  CheckResolverException('Duplicate identifier "TObject" at afile.pp(3,10)',nDuplicateIdentifier);
+end;
+
 procedure TTestResolver.TestClass_Method;
 procedure TTestResolver.TestClass_Method;
 begin
 begin
   StartProgram(false);
   StartProgram(false);