Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to exploit a compiler bug?

Tags:

delphi

Recently I found some odd-looking (to me) Delphi code and I have isolated it to a separate small project. Here is what I discovered. Unit1 compiles with no errors. Unit2 (which I provide for comparison) does not. The difference is in the way that Classes is used.

unit Unit1;

interface

uses Classes; // difference here

type TThread = class(Classes.TThread)
public
   property Terminated;
end;

implementation

end.

Unit2 does not compile. Various errors are produced.

unit Unit2;

interface

uses System.Classes; // difference here

type TThread = class(Classes.TThread)
public
  property Terminated;
end;

implementation

end.

[dcc32 Error] Unit1.pas(7): E2003 Undeclared identifier: 'Classes'
[dcc32 Error] Unit1.pas(7): E2029 ',' or ':' expected but ')' found
[dcc32 Error] Unit1.pas(9): E2147 Property 'Terminated' does not exist in base class

So my concern is that this project is exploiting a compiler bug to achieve it's goals. The compiler bug might be fixed in a later release, and then the code won't work anymore.

like image 488
Freddie Bell Avatar asked Apr 19 '26 15:04

Freddie Bell


2 Answers

There is no compiler bug that makes Unit1 compile. It compiles because in the project setting the entry for Unit Scope Names contains at least the item System, which is used to resolve the reference to Classes in the full name System.Classes. As the uses contains Classes, the reference to Classes.TThread also succeeds.

In Unit2 the uses contains System.Classes. Therefore the reference Classes.TThread cannot be resolved anymore. Change it to System.Classes.TThread and it works.

like image 197
Uwe Raabe Avatar answered Apr 21 '26 07:04

Uwe Raabe


If you uses System.Classes you must also use System.Classes when referring to the classes unit in the code as shown below.

unit Unit2;

interface

uses System.Classes; // difference here

type TThread = class(System.Classes.TThread)
public
  property Terminated;
end;

implementation

end.
like image 38
Jan Lauridsen Avatar answered Apr 21 '26 06:04

Jan Lauridsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!