uses in 'filename'

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

uses in 'filename'

Mattias Gaertner
Hi,

FPC allows to use two different units with the same name:

uses foo in 'unit1.pas', bar in 'sub/unit1.pas';

Which might fail with linking errors.

Is this a feature or a bug?

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: uses in 'filename'

Michael Van Canneyt


On Thu, 22 Feb 2018, Mattias Gaertner wrote:

> Hi,
>
> FPC allows to use two different units with the same name:
>
> uses foo in 'unit1.pas', bar in 'sub/unit1.pas';
>
> Which might fail with linking errors.
>
> Is this a feature or a bug?

A feature.

First of all, normally it won't work either way, because you must use "unit foo;" and "unit bar;" in the
files. A quick test confirms this:

home:~/source/testsub> fpc a.pp
unit1.pas(1,9) Error: Illegal unit name: foo (expecting FOO)
unit1.pas(7,1) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted

Where

a.pp:
-----
uses foo in 'unit1.pas', bar in 'sub/unit1.pas';

begin
   dofoo;
   dobar;
end.

Unit1.pp:
---------

unit foo;

interface

procedure dofoo;

implementation

procedure dofoo;

begin
   writeln('foo');
end;

end.


sub/unit1.pp:
-------------
unit bar;

interface

procedure dobar;

implementation

procedure dobar;

begin
   writeln('bar');
end;

end.

But if you tell the compiler to ignore the unit filenames (-Un) then you get

home:~/source/testsub> fpc -Un a.pp
home:~/source/testsub> ./a
foo
bar

And then it works perfectly.

All assembler is prefixed using the declared
unit name:
         .file "unit1.pas"
# Begin asmlist al_procedures

.section .text.n_foo_$$_dofoo

etc.


Michael.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: uses in 'filename'

Mattias Gaertner
On Thu, 22 Feb 2018 12:48:21 +0100 (CET)
Michael Van Canneyt <[hidden email]> wrote:

>[...]
> First of all, normally it won't work either way, because you must use "unit foo;" and "unit bar;" in the files.

No, I can use normal units. This works here:

---test1.pas---
uses foo in 'unit1.pas', unit1, bar in 'sub/unit1.pas';

Begin
  i:=3; // from unit1.pas
  foo.i:=foo.i+1; // from unit1.pas
  bar.j:=27; // from sub/unit1.pas
  writeln(foo.i,' ',unit1.i,' ',bar.j);
end.
---

---unit1.pas---
unit unit1;

interface

uses foo in 'sub/unit1.pas'; // unit1 uses another unit1

var i: integer;

implementation
end.
---

---sub/unit1.pas---
unit unit1;

interface

var j: integer;

implementation
end.
---

Mattias

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: uses in 'filename'

Michael Van Canneyt


On Thu, 22 Feb 2018, Mattias Gaertner wrote:

> On Thu, 22 Feb 2018 12:48:21 +0100 (CET)
> Michael Van Canneyt <[hidden email]> wrote:
>
>> [...]
>> First of all, normally it won't work either way, because you must use "unit foo;" and "unit bar;" in the files.
>
> No, I can use normal units. This works here:

Strange. I get the error I showed if I try that.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: uses in 'filename'

wkitty42
On 02/22/2018 09:49 AM, Michael Van Canneyt wrote:
> On Thu, 22 Feb 2018, Mattias Gaertner wrote:
>> On Thu, 22 Feb 2018 12:48:21 +0100 (CET)
>> Michael Van Canneyt <[hidden email]> wrote:
>>> First of all, normally it won't work either way, because you must use
>>> "unit foo;" and "unit bar;" in the files. >>
>> No, I can use normal units. This works here:
>
> Strange. I get the error I showed if I try that.


both of you are using the same mode? delphi or whatever? neither set of sample
code shows a specific mode to use...


--
  NOTE: No off-list assistance is given without prior approval.
        *Please keep mailing list traffic on the list unless*
        *a signed and pre-paid contract is in effect with us.*
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: uses in 'filename'

Mattias Gaertner
On Thu, 22 Feb 2018 10:41:29 -0500
[hidden email] wrote:

> On 02/22/2018 09:49 AM, Michael Van Canneyt wrote:
> > On Thu, 22 Feb 2018, Mattias Gaertner wrote:  
> >> On Thu, 22 Feb 2018 12:48:21 +0100 (CET)
> >> Michael Van Canneyt <[hidden email]> wrote:  
> >>> First of all, normally it won't work either way, because you must use
> >>> "unit foo;" and "unit bar;" in the files. >>  
> >> No, I can use normal units. This works here:  
> >
> > Strange. I get the error I showed if I try that.  
>
>
> both of you are using the same mode? delphi or whatever? neither set of sample
> code shows a specific mode to use...

It works here with both -Mdelphi and -Mobjfpc

Free Pascal Compiler version 3.0.4

Compiler date      : 2017/08/05
Compiler CPU target: x86_64

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: uses in 'filename'

Mattias Gaertner
On Thu, 22 Feb 2018 17:04:25 +0100
Mattias Gaertner <[hidden email]> wrote:

> On Thu, 22 Feb 2018 10:41:29 -0500
> [hidden email] wrote:
>
> > On 02/22/2018 09:49 AM, Michael Van Canneyt wrote:  
> > > On Thu, 22 Feb 2018, Mattias Gaertner wrote:    
> > >> On Thu, 22 Feb 2018 12:48:21 +0100 (CET)
> > >> Michael Van Canneyt <[hidden email]> wrote:    
> > >>> First of all, normally it won't work either way, because you must use
> > >>> "unit foo;" and "unit bar;" in the files. >>    
> > >> No, I can use normal units. This works here:    
> > >
> > > Strange. I get the error I showed if I try that.    
> >
> >
> > both of you are using the same mode? delphi or whatever? neither set of sample
> > code shows a specific mode to use...  
>
> It works here with both -Mdelphi and -Mobjfpc
>
> Free Pascal Compiler version 3.0.4
>
> Compiler date      : 2017/08/05
> Compiler CPU target: x86_64

And it works with

Free Pascal Compiler version 3.1.1

Compiler date      : 2018/01/15
Compiler CPU target: x86_64


Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: uses in 'filename'

Mattias Gaertner
In reply to this post by Michael Van Canneyt
On Thu, 22 Feb 2018 12:48:21 +0100 (CET)
Michael Van Canneyt <[hidden email]> wrote:

>[...]
> home:~/source/testsub> fpc a.pp  
> unit1.pas(1,9) Error: Illegal unit name: foo (expecting FOO)
> unit1.pas(7,1) Fatal: There were 1 errors compiling module, stopping
> Fatal: Compilation aborted
>
> Where
>
> a.pp:
> -----
> uses foo in 'unit1.pas', bar in 'sub/unit1.pas';
>
> begin
>    dofoo;
>    dobar;
> end.
>
> Unit1.pp:

This does not fit: Unit1.pp and unit1.pas.


Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal