Skip to content

Mode: ObjFPC - Duplicate Identifier: It's the order that counts

Summary

Depending on the order of the declaration, the compiler generates a “Duplicate Identifier” error message. This error message is misleading because it works the other way around.

System Information

  • Operating system: Linux Mint 21.3 Cinnamon
  • Processor architecture: x86-64
  • Compiler version: trunk gdfb7c2bd04
  • Device: Computer

Steps to reproduce

The following code can be compiled without errors, although the parameter “Name” is declared in the constructor and a property with the same name is defined.

program project1;

{$mode ObjFPC}{$H+}

uses sysutils;

type
  Person = class
    _name: string;
    constructor Create(Name: string);
    property Name: string read _name write _name;
  end;

  constructor Person.Create(Name: string);
    begin
      inherited Create;
      self.Name := Name;
    end;

var
  Client: Person;

begin
  Client := Person.Create('Name');
  writeln(Client.Name);
  FreeAndNil(Client);
end.

In the following code, only the order of the declaration of the constructor and the property is changed. The compiler generates a “Duplicate Identifier” error here because the “Name” parameter is identical to the previously declared property.

program project1;

{$mode ObjFPC}{$H+}

uses
  SysUtils;

type
  Person = class
    _name: string;
    property Name: string read _name write _name;
    constructor Create(Name: string); // <- Error: Duplicate identifier "Name"
  end;

  constructor Person.Create(Name: string);
    begin
      inherited Create;
      self.Name := Name;
    end;

var
  Client: Person;

begin
  Client := Person.Create('Name');
  writeln(Client.Name);
  FreeAndNil(Client);
end.

Example Project

See examples above.

What is the current bug behavior?

See examples above.

What is the expected (correct) behavior?

It is expected that no error message will be generated in the second case either. The scope of the parameter declaration is limited to the method and should be valid regardless of a higher-level name match.

Relevant logs and/or screenshots

Not necessary.

Possible fixes

I don't have the skills for that.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information