Skip to content
← Back

src/logging/diagnostics_store.ghul

1
namespace Logging is
2
use System.Exception
3
4
use Collections.Map
5
use Collections.MutableMap
6
use Collections.List
7
use Collections.MutableList
8
use Collections.Iterable
9
10
use Collections.MAP
11
use Collections.LIST
12
use Collections.STACK
13
14
use IO.TextWriter
15
16
use Ghul.Pipes
17
18
use Source.LOCATION
19
20
// A secondary location a diagnostic points at, alongside its own
21
// primary location - a prior declaration, the base member being
22
// overridden, the call that invalidated a narrowing. An editor
23
// renders this as a clickable cross-reference (LSP
24
// DiagnosticRelatedInformation); the text formatters append it to
25
// the line they print. `message` here is terse by comparison, so the
26
// diagnostic's own `text` still has to name the thing it points at
27
// - the related location says where, not what.
28
struct RELATED_LOCATION is
29
location: LOCATION
30
message: string
31
32
init(location: LOCATION, message: string) is
33
self.location = location
34
self.message = message
35
si
36
si
37
38
struct DIAGNOSTIC_MESSAGE is
39
is_analysis: bool
40
is_compile_expressions: bool
41
42
severity: DiagnosticSeverity
43
location: LOCATION
44
code: string?
45
text: string
46
// Other locations this diagnostic references. Null when there are
47
// none. See RELATED_LOCATION.
48
related: Collections.LIST[RELATED_LOCATION]?
49
is_hint: bool => (severity == DiagnosticSeverity.HINT)
50
is_fatal: bool => (severity == DiagnosticSeverity.FATAL \/ severity == DiagnosticSeverity.EXCEPTION)
51
52
init(
53
is_analysis: bool,
54
is_compile_expressions: bool,
55
severity: DiagnosticSeverity,
56
location: LOCATION,
57
text: string
58
) is
59
self.is_analysis = is_analysis
60
self.is_compile_expressions = is_compile_expressions
61
self.severity = severity
62
self.location = location
63
self.code = null
64
self.text = text
65
self.related = null
66
si
67
68
init(
69
is_analysis: bool,
70
is_compile_expressions: bool,
71
severity: DiagnosticSeverity,
72
location: LOCATION,
73
code: string?,
74
text: string
75
) is
76
self.is_analysis = is_analysis
77
self.is_compile_expressions = is_compile_expressions
78
self.severity = severity
79
self.location = location
80
self.code = code
81
self.text = text
82
self.related = null
83
si
84
85
init(
86
is_analysis: bool,
87
is_compile_expressions: bool,
88
severity: DiagnosticSeverity,
89
location: LOCATION,
90
code: string?,
91
text: string,
92
related: Collections.LIST[RELATED_LOCATION]?
93
) is
94
self.is_analysis = is_analysis
95
self.is_compile_expressions = is_compile_expressions
96
self.severity = severity
97
self.location = location
98
self.code = code
99
self.text = text
100
self.related = related
101
si
102
103
to_string() -> string => "{severity} {location} {text}"
104
si
105
106
// An editor-only inlay hint: ghost text at a source position, never
107
// published as a diagnostic and surfaced only by the INLAY_HINTS
108
// analysis query. Recorded in a per-path list of its own rather than
109
// among the diagnostics, so every diagnostics-side count, walk and
110
// clear is free of inlays and a kind producing one hint per statement
111
// cannot tax the diagnostics pipeline.
112
//
113
// `text` is the ghost-text label; `detail` the hover payload - for a
114
// narrowing-introduction carrier it holds only the narrowed-to type,
115
// with the hover sentence assembled at read-out, and for a kill
116
// carrier the full hover text.
117
struct INLAY is
118
is_analysis: bool
119
is_compile_expressions: bool
120
121
location: LOCATION
122
code: string?
123
text: string
124
detail: string?
125
126
init(
127
is_analysis: bool,
128
is_compile_expressions: bool,
129
location: LOCATION,
130
code: string?,
131
text: string,
132
detail: string?
133
) is
134
self.is_analysis = is_analysis
135
self.is_compile_expressions = is_compile_expressions
136
self.location = location
137
self.code = code
138
self.text = text
139
self.detail = detail
140
si
141
si
142
143
trait DiagnosticFormatter is
144
need_clear_errors: bool => false
145
format(diagnostic: DIAGNOSTIC_MESSAGE) -> string?
146
si
147
148
class TAB_DELIMITED_DIAGNOSTIC_FORMATTER: DiagnosticFormatter is
149
need_clear_errors: bool => true
150
151
init() is
152
si
153
154
format(diagnostic: DIAGNOSTIC_MESSAGE) -> string? is
155
let location = diagnostic.location
156
let severity =
157
if diagnostic.is_fatal then
158
cast int(DiagnosticSeverity.HINT)
159
else
160
cast int(diagnostic.severity)
161
fi
162
163
let message = diagnostic.text
164
let code = if diagnostic.code? then diagnostic.code; else ""; fi
165
166
let s = string.format(
167
"{{0}}\t{{1}}\t{{2}}\t{{3}}\t{{4}}\t{{5}}\t{{6}}\t{{7}}",
168
[
169
location.file_name,
170
location.start_line,
171
location.start_column,
172
location.end_line,
173
location.end_column+1,
174
cast int(severity),
175
message,
176
code
177
]
178
).replace('\n', ' ')
179
180
return s
181
si
182
si
183
184
class HUMAN_READABLE_DIAGNOSTIC_FORMATTER: DiagnosticFormatter is
185
init() is
186
si
187
188
format(diagnostic: DIAGNOSTIC_MESSAGE) -> string? is
189
if diagnostic.is_hint then
190
return null
191
fi
192
193
let location = diagnostic.location
194
let severity = diagnostic.severity.to_string().to_lower()
195
let message =
196
if diagnostic.code? then
197
"[{diagnostic.code}] {diagnostic.text}"
198
else
199
diagnostic.text
200
fi
201
202
let s mut = string.format(
203
"{{0}}: {{1}},{{2}}..{{3}},{{4}}: {{5}}: {{6}}",
204
[
205
location.file_name,
206
location.start_line,
207
location.start_column,
208
location.end_line,
209
location.end_column+1,
210
severity,
211
message
212
]
213
)
214
215
// Related locations are rendered textually so the CLI keeps
216
// the information a capable client shows as a jump-to link.
217
// One sitting on the diagnostic's own position is not a jump
218
// target - advice carried this way rather than in the message
219
// prose - so only its message is rendered.
220
if let related = diagnostic.related then
221
for r in related do
222
let rl = r.location
223
224
if rl =~ location then
225
s = "{s} [{r.message}]"
226
else
227
s = "{s} [{r.message}: {rl.file_name}: {rl.start_line},{rl.start_column}..{rl.end_line},{rl.end_column+1}]"
228
fi
229
od
230
fi
231
232
return s
233
si
234
si
235
236
// MSBuild's canonical error format:
237
//
238
// file(line,col,endLine,endCol): category code: text
239
//
240
// MSBuild recognises that shape and no other - a line it cannot parse
241
// is logged as untyped message text, so the build reports no error at
242
// the offending source position and counts no warnings at all. The
243
// category has to be exactly "error" or "warning"; the severity's own
244
// name is not one of those. The code slot takes the suppression slug,
245
// which lets a consuming project reach a single diagnostic through
246
// NoWarn / WarningsAsErrors / MSBuildWarningsAsMessages.
247
class MSBUILD_DIAGNOSTIC_FORMATTER: DiagnosticFormatter is
248
init() is
249
si
250
251
// The categories MSBuild parses. INFO has no counterpart, so it is
252
// written as plain text rather than inflating the warning count.
253
_category(severity: DiagnosticSeverity) -> string? static =>
254
if
255
severity == DiagnosticSeverity.ERROR \/
256
severity == DiagnosticSeverity.FATAL \/
257
severity == DiagnosticSeverity.EXCEPTION
258
then
259
"error"
260
elif severity == DiagnosticSeverity.WARN then
261
"warning"
262
else
263
null
264
fi
265
266
// The format is one diagnostic per line, so a text carrying line
267
// breaks would put everything after the first outside the shape
268
// MSBuild parses. An EXCEPTION diagnostic is exactly that: its
269
// text ends in a rendered .NET exception, stack trace included.
270
_one_line(s: string) -> string static =>
271
s.replace('\n', ' ').replace('\r', ' ')
272
273
format(diagnostic: Logging.DIAGNOSTIC_MESSAGE) -> string? is
274
// Editor-only carriers never reach a build log.
275
if diagnostic.is_hint then
276
return null
277
fi
278
279
let location = diagnostic.location
280
let message = diagnostic.text
281
let category = _category(diagnostic.severity)
282
let code = if diagnostic.code? then " {diagnostic.code}"; else ""; fi
283
284
// The uncategorised (info) path keeps the same head shape
285
// minus the category, so the shared tail below - code and
286
// related locations - applies to it too: `--warn-as-info`
287
// next to this flag must not drop the slug or the advice.
288
let head mut =
289
if category? then
290
"{location.file_name}({location.start_line},{location.start_column},{location.end_line},{location.end_column+1}): {category}{code}: "
291
else
292
"{location.file_name}({location.start_line},{location.start_column},{location.end_line},{location.end_column+1}){code}: "
293
fi
294
295
let s mut = "{head}{message}"
296
297
// Related locations are rendered textually so the CLI keeps
298
// the information a capable client shows as a jump-to link.
299
if let related = diagnostic.related then
300
for r in related do
301
let rl = r.location
302
303
if rl =~ location then
304
s = "{s} [{r.message}]"
305
else
306
s = "{s} [{r.message}: {rl.file_name}({rl.start_line},{rl.start_column})]"
307
fi
308
od
309
fi
310
311
return _one_line(s)
312
si
313
si
314
315
class DIAGNOSTICS_LIST is
316
_path: string
317
_dirty: bool
318
_deduplicator: DIAGNOSTIC_DEDUPLICATOR?
319
_diagnostics: LIST[DIAGNOSTIC_MESSAGE]
320
_inlays: LIST[INLAY]
321
322
// Positions of the statement boundaries the parser inferred
323
// (a terminator left off at end of line), as LOCATION-packed
324
// ints. Appended in source order during parse, so sorted by
325
// construction; homogeneous and per-statement, which is why
326
// they do not share the INLAY record the text-carrying kinds
327
// use. Wiped by clear(false), which every re-parse runs before
328
// regenerating them.
329
_inferred_terminators: LIST[int]
330
331
is_poisoned: bool
332
has_consumed_any: bool
333
has_consumed_error: bool
334
335
any_errors: bool => _diagnostics |> any(d => d.severity == DiagnosticSeverity.ERROR)
336
any_warnings: bool => _diagnostics |> any(d => d.severity == DiagnosticSeverity.WARN)
337
338
count: int => _diagnostics.count
339
error_count: int => _diagnostics |> filter(d => d.severity == DiagnosticSeverity.ERROR) |> count()
340
analysis_count: int => _diagnostics |> filter(d => d.is_analysis) |> count()
341
342
diagnostics: List[DIAGNOSTIC_MESSAGE] => _diagnostics
343
inlays: List[INLAY] => _inlays
344
inferred_terminators: List[int] => _inferred_terminators
345
346
init(path: string, deduplicate: bool) is
347
_path = path
348
349
if deduplicate then
350
_deduplicator = DIAGNOSTIC_DEDUPLICATOR()
351
fi
352
353
_diagnostics = LIST()
354
_inlays = LIST()
355
_inferred_terminators = LIST()
356
si
357
358
add(diagnostic_message: DIAGNOSTIC_MESSAGE) is
359
if let deduplicator = _deduplicator /\ !deduplicator.is_first(diagnostic_message) then
360
return
361
fi
362
363
_dirty = true
364
365
if diagnostic_message.severity == DiagnosticSeverity.FATAL \/ diagnostic_message.severity == DiagnosticSeverity.EXCEPTION then
366
is_poisoned = true
367
fi
368
369
_diagnostics.add(diagnostic_message)
370
si
371
372
add_inlay(inlay: INLAY) is
373
_dirty = true
374
375
_inlays.add(inlay)
376
si
377
378
add_inferred_terminators(positions: Iterable[int]) is
379
_dirty = true
380
381
for p in positions do
382
_inferred_terminators.add(p)
383
od
384
si
385
386
mark_poisoned() is
387
is_poisoned = true
388
si
389
390
mark_consumed_any() is
391
has_consumed_any = true
392
si
393
394
mark_consumed_error() is
395
has_consumed_error = true
396
si
397
398
clear_consumed_any() is
399
has_consumed_any = false
400
si
401
402
clear_consumed_error() is
403
has_consumed_error = false
404
si
405
406
clear(analysis_only: bool) is
407
if analysis_only == false then
408
_dirty = true
409
_diagnostics.clear()
410
_inlays.clear()
411
_inferred_terminators.clear()
412
else
413
let n = LIST[DIAGNOSTIC_MESSAGE](_diagnostics.count)
414
415
for d in _diagnostics do
416
if !d.is_analysis then
417
n.add(d)
418
fi
419
od
420
421
_diagnostics = n
422
423
let ni = LIST[INLAY](_inlays.count)
424
425
for i in _inlays do
426
if !i.is_analysis then
427
ni.add(i)
428
fi
429
od
430
431
_inlays = ni
432
fi
433
434
_reseed_deduplicator()
435
si
436
437
// The record of what has been reported has to match what
438
// survives a clear, or a diagnostic that is cleared and then
439
// reported again - a warning whose suppression was removed -
440
// is taken for a repeat and dropped.
441
_reseed_deduplicator() is
442
if !_deduplicator? then
443
return
444
fi
445
446
let deduplicator = DIAGNOSTIC_DEDUPLICATOR()
447
448
for d in _diagnostics do
449
let _ = deduplicator.is_first(d)
450
od
451
452
_deduplicator = deduplicator
453
si
454
455
clear_global_declaration_diagnostics() is
456
let n = LIST[DIAGNOSTIC_MESSAGE](_diagnostics.count)
457
458
for d in _diagnostics do
459
if !d.is_analysis \/ d.is_compile_expressions then
460
n.add(d)
461
fi
462
od
463
464
_diagnostics = n
465
466
let ni = LIST[INLAY](_inlays.count)
467
468
for i in _inlays do
469
if !i.is_analysis \/ i.is_compile_expressions then
470
ni.add(i)
471
fi
472
od
473
474
_inlays = ni
475
476
_reseed_deduplicator()
477
si
478
479
// The complement of clear_global_declaration_diagnostics: drop
480
// only the expression-level diagnostics, keeping parse and
481
// declaration-level ones. Used before a compile-expressions
482
// re-walk of one file, which re-reports the expression
483
// diagnostics but re-runs no earlier pass - the analysis-mode
484
// on-demand recompile, and a batch build that recompiles a
485
// file's expressions between solve rounds.
486
// The same, for the expression-level output of one definition
487
// whose body alone is being re-walked.
488
clear_expression_diagnostics_within(span: LOCATION) is
489
let n = LIST[DIAGNOSTIC_MESSAGE](_diagnostics.count)
490
491
for d in _diagnostics do
492
if !(d.is_compile_expressions /\ span.contains(d.location)) then
493
n.add(d)
494
fi
495
od
496
497
_diagnostics = n
498
499
let ni = LIST[INLAY](_inlays.count)
500
501
for i in _inlays do
502
if !(i.is_compile_expressions /\ span.contains(i.location)) then
503
ni.add(i)
504
fi
505
od
506
507
_inlays = ni
508
509
_reseed_deduplicator()
510
si
511
512
clear_expression_diagnostics() is
513
let n = LIST[DIAGNOSTIC_MESSAGE](_diagnostics.count)
514
515
for d in _diagnostics do
516
if !d.is_compile_expressions then
517
n.add(d)
518
fi
519
od
520
521
_diagnostics = n
522
523
let ni = LIST[INLAY](_inlays.count)
524
525
for i in _inlays do
526
if !i.is_compile_expressions then
527
ni.add(i)
528
fi
529
od
530
531
_inlays = ni
532
533
_reseed_deduplicator()
534
si
535
536
clear_inlays(code: string) is
537
let n = LIST[INLAY](_inlays.count)
538
539
for i in _inlays do
540
let carrier_code = i.code
541
542
if !(carrier_code? /\ carrier_code.starts_with(code)) then
543
n.add(i)
544
fi
545
od
546
547
_inlays = n
548
si
549
550
write_all_diagnostics(writer: TextWriter, formatter: DiagnosticFormatter) is
551
let written_any mut = false
552
553
for diagnostic in _diagnostics do
554
let formatted = formatter.format(diagnostic)
555
556
if formatted? then
557
written_any = true
558
writer.write(formatted)
559
writer.write("\n")
560
fi
561
od
562
563
if !written_any /\ formatter.need_clear_errors then
564
// clear errors in the client
565
writer.write(_path)
566
writer.write("\n")
567
fi
568
si
569
si
570
571
class DIAGNOSTICS_STATE is
572
// Off for a test run, where a snapshot has to show exactly
573
// what the passes reported.
574
deduplicate: bool public
575
count: int => _diagnostics_by_source_path.values |> reduce(0, (r, d) => r + d.count)
576
is_poisoned: bool => _diagnostics_by_source_path.values |> any(d => d.is_poisoned)
577
has_consumed_error: bool
578
has_consumed_any: bool
579
any_errors: bool => _diagnostics_by_source_path.values |> any(d => d.any_errors)
580
any_warnings: bool => _diagnostics_by_source_path.values |> any(d => d.any_warnings)
581
582
paths_with_errors: Iterable[string] is
583
let result = LIST[string]()
584
585
for pair in _diagnostics_by_source_path do
586
if pair.value.any_errors then
587
result.add(pair.key)
588
fi
589
od
590
591
return result
592
si
593
594
diagnostic_paths: Iterable[string] => _diagnostics_by_source_path.keys
595
596
diagnostics_for(path: string) -> Iterable[DIAGNOSTIC_MESSAGE] is
597
let list: DIAGNOSTICS_LIST mut
598
599
if !_diagnostics_by_source_path.try_get_value(path, list ref) then
600
return LIST[DIAGNOSTIC_MESSAGE]()
601
fi
602
603
return list.diagnostics
604
si
605
606
_diagnostics_by_source_path: MutableMap[string, DIAGNOSTICS_LIST]
607
608
init(deduplicate: bool) is
609
self.deduplicate = deduplicate
610
_diagnostics_by_source_path = MAP[string, DIAGNOSTICS_LIST]()
611
si
612
613
clear() is
614
for i in _diagnostics_by_source_path.values do
615
i.clear(false)
616
od
617
618
init(deduplicate)
619
si
620
621
clear(source_path: string, analysis_only: bool) is
622
get_diagnostics_list(source_path).clear(analysis_only)
623
si
624
625
clear_global_declaration_diagnostics(source_path: string) is
626
get_diagnostics_list(source_path).clear_global_declaration_diagnostics()
627
si
628
629
clear_expression_diagnostics(source_path: string) is
630
get_diagnostics_list(source_path).clear_expression_diagnostics()
631
si
632
633
clear_expression_diagnostics_within(source_path: string, span: LOCATION) is
634
get_diagnostics_list(source_path).clear_expression_diagnostics_within(span)
635
si
636
637
clear_inlays(source_path: string, code: string) is
638
get_diagnostics_list(source_path).clear_inlays(code)
639
si
640
641
642
643
get_diagnostics_list(source_path: string) -> DIAGNOSTICS_LIST is
644
let diagnostics_for_path: DIAGNOSTICS_LIST mut
645
646
if !_diagnostics_by_source_path.try_get_value(source_path, diagnostics_for_path ref) then
647
diagnostics_for_path = DIAGNOSTICS_LIST(source_path, deduplicate)
648
649
_diagnostics_by_source_path.add(source_path, diagnostics_for_path)
650
fi
651
652
return diagnostics_for_path
653
si
654
655
add_diagnostic_message(source_path: string, message: DIAGNOSTIC_MESSAGE) is
656
get_diagnostics_list(source_path).add(message)
657
si
658
659
// Whether the file at `source_path` already carries a diagnostic
660
// with the code `code`.
661
has_code(source_path: string, code: string) -> bool is
662
for d in get_diagnostics_list(source_path).diagnostics do
663
if d.code =~ code then
664
return true
665
fi
666
od
667
668
return false
669
si
670
671
add_inlay(source_path: string, inlay: INLAY) is
672
get_diagnostics_list(source_path).add_inlay(inlay)
673
si
674
675
add_inferred_terminators(source_path: string, positions: Iterable[int]) is
676
get_diagnostics_list(source_path).add_inferred_terminators(positions)
677
si
678
679
mark_poisoned(source_path: string) is
680
get_diagnostics_list(source_path).mark_poisoned()
681
si
682
683
mark_consumed_error() is
684
has_consumed_error = true
685
si
686
687
mark_consumed_any() is
688
has_consumed_any = true
689
si
690
691
clear_consumed_error() is
692
for i in _diagnostics_by_source_path.values do
693
i.clear_consumed_error()
694
od
695
si
696
697
clear_consumed_any() is
698
for i in _diagnostics_by_source_path.values do
699
i.clear_consumed_any()
700
od
701
si
702
703
merge(state: DIAGNOSTICS_STATE) is
704
for i in state._diagnostics_by_source_path do
705
add_diagnostic_messages(i.key, i.value.diagnostics)
706
add_inlays(i.key, i.value.inlays)
707
add_inferred_terminators(i.key, i.value.inferred_terminators)
708
709
if i.value.is_poisoned then
710
mark_poisoned(i.key)
711
fi
712
od
713
714
if state.has_consumed_error then
715
mark_consumed_error()
716
fi
717
718
if state.has_consumed_any then
719
mark_consumed_any()
720
fi
721
si
722
723
add_diagnostic_messages(source_path: string, diagnostics: Iterable[DIAGNOSTIC_MESSAGE]) is
724
let list = get_diagnostics_list(source_path)
725
726
for d in diagnostics do
727
list.add(d)
728
od
729
si
730
731
add_inlays(source_path: string, inlays: Iterable[INLAY]) is
732
let list = get_diagnostics_list(source_path)
733
734
for i in inlays do
735
list.add_inlay(i)
736
od
737
si
738
739
write_all_diagnostics(writer: TextWriter, formatter: DiagnosticFormatter) is
740
for list in _diagnostics_by_source_path.values do
741
list.write_all_diagnostics(writer, formatter)
742
od
743
si
744
si
745
746
class DIAGNOSTICS_STORE: Logger is
747
_states: STACK[DIAGNOSTICS_STATE]
748
// The bottom, never-speculated diagnostics state. Lexer errors are
749
// written straight here (see lexer_error).
750
_base_state: DIAGNOSTICS_STATE
751
_suppressed_codes: Collections.SET[string]
752
_suppression_regions: SUPPRESSION_REGIONS
753
_is_quiet_file: ((string) -> bool)?
754
_quiet_by_file: Collections.MAP[string, bool]
755
_all_warnings_are_errors: bool
756
_error_codes: Collections.SET[string]
757
_hint_codes: Collections.SET[string]
758
_info_codes: Collections.SET[string]
759
_enabled_inlay_kinds: Collections.SET[string]
760
761
is_poisoned: bool => _states.peek().is_poisoned
762
has_consumed_error: bool => _states.peek().has_consumed_error
763
has_consumed_any: bool => _states.peek().has_consumed_any
764
765
error_count: int => _states.peek().count
766
any_errors: bool => _states.peek().any_errors
767
any_warnings: bool => _states.peek().any_warnings
768
paths_with_errors: Iterable[string] => _states.peek().paths_with_errors
769
diagnostic_paths: Iterable[string] => _states.peek().diagnostic_paths
770
771
diagnostics_for(path: string) -> Iterable[DIAGNOSTIC_MESSAGE] =>
772
_states.peek().diagnostics_for(path)
773
774
is_clean: bool => !any_errors /\ !has_consumed_error /\ !has_consumed_any
775
776
is_analysis: bool public
777
is_compile_expressions: bool public
778
779
_open_files: Collections.SET[string]
780
781
depth: int => _states.count
782
783
// Off for a test run, so a captured snapshot shows exactly what
784
// the passes reported and a pass that reports four times is
785
// visible as four lines rather than hidden.
786
_deduplicate: bool
787
788
deduplicate: bool => _deduplicate
789
790
keep_duplicate_diagnostics() is
791
_deduplicate = false
792
_base_state.deduplicate = false
793
si
794
795
init() is
796
_states = STACK()
797
_deduplicate = true
798
_base_state = DIAGNOSTICS_STATE(_deduplicate)
799
_states.push(_base_state)
800
_suppressed_codes = Collections.SET[string]()
801
802
// Suppressed by default: writing statement terminators out is
803
// a style choice rather than a defect, so the warning is on
804
// only when a project asks with `--warn redundant-semicolon`.
805
_suppressed_codes.add("redundant-semicolon")
806
807
_suppression_regions = SUPPRESSION_REGIONS()
808
_quiet_by_file = Collections.MAP[string, bool]()
809
_error_codes = Collections.SET[string]()
810
_hint_codes = Collections.SET[string]()
811
_info_codes = Collections.SET[string]()
812
_open_files = Collections.SET[string]()
813
814
// On by default: the sparse kinds. The terminator kind (one
815
// hint per inferred statement boundary) is off unless a
816
// client asks with `--inlay terminator`.
817
_enabled_inlay_kinds = Collections.SET[string]()
818
_enabled_inlay_kinds.add(INLAY_KINDS.NARROWING)
819
_enabled_inlay_kinds.add(INLAY_KINDS.DEFINITION_VIRTUALITY)
820
si
821
822
enable_inlay_kind(kind: string) is
823
_enabled_inlay_kinds.add(kind)
824
si
825
826
disable_inlay_kind(kind: string) is
827
_enabled_inlay_kinds.remove(kind)
828
si
829
830
is_inlay_kind_enabled(kind: string) -> bool =>
831
_enabled_inlay_kinds.contains(kind)
832
833
// Returns the paths whose open state changed - opened or closed.
834
// Only those files' hint gating moves, so only their expression
835
// walks have anything to redo.
836
set_open_files(paths: Collections.Iterable[string]) -> Collections.List[string] is
837
let previous = _open_files
838
_open_files = Collections.SET[string]()
839
840
let changed = Collections.LIST[string]()
841
842
for path in paths do
843
_open_files.add(path)
844
845
if !previous.contains(path) then
846
changed.add(path)
847
fi
848
od
849
850
for path in previous do
851
if !_open_files.contains(path) then
852
changed.add(path)
853
fi
854
od
855
856
return changed
857
si
858
859
is_file_open(path: string?) -> bool => path? /\ _open_files.contains(path)
860
861
suppress(code: string) is
862
_suppressed_codes.add(code)
863
si
864
865
unsuppress(code: string) is
866
_suppressed_codes.remove(code)
867
si
868
869
is_suppressed(code: string?) -> bool =>
870
code? /\ _suppressed_codes.contains(code)
871
872
set_quiet_files(is_quiet: (string) -> bool) is
873
_is_quiet_file = is_quiet
874
_quiet_by_file.clear()
875
si
876
877
_is_quiet(location: LOCATION) -> bool is
878
let is_quiet_file = _is_quiet_file
879
880
if !is_quiet_file? then
881
return false
882
fi
883
884
let path = location.file_name
885
let known: bool mut
886
887
if _quiet_by_file.try_get_value(path, known ref) then
888
return known
889
fi
890
891
let result = is_quiet_file(path)
892
893
_quiet_by_file[path] = result
894
895
return result
896
si
897
898
set_all_warnings_are_errors(value: bool) is
899
_all_warnings_are_errors = value
900
si
901
902
promote_to_error(code: string) is
903
_error_codes.add(code)
904
si
905
906
is_promoted_to_error(code: string?) -> bool =>
907
_all_warnings_are_errors \/ (code? /\ _error_codes.contains(code))
908
909
demote_to_hint(code: string) is
910
_hint_codes.add(code)
911
si
912
913
is_demoted_to_hint(code: string?) -> bool =>
914
code? /\ _hint_codes.contains(code)
915
916
demote_to_info(code: string) is
917
_info_codes.add(code)
918
si
919
920
is_demoted_to_info(code: string?) -> bool =>
921
code? /\ _info_codes.contains(code)
922
923
want_hint_for(location: LOCATION?) -> bool =>
924
is_analysis /\ location? /\ is_file_open(location.file_name)
925
926
register_suppression_region(region: LOCATION, code: string) is
927
_suppression_regions.register(region, code)
928
si
929
930
clear_suppression_regions(path: string) is
931
_suppression_regions.clear(path)
932
si
933
934
clear_suppression_regions() is
935
_suppression_regions.clear()
936
si
937
938
is_suppressed(code: string?, location: LOCATION) -> bool =>
939
code? /\
940
(_suppressed_codes.contains(code) \/ _suppression_regions.contains(code, location))
941
942
start_analysis() is
943
is_analysis = true
944
si
945
946
end_analysis() is
947
is_analysis = false
948
is_compile_expressions = false
949
si
950
951
set_is_compiling_expressions(value: bool) is
952
is_compile_expressions = value
953
si
954
955
only: DIAGNOSTICS_STATE is
956
assert _states.count == 1 else "expected exactly one stacked diagnostics state, found {_states.count}"
957
958
return _states.peek()
959
si
960
961
top: DIAGNOSTICS_STATE is
962
assert _states.count >= 1 else "expected at least one stacked diagnostics state"
963
964
return _states.peek()
965
si
966
967
pop() -> DIAGNOSTICS_STATE is
968
assert _states.count >= 1 else "expected at least one stacked diagnostics state"
969
970
return _states.pop()
971
si
972
973
clear(source_path: string, analysis_only: bool) is
974
only.clear(source_path, analysis_only)
975
si
976
977
clear_global_declaration_diagnostics(source_path: string) is
978
only.clear_global_declaration_diagnostics(source_path)
979
si
980
981
clear_expression_diagnostics(source_path: string) is
982
only.clear_expression_diagnostics(source_path)
983
si
984
985
clear_expression_diagnostics_within(source_path: string, span: LOCATION) is
986
only.clear_expression_diagnostics_within(source_path, span)
987
si
988
989
clear_inlays(source_path: string, code: string) is
990
only.clear_inlays(source_path, code)
991
si
992
993
994
speculate() is
995
_states.push(DIAGNOSTICS_STATE(_deduplicate))
996
si
997
998
roll_back() -> DIAGNOSTICS_STATE =>
999
pop()
1000
1001
commit() is
1002
let to_merge = pop()
1003
1004
top.merge(to_merge)
1005
si
1006
1007
mark() -> int => _states.count
1008
release(mark: int) is
1009
while _states.count > mark do
1010
pop()
1011
od
1012
si
1013
1014
speculate_then_commit() -> LOGGER_SPECULATE_THEN_COMMIT =>
1015
LOGGER_SPECULATE_THEN_COMMIT(self)
1016
1017
speculate_then_backtrack() -> LOGGER_SPECULATE_THEN_BACKTRACK =>
1018
LOGGER_SPECULATE_THEN_BACKTRACK(self)
1019
1020
mark_then_release() -> MARK_THEN_RELEASE =>
1021
MARK_THEN_RELEASE(self)
1022
1023
merge(state: DIAGNOSTICS_STATE) is
1024
top.merge(state)
1025
si
1026
1027
write_all_diagnostics(writer: TextWriter, formatter: DiagnosticFormatter) is
1028
only.write_all_diagnostics(writer, formatter)
1029
si
1030
1031
poison(location: LOCATION) is
1032
top.mark_poisoned(location.file_name)
1033
si
1034
1035
mark_consumed_error() is
1036
top.mark_consumed_error()
1037
si
1038
1039
mark_consumed_any() is
1040
top.mark_consumed_any()
1041
si
1042
1043
mark_consumed_any_if(consumed: bool) is
1044
if consumed then
1045
top.mark_consumed_any()
1046
fi
1047
si
1048
1049
clear_consumed_any() is
1050
top.clear_consumed_any()
1051
si
1052
1053
clear_consumed_error() is
1054
top.clear_consumed_error()
1055
si
1056
1057
exception(location: LOCATION, exception: Exception, message: string) is
1058
debug_always("{location} exception: {exception.to_string().replace_line_endings(" ")}")
1059
1060
// Every pass over every file fails the same way when a built-in
1061
// type is missing, so the file carries the cause once, at its
1062
// start, as an error an editor shows. It goes to the base state,
1063
// since the failure can leave speculation open.
1064
if let missing: MissingBuiltInException = exception then
1065
let start = LOCATION(location.file_name, 1, 1, 1, 1)
1066
1067
if !_base_state.has_code(location.file_name, MissingBuiltInException.CODE) then
1068
_base_state.add_diagnostic_message(
1069
location.file_name,
1070
DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, DiagnosticSeverity.ERROR, start, MissingBuiltInException.CODE, missing.message))
1071
fi
1072
1073
return
1074
fi
1075
1076
if _states.count == 0 then
1077
debug_always("diagnostics store: exception depth {_states.count}: {location}: {message}: {exception}")
1078
fi
1079
1080
top.add_diagnostic_message(location.file_name, DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, DiagnosticSeverity.EXCEPTION, location, "{message}: {exception}"))
1081
si
1082
1083
exception(mark: int, location: LOCATION, exception: Exception, message: string) is
1084
release(mark)
1085
1086
self.exception(location, exception, message)
1087
si
1088
1089
fatal(location: LOCATION, message: string) is
1090
debug_always("{location}: fatal: {message}")
1091
1092
debug_always("diagnostics store: fatal depth {_states.count}: {location}: {message}")
1093
1094
top.add_diagnostic_message(location.file_name, DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, DiagnosticSeverity.FATAL, location, message))
1095
si
1096
1097
error(location: LOCATION, message: string) is
1098
if let listen = DIAGNOSTIC_TRACE.on_error then
1099
listen(location, message)
1100
fi
1101
1102
top.add_diagnostic_message(location.file_name, DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, DiagnosticSeverity.ERROR, location, message))
1103
si
1104
1105
error(location: LOCATION, message: string, related_location: LOCATION, related_message: string) is
1106
let related = LIST[RELATED_LOCATION]()
1107
1108
related.add(RELATED_LOCATION(related_location, related_message))
1109
1110
error(location, message, related)
1111
si
1112
1113
error(location: LOCATION, message: string, related: LIST[RELATED_LOCATION]) is
1114
if let usable = _usable_related(related) then
1115
top.add_diagnostic_message(
1116
location.file_name,
1117
DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, DiagnosticSeverity.ERROR, location, null, message, usable)
1118
)
1119
else
1120
error(location, message)
1121
fi
1122
si
1123
1124
// A related location sitting on a sentinel ("internal", "unknown",
1125
// "reflected")
1126
// is not a jump target - there is no file behind it - so it is
1127
// dropped rather than rendered as noise. Returns null when nothing
1128
// usable is left, so the caller falls back to the related-free form.
1129
_usable_related(related: LIST[RELATED_LOCATION]) -> LIST[RELATED_LOCATION]? is
1130
let usable = LIST[RELATED_LOCATION](related.count)
1131
1132
for r in related do
1133
if !r.location.is_reflected /\ !r.location.is_unknown /\ !r.location.is_internal then
1134
usable.add(r)
1135
fi
1136
od
1137
1138
if usable.count == 0 then
1139
return null
1140
fi
1141
1142
return usable
1143
si
1144
1145
// A lexical error comes from the tokenizer, which reads each source
1146
// position exactly once — parser speculation replays buffered tokens,
1147
// it never re-lexes. So a lexer error is produced once no matter how
1148
// the parser speculates over those tokens. It is written to the base
1149
// state rather than the current speculative one, so a parse that reads
1150
// the tokens speculatively and then backtracks does not roll it back
1151
// and lose it. Unlike a parse error — which the parser regenerates on
1152
// every re-walk and so must roll back to avoid duplicates — a lexer
1153
// error cannot duplicate, because nothing regenerates it.
1154
lexer_error(location: LOCATION, message: string) is
1155
environment_error(location, message)
1156
si
1157
1158
environment_error(location: LOCATION, message: string) is
1159
_base_state.add_diagnostic_message(location.file_name, DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, DiagnosticSeverity.ERROR, location, message))
1160
si
1161
1162
warn(location: LOCATION, message: string) is
1163
if _is_quiet(location) then
1164
return
1165
fi
1166
1167
let severity =
1168
if _all_warnings_are_errors then
1169
DiagnosticSeverity.ERROR
1170
else
1171
DiagnosticSeverity.WARN
1172
fi
1173
top.add_diagnostic_message(location.file_name, DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, severity, location, message))
1174
si
1175
1176
warn(location: LOCATION, code: string, message: string) is
1177
if _is_quiet(location) then
1178
return
1179
fi
1180
1181
if is_suppressed(code, location) then
1182
return
1183
fi
1184
1185
if is_demoted_to_hint(code) then
1186
// Emitted as an editor-only hint under the same gate as a
1187
// native hint: dropped in batch compilation, surfaced in
1188
// analysis mode only for a file the client has open.
1189
if !want_hint_for(location) then
1190
return
1191
fi
1192
1193
top.add_diagnostic_message(location.file_name, DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, DiagnosticSeverity.HINT, location, code, message))
1194
1195
return
1196
fi
1197
1198
if is_demoted_to_info(code) then
1199
// Unlike a hint, info stays a normal batch-visible
1200
// diagnostic; it is not subject to the editor-only gate.
1201
top.add_diagnostic_message(location.file_name, DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, DiagnosticSeverity.INFO, location, code, message))
1202
1203
return
1204
fi
1205
1206
let severity =
1207
if is_promoted_to_error(code) then
1208
DiagnosticSeverity.ERROR
1209
else
1210
DiagnosticSeverity.WARN
1211
fi
1212
1213
top.add_diagnostic_message(location.file_name, DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, severity, location, code, message))
1214
si
1215
1216
warn(location: LOCATION, code: string, message: string, related_location: LOCATION, related_message: string) is
1217
if _is_quiet(location) then
1218
return
1219
fi
1220
1221
let related = LIST[RELATED_LOCATION]()
1222
1223
related.add(RELATED_LOCATION(related_location, related_message))
1224
1225
warn(location, code, message, related)
1226
si
1227
1228
warn(location: LOCATION, code: string, message: string, related: LIST[RELATED_LOCATION]) is
1229
if _is_quiet(location) then
1230
return
1231
fi
1232
1233
if is_suppressed(code, location) then
1234
// Suppressed: the plain coded route drops it, exactly as
1235
// it would without a related location.
1236
warn(location, code, message)
1237
1238
return
1239
fi
1240
1241
if is_demoted_to_hint(code) then
1242
warn(location, code, message)
1243
1244
return
1245
fi
1246
1247
if is_demoted_to_info(code) then
1248
// Info is a normal batch-visible diagnostic, so its
1249
// related locations survive the demotion - the reader
1250
// still needs the advice and the slug to suppress by.
1251
top.add_diagnostic_message(
1252
location.file_name,
1253
DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, DiagnosticSeverity.INFO, location, code, message, _usable_related(related))
1254
)
1255
1256
return
1257
fi
1258
1259
let usable = _usable_related(related)
1260
1261
if !usable? then
1262
warn(location, code, message)
1263
1264
return
1265
fi
1266
1267
let severity =
1268
if is_promoted_to_error(code) then
1269
DiagnosticSeverity.ERROR
1270
else
1271
DiagnosticSeverity.WARN
1272
fi
1273
1274
top.add_diagnostic_message(
1275
location.file_name,
1276
DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, severity, location, code, message, usable)
1277
)
1278
si
1279
1280
info(location: LOCATION, message: string) is
1281
if _is_quiet(location) then
1282
return
1283
fi
1284
1285
top.add_diagnostic_message(location.file_name, DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, DiagnosticSeverity.INFO, location, message))
1286
si
1287
1288
info(location: LOCATION, code: string, message: string) is
1289
if _is_quiet(location) then
1290
return
1291
fi
1292
1293
if is_suppressed(code, location) then
1294
return
1295
fi
1296
1297
top.add_diagnostic_message(location.file_name, DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, DiagnosticSeverity.INFO, location, code, message))
1298
si
1299
1300
hint(location: LOCATION, message: string) is
1301
if _is_quiet(location) then
1302
return
1303
fi
1304
1305
top.add_diagnostic_message(location.file_name, DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, DiagnosticSeverity.HINT, location, message))
1306
si
1307
1308
hint(location: LOCATION, code: string, message: string) is
1309
if _is_quiet(location) then
1310
return
1311
fi
1312
1313
if is_suppressed(code, location) then
1314
return
1315
fi
1316
1317
top.add_diagnostic_message(location.file_name, DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, DiagnosticSeverity.HINT, location, code, message))
1318
si
1319
1320
hint(location: LOCATION, code: string, message: string, related_location: LOCATION, related_message: string) is
1321
if _is_quiet(location) then
1322
return
1323
fi
1324
1325
if is_suppressed(code, location) then
1326
return
1327
fi
1328
1329
let related = LIST[RELATED_LOCATION]()
1330
1331
related.add(RELATED_LOCATION(related_location, related_message))
1332
1333
let usable = _usable_related(related)
1334
1335
if !usable? then
1336
hint(location, code, message)
1337
1338
return
1339
fi
1340
1341
top.add_diagnostic_message(
1342
location.file_name,
1343
DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, DiagnosticSeverity.HINT, location, code, message, usable)
1344
)
1345
si
1346
1347
inlay(location: LOCATION, code: string, label: string, detail: string?) is
1348
if is_suppressed(code, location) then
1349
return
1350
fi
1351
1352
// A disabled kind is never recorded, so it costs nothing to
1353
// collect or serve.
1354
let kind = INLAY_KINDS.for_code(code)
1355
1356
if kind? /\ !_enabled_inlay_kinds.contains(kind) then
1357
return
1358
fi
1359
1360
top.add_inlay(location.file_name, INLAY(is_analysis, is_compile_expressions, location, code, label, detail))
1361
si
1362
1363
inlays_for(path: string) -> Collections.Iterable[INLAY] =>
1364
top.get_diagnostics_list(path).inlays
1365
1366
// Positions of the statement boundaries the parser inferred in
1367
// `path`, as LOCATION-packed ints in source order. Served by the
1368
// INLAY_HINTS handler when the terminator kind is enabled.
1369
inferred_terminators_for(path: string) -> Collections.List[int] =>
1370
top.get_diagnostics_list(path).inferred_terminators
1371
1372
// LOCATION packs a position as (line << 12) | column, so a column
1373
// beyond this would carry into the line.
1374
_MAX_PACKED_COLUMN: int static => 0xFFF
1375
1376
note_inferred_terminator(location: LOCATION) is
1377
if !is_inlay_kind_enabled(INLAY_KINDS.TERMINATOR) then
1378
return
1379
fi
1380
1381
// The hint sits where the `;` would have gone: one past the
1382
// end of the construct being terminated. A LOCATION's end
1383
// column is inclusive - it is the construct's last character,
1384
// which is why every diagnostic on the wire sends end_column + 1
1385
// - so the position is one to the right of it. Capped at the
1386
// 12 bits the packed pair holds.
1387
let column = System.Math.min(location.end_column + 1, _MAX_PACKED_COLUMN)
1388
1389
top.get_diagnostics_list(location.file_name)
1390
.add_inferred_terminators([LOCATION.pair(location.end_line, column)])
1391
si
1392
1393
poison(location: LOCATION, message: string) is
1394
top.add_diagnostic_message(location.file_name, DIAGNOSTIC_MESSAGE(is_analysis, is_compile_expressions, DiagnosticSeverity.FATAL, location, message))
1395
top.mark_poisoned(location.file_name)
1396
si
1397
1398
write_poison_messages() is
1399
if is_poisoned then
1400
debug_always("internal compiler error")
1401
IO.Std.error.flush()
1402
fi
1403
si
1404
si
1405
si