Skip to content

Commit 57600b8

Browse files
committed
Add bullet points for error messages and use them in query cycle errors
1 parent 0559fbb commit 57600b8

File tree

71 files changed

+330
-243
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+330
-243
lines changed

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,28 @@ fn annotation_level_for_level(level: Level) -> annotate_snippets::level::Level<'
116116
Level::Fatal | Level::Error => annotate_snippets::level::ERROR,
117117
Level::ForceWarning | Level::Warning => annotate_snippets::Level::WARNING,
118118
Level::Note | Level::OnceNote => annotate_snippets::Level::NOTE,
119+
Level::BulletPoint => annotate_snippets::Level::NOTE.no_name(),
119120
Level::Help | Level::OnceHelp => annotate_snippets::Level::HELP,
120121
Level::FailureNote => annotate_snippets::Level::NOTE.no_name(),
121122
Level::Allow => panic!("Should not call with Allow"),
122123
Level::Expect => panic!("Should not call with Expect"),
123124
}
124125
}
125126

127+
fn message_level_for_level(level: Level) -> annotate_snippets::level::Level<'static> {
128+
match level {
129+
Level::BulletPoint => annotate_snippets::Level::NOTE.no_name(),
130+
_ => annotation_level_for_level(level),
131+
}
132+
}
133+
134+
fn format_message_for_level(level: Level, msg: Cow<'static, str>) -> Cow<'static, str> {
135+
match level {
136+
Level::BulletPoint => Cow::Owned(format!(" - {msg}")),
137+
_ => msg,
138+
}
139+
}
140+
126141
impl AnnotateSnippetEmitter {
127142
pub fn new(dst: Destination) -> Self {
128143
Self {
@@ -174,11 +189,26 @@ impl AnnotateSnippetEmitter {
174189

175190
// If we don't have span information, emit and exit
176191
let Some(sm) = self.sm.as_ref() else {
177-
group = group.elements(children.iter().map(|c| {
178-
let msg = format_diag_messages(&c.messages, args).to_string();
179-
let level = annotation_level_for_level(c.level);
180-
level.message(msg)
181-
}));
192+
for c in children {
193+
let msg = format_message_for_level(
194+
c.level,
195+
Cow::Owned(format_diag_messages(&c.messages, args).to_string()),
196+
);
197+
let annotation_level = annotation_level_for_level(c.level);
198+
let message_level = message_level_for_level(c.level);
199+
200+
if c.level == Level::BulletPoint
201+
&& !c.span.has_primary_spans()
202+
&& !c.span.has_span_labels()
203+
{
204+
report.push(std::mem::replace(
205+
&mut group,
206+
Group::with_title(annotation_level.secondary_title(msg)),
207+
));
208+
} else {
209+
group = group.element(message_level.message(msg));
210+
}
211+
}
182212

183213
report.push(group);
184214
if let Err(e) = emit_to_destination(
@@ -239,7 +269,8 @@ impl AnnotateSnippetEmitter {
239269
}
240270

241271
for c in children {
242-
let level = annotation_level_for_level(c.level);
272+
let annotation_level = annotation_level_for_level(c.level);
273+
let message_level = message_level_for_level(c.level);
243274

244275
// If at least one portion of the message is styled, we need to
245276
// "pre-style" the message
@@ -248,16 +279,24 @@ impl AnnotateSnippetEmitter {
248279
} else {
249280
format_diag_messages(&c.messages, args)
250281
};
282+
let msg = format_message_for_level(c.level, msg);
251283

252284
// This is a secondary message with no span info
253285
if !c.span.has_primary_spans() && !c.span.has_span_labels() {
254-
group = group.element(level.clone().message(msg));
286+
if c.level == Level::BulletPoint {
287+
report.push(std::mem::replace(
288+
&mut group,
289+
Group::with_title(annotation_level.secondary_title(msg)),
290+
));
291+
} else {
292+
group = group.element(message_level.message(msg));
293+
}
255294
continue;
256295
}
257296

258297
report.push(std::mem::replace(
259298
&mut group,
260-
Group::with_title(level.clone().secondary_title(msg)),
299+
Group::with_title(annotation_level.clone().secondary_title(msg)),
261300
));
262301

263302
let mut file_ann = collect_annotations(args, &c.span, sm);
@@ -286,7 +325,7 @@ impl AnnotateSnippetEmitter {
286325
file_idx,
287326
&mut report,
288327
group,
289-
&level,
328+
&annotation_level,
290329
);
291330
}
292331
}

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ impl DiagInner {
295295
Level::ForceWarning
296296
| Level::Warning
297297
| Level::Note
298+
| Level::BulletPoint
298299
| Level::OnceNote
299300
| Level::Help
300301
| Level::OnceHelp
@@ -686,6 +687,20 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
686687
self
687688
}
688689

690+
with_fn! { with_bullet_point,
691+
/// Add a bullet point to this diagnostic.
692+
pub fn bullet_point(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
693+
self.sub(Level::BulletPoint, msg, MultiSpan::new());
694+
self
695+
} }
696+
697+
with_fn! { with_span_bullet_point,
698+
/// Add a bullet point to this diagnostic.
699+
pub fn span_bullet_point(&mut self, sp: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) -> &mut Self {
700+
self.sub(Level::BulletPoint, msg, sp.into());
701+
self
702+
} }
703+
689704
pub fn highlighted_span_note(
690705
&mut self,
691706
span: impl Into<MultiSpan>,

compiler/rustc_errors/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,8 @@ impl<'a> DiagCtxtHandle<'a> {
600600
DelayedBug => {
601601
return self.inner.borrow_mut().emit_diagnostic(diag, self.tainted_with_errors);
602602
}
603-
ForceWarning | Warning | Note | OnceNote | Help | OnceHelp | FailureNote | Allow
604-
| Expect => None,
603+
ForceWarning | Warning | Note | BulletPoint | OnceNote | Help | OnceHelp
604+
| FailureNote | Allow | Expect => None,
605605
};
606606

607607
// FIXME(Centril, #69537): Consider reintroducing panic on overwriting a stashed diagnostic
@@ -1261,7 +1261,7 @@ impl DiagCtxtInner {
12611261
return None;
12621262
}
12631263
}
1264-
Note | Help | FailureNote => {}
1264+
Note | BulletPoint | Help | FailureNote => {}
12651265
OnceNote | OnceHelp => panic!("bad level: {:?}", diagnostic.level),
12661266
Allow => {
12671267
// Nothing emitted for allowed lints.
@@ -1531,6 +1531,7 @@ impl DelayedDiagInner {
15311531
/// | ForceWarning | - | () | yes | - | lint-only
15321532
/// | Warning | - | () | yes | yes | yes
15331533
/// | Note | - | () | rare | yes | -
1534+
/// | BulletPoint | - | () | - | yes | -
15341535
/// | OnceNote | - | () | - | yes | lint-only
15351536
/// | Help | - | () | rare | yes | -
15361537
/// | OnceHelp | - | () | - | yes | lint-only
@@ -1574,6 +1575,9 @@ pub enum Level {
15741575
/// A message giving additional context.
15751576
Note,
15761577

1578+
/// A message rendered as a bullet point.
1579+
BulletPoint,
1580+
15771581
/// A note that is only emitted once.
15781582
OnceNote,
15791583

@@ -1611,7 +1615,7 @@ impl Level {
16111615
AnsiColor::Yellow.on_default()
16121616
}
16131617
}
1614-
Note | OnceNote => AnsiColor::BrightGreen.on_default(),
1618+
Note | BulletPoint | OnceNote => AnsiColor::BrightGreen.on_default(),
16151619
Help | OnceHelp => AnsiColor::BrightCyan.on_default(),
16161620
FailureNote => anstyle::Style::new(),
16171621
Allow | Expect => unreachable!(),
@@ -1624,6 +1628,7 @@ impl Level {
16241628
Fatal | Error => "error",
16251629
ForceWarning | Warning => "warning",
16261630
Note | OnceNote => "note",
1631+
BulletPoint => "bullet-point",
16271632
Help | OnceHelp => "help",
16281633
FailureNote => "failure-note",
16291634
Allow | Expect => unreachable!(),

compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ impl DiagnosticDeriveVariantBuilder {
237237
let fn_ident = format_ident!("{}", subdiag);
238238
match subdiag {
239239
SubdiagnosticKind::Note
240+
| SubdiagnosticKind::BulletPoint
240241
| SubdiagnosticKind::NoteOnce
241242
| SubdiagnosticKind::Help
242243
| SubdiagnosticKind::HelpOnce
@@ -362,6 +363,7 @@ impl DiagnosticDeriveVariantBuilder {
362363
Ok(self.add_spanned_subdiagnostic(binding, &fn_ident, message, variant))
363364
}
364365
SubdiagnosticKind::Note
366+
| SubdiagnosticKind::BulletPoint
365367
| SubdiagnosticKind::NoteOnce
366368
| SubdiagnosticKind::Help
367369
| SubdiagnosticKind::HelpOnce

compiler/rustc_macros/src/diagnostics/utils.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,8 @@ pub(super) enum SubdiagnosticKind {
560560
Label,
561561
/// `#[note(...)]`
562562
Note,
563+
/// `#[bullet_point(...)]`
564+
BulletPoint,
563565
/// `#[note_once(...)]`
564566
NoteOnce,
565567
/// `#[help(...)]`
@@ -612,6 +614,7 @@ impl SubdiagnosticVariant {
612614
let mut kind = match name {
613615
"label" => SubdiagnosticKind::Label,
614616
"note" => SubdiagnosticKind::Note,
617+
"bullet_point" => SubdiagnosticKind::BulletPoint,
615618
"note_once" => SubdiagnosticKind::NoteOnce,
616619
"help" => SubdiagnosticKind::Help,
617620
"help_once" => SubdiagnosticKind::HelpOnce,
@@ -672,6 +675,7 @@ impl SubdiagnosticVariant {
672675
match kind {
673676
SubdiagnosticKind::Label
674677
| SubdiagnosticKind::Note
678+
| SubdiagnosticKind::BulletPoint
675679
| SubdiagnosticKind::NoteOnce
676680
| SubdiagnosticKind::Help
677681
| SubdiagnosticKind::HelpOnce
@@ -819,6 +823,7 @@ impl SubdiagnosticVariant {
819823
}
820824
SubdiagnosticKind::Label
821825
| SubdiagnosticKind::Note
826+
| SubdiagnosticKind::BulletPoint
822827
| SubdiagnosticKind::NoteOnce
823828
| SubdiagnosticKind::Help
824829
| SubdiagnosticKind::HelpOnce
@@ -834,6 +839,7 @@ impl quote::IdentFragment for SubdiagnosticKind {
834839
match self {
835840
SubdiagnosticKind::Label => write!(f, "label"),
836841
SubdiagnosticKind::Note => write!(f, "note"),
842+
SubdiagnosticKind::BulletPoint => write!(f, "bullet_point"),
837843
SubdiagnosticKind::NoteOnce => write!(f, "note_once"),
838844
SubdiagnosticKind::Help => write!(f, "help"),
839845
SubdiagnosticKind::HelpOnce => write!(f, "help_once"),

compiler/rustc_macros/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ decl_derive!(
184184
help,
185185
help_once,
186186
note,
187+
bullet_point,
187188
note_once,
188189
warning,
189190
// field attributes
@@ -203,6 +204,7 @@ decl_derive!(
203204
help,
204205
help_once,
205206
note,
207+
bullet_point,
206208
note_once,
207209
warning,
208210
subdiagnostic,

compiler/rustc_query_impl/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub(crate) struct QueryOverflowNote {
2525
}
2626

2727
#[derive(Subdiagnostic)]
28-
#[note("...which requires {$desc}...")]
28+
#[bullet_point("which requires {$desc}...")]
2929
pub(crate) struct CycleStack {
3030
#[primary_span]
3131
pub span: Span,
@@ -34,9 +34,9 @@ pub(crate) struct CycleStack {
3434

3535
#[derive(Subdiagnostic)]
3636
pub(crate) enum StackCount {
37-
#[note("...which immediately requires {$stack_bottom} again")]
37+
#[bullet_point("which immediately requires {$stack_bottom} again")]
3838
Single { stack_bottom: String },
39-
#[note("...which again requires {$stack_bottom}, completing the cycle")]
39+
#[bullet_point("which again requires {$stack_bottom}, completing the cycle")]
4040
Multiple { stack_bottom: String },
4141
}
4242

src/tools/compiletest/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl ErrorKind {
2525
match s {
2626
"help" => ErrorKind::Help,
2727
"error" | "error: internal compiler error" => ErrorKind::Error,
28-
"note" | "failure-note" => ErrorKind::Note,
28+
"note" | "failure-note" | "bullet-point" => ErrorKind::Note,
2929
"warning" => ErrorKind::Warning,
3030
_ => panic!("unexpected compiler diagnostic kind `{s}`"),
3131
}

tests/ui/associated-consts/defaults-cyclic-fail.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ error[E0391]: cycle detected when simplifying constant for the type system `Tr::
44
LL | const A: u8 = Self::B;
55
| ^^^^^^^^^^^
66
|
7-
note: ...which requires const-evaluating + checking `Tr::A`...
7+
- which requires const-evaluating + checking `Tr::A`...
88
--> $DIR/defaults-cyclic-fail.rs:5:19
99
|
1010
LL | const A: u8 = Self::B;
1111
| ^^^^^^^
12-
note: ...which requires simplifying constant for the type system `Tr::B`...
12+
- which requires simplifying constant for the type system `Tr::B`...
1313
--> $DIR/defaults-cyclic-fail.rs:8:5
1414
|
1515
LL | const B: u8 = Self::A;
1616
| ^^^^^^^^^^^
17-
note: ...which requires const-evaluating + checking `Tr::B`...
17+
- which requires const-evaluating + checking `Tr::B`...
1818
--> $DIR/defaults-cyclic-fail.rs:8:19
1919
|
2020
LL | const B: u8 = Self::A;
2121
| ^^^^^^^
22-
= note: ...which again requires simplifying constant for the type system `Tr::A`, completing the cycle
22+
- which again requires simplifying constant for the type system `Tr::A`, completing the cycle
2323
note: cycle used when optimizing promoted MIR for `main`
2424
--> $DIR/defaults-cyclic-fail.rs:16:16
2525
|

tests/ui/associated-consts/issue-24949-assoc-const-static-recursion-impl.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ error[E0391]: cycle detected when checking if `IMPL_REF_BAR` is a trivial const
44
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
55
| ^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
note: ...which requires building MIR for `IMPL_REF_BAR`...
7+
- which requires building MIR for `IMPL_REF_BAR`...
88
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
99
|
1010
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
1111
| ^^^^^^^^^^^^^^^^^^^^^^^
12-
note: ...which requires checking if `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR` is a trivial const...
12+
- which requires checking if `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR` is a trivial const...
1313
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:5
1414
|
1515
LL | const BAR: u32 = IMPL_REF_BAR;
1616
| ^^^^^^^^^^^^^^
17-
note: ...which requires building MIR for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR`...
17+
- which requires building MIR for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR`...
1818
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:5
1919
|
2020
LL | const BAR: u32 = IMPL_REF_BAR;
2121
| ^^^^^^^^^^^^^^
22-
= note: ...which again requires checking if `IMPL_REF_BAR` is a trivial const, completing the cycle
22+
- which again requires checking if `IMPL_REF_BAR` is a trivial const, completing the cycle
2323
note: cycle used when simplifying constant for the type system `IMPL_REF_BAR`
2424
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
2525
|

0 commit comments

Comments
 (0)