Skip to content

Commit 3d4af01

Browse files
committed
Rename diagnostic span methods in accordance with their usage patterns
Diagnostic:: * labeled_primary_span -> span * labeled_secondary_span -> label * primary_span -> unlabeled_span * secondary_span -> unlabeled_secondary_span + made private * labeled_primary_spans -> spans * primary_spans -> unlabeled_spans
1 parent acb4aac commit 3d4af01

File tree

20 files changed

+192
-216
lines changed

20 files changed

+192
-216
lines changed

compiler/diagnostics/src/format/test.rs

+45-41
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn format_single_line_primary_highlight() {
3232

3333
let diagnostic = Diagnostic::error()
3434
.message("message")
35-
.primary_span(span(8, 11));
35+
.unlabeled_span(span(8, 11));
3636

3737
assert_format(
3838
&diagnostic,
@@ -51,7 +51,7 @@ fn format_two_line_primary_highlight() {
5151
let mut map = SourceMap::default();
5252
map.add_str(Anonymous, "alpha\nbeta\n");
5353

54-
let diagnostic = Diagnostic::error().primary_span(span(1, 9));
54+
let diagnostic = Diagnostic::error().unlabeled_span(span(1, 9));
5555

5656
assert_format(
5757
&diagnostic,
@@ -75,7 +75,7 @@ fn format_multi_line_primary_highlight() {
7575
let diagnostic = Diagnostic::error()
7676
.code(ErrorCode::E000)
7777
.message("explanation")
78-
.primary_span(span(9, 23));
78+
.unlabeled_span(span(9, 23));
7979

8080
assert_format(
8181
&diagnostic,
@@ -106,7 +106,7 @@ fn format_triple_digit_line_number() {
106106

107107
let diagnostic = Diagnostic::warning()
108108
.message("this is a sentence")
109-
.primary_span(span(124, 133));
109+
.unlabeled_span(span(124, 133));
110110

111111
assert_format(
112112
&diagnostic,
@@ -128,9 +128,9 @@ fn format_primary_secondary_highlights() {
128128
let diagnostic = Diagnostic::error()
129129
.code(ErrorCode::E001)
130130
.message("important")
131-
.primary_span(span(7, 11))
132-
.secondary_span(span(1, 4))
133-
.secondary_span(span(15, 17));
131+
.unlabeled_span(span(7, 11))
132+
.unlabeled_secondary_span(span(1, 4))
133+
.unlabeled_secondary_span(span(15, 17));
134134

135135
assert_format(
136136
&diagnostic,
@@ -161,8 +161,8 @@ fn format_primary_secondary_highlight_differing_line_number_widths() {
161161

162162
let diagnostic = Diagnostic::bug()
163163
.message("placeholder")
164-
.primary_span(span(3, 7))
165-
.secondary_span(span(19, 28));
164+
.unlabeled_span(span(3, 7))
165+
.unlabeled_secondary_span(span(19, 28));
166166

167167
assert_format(
168168
&diagnostic,
@@ -188,8 +188,8 @@ fn format_highlights_in_different_files() {
188188
map.add_str("TWO", "zyx");
189189

190190
let diagnostic = Diagnostic::debug()
191-
.primary_span(span(4, 5))
192-
.secondary_span(span(11, 13));
191+
.unlabeled_span(span(4, 5))
192+
.unlabeled_secondary_span(span(11, 13));
193193

194194
assert_format(
195195
&diagnostic,
@@ -215,8 +215,8 @@ fn format_highlights_same_line() {
215215

216216
let diagnostic = Diagnostic::error()
217217
.message("tag")
218-
.primary_span(span(1, 6))
219-
.secondary_span(span(5, 9));
218+
.unlabeled_span(span(1, 6))
219+
.unlabeled_secondary_span(span(5, 9));
220220

221221
assert_format(
222222
&diagnostic,
@@ -242,10 +242,10 @@ fn format_labeled_highlights() {
242242

243243
let diagnostic = Diagnostic::error()
244244
.message("labels")
245-
.labeled_primary_span(span(2, 4), "pointer")
246-
.labeled_secondary_span(span(7, 11), "content")
247-
.labeled_primary_span(span(12, 23), "explanation")
248-
.labeled_secondary_span(span(28, 33), "message");
245+
.span(span(2, 4), "pointer")
246+
.label(span(7, 11), "content")
247+
.span(span(12, 23), "explanation")
248+
.label(span(28, 33), "message");
249249

250250
assert_format(
251251
&diagnostic,
@@ -285,10 +285,10 @@ fn format_multi_line_labeled_highlights() {
285285

286286
let diagnostic = Diagnostic::error()
287287
.message("multi-line labels")
288-
.labeled_primary_span(span(2, 4), "pointer\ncontext\nfiller")
289-
.labeled_secondary_span(span(7, 11), "content\n indented")
290-
.labeled_primary_span(span(12, 23), "explanation\naddendum")
291-
.labeled_secondary_span(span(28, 33), "message\n\nbottom");
288+
.span(span(2, 4), "pointer\ncontext\nfiller")
289+
.label(span(7, 11), "content\n indented")
290+
.span(span(12, 23), "explanation\naddendum")
291+
.label(span(28, 33), "message\n\nbottom");
292292

293293
assert_format(
294294
&diagnostic,
@@ -334,10 +334,10 @@ fn format_multi_line_labeled_highlights_no_trailing_line_break() {
334334

335335
let diagnostic = Diagnostic::error()
336336
.message("multi-line labels")
337-
.labeled_primary_span(span(2, 4), "pointer\ncontext\nfiller")
338-
.labeled_secondary_span(span(7, 11), "content\n indented")
339-
.labeled_primary_span(span(12, 23), "explanation\naddendum")
340-
.labeled_secondary_span(span(28, 33), "message\n\nbottom");
337+
.span(span(2, 4), "pointer\ncontext\nfiller")
338+
.label(span(7, 11), "content\n indented")
339+
.span(span(12, 23), "explanation\naddendum")
340+
.label(span(28, 33), "message\n\nbottom");
341341

342342
assert_format(
343343
&diagnostic,
@@ -403,7 +403,7 @@ fn format_subdiagnostics() {
403403

404404
let diagnostic = Diagnostic::warning()
405405
.message("it")
406-
.primary_span(span(5, 7))
406+
.unlabeled_span(span(5, 7))
407407
.help("helpful")
408408
.help("less helpful");
409409

@@ -429,7 +429,7 @@ fn format_subdiagnostics_two_digit_line_numbers() {
429429

430430
let diagnostic = Diagnostic::warning()
431431
.message("it")
432-
.primary_span(span(104, 106))
432+
.unlabeled_span(span(104, 106))
433433
.help("helpful")
434434
.help("less helpful");
435435

@@ -455,7 +455,7 @@ fn format_multi_line_subdiagnostics() {
455455

456456
let diagnostic = Diagnostic::warning()
457457
.message("it")
458-
.primary_span(span(5, 7))
458+
.unlabeled_span(span(5, 7))
459459
.help("helpful\ntip\nhopefully")
460460
.help("less helpful\ntip\n");
461461

@@ -482,7 +482,7 @@ fn format_multiple_primary_highlights() {
482482
let mut map = SourceMap::default();
483483
map.add_str(Anonymous, "gamma\n");
484484

485-
let diagnostic = Diagnostic::error().primary_spans([span(1, 2), span(3, 4), span(5, 6)]);
485+
let diagnostic = Diagnostic::error().unlabeled_spans([span(1, 2), span(3, 4), span(5, 6)]);
486486

487487
assert_format(
488488
&diagnostic,
@@ -511,7 +511,9 @@ fn format_zero_length_highlight() {
511511
let mut map = SourceMap::default();
512512
map.add_str(Anonymous, "sample\n");
513513

514-
let diagnostic = Diagnostic::debug().message("nil").primary_span(span(3, 3));
514+
let diagnostic = Diagnostic::debug()
515+
.message("nil")
516+
.unlabeled_span(span(3, 3));
515517

516518
assert_format(
517519
&diagnostic,
@@ -530,7 +532,9 @@ fn format_zero_length_highlight_start_of_line() {
530532
let mut map = SourceMap::default();
531533
map.add_str(Anonymous, "sample\n");
532534

533-
let diagnostic = Diagnostic::debug().message("nil").primary_span(span(1, 1));
535+
let diagnostic = Diagnostic::debug()
536+
.message("nil")
537+
.unlabeled_span(span(1, 1));
534538

535539
assert_format(
536540
&diagnostic,
@@ -551,7 +555,7 @@ fn format_zero_length_secondary_highlight() {
551555

552556
let diagnostic = Diagnostic::debug()
553557
.message("nil")
554-
.secondary_span(span(3, 3));
558+
.unlabeled_secondary_span(span(3, 3));
555559

556560
assert_format(
557561
&diagnostic,
@@ -573,7 +577,7 @@ fn format_highlight_line_break() {
573577
"This is a sentence.\nThis is a follow-up sentence.\n",
574578
);
575579

576-
let diagnostic = Diagnostic::error().labeled_primary_span(span(20, 20), "EOL");
580+
let diagnostic = Diagnostic::error().span(span(20, 20), "EOL");
577581

578582
assert_format(
579583
&diagnostic,
@@ -592,7 +596,7 @@ fn format_highlight_end_of_input() {
592596
let mut map = SourceMap::default();
593597
map.add_str(Anonymous, "This is a sentence.");
594598

595-
let diagnostic = Diagnostic::error().labeled_primary_span(span(20, 20), "EOI");
599+
let diagnostic = Diagnostic::error().span(span(20, 20), "EOI");
596600

597601
assert_format(
598602
&diagnostic,
@@ -613,7 +617,7 @@ fn format_highlight_end_of_input_with_trailing_line_break() {
613617
let mut map = SourceMap::default();
614618
map.add_str(Anonymous, "This is a sentence.\n");
615619

616-
let diagnostic = Diagnostic::error().labeled_primary_span(span(21, 21), "EOI");
620+
let diagnostic = Diagnostic::error().span(span(21, 21), "EOI");
617621

618622
assert_format(
619623
&diagnostic,
@@ -638,7 +642,7 @@ fn format_highlight_containing_final_line_break() {
638642

639643
let diagnostic = Diagnostic::warning()
640644
.message("weird corner case")
641-
.primary_span(span(1, 21));
645+
.unlabeled_span(span(1, 21));
642646

643647
assert_format(
644648
&diagnostic,
@@ -661,7 +665,7 @@ fn format_highlight_containing_final_end_of_input() {
661665
let mut map = SourceMap::default();
662666
map.add_str(Anonymous, "EVERYTHING\n");
663667

664-
let diagnostic = Diagnostic::bug().primary_span(span(1, 13));
668+
let diagnostic = Diagnostic::bug().unlabeled_span(span(1, 13));
665669

666670
assert_format(
667671
&diagnostic,
@@ -682,7 +686,7 @@ fn format_highlight_in_empty_file() {
682686

683687
let diagnostic = Diagnostic::error()
684688
.message("this file has to contain something reasonable")
685-
.primary_span(span(1, 1));
689+
.unlabeled_span(span(1, 1));
686690

687691
assert_format(
688692
&diagnostic,
@@ -733,7 +737,7 @@ fn format_path_together_with_highlight() {
733737
let diagnostic = Diagnostic::error()
734738
.message("this file is not acceptable")
735739
.path("problematic.txt".into())
736-
.labeled_primary_span(span(20, 25), "because you set this");
740+
.span(span(20, 25), "because you set this");
737741

738742
assert_format(
739743
&diagnostic,
@@ -774,7 +778,7 @@ fn format_two_line_break_highlight_containing_first() {
774778
let mut map = SourceMap::default();
775779
map.add_str(Anonymous, "alpha\nbeta\n\ngamma");
776780

777-
let diagnostic = Diagnostic::bug().primary_span(span(1, 12));
781+
let diagnostic = Diagnostic::bug().unlabeled_span(span(1, 12));
778782

779783
// @Note I don't actually know how it should be rendered
780784
assert_format(
@@ -798,7 +802,7 @@ fn format_two_line_breaks_highlight_containing_second() {
798802
let mut map = SourceMap::default();
799803
map.add_str(Anonymous, "alpha\n\n");
800804

801-
let diagnostic = Diagnostic::bug().primary_span(span(1, 8));
805+
let diagnostic = Diagnostic::bug().unlabeled_span(span(1, 8));
802806

803807
// @Note I don't actually know how it should be rendered
804808
assert_format(

compiler/diagnostics/src/lib.rs

+21-33
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
//! The diagnostics system.
2-
//!
3-
//! # Unimplemented Features
4-
//!
5-
//! * (maybe) subdiagnostics with a span
6-
//! * display style: rich (current system) <-> short
7-
//! * a rust script (in /misc) that finds the lowest [`ErrorCode`] that can be used
8-
//! as well as any unused error codes (searching `compiler/`)
9-
#![feature(
10-
adt_const_params,
11-
anonymous_lifetime_in_impl_trait,
12-
associated_type_bounds,
13-
default_free_fn
14-
)]
2+
#![feature(adt_const_params, associated_type_bounds, default_free_fn)]
153
#![allow(incomplete_features)] // adt_const_params
164

175
pub use code::{Code, ErrorCode, LintCode};
@@ -59,7 +47,7 @@ impl<const S: Severity> Diagnostic<S> {
5947
self
6048
}
6149

62-
fn span(mut self, spanning: impl Spanning, label: Option<Str>, role: Role) -> Self {
50+
fn _span(mut self, spanning: impl Spanning, label: Option<Str>, role: Role) -> Self {
6351
self.untagged.highlights.insert(Highlight {
6452
span: spanning.span(),
6553
label: label.map(Into::into),
@@ -68,27 +56,27 @@ impl<const S: Severity> Diagnostic<S> {
6856
self
6957
}
7058

71-
/// Reference a code snippet as one of the focal points of the diagnostic.
72-
pub fn primary_span(self, spanning: impl Spanning) -> Self {
73-
self.span(spanning, None, Role::Primary)
74-
}
75-
7659
/// Reference and label a code snippet as one of the focal points of the diagnostic.
77-
pub fn labeled_primary_span(self, spanning: impl Spanning, label: impl Into<Str>) -> Self {
78-
self.span(spanning, Some(label.into()), Role::Primary)
60+
pub fn span(self, spanning: impl Spanning, label: impl Into<Str>) -> Self {
61+
self._span(spanning, Some(label.into()), Role::Primary)
7962
}
8063

81-
/// Reference a code snippet as auxiliary information for the diagnostic.
82-
pub fn secondary_span(self, spanning: impl Spanning) -> Self {
83-
self.span(spanning, None, Role::Secondary)
64+
/// Reference a code snippet as one of the focal points of the diagnostic.
65+
pub fn unlabeled_span(self, spanning: impl Spanning) -> Self {
66+
self._span(spanning, None, Role::Primary)
8467
}
8568

8669
/// Reference and label a code snippet as auxiliary information for the diagnostic.
87-
pub fn labeled_secondary_span(self, spanning: impl Spanning, label: impl Into<Str>) -> Self {
88-
self.span(spanning, Some(label.into()), Role::Secondary)
70+
pub fn label(self, spanning: impl Spanning, label: impl Into<Str>) -> Self {
71+
self._span(spanning, Some(label.into()), Role::Secondary)
72+
}
73+
74+
#[cfg(test)]
75+
fn unlabeled_secondary_span(self, spanning: impl Spanning) -> Self {
76+
self._span(spanning, None, Role::Secondary)
8977
}
9078

91-
fn spans<I>(mut self, spannings: I, label: Option<Str>, role: Role) -> Self
79+
fn _spans<I>(mut self, spannings: I, label: Option<Str>, role: Role) -> Self
9280
where
9381
I: Iterator<Item: Spanning>,
9482
{
@@ -101,20 +89,20 @@ impl<const S: Severity> Diagnostic<S> {
10189
self
10290
}
10391

104-
/// Reference several equally important code snippets.
105-
pub fn primary_spans<I>(self, spannings: I) -> Self
92+
/// Reference and label several very and equally important code snippets.
93+
pub fn spans<I>(self, spannings: I, label: impl Into<Str>) -> Self
10694
where
10795
I: IntoIterator<Item: Spanning>,
10896
{
109-
self.spans(spannings.into_iter(), None, Role::Primary)
97+
self._spans(spannings.into_iter(), Some(label.into()), Role::Primary)
11098
}
11199

112-
/// Reference and label several very and equally important code snippets.
113-
pub fn labeled_primary_spans<I>(self, spannings: I, label: impl Into<Str>) -> Self
100+
/// Reference several equally important code snippets.
101+
pub fn unlabeled_spans<I>(self, spannings: I) -> Self
114102
where
115103
I: IntoIterator<Item: Spanning>,
116104
{
117-
self.spans(spannings.into_iter(), Some(label.into()), Role::Primary)
105+
self._spans(spannings.into_iter(), None, Role::Primary)
118106
}
119107

120108
fn subdiagnostic(mut self, severity: Subseverity, message: Str) -> Self {

compiler/diagnostics/src/reporter.rs

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ use std::{
1313
};
1414
use utilities::{pluralize, Conjunction, ListingExt};
1515

16+
// @Task diagnostic formatting options
17+
// like display style: verbose (current default) vs. terse
18+
1619
/// A diagnostic reporter.
1720
pub struct Reporter {
1821
kind: ReporterKind,

compiler/driver/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ fn build_unit(
295295
Diagnostic::error()
296296
.message(message)
297297
.path(path.bare.into())
298-
.primary_span(path)
298+
.unlabeled_span(path)
299299
.note(error.format())
300300
.report(session.reporter())
301301
})?;
@@ -381,7 +381,7 @@ fn build_unit(
381381
unit.name,
382382
Session::PROGRAM_ENTRY_IDENTIFIER,
383383
))
384-
.primary_span(&session.shared_map()[file])
384+
.unlabeled_span(&session.shared_map()[file])
385385
.report(session.reporter()));
386386
}
387387

0 commit comments

Comments
 (0)