raz / commit 070e473d

commit
070e473d0d4257e7288ba74a5873905d02d9546a
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
302c3acd
An error in a library from -I or RAZ_PATH says the raz its RAZ-VERSION names

patch · log

11 files changed, +94 -8

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

@@ -16,17 +16,17 @@
1616
1717 | File | Lines | Code | What |
1818 |---|---|---|---|
19-| `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts |
19+| `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts |
2020 | `src/Scan.raz` | 1,148 | 949 | Symbols, literals, comments, positions |
2121 | `src/Sym.raz` | 715 | 573 | Types, objects, scopes, layout |
2222 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2323 | `src/X64.raz` | 1,584 | 1,304 | The instruction encoder, jumps within a procedure, the assembler |
2424 | `src/Gen.raz` | 2,592 | 1,843 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2525 | `src/Item.raz` | 2,223 | 1,785 | Items: types, constants, variables, operators, conversions, calls |
26-| `src/Parse.raz` | 4,266 | 3,689 | Declarations, statements, expressions, built-ins, recovery, warnings |
26+| `src/Parse.raz` | 4,323 | 3,735 | Declarations, statements, expressions, built-ins, recovery, warnings |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
29-| `src/Raz.raz` | 417 | 332 | The command line |
29+| `src/Raz.raz` | 418 | 333 | The command line |
3030
3131 Every file opens with a comment that says what it is, what it depends
3232 on and what it leaves out. Read those first; they are the map.
@@ -895,7 +895,9 @@
895895 follows from another (see Parse.raz). The line and column are worked out
896896 only when a message is printed (`locate`, `chars`), so positions cost
897897 nothing while compiling. `addFile` keeps every source file the scanner
898-opens, so a note can show a line of any of them. `finish` prints the
898+opens, so a note can show a line of any of them. A file may carry one
899+line to tell (`setOrigin`), shown once under the first message about
900+it: the parser gives a library's. `finish` prints the
899901 count of errors and warnings and stops if there were errors; `fatal` is
900902 for errors with no place in the source. A message is made by `say` from
901903 a format and its arguments, and kept for good, since a message is made
@@ -1537,7 +1539,11 @@
15371539
15381540 Last, modules, at the end of the file. `compileFile` reads one module.
15391541 `importModule` finds a module's file, trying the target's names first,
1540-and compiles it if it isn't yet. `checkOneModule` first makes sure no
1542+and compiles it if it isn't yet. A module found in an `-I` or
1543+`RAZ_PATH` directory that holds a file `RAZ-VERSION` gets a line
1544+(`libraryNote`, `majorMinor`) saying the raz its library was written
1545+for and this one's, "0.21" for `0.21.0-5-g38d29a8`; the compiler only
1546+reads the file. `checkOneModule` first makes sure no
15411547 later directory has the module too, unless the file is a link to one
15421548 of its files there; `checkSiblings` then reads the files for other
15431549 targets beside it, declarations only, and `compareIface` holds them

doc/raz.1 (history) modified +9 -0

@@ -89,6 +89,15 @@
8989 .Ar dir
9090 too, after the main module's own directory and before the library.
9191 It may be given up to 16 times.
92+An error in a module found in such a directory, or in one of
93+.Ev RAZ_PATH ,
94+names that directory in its path; and if the directory holds a file
95+.Pa RAZ-VERSION ,
96+of one line such as
97+.Ql 0.21 ,
98+a second line says the raz the library was written for and this one:
99+.Ql (a library written for raz 0.18; this is raz 0.21) .
100+The compiler only reads the file; it never refuses to build for it.
92101 .It Fl Werror
93102 A warning counts as an error.
94103 .It Fl S

src/Diag.raz (history) modified +13 -1

