Under some circumstances (with Delphi 11.1), Error Insight reports errors in code at the wrong line number(s). However, it is not just Error Insight - compiler messages show the wrong line numbers and the debugger highlights the wrong line when stepping through code.
I have reported this to Embarcadero but (while waiting for their official response) I would like to ask if anyone here knows if I have done something stupid or if this is a genuine issue with Delphi?
Below is the code for a very small project which suffers from this issue (by deliberately including an undeclared identifier). It uses Spring4D (Spring.Collections.Dictionaries) and I am not sure if that has anything to do with it or not.
See where errors are reported in the IDE. Why is error line number reported correctly in Unit4 but not in Unit2?
program TestErrorInsight;
uses
Vcl.Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas',
Unit3 in 'Unit3.pas',
Unit4 in 'Unit4.pas';
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
end.
unit Unit2;
interface
uses
Unit3;
type
TMyClass01 = class
private
public
function GetMyDictionary: TMyDictionary01;
end;
implementation
{ TMyClass01 }
function TMyClass01.GetMyDictionary: TMyDictionary01;
begin
xyz // This is line 21 but ErrorInsight reports this undeclared identifier to be in line 9
// If TMyDictionary01 is declared in a different unit from TMyClass01 then ErrorInsight line numbers are incorrect.
// This also causes the debugger to highlight the wrong lines when stepping through code
// and it also causes runtime error line numbers to be incorrect.
end;
end.
unit Unit3;
interface
uses
Spring.Collections.Dictionaries;
type
TMyDictionary01 = TDictionary<string, string>;
implementation
end.
unit Unit4;
interface
uses
Spring.Collections.Dictionaries;
type
TMyDictionary02 = TDictionary<string, string>;
TMyClass02 = class
private
public
function GetMyDictionary: TMyDictionary02;
end;
implementation
{ TMyClass02 }
function TMyClass02.GetMyDictionary: TMyDictionary02;
begin
xyz // This is line 22 and ErrorInsight correctly reports the line number for this undeclared identifier
// If TMyDictionary02 is declared in the same unit as TMyClass02 then ErrorInsight line numbers are correct.
end;
end.