raz / commit cc7c4ad5

commit
cc7c4ad5a6395fdb084bf26c1b561f2c6acd2400
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
fdcb312c
One module to a name: a module in two directories is an error

Report 2026-10.5. CheckOneModule looks on through the later
directories once ImportModule has found a module's file, and names
every other file of the module unless the one found is a link to it
(same device and inode). test/err/Clash has a C.raz of its own;
test/libc and ravite-c still build through their link to lib/Sys.raz.
Report stays at 2026-10.0 until step 49b is done.

patch · log

7 files changed, +65 -10

PLAN.md (history) modified +2 -2

@@ -499,12 +499,12 @@
499499 A module found in two directories was taken from the first, silently,
500500 by every module that imports it: a program's `C.raz` stood in for the
501501 library's `C` inside `Rt`, and the compiler said only "Rt imports
502-itself". Not started: each step waits for the owner's word.
502+itself". Done.
503503
504504 - [x] 50. The report, version 2026-10.5: one module to a name, the
505505 link that chooses a file, libraries' prefixes (section 3), for
506506 review before any code.
507-- [ ] 51. `ModuleFile` goes on through the later directories once it
507+- [x] 51. `ModuleFile` goes on through the later directories once it
508508 has found a module's file, and an error names the files of each
509509 other directory that has the module, unless the file found is a
510510 symbolic link to one of them (compared by device and inode). A

doc/compiler.md (history) modified +5 -4

@@ -13,7 +13,7 @@
1313 | `src/sym.pas` | 583 | 483 | Types, objects, scopes, layout |
1414 | `src/x64.pas` | 691 | 554 | Code buffer, chunks, fixups, places, instruction encoder |
1515 | `src/gen.pas` | 1,538 | 1,332 | Items: loading, storing, copying, frames, calls |
16-| `src/parse.pas` | 2,240 | 2,027 | Declarations, statements, expressions, built-ins, recovery, warnings |
16+| `src/parse.pas` | 2,282 | 2,062 | Declarations, statements, expressions, built-ins, recovery, warnings |
1717 | `src/link.pas` | 180 | 138 | Tree shaking, layout, patching, the places |
1818 | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers |
1919 | `src/raz.pas` | 227 | 178 | The command line, the entry points |
@@ -328,9 +328,10 @@
328328
329329 Last, modules, at the end of the file. `CompileFile` reads one module.
330330 `ImportModule` finds a module's file, trying the target's names first,
331-and compiles it if it isn't yet; `CheckSiblings` then reads the files
332-for other targets beside it, declarations only, and `CompareIface`
333-holds them to the same exports by their fingerprints. A generic module
331+and compiles it if it isn't yet. `CheckOneModule` first makes sure no
332+later directory has the module too, unless the file is a link to one
333+of its files there; `CheckSiblings` then reads the files for other
334+targets beside it, declarations only, and `CompareIface` holds them to the same exports by their fingerprints. A generic module
334335 is compiled once per distinct list of arguments: `ParseArgs` reads
335336 them at the import, `Params` binds them to the module's parameters, and
336337 `IsModule` decides whether an instance already exists.

src/parse.pas modified +46 -4

@@ -2171,14 +2171,21 @@
21712171 CompareIface(m, CompileFile(dir + '/' + others[i], n, args, true));
21722172 end;
21732173
2174+const Suffixes: array[0..3] of string[16] = ('.' + Target, '.' + TargetOS, '.' + TargetArch, '');
2175+
2176+{ The directories searched for modules, in order, from -1 to numInc }
2177+function SearchDir(i: longint): shortstring;
2178+begin
2179+ if i = -1 then SearchDir := mainDir
2180+ else if i = numInc then SearchDir := libDir
2181+ else SearchDir := incDirs[i];
2182+end;
2183+
21742184 function ModuleFile(n: longint; var dir, f: shortstring): boolean;
2175-const Suffixes: array[0..3] of string[16] = ('.' + Target, '.' + TargetOS, '.' + TargetArch, '');
21762185 var i, j: longint;
21772186 begin
21782187 for i := -1 to numInc do begin
2179- if i = -1 then dir := mainDir
2180- else if i = numInc then dir := libDir
2181- else dir := incDirs[i];
2188+ dir := SearchDir(i);
21822189 for j := 0 to 3 do begin
21832190 f := NameOf(n) + Suffixes[j] + '.raz';
21842191 if Exists(dir + '/' + f) then exit(true);
@@ -2187,6 +2194,40 @@
21872194 ModuleFile := false;
21882195 end;
21892196
2197+{ A program has one module of each name: module n, found as dir/f,
2198+ must have no files in a later directory, unless dir/f is a link to
2199+ one of them, which chooses it (report, section 3) }
2200+procedure CheckOneModule(n: longint; const dir, f: shortstring; const at: TPos);
2201+var i, j: longint; past, chosen: boolean; g, files, others: shortstring;
2202+ st, mine: Stat;
2203+begin
2204+ g := dir + '/' + f + #0;
2205+ FpStat(@g[1], mine);
2206+ others := '';
2207+ past := false;
2208+ for i := -1 to numInc do begin
2209+ if past then begin
2210+ files := '';
2211+ chosen := false;
2212+ for j := 0 to 3 do begin
2213+ g := SearchDir(i) + '/' + NameOf(n) + Suffixes[j] + '.raz';
2214+ if Exists(g) then begin
2215+ files := files + ' and ' + ShownPath(g);
2216+ g := g + #0;
2217+ FpStat(@g[1], st);
2218+ chosen := chosen or ((st.st_dev = mine.st_dev) and (st.st_ino = mine.st_ino));
2219+ end;
2220+ end;
2221+ if not chosen then others := others + files;
2222+ end;
2223+ past := past or (SearchDir(i) = dir);
2224+ end;
2225+ if others = '' then exit;
2226+ Error(at, 'module ' + NameOf(n) + ' is in ' + ShownPath(dir + '/' + f) + others);
2227+ Help('a program has one module of each name; a symbolic link to one of these files chooses it');
2228+ Fail;
2229+end;
2230+
21902231 function ImportModule(name: longint; const args: TArgs): PModule;
21912232 var m: PModule; i: longint; dir, f: shortstring;
21922233 begin
@@ -2195,6 +2236,7 @@
21952236 for i := 0 to numPending - 1 do
21962237 if IsModule(pending[i], name, args) then MarkAt(args.at, NameOf(name) + ' imports itself');
21972238 if not ModuleFile(name, dir, f) then MarkAt(args.at, 'can''t find module ' + NameOf(name));
2239+ CheckOneModule(name, dir, f, args.at);
21982240 m := CompileFile(dir + '/' + f, name, args, false);
21992241 CheckSiblings(dir, f, name, m, args);
22002242 ImportModule := m;

test/err/Clash.args (history) added +1 -0

@@ -0,0 +1 @@
1+-I test/err/mods/clash

test/err/Clash.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+lib/Sys.raz:12:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz
2+ 12 | import C;
3+ | ^
4+help: a program has one module of each name; a symbolic link to one of these files chooses it
5+raz: 1 error

test/err/Clash.raz (history) added +5 -0

@@ -0,0 +1,5 @@
1+module Clash;
2+import Out;
3+begin
4+ Out.str("hi\n");
5+end

test/err/mods/clash/C.raz (history) added +1 -0

@@ -0,0 +1 @@
1+module C;