Recent

Author Topic: Weirdest Error resolution ever!  (Read 2804 times)

EganSolo

  • Sr. Member
  • ****
  • Posts: 394
Weirdest Error resolution ever!
« on: July 30, 2025, 02:01:04 am »
I must not understand unit references properly.
Take a look at Unit1 and Unit2 below:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. uses Unit2;
  3. Type
  4.   TMyDM = class(TDataModule)
  5.   //published stuff as usual
  6.   private
  7.      fWidget : TWidget;
  8.      procedure SetWidget(const Value: TWidget);
  9.  public
  10.     property ActiveWidget : TWidget read fWidget write SetWidget;
  11.  end;
  12. Implementation
  13. uses UnitB;
  14. //implementation code here.
  15. end.
  16. var dm : TMyDM;
  17.  

Then Unit2
Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2. Type
  3.   TWidget = class
  4.   //class declarations
  5.   public
  6.     Procedure SetAsActiveWidget;
  7.   end;
  8. Implementation
  9. uses Unit1;
  10.  
  11. procedure TWidget.SetAsActiveWidget;
  12. begin
  13.   dm.ActiveWidget := Self;
  14. end;
  15.  
  16. end.
  17.  

It's the classic cyclical dependency mediated by use clauses in the declarative and implementation sections of each unit. Recently, I've introduced some changes and was stumped when the compiler returned the following error while compiling unit 2:

Identifier idents no member: "ActiveWidget"

Suddenly, the compiler could no longer see ActiveWidget in the scope of its compilation. I found this odd, as I had assumed the compiler would compile the definition sections of all units before moving on to the implementation side, but I may be mistaken.

Anyway, here's what blew me away: I moved the declaration of ActiveWidget in Unit1 to the top of TMyDM as follows:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. uses Unit2;
  3. Type
  4.   TMyDM = class(TDataModule)
  5.   private
  6.      fWidget : TWidget;
  7.      procedure SetWidget(const Value: TWidget);
  8.  public
  9.     property ActiveWidget : TWidget read fWidget write SetWidget;
  10.  published
  11.   //published stuff as usual
  12.  end;
  13. Implementation
  14. uses UnitB;
  15. //implementation code here.
  16. end.
  17. var dm : TMyDM;
  18.  

The program compiled without a hitch ...

Thought?


cdbc

  • Hero Member
  • *****
  • Posts: 2531
    • http://www.cdbc.dk
Re: Weirdest Error resolution ever!
« Reply #1 on: July 30, 2025, 05:47:44 am »
Hi
Code: Pascal  [Select][+][-]
  1. Implementation
  2. uses UnitB;
  3. //implementation code here.
  4. end. //<-- END OF UNIT
  5. var dm : TMyDM; //<-- WHAT THE H***?!?
  6.  
What's that 'var' declaration doing AFTER unit-end?!?!?!?
/bc
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: Weirdest Error resolution ever!
« Reply #2 on: July 30, 2025, 05:54:18 am »
Indeed.
var dm : TMyDM;
 Is should be or is ignored and will give a warning? Any code after the end dot should not be compiled at all.
Otherwise is see no strange things. (Apart from dm not being created in these units.)
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

WooBean

  • Sr. Member
  • ****
  • Posts: 301
Re: Weirdest Error resolution ever!
« Reply #3 on: July 30, 2025, 07:40:21 am »
It would be much productive to present a real project.

Now we are invited to guess which changes were introduced during publishing a code essay rather than a code.
Platforms: Win7/64, Linux Mint 22.1 Xia

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: Weirdest Error resolution ever!
« Reply #4 on: July 30, 2025, 09:32:12 am »
Good observation. The uses clauses are also incomplete, so even if you write an example program unit it will not compile in any case.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

n7800

  • Hero Member
  • *****
  • Posts: 587
  • Lazarus IDE contributor
    • GitLab profile
Re: Weirdest Error resolution ever!
« Reply #5 on: July 30, 2025, 02:11:10 pm »
I've stepped on the "end." error a couple of times. And recently fixed the same one in Lazarus. The projects had a whole copy of the program block!

I don't think the developers will agree to throw an error, since it may affect compatibility (though personally I don't mind). For example, here is an interesting exploitation of this "hack".

But I'm sure they will agree to add a warning message. I'll create an issue in FPC today, using this topic as another argument.

I'll also create another bug report in Lazarus, since syntax highlighting shouldn't colorize code after "end." as if it were valid.

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: Weirdest Error resolution ever!
« Reply #6 on: July 30, 2025, 04:09:05 pm »
There should be a warning and there is AFAIK: "code after end. is ignored." If it isn't, it is a regression.
That is - if that's the case - not a lazarus bug, but a compiler bug.
« Last Edit: July 30, 2025, 04:18:00 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

n7800

  • Hero Member
  • *****
  • Posts: 587
  • Lazarus IDE contributor
    • GitLab profile
Re: Weirdest Error resolution ever!
« Reply #7 on: July 30, 2025, 05:08:08 pm »
There should be a warning and there is AFAIK: "code after end. is ignored." If it isn't, it is a regression.

Compiling the test project does not show any messages (I copied the original messages from the IDE, not filtered).

I searched for keywords ("ignor", "after") in the FPC (3.2.2 and trunk) message file, and did not find anything there.

Maybe this warning exists in Delphi?

That is - if that's the case - not a lazarus bug, but a compiler bug.

I know, about Lazarus, I was talking about the syntax highlighting issue, which should not highlight the code after "end.", since it is ignored by the compiler. This is a separate issue.

EganSolo

  • Sr. Member
  • ****
  • Posts: 394
Re: Weirdest Error resolution ever!
« Reply #8 on: July 31, 2025, 05:15:00 am »
My bad, the actual program is far more involved, and I tried to replicate it and failed. I should have compiled that example before posting it. Lesson learned.

The issue was as I stated. It popped up in a program that I've compiled a few hundred times without a hitch.

What's really weird, though, is that after the program compiled, I moved the property declaration back to its initial location and the program compiled without a hitch.

I'll close this issue and hope not to have to open it again. It was weird


n7800

  • Hero Member
  • *****
  • Posts: 587
  • Lazarus IDE contributor
    • GitLab profile
Re: Weirdest Error resolution ever!
« Reply #9 on: August 01, 2025, 02:29:40 am »
I should have compiled that example before posting it. Lesson learned.

Got it. We're all too lazy to test code before posting a comment, but it needs to be done ))

But of course I still created bug reports if anyone is interested (#41352, #41783).

The issue was as I stated. It popped up in a program that I've compiled a few hundred times without a hitch.

What's really weird, though, is that after the program compiled, I moved the property declaration back to its initial location and the program compiled without a hitch.

In fact, creating circular dependencies is always a dead end. And creating mutual dependencies is especially bad.

As far as I know, rebuilding can have problems in such cases, so try a clean build. To do this, call "Menu > Run > Clean up and Build". Mark the project files, packages are optional (although it wouldn't hurt).

 

TinyPortal © 2005-2018