Notes and help: declared here, did you mean, slips from other languages
Objects keep where their names are declared (TObj.pos), for notes:
"the first one is here" for a name declared twice, "the signature is
here" for a wrong count of args, a fwd mismatch and a generic's proc,
"declared here, without a *" for what isn't exported. main's checks
point at main. Notes and help show only with a shown error; quiet
moves to diag, and scan's Fail does the recovery jump.
Did you mean: sym's Closest, by edit distance ignoring case, over the
scopes in reach, a module's exports, a record's fields. A name not
declared that is a module's file gets "import X"; elif gets elsif.
Slips: scan's Slip reports !=, !, ==, &&, || and reads on as the right
symbol, not while re-skipping after an error; help for x = 1 as a
statement and { for then/do. Tests for each in test/err.
Code: 5,545 -> 5,664 lines.
| @@ -57,7 +57,8 @@ | ||
| 57 | 57 | ravite (vim's). What they needed of the system went into `Sys` (report |
| 58 | 58 | 2026-10.2, approved): the terminal, files, directories and the |
| 59 | 59 | environment. `tools/Scan.raz` is theirs to use, as a tool, not part of |
| 60 | -the library. New work starts only on the owner's word. Keep | |
| 60 | +the library. Stage 4, diagnostics (steps 19-27), is under way: | |
| 61 | +steps 19-24 are done. New work starts only on the owner's word. Keep | |
| 61 | 62 | `doc/compiler.md`'s table of lines and its routine names true when the |
| 62 | 63 | compiler changes. Left for later at the owner's word: a rewrite of |
| 63 | 64 | var parameters. |
| @@ -249,7 +249,7 @@ | ||
| 249 | 249 | (`scan.pas`, `parse.pas` in two halves, `gen.pas`, the rest), |
| 250 | 250 | against a list of messages and their tests the main model writes |
| 251 | 251 | first; *main* reviews the wording as a whole. |
| 252 | -- [ ] 24. Notes and help. Objects remember where they are declared: | |
| 252 | +- [x] 24. Notes and help. Objects remember where they are declared: | |
| 253 | 253 | "declared here", "the first one is here", "the signature is |
| 254 | 254 | here". The checks on `main` in `raz.pas` point at |
| 255 | 255 | it, so that every error has a place (the fuzzer finds them). Did you mean: the closest name by edit distance in the |
| @@ -8,15 +8,15 @@ | ||
| 8 | 8 | |
| 9 | 9 | | File | Lines | Code | What | |
| 10 | 10 | |---|---|---|---| |
| 11 | -| `src/diag.pas` | 260 | 187 | Messages: positions, source excerpts, colour, counts | | |
| 12 | -| `src/scan.pas` | 659 | 529 | Symbols, literals, comments, positions | | |
| 13 | -| `src/sym.pas` | 526 | 437 | Types, objects, scopes, layout | | |
| 11 | +| `src/diag.pas` | 267 | 191 | Messages: positions, source excerpts, colour, counts | | |
| 12 | +| `src/scan.pas` | 687 | 553 | Symbols, literals, comments, positions | | |
| 13 | +| `src/sym.pas` | 578 | 479 | Types, objects, scopes, layout | | |
| 14 | 14 | | `src/x64.pas` | 682 | 548 | Code buffer, chunks, fixups, line notes, instruction encoder | |
| 15 | -| `src/gen.pas` | 1,512 | 1,318 | Items: loading, storing, copying, frames, calls | | |
| 16 | -| `src/parse.pas` | 2,094 | 1,908 | Declarations, statements, expressions, built-ins, recovery | | |
| 15 | +| `src/gen.pas` | 1,515 | 1,319 | Items: loading, storing, copying, frames, calls | | |
| 16 | +| `src/parse.pas` | 2,151 | 1,955 | Declarations, statements, expressions, built-ins, recovery | | |
| 17 | 17 | | `src/link.pas` | 131 | 95 | Tree shaking, layout, patching | |
| 18 | 18 | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | |
| 19 | -| `src/raz.pas` | 219 | 172 | The command line, the entry points | | |
| 19 | +| `src/raz.pas` | 220 | 173 | The command line, the entry points | | |
| 20 | 20 | |
| 21 | 21 | Every file opens with a comment that says what it is, what it depends |
| 22 | 22 | on and what it leaves out. Read those first; they are the map. |
| @@ -111,7 +111,10 @@ | ||
| 111 | 111 | Every message the compiler prints is made here. A position, `TPos`, is |
| 112 | 112 | a file and a stretch of bytes in it; `Error`, `Warning` and `Note` show |
| 113 | 113 | it as `file:line:column`, then the line of source with the stretch |
| 114 | -underlined, and `Help` adds a line of advice. The line and column are | |
| 114 | +underlined, and `Help` adds a line of advice. Notes and help are shown | |
| 115 | +only with an error that was: not one at the place of the last, and | |
| 116 | +not one under `quiet`, which the parser sets when an error follows | |
| 117 | +from another (see parse.pas). The line and column are | |
| 115 | 118 | worked out only when a message is printed (`Locate`, `Chars`), so |
| 116 | 119 | positions cost nothing while compiling. `AddFile` keeps every source |
| 117 | 120 | file the scanner opens, so a note can show a line of any of them. |
| @@ -146,13 +149,16 @@ | ||
| 146 | 149 | through `Mark`, at the current symbol, `MarkAt`, at a position, or |
| 147 | 150 | `MarkHelp`, which adds a line of help; each reports the error |
| 148 | 151 | (`diag.Error`) and jumps to `recovery`, the parser's place to go on |
| 149 | -from (see parse.pas). A message names what it found: `Found` describes | |
| 152 | +from (see parse.pas), through `Fail`. An error with notes is reported | |
| 153 | +with `Error` and `Note`, then `Fail`. A message names what it found: `Found` describes | |
| 150 | 154 | the current symbol and `SymText` spells an operator or reserved word, |
| 151 | 155 | so that the parser's `Expect` and `Expected` say `expected ";", found |
| 152 | 156 | "end"`. Errors in the text itself stop the |
| 153 | 157 | compiler (`MarkBytes`, `LexError`): past a string left open, the |
| 154 | -symbols can't be trusted. There is no | |
| 155 | -recovery: the first error ends the compilation. `SaveScan` and `RestoreScan` keep | |
| 158 | +symbols can't be trusted. A slip from another language, such as `!=` | |
| 159 | +for `#`, is reported by `Slip`, which reads on as if the right symbol | |
| 160 | +were written; not while the parser is `skipping` what it read once | |
| 161 | +before an error. `SaveScan` and `RestoreScan` keep | |
| 156 | 162 | the scanner's state while an imported module is compiled. |
| 157 | 163 | |
| 158 | 164 | ## sym.pas: types, objects and scopes |
| @@ -168,7 +174,8 @@ | ||
| 168 | 174 | the C rules. `obj` is the declaration that named the type, if any. |
| 169 | 175 | |
| 170 | 176 | `TObj` is anything a name can stand for: a constant, type, variable, |
| 171 | -procedure, module, field, variant case or built-in (`cls`). Where it | |
| 177 | +procedure, module, field, variant case or built-in (`cls`), and `pos` | |
| 178 | +is where its name is declared, for notes. Where it | |
| 172 | 179 | lives is decided here too: a module variable's chunk, a local's frame |
| 173 | 180 | offset in `val`, a procedure's chunk. |
| 174 | 181 | |
| @@ -177,7 +184,9 @@ | ||
| 177 | 184 | sit in one open-addressed hash table, keyed by scope and name: `Find` |
| 178 | 185 | looks in one scope, `Lookup` in each open scope from the innermost out. |
| 179 | 186 | A lookup costs the same however big a program grows, which matters at |
| 180 | -a million lines a second. | |
| 187 | +a million lines a second. After an error, `Closest` looks through the | |
| 188 | +whole table for the name in reach spelled most like one not found | |
| 189 | +(`Distance`), for "did you mean". | |
| 181 | 190 | |
| 182 | 191 | `Same` and `Alike` decide whether two types are the same: one |
| 183 | 192 | declaration for named types, built alike otherwise. `Fingerprint` |
| @@ -329,9 +338,11 @@ | ||
| 329 | 338 | blocks. A symbol at the start of a line ends the skip: by the usual |
| 330 | 339 | layout it starts the next declaration, so the statement gives up its |
| 331 | 340 | procedure (`GiveUp`), and `SkipProc` reads to the procedure's |
| 332 | -`end Name;`. A name not declared is declared where it is used, with | |
| 333 | -the type `tPoison` (`Poison`), as are the names of a declaration that | |
| 334 | -had an error; a construct that uses poison sets `quiet`, and its errors | |
| 341 | +`end Name;`. A name not declared is reported by `NotDeclared`, with | |
| 342 | +what it may have meant: a module to import (`ModuleFile`) or a name | |
| 343 | +spelled alike. It is then declared where it is used, with the type | |
| 344 | +`tPoison` (`Poison`), as are the names of a declaration that had an | |
| 345 | +error; a construct that uses poison sets `quiet`, and its errors | |
| 335 | 346 | aren't reported, since they follow from one that was. Nothing breaks |
| 336 | 347 | for the half-made code: once there is an error, no executable is |
| 337 | 348 | written. |
| @@ -32,6 +32,9 @@ | ||
| 32 | 32 | var |
| 33 | 33 | errors, warnings: longint; |
| 34 | 34 | werror: boolean; { -Werror: a warning counts as an error } |
| 35 | + { The construct being read uses a name already reported as not | |
| 36 | + declared: its errors follow from that one, and aren't shown. } | |
| 37 | + quiet: boolean; | |
| 35 | 38 | |
| 36 | 39 | function AddFile(const path: shortstring; buf: PChar; len: longint): longint; |
| 37 | 40 | function PathOf(f: longint): shortstring; |
| @@ -63,6 +66,7 @@ | ||
| 63 | 66 | colour, colourKnown: boolean; |
| 64 | 67 | cwd: shortstring; { the working directory and a /, or '' if unknown } |
| 65 | 68 | lastError: TPos; { where the last error was } |
| 69 | + shown: boolean; { whether it was shown: its notes and help are only then } | |
| 66 | 70 | |
| 67 | 71 | { Paths are shown from the working directory where they are under it, |
| 68 | 72 | so messages read the same wherever the compiler or its library is. } |
| @@ -204,7 +208,8 @@ | ||
| 204 | 208 | begin |
| 205 | 209 | { a second error at one place follows from the first: when a symbol |
| 206 | 210 | ends a construct with an error, the one around it may stop there too } |
| 207 | - if (errors > 0) and (p.f = lastError.f) and (p.a = lastError.a) then exit; | |
| 211 | + shown := not quiet and not ((errors > 0) and (p.f = lastError.f) and (p.a = lastError.a)); | |
| 212 | + if not shown then exit; | |
| 208 | 213 | lastError := p; |
| 209 | 214 | Show(p, 'error', Red, msg); |
| 210 | 215 | inc(errors); |
| @@ -219,18 +224,20 @@ | ||
| 219 | 224 | if werror then Error(p, msg) |
| 220 | 225 | else begin |
| 221 | 226 | Show(p, 'warning', Yellow, msg); |
| 227 | + shown := true; | |
| 222 | 228 | inc(warnings); |
| 223 | 229 | end; |
| 224 | 230 | end; |
| 225 | 231 | |
| 232 | +{ Notes and help follow an error or a warning, and are shown with it } | |
| 226 | 233 | procedure Note(const p: TPos; const msg: shortstring); |
| 227 | 234 | begin |
| 228 | - Show(p, 'note', Cyan, msg); | |
| 235 | + if shown then Show(p, 'note', Cyan, msg); | |
| 229 | 236 | end; |
| 230 | 237 | |
| 231 | 238 | procedure Help(const msg: shortstring); |
| 232 | 239 | begin |
| 233 | - writeln(StdErr, Paint(Green, 'help:'), ' ', msg); | |
| 240 | + if shown then writeln(StdErr, Paint(Green, 'help:'), ' ', msg); | |
| 234 | 241 | end; |
| 235 | 242 | |
| 236 | 243 | { An error with no place in the source, such as a file that can't be |
| @@ -1115,6 +1115,15 @@ | ||
| 1115 | 1115 | c.n := 0; |
| 1116 | 1116 | end; |
| 1117 | 1117 | |
| 1118 | +{ Too many arguments or too few, at p } | |
| 1119 | +procedure WrongCount(var c: TCall; const p: TPos; const what: shortstring); | |
| 1120 | +begin | |
| 1121 | + if c.obj = nil then MarkAt(p, what); | |
| 1122 | + Error(p, what + ': ' + NameOf(c.obj^.name) + ' takes ' + IntStr(c.sig^.len)); | |
| 1123 | + if c.obj^.module <> nil then Note(c.obj^.pos, 'the signature is here'); | |
| 1124 | + Fail; | |
| 1125 | +end; | |
| 1126 | + | |
| 1118 | 1127 | procedure PassArg(var c: TCall; var a: TItem); |
| 1119 | 1128 | var p: PObj; s: int64; |
| 1120 | 1129 | begin |
| @@ -1124,10 +1133,7 @@ | ||
| 1124 | 1133 | { past the parameters of a C procedure that ends in ...: a string |
| 1125 | 1134 | goes as a C string, an integer constant as an I64, a real |
| 1126 | 1135 | constant as an F64; C takes no F32 there } |
| 1127 | - if not c.sig^.variadic then begin | |
| 1128 | - if c.obj = nil then MarkAt(a.pos, 'too many arguments'); | |
| 1129 | - MarkAt(a.pos, 'too many arguments: ' + NameOf(c.obj^.name) + ' takes ' + IntStr(c.sig^.len)); | |
| 1130 | - end; | |
| 1136 | + if not c.sig^.variadic then WrongCount(c, a.pos, 'too many arguments'); | |
| 1131 | 1137 | if a.mode = mStr then Coerce(a, tPtrU8) |
| 1132 | 1138 | else if a.typ^.form = fConst then Coerce(a, tI64) |
| 1133 | 1139 | else if a.typ = tRConst then Coerce(a, tReal) |
| @@ -1166,10 +1172,7 @@ | ||
| 1166 | 1172 | res: PType; i, ni, nf, k, pad: longint; ret: int64; |
| 1167 | 1173 | onStack: array[0..63] of boolean; |
| 1168 | 1174 | begin |
| 1169 | - if c.param <> nil then begin | |
| 1170 | - if c.obj = nil then Mark('too few arguments'); | |
| 1171 | - Mark('too few arguments: ' + NameOf(c.obj^.name) + ' takes ' + IntStr(c.sig^.len)); | |
| 1172 | - end; | |
| 1175 | + if c.param <> nil then WrongCount(c, TokPos, 'too few arguments'); | |
| 1173 | 1176 | res := c.sig^.base; |
| 1174 | 1177 | ret := 0; |
| 1175 | 1178 | ni := 0; nf := 0; |
| @@ -85,6 +85,8 @@ | ||
| 85 | 85 | |
| 86 | 86 | procedure Expect(s: TSym); |
| 87 | 87 | begin |
| 88 | + if (tok = sLbrace) and (s in [sThen, sDo]) then | |
| 89 | + MarkHelp(MissingPos, 'expected ' + SymText(s) + ', found ' + Found, 'a block is written ' + SymText(s) + ' ... end'); | |
| 88 | 90 | if tok <> s then Expected(SymText(s)); |
| 89 | 91 | Next; |
| 90 | 92 | end; |
| @@ -94,10 +96,12 @@ | ||
| 94 | 96 | Name := Intern(@s[1], length(s)); |
| 95 | 97 | end; |
| 96 | 98 | |
| 97 | -procedure IdentDef(var n: longint; var exp: boolean); | |
| 99 | +{ A name being declared, and whether it is exported; at is the name } | |
| 100 | +procedure IdentDef(var n: longint; var exp: boolean; var at: TPos); | |
| 98 | 101 | begin |
| 99 | 102 | if tok <> sIdent then Expected('a name'); |
| 100 | 103 | n := id; |
| 104 | + at := TokPos; | |
| 101 | 105 | Next; |
| 102 | 106 | exp := false; |
| 103 | 107 | if tok = sTimes then begin |
| @@ -162,6 +166,7 @@ | ||
| 162 | 166 | begin |
| 163 | 167 | recovery := r.outer; |
| 164 | 168 | quiet := r.quiet; |
| 169 | + skipping := false; | |
| 165 | 170 | end; |
| 166 | 171 | |
| 167 | 172 | { After an error: the state as it was at the start of r's construct, |
| @@ -169,6 +174,7 @@ | ||
| 169 | 174 | procedure Restore(var r: TRecover); |
| 170 | 175 | begin |
| 171 | 176 | RestoreScan(r.scan); |
| 177 | + skipping := true; | |
| 172 | 178 | CutScopes(r.scopes); depth := r.depth; numDefers := r.numDefers; |
| 173 | 179 | curLoop := r.loop; curResult := r.result; |
| 174 | 180 | deferring := r.deferring; directDefer := r.directDefer; |
| @@ -247,6 +253,25 @@ | ||
| 247 | 253 | ConstExpr := ConstValue(x); |
| 248 | 254 | end; |
| 249 | 255 | |
| 256 | +{ Help after a name that isn't found: o, the closest name in reach } | |
| 257 | +procedure DidYouMean(o: PObj); | |
| 258 | +begin | |
| 259 | + if o <> nil then Help('did you mean ' + NameOf(o^.name) + '?'); | |
| 260 | +end; | |
| 261 | + | |
| 262 | +{ Where module n is found, if it is: dir and the file in it } | |
| 263 | +function ModuleFile(n: longint; var dir, f: shortstring): boolean; forward; | |
| 264 | + | |
| 265 | +{ Reports name n at p as not declared, with what it may have meant } | |
| 266 | +procedure NotDeclared(const p: TPos; n: longint); | |
| 267 | +var dir, f: shortstring; | |
| 268 | +begin | |
| 269 | + Error(p, NameOf(n) + ' isn''t declared'); | |
| 270 | + if ModuleFile(n, dir, f) then Help(NameOf(n) + ' is a module: import ' + NameOf(n)) | |
| 271 | + else if (NameOf(n) = 'elif') or (NameOf(n) = 'elseif') then Help('raz writes elsif') | |
| 272 | + else DidYouMean(Closest(n, -1)); | |
| 273 | +end; | |
| 274 | + | |
| 250 | 275 | function QualIdent: PObj; |
| 251 | 276 | var o: PObj; m: PModule; |
| 252 | 277 | begin |
| @@ -255,7 +280,8 @@ | ||
| 255 | 280 | if o = nil then begin |
| 256 | 281 | Poison(id); |
| 257 | 282 | quiet := false; |
| 258 | - Mark(NameOf(id) + ' isn''t declared'); | |
| 283 | + NotDeclared(TokPos, id); | |
| 284 | + Fail; | |
| 259 | 285 | end; |
| 260 | 286 | if o^.typ = tPoison then quiet := true; |
| 261 | 287 | Next; |
| @@ -264,12 +290,35 @@ | ||
| 264 | 290 | if tok <> sIdent then Expected('a name'); |
| 265 | 291 | m := o^.module; |
| 266 | 292 | o := Find(m^.scope, id); |
| 267 | - if (o = nil) or not o^.exported then Mark(NameOf(id) + ' isn''t exported by ' + NameOf(m^.name)); | |
| 293 | + if (o = nil) or not o^.exported then begin | |
| 294 | + Error(TokPos, NameOf(id) + ' isn''t exported by ' + NameOf(m^.name)); | |
| 295 | + if o <> nil then Note(o^.pos, NameOf(id) + ' is declared here, without a *') | |
| 296 | + else DidYouMean(Closest(id, m^.scope)); | |
| 297 | + Fail; | |
| 298 | + end; | |
| 268 | 299 | Next; |
| 269 | 300 | end; |
| 270 | 301 | QualIdent := o; |
| 271 | 302 | end; |
| 272 | 303 | |
| 304 | +{ The field of record type t named by the current symbol } | |
| 305 | +function FieldOf(t: PType): PObj; | |
| 306 | +var f: PObj; | |
| 307 | +begin | |
| 308 | + f := Find(t^.scope, id); | |
| 309 | + if f = nil then begin | |
| 310 | + Error(TokPos, TypeName(t) + ' has no field ' + NameOf(id)); | |
| 311 | + DidYouMean(Closest(id, t^.scope)); | |
| 312 | + Fail; | |
| 313 | + end; | |
| 314 | + if not f^.exported and (t^.module <> curModule) then begin | |
| 315 | + Error(TokPos, 'field ' + NameOf(id) + ' of ' + TypeName(t) + ' isn''t exported'); | |
| 316 | + Note(f^.pos, NameOf(id) + ' is declared here, without a *'); | |
| 317 | + Fail; | |
| 318 | + end; | |
| 319 | + FieldOf := f; | |
| 320 | +end; | |
| 321 | + | |
| 273 | 322 | function NewPtr(base: PType; vol: boolean): PType; |
| 274 | 323 | var t: PType; |
| 275 | 324 | begin |
| @@ -284,7 +333,8 @@ | ||
| 284 | 333 | function RecordType: PType; |
| 285 | 334 | var |
| 286 | 335 | t, ft: PType; f, last: PObj; names: array[0..63] of longint; |
| 287 | - exps: array[0..63] of boolean; n, i: longint; off, a, maxA: int64; | |
| 336 | + exps: array[0..63] of boolean; poss: array[0..63] of TPos; | |
| 337 | + n, i: longint; off, a, maxA: int64; | |
| 288 | 338 | begin |
| 289 | 339 | t := NewType(fRecord, 0, 1); |
| 290 | 340 | if tok = sPacked then begin t^.packd := true; Next end; |
| @@ -295,7 +345,7 @@ | ||
| 295 | 345 | n := 0; |
| 296 | 346 | repeat |
| 297 | 347 | if n > High(names) then Mark('more than ' + IntStr(High(names) + 1) + ' names in one declaration'); |
| 298 | - IdentDef(names[n], exps[n]); | |
| 348 | + IdentDef(names[n], exps[n], poss[n]); | |
| 299 | 349 | inc(n); |
| 300 | 350 | if tok <> sComma then break; |
| 301 | 351 | Next; |
| @@ -307,6 +357,7 @@ | ||
| 307 | 357 | off := AlignUp(off, a); |
| 308 | 358 | f := NewObj(oField, names[i], ft); |
| 309 | 359 | f^.exported := exps[i]; |
| 360 | + f^.pos := poss[i]; | |
| 310 | 361 | f^.val := off; |
| 311 | 362 | Insert(f, t^.scope); |
| 312 | 363 | if last = nil then t^.fields := f else last^.next := f; |
| @@ -778,10 +829,7 @@ | ||
| 778 | 829 | Next; |
| 779 | 830 | if (x.mode <> mVar) or (x.typ^.form <> fRecord) then MarkAt(x.pos, 'expected a record, found ' + What(x)); |
| 780 | 831 | if tok <> sIdent then Expected('a field'); |
| 781 | - f := Find(x.typ^.scope, id); | |
| 782 | - if f = nil then Mark(TypeName(x.typ) + ' has no field ' + NameOf(id)); | |
| 783 | - if not f^.exported and (x.typ^.module <> curModule) then | |
| 784 | - Mark('field ' + NameOf(id) + ' of ' + TypeName(x.typ) + ' isn''t exported'); | |
| 832 | + f := FieldOf(x.typ); | |
| 785 | 833 | Next; |
| 786 | 834 | Field(x, f); |
| 787 | 835 | called := false; |
| @@ -885,10 +933,7 @@ | ||
| 885 | 933 | if tok <> sRbrace then |
| 886 | 934 | repeat |
| 887 | 935 | if tok <> sIdent then Expected('a field'); |
| 888 | - f := Find(t^.scope, id); | |
| 889 | - if f = nil then Mark(TypeName(t) + ' has no field ' + NameOf(id)); | |
| 890 | - if not f^.exported and (t^.module <> curModule) then | |
| 891 | - Mark('field ' + NameOf(id) + ' of ' + TypeName(t) + ' isn''t exported'); | |
| 936 | + f := FieldOf(t); | |
| 892 | 937 | for i := 0 to n - 1 do if seen[i] = id then Mark('field ' + NameOf(id) + ' is given twice'); |
| 893 | 938 | if n <= High(seen) then begin seen[n] := id; inc(n) end; |
| 894 | 939 | Next; |
| @@ -1131,7 +1176,7 @@ | ||
| 1131 | 1176 | Next; |
| 1132 | 1177 | if tok <> sIdent then Expected('a name'); |
| 1133 | 1178 | o := Lookup(id); |
| 1134 | - if o = nil then Mark(NameOf(id) + ' isn''t declared'); | |
| 1179 | + if o = nil then begin NotDeclared(TokPos, id); Fail end; | |
| 1135 | 1180 | if (o^.cls <> oVar) or not (o^.typ^.form in [fInt, fEnum]) then |
| 1136 | 1181 | if o^.cls = oVar then |
| 1137 | 1182 | Mark('a for loop needs an integer or enumeration variable, found ' + TypeName(o^.typ)) |
| @@ -1346,7 +1391,9 @@ | ||
| 1346 | 1391 | if (x.mode = mProc) or ((x.mode in [mVar, mReg]) and (x.typ^.form = fProc)) then begin |
| 1347 | 1392 | BeginCall(c, x); |
| 1348 | 1393 | EndCall(c, x); |
| 1349 | - end else MarkAt(x.pos, Subject(x, first) + ' isn''t a statement'); | |
| 1394 | + end else if tok = sEql then | |
| 1395 | + MarkHelp(x.pos, Subject(x, first) + ' isn''t a statement', 'assignment is written :=') | |
| 1396 | + else MarkAt(x.pos, Subject(x, first) + ' isn''t a statement'); | |
| 1350 | 1397 | end; |
| 1351 | 1398 | end; |
| 1352 | 1399 | sIf: IfStatement; |
| @@ -1518,18 +1565,21 @@ | ||
| 1518 | 1565 | begin |
| 1519 | 1566 | line := CurLine; |
| 1520 | 1567 | Next; |
| 1521 | - at := TokPos; | |
| 1522 | - IdentDef(n, exp); | |
| 1523 | - at := SpanFrom(at); | |
| 1568 | + IdentDef(n, exp, at); | |
| 1524 | 1569 | sig := Signature; |
| 1525 | 1570 | Expect(sSemicolon); |
| 1526 | 1571 | p := Find(TopScope, n); |
| 1527 | 1572 | if (p <> nil) and (p^.cls = oProc) and p^.fwd then begin |
| 1528 | - if not Same(p^.typ, sig) then MarkAt(at, NameOf(n) + ' doesn''t have the signature it was declared forward with'); | |
| 1573 | + if not Same(p^.typ, sig) then begin | |
| 1574 | + Error(at, NameOf(n) + ' doesn''t have the signature it was declared forward with'); | |
| 1575 | + Note(p^.pos, 'the signature is here'); | |
| 1576 | + Fail; | |
| 1577 | + end; | |
| 1529 | 1578 | if tok = sForward then Mark(NameOf(n) + ' is declared forward twice'); |
| 1530 | 1579 | p^.exported := p^.exported or exp; |
| 1531 | 1580 | end else begin |
| 1532 | 1581 | p := NewObj(oProc, n, sig); |
| 1582 | + p^.pos := at; | |
| 1533 | 1583 | p^.exported := exp; |
| 1534 | 1584 | if (curModule^.name = nameRt) and not checkMode and (n = nameFail) then p^.chunk := rtFail |
| 1535 | 1585 | else if (curModule^.name = nameRt) and not checkMode and (n = nameEq) then p^.chunk := rtEq |
| @@ -1564,10 +1614,8 @@ | ||
| 1564 | 1614 | curModule^.usesC := true; |
| 1565 | 1615 | if tok = sProc then begin |
| 1566 | 1616 | Next; |
| 1567 | - at := TokPos; | |
| 1568 | - IdentDef(n, exp); | |
| 1617 | + IdentDef(n, exp, at); | |
| 1569 | 1618 | WillDeclare(n); |
| 1570 | - at := SpanFrom(at); | |
| 1571 | 1619 | inExtern := true; |
| 1572 | 1620 | t := Signature; |
| 1573 | 1621 | inExtern := false; |
| @@ -1582,7 +1630,7 @@ | ||
| 1582 | 1630 | end else begin |
| 1583 | 1631 | if tok <> sVar then Expected('"proc" or "var"'); |
| 1584 | 1632 | Next; |
| 1585 | - IdentDef(n, exp); | |
| 1633 | + IdentDef(n, exp, at); | |
| 1586 | 1634 | WillDeclare(n); |
| 1587 | 1635 | Expect(sColon); |
| 1588 | 1636 | declExported := exp; |
| @@ -1590,6 +1638,7 @@ | ||
| 1590 | 1638 | declExported := false; |
| 1591 | 1639 | o := NewObj(oVar, n, t); |
| 1592 | 1640 | end; |
| 1641 | + o^.pos := at; | |
| 1593 | 1642 | o^.exported := exp; |
| 1594 | 1643 | o^.ext := true; |
| 1595 | 1644 | o^.chunk := ExternChunk(n); |
| @@ -1597,9 +1646,9 @@ | ||
| 1597 | 1646 | end; |
| 1598 | 1647 | |
| 1599 | 1648 | procedure ConstDecl(top: boolean); |
| 1600 | -var n: longint; exp: boolean; x: TItem; o: PObj; t: PType; start: TPos; | |
| 1649 | +var n: longint; exp: boolean; x: TItem; o: PObj; t: PType; start, at: TPos; | |
| 1601 | 1650 | begin |
| 1602 | - IdentDef(n, exp); | |
| 1651 | + IdentDef(n, exp, at); | |
| 1603 | 1652 | WillDeclare(n); |
| 1604 | 1653 | t := nil; |
| 1605 | 1654 | if tok = sColon then begin |
| @@ -1611,6 +1660,7 @@ | ||
| 1611 | 1660 | Expect(sEql); |
| 1612 | 1661 | Expression(x, nil); |
| 1613 | 1662 | o := NewObj(oConst, n, x.typ); |
| 1663 | + o^.pos := at; | |
| 1614 | 1664 | o^.exported := exp and top; |
| 1615 | 1665 | if t = tError then begin |
| 1616 | 1666 | o^.typ := tError; |
| @@ -1622,15 +1672,16 @@ | ||
| 1622 | 1672 | end; |
| 1623 | 1673 | |
| 1624 | 1674 | procedure TypeDecl(top: boolean); |
| 1625 | -var n: longint; exp: boolean; o: PObj; t: PType; | |
| 1675 | +var n: longint; exp: boolean; o: PObj; t: PType; at: TPos; | |
| 1626 | 1676 | begin |
| 1627 | - IdentDef(n, exp); | |
| 1677 | + IdentDef(n, exp, at); | |
| 1628 | 1678 | WillDeclare(n); |
| 1629 | 1679 | Expect(sEql); |
| 1630 | 1680 | declExported := exp and top; |
| 1631 | 1681 | inTypes := true; |
| 1632 | 1682 | t := ParseType; |
| 1633 | 1683 | o := NewObj(oType, n, t); |
| 1684 | + o^.pos := at; | |
| 1634 | 1685 | o^.exported := exp and top; |
| 1635 | 1686 | if t^.obj = nil then t^.obj := o; |
| 1636 | 1687 | Declare(o); |
| @@ -1641,11 +1692,12 @@ | ||
| 1641 | 1692 | var |
| 1642 | 1693 | i, k: longint; o: PObj; t: PType; |
| 1643 | 1694 | names: array[0..63] of longint; exps: array[0..63] of boolean; |
| 1695 | + poss: array[0..63] of TPos; | |
| 1644 | 1696 | begin |
| 1645 | 1697 | k := 0; |
| 1646 | 1698 | repeat |
| 1647 | 1699 | if k > High(names) then Mark('more than ' + IntStr(High(names) + 1) + ' names in one declaration'); |
| 1648 | - IdentDef(names[k], exps[k]); | |
| 1700 | + IdentDef(names[k], exps[k], poss[k]); | |
| 1649 | 1701 | WillDeclare(names[k]); |
| 1650 | 1702 | inc(k); |
| 1651 | 1703 | if tok <> sComma then break; |
| @@ -1657,6 +1709,7 @@ | ||
| 1657 | 1709 | declExported := false; |
| 1658 | 1710 | for i := 0 to k - 1 do begin |
| 1659 | 1711 | o := NewObj(oVar, names[i], t); |
| 1712 | + o^.pos := poss[i]; | |
| 1660 | 1713 | o^.exported := exps[i] and top; |
| 1661 | 1714 | o^.isVar := false; |
| 1662 | 1715 | if top then begin |
| @@ -1741,7 +1794,7 @@ | ||
| 1741 | 1794 | for i := 0 to numFwd - 1 do begin |
| 1742 | 1795 | o := Lookup(fwdTypes[i]^.fwd); |
| 1743 | 1796 | if (o = nil) or (o^.cls <> oType) then begin |
| 1744 | - Error(fwdPos[i], NameOf(fwdTypes[i]^.fwd) + ' isn''t declared'); | |
| 1797 | + NotDeclared(fwdPos[i], fwdTypes[i]^.fwd); | |
| 1745 | 1798 | fwdTypes[i]^.base := tPoison; |
| 1746 | 1799 | end else fwdTypes[i]^.base := o^.typ; |
| 1747 | 1800 | fwdTypes[i]^.fwd := -1; |
| @@ -1830,7 +1883,7 @@ | ||
| 1830 | 1883 | if a = nil then MarkAt(m^.args.pos[i], NameOf(m^.name) + ' needs a procedure for ' + NameOf(o^.name)); |
| 1831 | 1884 | if not Same(a^.typ, sig) then begin |
| 1832 | 1885 | Error(m^.args.pos[i], NameOf(a^.name) + ' doesn''t have the signature of ' + NameOf(o^.name)); |
| 1833 | - Note(at, NameOf(o^.name) + ' is declared here'); | |
| 1886 | + Note(at, 'the signature is here'); | |
| 1834 | 1887 | Finish; |
| 1835 | 1888 | end; |
| 1836 | 1889 | o^.typ := a^.typ; o^.chunk := a^.chunk; o^.isCode := a^.isCode; o^.ext := a^.ext; |
| @@ -2029,29 +2082,33 @@ | ||
| 2029 | 2082 | CompareIface(m, CompileFile(dir + '/' + others[i], n, args, true)); |
| 2030 | 2083 | end; |
| 2031 | 2084 | |
| 2032 | -function ImportModule(name: longint; const args: TArgs): PModule; | |
| 2085 | +function ModuleFile(n: longint; var dir, f: shortstring): boolean; | |
| 2033 | 2086 | const Suffixes: array[0..3] of string[16] = ('.' + Target, '.' + TargetOS, '.' + TargetArch, ''); |
| 2034 | -var m: PModule; i, j: longint; dir, f: shortstring; | |
| 2087 | +var i, j: longint; | |
| 2035 | 2088 | begin |
| 2036 | - m := FindModule(name, args); | |
| 2037 | - if m <> nil then exit(m); | |
| 2038 | - for i := 0 to numPending - 1 do | |
| 2039 | - if IsModule(pending[i], name, args) then MarkAt(args.at, NameOf(name) + ' imports itself'); | |
| 2040 | 2089 | for i := -1 to numInc do begin |
| 2041 | 2090 | if i = -1 then dir := mainDir |
| 2042 | 2091 | else if i = numInc then dir := libDir |
| 2043 | 2092 | else dir := incDirs[i]; |
| 2044 | 2093 | for j := 0 to 3 do begin |
| 2045 | - f := NameOf(name) + Suffixes[j] + '.raz'; | |
| 2046 | - if Exists(dir + '/' + f) then begin | |
| 2047 | - m := CompileFile(dir + '/' + f, name, args, false); | |
| 2048 | - CheckSiblings(dir, f, name, m, args); | |
| 2049 | - exit(m); | |
| 2050 | - end; | |
| 2094 | + f := NameOf(n) + Suffixes[j] + '.raz'; | |
| 2095 | + if Exists(dir + '/' + f) then exit(true); | |
| 2051 | 2096 | end; |
| 2052 | 2097 | end; |
| 2053 | - MarkAt(args.at, 'can''t find module ' + NameOf(name)); | |
| 2054 | - ImportModule := nil; | |
| 2098 | + ModuleFile := false; | |
| 2099 | +end; | |
| 2100 | + | |
| 2101 | +function ImportModule(name: longint; const args: TArgs): PModule; | |
| 2102 | +var m: PModule; i: longint; dir, f: shortstring; | |
| 2103 | +begin | |
| 2104 | + m := FindModule(name, args); | |
| 2105 | + if m <> nil then exit(m); | |
| 2106 | + for i := 0 to numPending - 1 do | |
| 2107 | + if IsModule(pending[i], name, args) then MarkAt(args.at, NameOf(name) + ' imports itself'); | |
| 2108 | + if not ModuleFile(name, dir, f) then MarkAt(args.at, 'can''t find module ' + NameOf(name)); | |
| 2109 | + m := CompileFile(dir + '/' + f, name, args, false); | |
| 2110 | + CheckSiblings(dir, f, name, m, args); | |
| 2111 | + ImportModule := m; | |
| 2055 | 2112 | end; |
| 2056 | 2113 | |
| 2057 | 2114 | function CompileMain(const path: shortstring): PModule; |
| @@ -152,9 +152,10 @@ | ||
| 152 | 152 | main := Find(m^.scope, Intern('main', 4)); |
| 153 | 153 | hasResult := false; |
| 154 | 154 | if main <> nil then begin |
| 155 | - if (main^.cls <> oProc) or not main^.exported then Fatal('main must be an exported procedure'); | |
| 156 | - if (main^.typ^.len <> 0) or ((main^.typ^.base <> nil) and (main^.typ^.base <> tI64)) then | |
| 157 | - Fatal('main must be proc main* or proc main*: I64'); | |
| 155 | + if (main^.cls <> oProc) or not main^.exported then Error(main^.pos, 'main must be an exported procedure') | |
| 156 | + else if (main^.typ^.len <> 0) or ((main^.typ^.base <> nil) and (main^.typ^.base <> tI64)) then | |
| 157 | + Error(main^.pos, 'main must be proc main* or proc main*: I64'); | |
| 158 | + Finish; | |
| 158 | 159 | hasResult := main^.typ^.base <> nil; |
| 159 | 160 | end; |
| 160 | 161 |
| @@ -71,6 +71,7 @@ | ||
| 71 | 71 | procedure Mark(const msg: shortstring); |
| 72 | 72 | procedure MarkAt(const p: TPos; const msg: shortstring); |
| 73 | 73 | procedure MarkHelp(const p: TPos; const msg, hint: shortstring); |
| 74 | +procedure Fail; | |
| 74 | 75 | function AtLineStart: boolean; |
| 75 | 76 | function SymText(s: TSym): shortstring; |
| 76 | 77 | function Found: shortstring; |
| @@ -79,9 +80,9 @@ | ||
| 79 | 80 | { Where the parser goes on after an error: the innermost statement or |
| 80 | 81 | declaration being read. nil stops the compiler at the first error. } |
| 81 | 82 | recovery: ^jmp_buf; |
| 82 | - { The construct being read uses a name already reported as not | |
| 83 | - declared: its errors follow from that one, and aren't reported. } | |
| 84 | - quiet: boolean; | |
| 83 | + { The parser is skipping what it has read once already, after an | |
| 84 | + error, so slips in it aren't reported again. } | |
| 85 | + skipping: boolean; | |
| 85 | 86 | procedure SaveScan(var st: TScanState); |
| 86 | 87 | procedure RestoreScan(const st: TScanState); |
| 87 | 88 | function TokPos: TPos; |
| @@ -237,21 +238,26 @@ | ||
| 237 | 238 | MarkAt(TokPos, msg); |
| 238 | 239 | end; |
| 239 | 240 | |
| 240 | -{ An error in the parser's construct at p: reported, then the parser | |
| 241 | - goes on from the recovery point. } | |
| 242 | -procedure MarkAt(const p: TPos; const msg: shortstring); | |
| 241 | +{ After an error in the parser's construct, reported with its notes | |
| 242 | + and help: the parser goes on from the recovery point. } | |
| 243 | +procedure Fail; | |
| 243 | 244 | begin |
| 244 | - if not quiet then Error(p, msg); | |
| 245 | 245 | if recovery = nil then Finish; |
| 246 | 246 | longjmp(recovery^, 1); |
| 247 | +end; | |
| 248 | + | |
| 249 | +procedure MarkAt(const p: TPos; const msg: shortstring); | |
| 250 | +begin | |
| 251 | + Error(p, msg); | |
| 252 | + Fail; | |
| 247 | 253 | end; |
| 248 | 254 | |
| 249 | 255 | { MarkAt, with a line of help after the message. } |
| 250 | 256 | procedure MarkHelp(const p: TPos; const msg, hint: shortstring); |
| 251 | 257 | begin |
| 252 | - if not quiet then begin Error(p, msg); Help(hint) end; | |
| 253 | - if recovery = nil then Finish; | |
| 254 | - longjmp(recovery^, 1); | |
| 258 | + Error(p, msg); | |
| 259 | + Help(hint); | |
| 260 | + Fail; | |
| 255 | 261 | end; |
| 256 | 262 | |
| 257 | 263 | { An error in the text at bytes a to b of the file being read, which |
| @@ -260,6 +266,7 @@ | ||
| 260 | 266 | var p: TPos; |
| 261 | 267 | begin |
| 262 | 268 | p.f := cur.f; p.a := a; p.b := b; |
| 269 | + quiet := false; | |
| 263 | 270 | Error(p, msg); |
| 264 | 271 | Finish; |
| 265 | 272 | end; |
| @@ -269,6 +276,17 @@ | ||
| 269 | 276 | MarkBytes(cur.tokPos, cur.pos, msg); |
| 270 | 277 | end; |
| 271 | 278 | |
| 279 | +{ The n characters at the scanner are how another language writes t: | |
| 280 | + an error, but reading goes on as if t were written. } | |
| 281 | +procedure Slip(n: longint; t: TSym; const msg: shortstring); | |
| 282 | +var p: TPos; | |
| 283 | +begin | |
| 284 | + p.f := cur.f; p.a := cur.pos; p.b := cur.pos + n; | |
| 285 | + if not skipping then Error(p, msg); | |
| 286 | + inc(cur.pos, n); | |
| 287 | + tok := t; | |
| 288 | +end; | |
| 289 | + | |
| 272 | 290 | { How an operator or reserved word is written, quoted, for messages. } |
| 273 | 291 | function SymText(s: TSym): shortstring; |
| 274 | 292 | begin |
| @@ -597,7 +615,12 @@ | ||
| 597 | 615 | '-': begin inc(cur.pos); tok := sMinus end; |
| 598 | 616 | '*': begin inc(cur.pos); tok := sTimes end; |
| 599 | 617 | '/': begin inc(cur.pos); tok := sSlash end; |
| 600 | - '=': begin inc(cur.pos); tok := sEql end; | |
| 618 | + '=': | |
| 619 | + if cur.buf[cur.pos + 1] = '=' then Slip(2, sEql, 'comparing is written =, not ==') | |
| 620 | + else begin inc(cur.pos); tok := sEql end; | |
| 621 | + '&': | |
| 622 | + if cur.buf[cur.pos + 1] = '&' then Slip(2, sAnd, '"and" is written and, not &&') | |
| 623 | + else MarkBytes(cur.pos, cur.pos + 1, 'unexpected character "&"'); | |
| 601 | 624 | '#': begin inc(cur.pos); tok := sNeq end; |
| 602 | 625 | '<': begin |
| 603 | 626 | inc(cur.pos); |
| @@ -631,7 +654,12 @@ | ||
| 631 | 654 | '{': begin inc(cur.pos); tok := sLbrace end; |
| 632 | 655 | '}': begin inc(cur.pos); tok := sRbrace end; |
| 633 | 656 | '^': begin inc(cur.pos); tok := sArrow end; |
| 634 | - '|': begin inc(cur.pos); tok := sBar end; | |
| 657 | + '|': | |
| 658 | + if cur.buf[cur.pos + 1] = '|' then Slip(2, sOr, '"or" is written or, not ||') | |
| 659 | + else begin inc(cur.pos); tok := sBar end; | |
| 660 | + '!': | |
| 661 | + if cur.buf[cur.pos + 1] = '=' then Slip(2, sNeq, '"not equal" is written #, not !=') | |
| 662 | + else Slip(1, sNot, '"not" is written not, not !'); | |
| 635 | 663 | #0: begin |
| 636 | 664 | if cur.pos < cur.len then MarkBytes(cur.pos, cur.pos + 1, 'NUL byte in the text'); |
| 637 | 665 | tok := sEof; |
| @@ -58,6 +58,7 @@ | ||
| 58 | 58 | typ: PType; |
| 59 | 59 | next: PObj; { the next in its list: scope, fields, parameters } |
| 60 | 60 | scope: longint; { where it is declared } |
| 61 | + pos: TPos; { where its name is declared, for messages } | |
| 61 | 62 | val: int64; { oConst: value; oBuiltin: which; oCase: tag; |
| 62 | 63 | oField: offset; oVar: frame offset } |
| 63 | 64 | chunk: longint; { global oVar, oProc, string oConst: its chunk } |
| @@ -140,6 +141,7 @@ | ||
| 140 | 141 | procedure Insert(obj: PObj; s: longint); |
| 141 | 142 | function Find(s, name: longint): PObj; |
| 142 | 143 | function Lookup(name: longint): PObj; |
| 144 | +function Closest(name, s: longint): PObj; | |
| 143 | 145 | |
| 144 | 146 | function AlignUp(x, a: int64): int64; |
| 145 | 147 | function Same(a, b: PType): boolean; |
| @@ -151,6 +153,8 @@ | ||
| 151 | 153 | function Fingerprint(o: PObj): qword; |
| 152 | 154 | |
| 153 | 155 | implementation |
| 156 | + | |
| 157 | +uses math; | |
| 154 | 158 | |
| 155 | 159 | type |
| 156 | 160 | PObjTab = ^TObjTab; |
| @@ -186,6 +190,7 @@ | ||
| 186 | 190 | o^.typ := typ; |
| 187 | 191 | o^.chunk := -1; |
| 188 | 192 | o^.module := curModule; |
| 193 | + o^.pos := TokPos; | |
| 189 | 194 | NewObj := o; |
| 190 | 195 | end; |
| 191 | 196 | |
| @@ -270,8 +275,11 @@ | ||
| 270 | 275 | i := Slot(s, obj^.name); |
| 271 | 276 | o := hashTab^[i]; |
| 272 | 277 | while o <> nil do begin |
| 273 | - if (o^.scope = s) and (o^.name = obj^.name) then | |
| 274 | - Mark(NameOf(obj^.name) + ' is declared twice'); | |
| 278 | + if (o^.scope = s) and (o^.name = obj^.name) then begin | |
| 279 | + Error(obj^.pos, NameOf(obj^.name) + ' is declared twice'); | |
| 280 | + Note(o^.pos, 'the first one is here'); | |
| 281 | + Fail; | |
| 282 | + end; | |
| 275 | 283 | i := (i + 1) and hashMask; |
| 276 | 284 | o := hashTab^[i]; |
| 277 | 285 | end; |
| @@ -302,6 +310,50 @@ | ||
| 302 | 310 | if o <> nil then exit(o); |
| 303 | 311 | end; |
| 304 | 312 | Lookup := nil; |
| 313 | +end; | |
| 314 | + | |
| 315 | +{ How far apart two names are: the edits, a character put in, taken out, | |
| 316 | + changed or swapped with the next, that make one the other, ignoring | |
| 317 | + case. } | |
| 318 | +function Distance(const a, b: shortstring): longint; | |
| 319 | +var d: array[0..MaxIdent + 1, 0..MaxIdent + 1] of longint; i, j, c: longint; | |
| 320 | +begin | |
| 321 | + for i := 0 to length(a) do d[i, 0] := i; | |
| 322 | + for j := 0 to length(b) do d[0, j] := j; | |
| 323 | + for i := 1 to length(a) do | |
| 324 | + for j := 1 to length(b) do begin | |
| 325 | + c := ord(UpCase(a[i]) <> UpCase(b[j])); | |
| 326 | + d[i, j] := Min(Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + c); | |
| 327 | + if (i > 1) and (j > 1) and (UpCase(a[i]) = UpCase(b[j - 1])) | |
| 328 | + and (UpCase(a[i - 1]) = UpCase(b[j])) then | |
| 329 | + d[i, j] := Min(d[i, j], d[i - 2, j - 2] + 1); | |
| 330 | + end; | |
| 331 | + Distance := d[length(a), length(b)]; | |
| 332 | +end; | |
| 333 | + | |
| 334 | +{ The name spelled most like name, for "did you mean": in scope s, if | |
| 335 | + it can be reached from here, or in the scopes in reach if s < 0; nil | |
| 336 | + if none is within an edit for every three characters. Only after an | |
| 337 | + error, so it looks at every object. } | |
| 338 | +function Closest(name, s: longint): PObj; | |
| 339 | +var i, j, d, best: longint; o: PObj; n: shortstring; reach: boolean; | |
| 340 | +begin | |
| 341 | + n := NameOf(name); | |
| 342 | + best := length(n) div 3 + 1; | |
| 343 | + Closest := nil; | |
| 344 | + for i := 0 to hashMask do begin | |
| 345 | + o := hashTab^[i]; | |
| 346 | + if (o = nil) or (o^.name = name) or (o^.name < 0) or (o^.typ = tPoison) then continue; | |
| 347 | + if s >= 0 then reach := (o^.scope = s) and (o^.exported or (o^.module = curModule)) | |
| 348 | + else begin | |
| 349 | + reach := false; | |
| 350 | + for j := 0 to depth - 1 do reach := reach or (o^.scope = scopes[j]); | |
| 351 | + end; | |
| 352 | + if reach then begin | |
| 353 | + d := Distance(n, NameOf(o^.name)); | |
| 354 | + if d < best then begin best := d; Closest := o end; | |
| 355 | + end; | |
| 356 | + end; | |
| 305 | 357 | end; |
| 306 | 358 | |
| 307 | 359 | { Types } |
| @@ -1,4 +1,7 @@ | ||
| 1 | 1 | test/err/AtMany.raz:7:16: error: too many arguments: Id takes 1 |
| 2 | 2 | 7 | x := Id(1, 2 * 3); |
| 3 | 3 | | ^^^^^ |
| 4 | +test/err/AtMany.raz:2:6: note: the signature is here | |
| 5 | + 2 | proc Id(a: I64): I64; | |
| 6 | + | ^^ | |
| 4 | 7 | raz: 1 error |
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | test/err/BadProc.raz:2:33: error: cmp doesn't have the signature of hash |
| 2 | 2 | 2 | import Str, M := Map([]U8, I64, Str.cmp, Str.eq); |
| 3 | 3 | | ^^^^^^^ |
| 4 | -lib/Map.raz:4:18: note: hash is declared here | |
| 4 | +lib/Map.raz:4:18: note: the signature is here | |
| 5 | 5 | 4 | module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); |
| 6 | 6 | | ^^^^ |
| 7 | 7 | raz: 1 error |
| @@ -1,4 +1,7 @@ | ||
| 1 | 1 | test/err/DeclLitNotExported.raz:6:11: error: field extra of Plat.Info isn't exported |
| 2 | 2 | 6 | i := {extra: 1}; |
| 3 | 3 | | ^^^^^ |
| 4 | +test/run/mods/Plat.linux-x86_64.raz:3:33: note: extra is declared here, without a * | |
| 5 | + 3 | type Info* = record kind*: I64; extra: I64; end; | |
| 6 | + | ^^^^^ | |
| 4 | 7 | raz: 1 error |
| @@ -1,4 +1,7 @@ | ||
| 1 | 1 | test/err/DeclNotExported.raz:6:21: error: hidden isn't exported by DeclHidden |
| 2 | 2 | 6 | n := DeclHidden.hidden; |
| 3 | 3 | | ^^^^^^ |
| 4 | +test/err/mods/DeclHidden.raz:3:7: note: hidden is declared here, without a * | |
| 5 | + 3 | const hidden = 2; | |
| 6 | + | ^^^^^^ | |
| 4 | 7 | raz: 1 error |
| @@ -1,4 +1,7 @@ | ||
| 1 | 1 | test/err/Field.raz:6:7: error: field extra of Plat.Info isn't exported |
| 2 | 2 | 6 | i.extra := 1; |
| 3 | 3 | | ^^^^^ |
| 4 | +test/run/mods/Plat.linux-x86_64.raz:3:33: note: extra is declared here, without a * | |
| 5 | + 3 | type Info* = record kind*: I64; extra: I64; end; | |
| 6 | + | ^^^^^ | |
| 4 | 7 | raz: 1 error |
| @@ -1,4 +1,7 @@ | ||
| 1 | 1 | test/err/GenTooFew.raz:6:14: error: too few arguments: Id takes 1 |
| 2 | 2 | 6 | x := Id(); |
| 3 | 3 | | ^ |
| 4 | +test/err/GenTooFew.raz:2:6: note: the signature is here | |
| 5 | + 2 | proc Id(a: I64): I64; begin return a; end; | |
| 6 | + | ^^ | |
| 4 | 7 | raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/NoteBuiltin.raz:5:10: error: len isn't declared | |
| 2 | + 5 | x := len(y); | |
| 3 | + | ^^^ | |
| 4 | +help: did you mean LEN? | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module NoteBuiltin; | |
| 2 | +proc main*; | |
| 3 | +var x, y: I64; | |
| 4 | +begin | |
| 5 | + x := len(y); | |
| 6 | +end; | |
| 7 | +end NoteBuiltin. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/NoteExport.raz:6:9: error: Int isn't exported by Out | |
| 2 | + 6 | Out.Int(x); | |
| 3 | + | ^^^ | |
| 4 | +help: did you mean int? | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module NoteExport; | |
| 2 | +import Out; | |
| 3 | +proc main*; | |
| 4 | +var x, y: I64; | |
| 5 | +begin | |
| 6 | + Out.Int(x); | |
| 7 | +end; | |
| 8 | +end NoteExport. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/NoteField.raz:6:7: error: P has no field cuont | |
| 2 | + 6 | p.cuont := 1; | |
| 3 | + | ^^^^^ | |
| 4 | +help: did you mean count? | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module NoteField; | |
| 2 | +type P = record count: I64; end; | |
| 3 | +proc main*; | |
| 4 | +var p: P; | |
| 5 | +begin | |
| 6 | + p.cuont := 1; | |
| 7 | +end; | |
| 8 | +end NoteField. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/NoteImport.raz:5:5: error: Fmt isn't declared | |
| 2 | + 5 | Fmt.print(x); | |
| 3 | + | ^^^ | |
| 4 | +help: Fmt is a module: import Fmt | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module NoteImport; | |
| 2 | +proc main*; | |
| 3 | +var x, y: I64; | |
| 4 | +begin | |
| 5 | + Fmt.print(x); | |
| 6 | +end; | |
| 7 | +end NoteImport. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/NoteLocal.raz:6:5: error: cuont isn't declared | |
| 2 | + 6 | cuont := 1; | |
| 3 | + | ^^^^^ | |
| 4 | +help: did you mean count? | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module NoteLocal; | |
| 2 | +proc main*; | |
| 3 | +var x, y, count: I64; | |
| 4 | +begin | |
| 5 | + | |
| 6 | + cuont := 1; | |
| 7 | +end; | |
| 8 | +end NoteLocal. |
| @@ -1 +1,4 @@ | ||
| 1 | -raz: main must be proc main* or proc main*: I64 | |
| 1 | +test/err/ScanMain.raz:2:6: error: main must be proc main* or proc main*: I64 | |
| 2 | + 2 | proc main*(n: I64); | |
| 3 | + | ^^^^ | |
| 4 | +raz: 1 error |
| @@ -1,4 +1,7 @@ | ||
| 1 | -test/err/ScanTwice.raz:4:11: error: x is declared twice | |
| 1 | +test/err/ScanTwice.raz:4:5: error: x is declared twice | |
| 2 | 2 | 4 | x: I64; |
| 3 | - | ^ | |
| 3 | + | ^ | |
| 4 | +test/err/ScanTwice.raz:3:5: note: the first one is here | |
| 5 | + 3 | var x: I64; | |
| 6 | + | ^ | |
| 4 | 7 | raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +test/err/SlipAnd.raz:5:16: error: "and" is written and, not && | |
| 2 | + 5 | if (x = 1) && (y = 2) then x := 2; end; | |
| 3 | + | ^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module SlipAnd; | |
| 2 | +proc main*; | |
| 3 | +var x, y: I64; | |
| 4 | +begin | |
| 5 | + if (x = 1) && (y = 2) then x := 2; end; | |
| 6 | +end; | |
| 7 | +end SlipAnd. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/SlipAssign.raz:5:5: error: x isn't a statement | |
| 2 | + 5 | x = 1; | |
| 3 | + | ^ | |
| 4 | +help: assignment is written := | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module SlipAssign; | |
| 2 | +proc main*; | |
| 3 | +var x, y: I64; | |
| 4 | +begin | |
| 5 | + x = 1; | |
| 6 | +end; | |
| 7 | +end SlipAssign. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/SlipBrace.raz:5:14: error: expected "then", found "{" | |
| 2 | + 5 | if x = 1 { x := 2 } | |
| 3 | + | ^ | |
| 4 | +help: a block is written "then" ... end | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module SlipBrace; | |
| 2 | +proc main*; | |
| 3 | +var x, y: I64; | |
| 4 | +begin | |
| 5 | + if x = 1 { x := 2 } | |
| 6 | +end; | |
| 7 | +end SlipBrace. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/SlipDo.raz:5:17: error: expected "do", found "{" | |
| 2 | + 5 | while x < 3 { x := x + 1 } | |
| 3 | + | ^ | |
| 4 | +help: a block is written "do" ... end | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module SlipDo; | |
| 2 | +proc main*; | |
| 3 | +var x, y: I64; | |
| 4 | +begin | |
| 5 | + while x < 3 { x := x + 1 } | |
| 6 | +end; | |
| 7 | +end SlipDo. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/err/SlipElif.raz:5:27: error: elif isn't declared | |
| 2 | + 5 | if x = 1 then x := 2; elif x = 3 then x := 4; end; | |
| 3 | + | ^^^^ | |
| 4 | +help: raz writes elsif | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module SlipElif; | |
| 2 | +proc main*; | |
| 3 | +var x, y: I64; | |
| 4 | +begin | |
| 5 | + if x = 1 then x := 2; elif x = 3 then x := 4; end; | |
| 6 | +end; | |
| 7 | +end SlipElif. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +test/err/SlipEql.raz:5:10: error: comparing is written =, not == | |
| 2 | + 5 | if x == 1 then x := 2; end; | |
| 3 | + | ^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module SlipEql; | |
| 2 | +proc main*; | |
| 3 | +var x, y: I64; | |
| 4 | +begin | |
| 5 | + if x == 1 then x := 2; end; | |
| 6 | +end; | |
| 7 | +end SlipEql. |
| @@ -0,0 +1,10 @@ | ||
| 1 | +test/err/SlipMany.raz:5:10: error: "not equal" is written #, not != | |
| 2 | + 5 | if x != 1 && z then x := y; end; | |
| 3 | + | ^^ | |
| 4 | +test/err/SlipMany.raz:5:15: error: "and" is written and, not && | |
| 5 | + 5 | if x != 1 && z then x := y; end; | |
| 6 | + | ^^ | |
| 7 | +test/err/SlipMany.raz:5:18: error: z isn't declared | |
| 8 | + 5 | if x != 1 && z then x := y; end; | |
| 9 | + | ^ | |
| 10 | +raz: 3 errors |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module SlipMany; | |
| 2 | +proc main*; | |
| 3 | +var x, y: I64; | |
| 4 | +begin | |
| 5 | + if x != 1 && z then x := y; end; | |
| 6 | +end; | |
| 7 | +end SlipMany. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +test/err/SlipNeq.raz:5:10: error: "not equal" is written #, not != | |
| 2 | + 5 | if x != 1 then x := 2; end; | |
| 3 | + | ^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module SlipNeq; | |
| 2 | +proc main*; | |
| 3 | +var x, y: I64; | |
| 4 | +begin | |
| 5 | + if x != 1 then x := 2; end; | |
| 6 | +end; | |
| 7 | +end SlipNeq. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +test/err/SlipNot.raz:5:8: error: "not" is written not, not ! | |
| 2 | + 5 | if ! (x = 1) then x := 2; end; | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module SlipNot; | |
| 2 | +proc main*; | |
| 3 | +var x, y: I64; | |
| 4 | +begin | |
| 5 | + if ! (x = 1) then x := 2; end; | |
| 6 | +end; | |
| 7 | +end SlipNot. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +test/err/SlipOr.raz:5:16: error: "or" is written or, not || | |
| 2 | + 5 | if (x = 1) || (y = 2) then x := 2; end; | |
| 3 | + | ^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module SlipOr; | |
| 2 | +proc main*; | |
| 3 | +var x, y: I64; | |
| 4 | +begin | |
| 5 | + if (x = 1) || (y = 2) then x := 2; end; | |
| 6 | +end; | |
| 7 | +end SlipOr. |
| @@ -1,6 +1,9 @@ | ||
| 1 | 1 | test/err/StmtSigForward.raz:3:6: error: f doesn't have the signature it was declared forward with |
| 2 | 2 | 3 | proc f(a: BOOL); begin end; |
| 3 | 3 | | ^ |
| 4 | +test/err/StmtSigForward.raz:2:6: note: the signature is here | |
| 5 | + 2 | proc f(a: I64); forward; | |
| 6 | + | ^ | |
| 4 | 7 | test/err/StmtSigForward.raz:4:19: error: f is declared forward but never defined |
| 5 | 8 | 4 | end StmtSigForward. |
| 6 | 9 | | ^ |