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.
| @@ -499,12 +499,12 @@ | ||
| 499 | 499 | A module found in two directories was taken from the first, silently, |
| 500 | 500 | by every module that imports it: a program's `C.raz` stood in for the |
| 501 | 501 | 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. | |
| 503 | 503 | |
| 504 | 504 | - [x] 50. The report, version 2026-10.5: one module to a name, the |
| 505 | 505 | link that chooses a file, libraries' prefixes (section 3), for |
| 506 | 506 | 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 | |
| 508 | 508 | has found a module's file, and an error names the files of each |
| 509 | 509 | other directory that has the module, unless the file found is a |
| 510 | 510 | symbolic link to one of them (compared by device and inode). A |
| @@ -13,7 +13,7 @@ | ||
| 13 | 13 | | `src/sym.pas` | 583 | 483 | Types, objects, scopes, layout | |
| 14 | 14 | | `src/x64.pas` | 691 | 554 | Code buffer, chunks, fixups, places, instruction encoder | |
| 15 | 15 | | `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 | | |
| 17 | 17 | | `src/link.pas` | 180 | 138 | Tree shaking, layout, patching, the places | |
| 18 | 18 | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | |
| 19 | 19 | | `src/raz.pas` | 227 | 178 | The command line, the entry points | |
| @@ -328,9 +328,10 @@ | ||
| 328 | 328 | |
| 329 | 329 | Last, modules, at the end of the file. `CompileFile` reads one module. |
| 330 | 330 | `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 | |
| 334 | 335 | is compiled once per distinct list of arguments: `ParseArgs` reads |
| 335 | 336 | them at the import, `Params` binds them to the module's parameters, and |
| 336 | 337 | `IsModule` decides whether an instance already exists. |
| @@ -2171,14 +2171,21 @@ | ||
| 2171 | 2171 | CompareIface(m, CompileFile(dir + '/' + others[i], n, args, true)); |
| 2172 | 2172 | end; |
| 2173 | 2173 | |
| 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 | + | |
| 2174 | 2184 | function ModuleFile(n: longint; var dir, f: shortstring): boolean; |
| 2175 | -const Suffixes: array[0..3] of string[16] = ('.' + Target, '.' + TargetOS, '.' + TargetArch, ''); | |
| 2176 | 2185 | var i, j: longint; |
| 2177 | 2186 | begin |
| 2178 | 2187 | 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); | |
| 2182 | 2189 | for j := 0 to 3 do begin |
| 2183 | 2190 | f := NameOf(n) + Suffixes[j] + '.raz'; |
| 2184 | 2191 | if Exists(dir + '/' + f) then exit(true); |
| @@ -2187,6 +2194,40 @@ | ||
| 2187 | 2194 | ModuleFile := false; |
| 2188 | 2195 | end; |
| 2189 | 2196 | |
| 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 | + | |
| 2190 | 2231 | function ImportModule(name: longint; const args: TArgs): PModule; |
| 2191 | 2232 | var m: PModule; i: longint; dir, f: shortstring; |
| 2192 | 2233 | begin |
| @@ -2195,6 +2236,7 @@ | ||
| 2195 | 2236 | for i := 0 to numPending - 1 do |
| 2196 | 2237 | if IsModule(pending[i], name, args) then MarkAt(args.at, NameOf(name) + ' imports itself'); |
| 2197 | 2238 | if not ModuleFile(name, dir, f) then MarkAt(args.at, 'can''t find module ' + NameOf(name)); |
| 2239 | + CheckOneModule(name, dir, f, args.at); | |
| 2198 | 2240 | m := CompileFile(dir + '/' + f, name, args, false); |
| 2199 | 2241 | CheckSiblings(dir, f, name, m, args); |
| 2200 | 2242 | ImportModule := m; |
| @@ -0,0 +1 @@ | ||
| 1 | +-I test/err/mods/clash |
| @@ -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 |
| @@ -0,0 +1,5 @@ | ||
| 1 | +module Clash; | |
| 2 | +import Out; | |
| 3 | +begin | |
| 4 | + Out.str("hi\n"); | |
| 5 | +end |
| @@ -0,0 +1 @@ | ||
| 1 | +module C; |