@@ -26,6 +26,7 @@
2626 path: []U8;
2727 buf: ^U8;
2828 len: INT;
29+ origin: []U8; (* a line told once, under the first message about the file; "" for none *)
2930 end;
3031
3132 const
@@ -106,11 +107,18 @@
106107 end;
107108 p, ok := Str.copy(Heap.heap, shownPath(path));
108109 if not ok then fatal("out of memory"); end;
109- files[numFiles] := {path: p, buf: buf, len: len};
110+ files[numFiles] := {path: p, buf: buf, len: len, origin: ""};
110111 INC(numFiles);
111112 return numFiles - 1;
112113 end;
113114
115+(* Gives file f a line to tell once, under the first message about it:
116+ where a library came from and the raz it was written for *)
117+proc setOrigin*(f: INT; text: []U8);
118+begin
119+ files[f].origin := text;
120+end;
121+
114122 proc pathOf*(f: INT): []U8;
115123 begin
116124 return files[f].path;
@@ -251,6 +259,10 @@
251259 put(" ");
252260 paint(Bold, msg);
253261 putc('\n');
262+ if LEN(f^.origin) > 0 then
263+ put(" ("); put(f^.origin); put(")\n");
264+ f^.origin := "";
265+ end;
254266 stop := start;
255267 while (stop < f^.len) and (buf[stop] # 10) do INC(stop); end;
256268 lo := start;

src/Parse.raz (history) modified +44 -0

@@ -76,6 +76,8 @@
7676 incDirs*: [16][]U8;
7777 numInc*: INT;
7878 userDirs: [][]U8; (* RAZ_PATH, or HOME/.raz/lib; set by the driver *)
79+ thisVersion: []U8; (* the compiler's version; set by the driver *)
80+ originNote: []U8; (* for the module about to be opened, see libraryNote *)
7981 firstModule*, lastModule*: PModule; (* in the order they were finished *)
8082
8183 checkMode: BOOL; (* reading a file only for its interface *)
@@ -148,6 +150,12 @@
148150 userDirs := dirs;
149151 end;
150152
153+(* The driver's version of the compiler, which a library's note names *)
154+proc setVersion*(v: []U8);
155+begin
156+ thisVersion := v;
157+end;
158+
151159 proc nameOf(n: INT): []U8;
152160 begin
153161 return Scan.nameOf(n);
@@ -3861,6 +3869,8 @@
38613869 Chunk.setCurFile(internName(path));
38623870 why := Scan.open(path);
38633871 if LEN(why) # 0 then Diag.fatal(say2("can't read {}: {}", path, why)); end;
3872+ Diag.setOrigin(Scan.tokPos().f, originNote);
3873+ originNote := "";
38643874 NEW(m, Heap.heap);
38653875 m^.name := n;
38663876 m^.sname := n;
@@ -4069,6 +4079,39 @@
40694079 return nil;
40704080 end;
40714081
4082+(* The compiler's version as a library's RAZ-VERSION would say it:
4083+ "0.21" for "0.21.0-5-g38d29a8"; a name with fewer dots stays whole *)
4084+proc majorMinor(v: []U8): []U8;
4085+var i, dots: INT;
4086+begin
4087+ dots := 0;
4088+ for i := 0 to LEN(v) - 1 do
4089+ if v[i] = '.' then
4090+ INC(dots);
4091+ if dots = 2 then return v[0..i]; end;
4092+ end;
4093+ end;
4094+ return v;
4095+end;
4096+
4097+(* The line told with the first error in a module found in dir, an -I or
4098+ RAZ_PATH directory: if dir holds a file RAZ-VERSION, the raz its
4099+ library was written for. The file is only read; a library with none,
4100+ or a module of the program's own directory or of lib, has no line. *)
4101+proc libraryNote(dir: []U8): []U8;
4102+var fd, n, i: INT; b: [32]U8;
4103+begin
4104+ if (dir = mainDir) or (dir = libDir) then return ""; end;
4105+ fd := Sys.openRead(say1("{}/RAZ-VERSION", dir));
4106+ if fd < 0 then return ""; end;
4107+ n := Sys.read(fd, b[0..32]);
4108+ Sys.close(fd);
4109+ i := 0;
4110+ while (i < n) and (b[i] # 10) and (b[i] # 32) do INC(i); end;
4111+ if i = 0 then return ""; end;
4112+ return say2("a library written for raz {}; this is raz {}", b[0..i], majorMinor(thisVersion));
4113+end;
4114+
40724115 (* The directories searched for modules: main, -I, user libraries, lib *)
40734116 proc searchDir(i: INT): []U8;
40744117 begin
@@ -4150,6 +4193,7 @@
41504193 return nil, Scan.Failed;
41514194 end;
41524195 try checkOneModule(name, dir, f, args.at);
4196+ originNote := libraryNote(dir);
41534197 m := try compileFile(say2("{}/{}", dir, f), name, args, false);
41544198 try checkSiblings(dir, f, name, m, args);
41554199 return m, nil;

src/Raz.raz (history) modified +1 -0

@@ -320,6 +320,7 @@
320320 if errno # 0 then dir := Files.arg(0); end;
321321 i := LEN(dir);
322322 while (i > 0) and (dir[i - 1] # '/') do DEC(i); end;
323+ Parse.setVersion(Version.Text);
323324 if i = 0 then Parse.setLibDir("lib"); else Parse.setLibDir(say1("{}lib", dir[0..i])); end;
324325 userLibraries();
325326 if LEN(outPath) = 0 then

test/check.sh (history) modified +3 -2

@@ -12,7 +12,8 @@
1212 # each address in it, 0x and hex digits, written 0x?
1313 # A program without X.expect is compiled, not run. test/libc puts
1414 # lib/Sys.raz, over the C library, before the system call one.
15-# Programs in test/err must not compile, and must print X.err; there
15+# Programs in test/err must not compile, and must print X.err (with
16+# the compiler's own version in a library's note written as N); there
1617 # X.args holds the compiler's options. The examples, test/libc, the
1718 # tools and the library's tests are compiled with -Werror, so warnings
1819 # fail them. Then the version, tools/Doc.raz, the report's examples,
@@ -99,7 +100,7 @@
99100 if $raz $flags -I test/err/mods -I test/run/mods -o $out/$name $f 2>$out/$name.cerr; then
100101 echo "FAIL $f: compiles"
101102 fail=$((fail + 1))
102- elif ! cmp -s test/err/$name.err $out/$name.cerr; then
103+ elif ! sed 's/ this is raz [^)]*)/ this is raz N)/' $out/$name.cerr | cmp -s test/err/$name.err -; then
103104 echo "FAIL $f: says"; diff test/err/$name.err $out/$name.cerr
104105 fail=$((fail + 1))
105106 else

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

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

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

@@ -0,0 +1,5 @@
1+test/err/mods/oldlib/OldLib.raz:2:17: error: an integer constant and a string are different types
2+ (a library written for raz 0.18; this is raz N)
3+ 2 | const Answer* = 1 + "a";
4+ | ^^^^^^^
5+raz: 1 error

test/err/Stale.raz (history) added +3 -0

@@ -0,0 +1,3 @@
1+module Stale;
2+import OldLib;
3+end Stale.

test/err/mods/oldlib/OldLib.raz (history) added +3 -0

@@ -0,0 +1,3 @@
1+module OldLib;
2+const Answer* = 1 + "a";
3+end OldLib.

test/err/mods/oldlib/RAZ-VERSION (history) added +1 -0

@@ -0,0 +1 @@
1+0.18