0

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.
plumothy
  • 31
  • 3
  • Please copy the complete error message to your question. Can you provide an example without external libraries? – Tom Brunberg Apr 27 '22 at 13:24
  • 1
    Sometimes this can be caused by line endings not complete with CRLF. – LU RD Apr 27 '22 at 13:34
  • I know it is nothing to do with line endings - have tried that. – plumothy Apr 27 '22 at 13:42
  • I have just amended my example to eliminate the use of Spring4D and I can no longer reproduce the issue. So, that points the finger at Spring.Collections.Dictionaries.pas - unless I have done something stupid (quite likely). But, I am unable to explain why the location of `uses Spring.Collections.Dictionaries` makes such a difference. – plumothy Apr 27 '22 at 13:47
  • The error message I was getting was fully expected: `E2003 Undeclared identifier 'xyz' at line 9`. The problem is that it should say `line 21`. – plumothy Apr 27 '22 at 13:50
  • 1
    @plumothy: how did you verify that line endings are not the issue, or why are you so sure? Just asking, because I’ve had similar issues, and it WAS due to a mix of \n and \r\n after somebody used an external editor. – Wouter van Nifterick Apr 27 '22 at 17:39
  • In [this answer](https://stackoverflow.com/a/53360447/2292722) I clarify how to visually detect actual used line endings, and how to correct the wrong ones. – Tom Brunberg Apr 27 '22 at 17:51
  • I have not used any external editors - only Delphi. – plumothy Apr 27 '22 at 21:23

0 Answers0