Merge branch 'dup' into 'main'

Allow DuplicateLocals for objects.

See merge request freepascal.org/fpc/source!739
This commit is contained in:
Rika 2025-04-04 13:31:13 +03:00
commit a7838b8ebf
2 changed files with 41 additions and 2 deletions

View File

@ -2522,7 +2522,8 @@ implementation
(tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
(
not(m_duplicate_names in current_settings.modeswitches) or
is_object(tprocdef(defowner).struct)
{ Delphi allows duplicates in classes/records but disallows in objects. }
(m_delphi in current_settings.modeswitches) and is_object(tprocdef(defowner).struct)
) then
result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
end;

View File

@ -2,6 +2,7 @@ program Project1;
{$mode objfpc}{$H+}
{$ModeSwitch duplicatelocals}
{$ModeSwitch advancedrecords}
type
TClass1 = class(TObject)
@ -18,13 +19,50 @@ begin
end;
// Local variable has same name as member field.
// Unlike with delphi mode, this is compiler error, even with duplicatelocals!
// This is okay with duplicatelocals, too.
procedure TClass1.Proc2;
var
X: Integer;
begin
end;
// Same for objects and managed records. (Though in Delphi mode, which implicitly enables duplicatelocals, disallowed specifically for objects.)
type
TRecord1 = record
public
X: Integer;
procedure Proc1(X: Integer);
procedure Proc2;
end;
procedure TRecord1.Proc1(X: Integer);
begin
end;
procedure TRecord1.Proc2;
var
X: Integer;
begin
end;
type
TObject1 = object
public
X: Integer;
procedure Proc1(X: Integer);
procedure Proc2;
end;
procedure TObject1.Proc1(X: Integer);
begin
end;
procedure TObject1.Proc2;
var
X: Integer;
begin
end;
begin
end.