From 070e473d0d4257e7288ba74a5873905d02d9546a Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:23:35 +0100 Subject: [PATCH] An error in a library from -I or RAZ_PATH says the raz its RAZ-VERSION names --- diff --git a/doc/compiler.md b/doc/compiler.md index 0c6d872..f40b981 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -16,17 +16,17 @@ | File | Lines | Code | What | |---|---|---|---| -| `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts | +| `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts | | `src/Scan.raz` | 1,148 | 949 | Symbols, literals, comments, positions | | `src/Sym.raz` | 715 | 573 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,584 | 1,304 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,592 | 1,843 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,223 | 1,785 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,266 | 3,689 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,323 | 3,735 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | -| `src/Raz.raz` | 417 | 332 | The command line | +| `src/Raz.raz` | 418 | 333 | The command line | Every file opens with a comment that says what it is, what it depends on and what it leaves out. Read those first; they are the map. @@ -895,7 +895,9 @@ follows from another (see Parse.raz). The line and column are worked out only when a message is printed (`locate`, `chars`), so positions cost nothing while compiling. `addFile` keeps every source file the scanner -opens, so a note can show a line of any of them. `finish` prints the +opens, so a note can show a line of any of them. A file may carry one +line to tell (`setOrigin`), shown once under the first message about +it: the parser gives a library's. `finish` prints the count of errors and warnings and stops if there were errors; `fatal` is for errors with no place in the source. A message is made by `say` from a format and its arguments, and kept for good, since a message is made @@ -1537,7 +1539,11 @@ 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. `checkOneModule` first makes sure no +and compiles it if it isn't yet. A module found in an `-I` or +`RAZ_PATH` directory that holds a file `RAZ-VERSION` gets a line +(`libraryNote`, `majorMinor`) saying the raz its library was written +for and this one's, "0.21" for `0.21.0-5-g38d29a8`; the compiler only +reads the file. `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 diff --git a/doc/raz.1 b/doc/raz.1 index f90f6a3..47903c2 100644 --- a/doc/raz.1 +++ b/doc/raz.1 @@ -89,6 +89,15 @@ .Ar dir too, after the main module's own directory and before the library. It may be given up to 16 times. +An error in a module found in such a directory, or in one of +.Ev RAZ_PATH , +names that directory in its path; and if the directory holds a file +.Pa RAZ-VERSION , +of one line such as +.Ql 0.21 , +a second line says the raz the library was written for and this one: +.Ql (a library written for raz 0.18; this is raz 0.21) . +The compiler only reads the file; it never refuses to build for it. .It Fl Werror A warning counts as an error. .It Fl S diff --git a/src/Diag.raz b/src/Diag.raz index e5204ed..533e7ec 100644 --- a/src/Diag.raz +++ b/src/Diag.raz @@ -26,6 +26,7 @@ path: []U8; buf: ^U8; len: INT; + origin: []U8; (* a line told once, under the first message about the file; "" for none *) end; const @@ -106,11 +107,18 @@ end; p, ok := Str.copy(Heap.heap, shownPath(path)); if not ok then fatal("out of memory"); end; - files[numFiles] := {path: p, buf: buf, len: len}; + files[numFiles] := {path: p, buf: buf, len: len, origin: ""}; INC(numFiles); return numFiles - 1; end; +(* Gives file f a line to tell once, under the first message about it: + where a library came from and the raz it was written for *) +proc setOrigin*(f: INT; text: []U8); +begin + files[f].origin := text; +end; + proc pathOf*(f: INT): []U8; begin return files[f].path; @@ -251,6 +259,10 @@ put(" "); paint(Bold, msg); putc('\n'); + if LEN(f^.origin) > 0 then + put(" ("); put(f^.origin); put(")\n"); + f^.origin := ""; + end; stop := start; while (stop < f^.len) and (buf[stop] # 10) do INC(stop); end; lo := start; diff --git a/src/Parse.raz b/src/Parse.raz index fe856a7..1fd4736 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -76,6 +76,8 @@ incDirs*: [16][]U8; numInc*: INT; userDirs: [][]U8; (* RAZ_PATH, or HOME/.raz/lib; set by the driver *) + thisVersion: []U8; (* the compiler's version; set by the driver *) + originNote: []U8; (* for the module about to be opened, see libraryNote *) firstModule*, lastModule*: PModule; (* in the order they were finished *) checkMode: BOOL; (* reading a file only for its interface *) @@ -148,6 +150,12 @@ userDirs := dirs; end; +(* The driver's version of the compiler, which a library's note names *) +proc setVersion*(v: []U8); +begin + thisVersion := v; +end; + proc nameOf(n: INT): []U8; begin return Scan.nameOf(n); @@ -3861,6 +3869,8 @@ Chunk.setCurFile(internName(path)); why := Scan.open(path); if LEN(why) # 0 then Diag.fatal(say2("can't read {}: {}", path, why)); end; + Diag.setOrigin(Scan.tokPos().f, originNote); + originNote := ""; NEW(m, Heap.heap); m^.name := n; m^.sname := n; @@ -4069,6 +4079,39 @@ return nil; end; +(* The compiler's version as a library's RAZ-VERSION would say it: + "0.21" for "0.21.0-5-g38d29a8"; a name with fewer dots stays whole *) +proc majorMinor(v: []U8): []U8; +var i, dots: INT; +begin + dots := 0; + for i := 0 to LEN(v) - 1 do + if v[i] = '.' then + INC(dots); + if dots = 2 then return v[0..i]; end; + end; + end; + return v; +end; + +(* The line told with the first error in a module found in dir, an -I or + RAZ_PATH directory: if dir holds a file RAZ-VERSION, the raz its + library was written for. The file is only read; a library with none, + or a module of the program's own directory or of lib, has no line. *) +proc libraryNote(dir: []U8): []U8; +var fd, n, i: INT; b: [32]U8; +begin + if (dir = mainDir) or (dir = libDir) then return ""; end; + fd := Sys.openRead(say1("{}/RAZ-VERSION", dir)); + if fd < 0 then return ""; end; + n := Sys.read(fd, b[0..32]); + Sys.close(fd); + i := 0; + while (i < n) and (b[i] # 10) and (b[i] # 32) do INC(i); end; + if i = 0 then return ""; end; + return say2("a library written for raz {}; this is raz {}", b[0..i], majorMinor(thisVersion)); +end; + (* The directories searched for modules: main, -I, user libraries, lib *) proc searchDir(i: INT): []U8; begin @@ -4150,6 +4193,7 @@ return nil, Scan.Failed; end; try checkOneModule(name, dir, f, args.at); + originNote := libraryNote(dir); m := try compileFile(say2("{}/{}", dir, f), name, args, false); try checkSiblings(dir, f, name, m, args); return m, nil; diff --git a/src/Raz.raz b/src/Raz.raz index faafc38..ae05e0c 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -320,6 +320,7 @@ if errno # 0 then dir := Files.arg(0); end; i := LEN(dir); while (i > 0) and (dir[i - 1] # '/') do DEC(i); end; + Parse.setVersion(Version.Text); if i = 0 then Parse.setLibDir("lib"); else Parse.setLibDir(say1("{}lib", dir[0..i])); end; userLibraries(); if LEN(outPath) = 0 then diff --git a/test/check.sh b/test/check.sh index 92fa484..bb72973 100755 --- a/test/check.sh +++ b/test/check.sh @@ -12,7 +12,8 @@ # each address in it, 0x and hex digits, written 0x? # A program without X.expect is compiled, not run. test/libc puts # lib/Sys.raz, over the C library, before the system call one. -# Programs in test/err must not compile, and must print X.err; there +# Programs in test/err must not compile, and must print X.err (with +# the compiler's own version in a library's note written as N); there # X.args holds the compiler's options. The examples, test/libc, the # tools and the library's tests are compiled with -Werror, so warnings # fail them. Then the version, tools/Doc.raz, the report's examples, @@ -99,7 +100,7 @@ if $raz $flags -I test/err/mods -I test/run/mods -o $out/$name $f 2>$out/$name.cerr; then echo "FAIL $f: compiles" fail=$((fail + 1)) - elif ! cmp -s test/err/$name.err $out/$name.cerr; then + elif ! sed 's/ this is raz [^)]*)/ this is raz N)/' $out/$name.cerr | cmp -s test/err/$name.err -; then echo "FAIL $f: says"; diff test/err/$name.err $out/$name.cerr fail=$((fail + 1)) else diff --git a/test/err/Stale.args b/test/err/Stale.args new file mode 100644 index 0000000..776968b --- /dev/null +++ b/test/err/Stale.args @@ -0,0 +1 @@ +-I test/err/mods/oldlib diff --git a/test/err/Stale.err b/test/err/Stale.err new file mode 100644 index 0000000..d6b01eb --- /dev/null +++ b/test/err/Stale.err @@ -0,0 +1,5 @@ +test/err/mods/oldlib/OldLib.raz:2:17: error: an integer constant and a string are different types + (a library written for raz 0.18; this is raz N) + 2 | const Answer* = 1 + "a"; + | ^^^^^^^ +raz: 1 error diff --git a/test/err/Stale.raz b/test/err/Stale.raz new file mode 100644 index 0000000..6d1e87a --- /dev/null +++ b/test/err/Stale.raz @@ -0,0 +1,3 @@ +module Stale; +import OldLib; +end Stale. diff --git a/test/err/mods/oldlib/OldLib.raz b/test/err/mods/oldlib/OldLib.raz new file mode 100644 index 0000000..2a00594 --- /dev/null +++ b/test/err/mods/oldlib/OldLib.raz @@ -0,0 +1,3 @@ +module OldLib; +const Answer* = 1 + "a"; +end OldLib. diff --git a/test/err/mods/oldlib/RAZ-VERSION b/test/err/mods/oldlib/RAZ-VERSION new file mode 100644 index 0000000..a4d2ace --- /dev/null +++ b/test/err/mods/oldlib/RAZ-VERSION @@ -0,0 +1 @@ +0.18