From cc7c4ad5a6395fdb084bf26c1b561f2c6acd2400 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 11:30:07 +0100 Subject: [PATCH] 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. --- diff --git a/PLAN.md b/PLAN.md index 657794c..1caf7db 100644 --- a/PLAN.md +++ b/PLAN.md @@ -499,12 +499,12 @@ A module found in two directories was taken from the first, silently, by every module that imports it: a program's `C.raz` stood in for the library's `C` inside `Rt`, and the compiler said only "Rt imports -itself". Not started: each step waits for the owner's word. +itself". Done. - [x] 50. The report, version 2026-10.5: one module to a name, the link that chooses a file, libraries' prefixes (section 3), for review before any code. -- [ ] 51. `ModuleFile` goes on through the later directories once it +- [x] 51. `ModuleFile` goes on through the later directories once it has found a module's file, and an error names the files of each other directory that has the module, unless the file found is a symbolic link to one of them (compared by device and inode). A diff --git a/doc/compiler.md b/doc/compiler.md index 0239bcd..aa9bfe6 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -13,7 +13,7 @@ | `src/sym.pas` | 583 | 483 | Types, objects, scopes, layout | | `src/x64.pas` | 691 | 554 | Code buffer, chunks, fixups, places, instruction encoder | | `src/gen.pas` | 1,538 | 1,332 | Items: loading, storing, copying, frames, calls | -| `src/parse.pas` | 2,240 | 2,027 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/parse.pas` | 2,282 | 2,062 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 180 | 138 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | | `src/raz.pas` | 227 | 178 | The command line, the entry points | @@ -328,9 +328,10 @@ Last, modules, at the end of the file. `CompileFile` reads one module. `ImportModule` finds a module's file, trying the target's names first, -and compiles it if it isn't yet; `CheckSiblings` then reads the files -for other targets beside it, declarations only, and `CompareIface` -holds them to the same exports by their fingerprints. A generic module +and compiles it if it isn't yet. `CheckOneModule` first makes sure no +later directory has the module too, unless the file is a link to one +of its files there; `CheckSiblings` then reads the files for other +targets beside it, declarations only, and `CompareIface` holds them to the same exports by their fingerprints. A generic module is compiled once per distinct list of arguments: `ParseArgs` reads them at the import, `Params` binds them to the module's parameters, and `IsModule` decides whether an instance already exists. diff --git a/src/parse.pas b/src/parse.pas index a52f184..0f2e68b 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -2171,14 +2171,21 @@ CompareIface(m, CompileFile(dir + '/' + others[i], n, args, true)); end; +const Suffixes: array[0..3] of string[16] = ('.' + Target, '.' + TargetOS, '.' + TargetArch, ''); + +{ The directories searched for modules, in order, from -1 to numInc } +function SearchDir(i: longint): shortstring; +begin + if i = -1 then SearchDir := mainDir + else if i = numInc then SearchDir := libDir + else SearchDir := incDirs[i]; +end; + function ModuleFile(n: longint; var dir, f: shortstring): boolean; -const Suffixes: array[0..3] of string[16] = ('.' + Target, '.' + TargetOS, '.' + TargetArch, ''); var i, j: longint; begin for i := -1 to numInc do begin - if i = -1 then dir := mainDir - else if i = numInc then dir := libDir - else dir := incDirs[i]; + dir := SearchDir(i); for j := 0 to 3 do begin f := NameOf(n) + Suffixes[j] + '.raz'; if Exists(dir + '/' + f) then exit(true); @@ -2187,6 +2194,40 @@ ModuleFile := false; end; +{ A program has one module of each name: module n, found as dir/f, + must have no files in a later directory, unless dir/f is a link to + one of them, which chooses it (report, section 3) } +procedure CheckOneModule(n: longint; const dir, f: shortstring; const at: TPos); +var i, j: longint; past, chosen: boolean; g, files, others: shortstring; + st, mine: Stat; +begin + g := dir + '/' + f + #0; + FpStat(@g[1], mine); + others := ''; + past := false; + for i := -1 to numInc do begin + if past then begin + files := ''; + chosen := false; + for j := 0 to 3 do begin + g := SearchDir(i) + '/' + NameOf(n) + Suffixes[j] + '.raz'; + if Exists(g) then begin + files := files + ' and ' + ShownPath(g); + g := g + #0; + FpStat(@g[1], st); + chosen := chosen or ((st.st_dev = mine.st_dev) and (st.st_ino = mine.st_ino)); + end; + end; + if not chosen then others := others + files; + end; + past := past or (SearchDir(i) = dir); + end; + if others = '' then exit; + Error(at, 'module ' + NameOf(n) + ' is in ' + ShownPath(dir + '/' + f) + others); + Help('a program has one module of each name; a symbolic link to one of these files chooses it'); + Fail; +end; + function ImportModule(name: longint; const args: TArgs): PModule; var m: PModule; i: longint; dir, f: shortstring; begin @@ -2195,6 +2236,7 @@ for i := 0 to numPending - 1 do if IsModule(pending[i], name, args) then MarkAt(args.at, NameOf(name) + ' imports itself'); if not ModuleFile(name, dir, f) then MarkAt(args.at, 'can''t find module ' + NameOf(name)); + CheckOneModule(name, dir, f, args.at); m := CompileFile(dir + '/' + f, name, args, false); CheckSiblings(dir, f, name, m, args); ImportModule := m; diff --git a/test/err/Clash.args b/test/err/Clash.args new file mode 100644 index 0000000..bbb287d --- /dev/null +++ b/test/err/Clash.args @@ -0,0 +1 @@ +-I test/err/mods/clash diff --git a/test/err/Clash.err b/test/err/Clash.err new file mode 100644 index 0000000..ce01459 --- /dev/null +++ b/test/err/Clash.err @@ -0,0 +1,5 @@ +lib/Sys.raz:12:8: error: module C is in test/err/mods/clash/C.raz and lib/C.raz + 12 | import C; + | ^ +help: a program has one module of each name; a symbolic link to one of these files chooses it +raz: 1 error diff --git a/test/err/Clash.raz b/test/err/Clash.raz new file mode 100644 index 0000000..b9e0f59 --- /dev/null +++ b/test/err/Clash.raz @@ -0,0 +1,5 @@ +module Clash; +import Out; +begin + Out.str("hi\n"); +end diff --git a/test/err/mods/clash/C.raz b/test/err/mods/clash/C.raz new file mode 100644 index 0000000..aa7446b --- /dev/null +++ b/test/err/mods/clash/C.raz @@ -0,0 +1 @@ +module C;