Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,28 @@ fn annotation_level_for_level(level: Level) -> annotate_snippets::level::Level<'
Level::Fatal | Level::Error => annotate_snippets::level::ERROR,
Level::ForceWarning | Level::Warning => annotate_snippets::Level::WARNING,
Level::Note | Level::OnceNote => annotate_snippets::Level::NOTE,
Level::BulletPoint => annotate_snippets::Level::NOTE.no_name(),
Level::Help | Level::OnceHelp => annotate_snippets::Level::HELP,
Level::FailureNote => annotate_snippets::Level::NOTE.no_name(),
Level::Allow => panic!("Should not call with Allow"),
Level::Expect => panic!("Should not call with Expect"),
}
}

fn message_level_for_level(level: Level) -> annotate_snippets::level::Level<'static> {
match level {
Level::BulletPoint => annotate_snippets::Level::NOTE.no_name(),
_ => annotation_level_for_level(level),
}
}

fn format_message_for_level(level: Level, msg: Cow<'static, str>) -> Cow<'static, str> {
match level {
Level::BulletPoint => Cow::Owned(format!(" - {msg}")),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work correctly to align the "bullet point" regardless of gutter width? In the test suite we normalize the output to LL |, but can you manually test a case where LL is single or triple digit? I highly suspect this is not correct.

_ => msg,
}
}

impl AnnotateSnippetEmitter {
pub fn new(dst: Destination) -> Self {
Self {
Expand Down Expand Up @@ -174,11 +189,26 @@ impl AnnotateSnippetEmitter {

// If we don't have span information, emit and exit
let Some(sm) = self.sm.as_ref() else {
group = group.elements(children.iter().map(|c| {
let msg = format_diag_messages(&c.messages, args).to_string();
let level = annotation_level_for_level(c.level);
level.message(msg)
}));
for c in children {
let msg = format_message_for_level(
c.level,
Cow::Owned(format_diag_messages(&c.messages, args).to_string()),
);
let annotation_level = annotation_level_for_level(c.level);
let message_level = message_level_for_level(c.level);

if c.level == Level::BulletPoint
&& !c.span.has_primary_spans()
&& !c.span.has_span_labels()
{
report.push(std::mem::replace(
&mut group,
Group::with_title(annotation_level.secondary_title(msg)),
));
} else {
group = group.element(message_level.message(msg));
}
}

report.push(group);
if let Err(e) = emit_to_destination(
Expand Down Expand Up @@ -239,7 +269,8 @@ impl AnnotateSnippetEmitter {
}

for c in children {
let level = annotation_level_for_level(c.level);
let annotation_level = annotation_level_for_level(c.level);
let message_level = message_level_for_level(c.level);

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

// This is a secondary message with no span info
if !c.span.has_primary_spans() && !c.span.has_span_labels() {
group = group.element(level.clone().message(msg));
if c.level == Level::BulletPoint {
report.push(std::mem::replace(
&mut group,
Group::with_title(annotation_level.secondary_title(msg)),
));
} else {
group = group.element(message_level.message(msg));
}
continue;
}

report.push(std::mem::replace(
&mut group,
Group::with_title(level.clone().secondary_title(msg)),
Group::with_title(annotation_level.clone().secondary_title(msg)),
));

let mut file_ann = collect_annotations(args, &c.span, sm);
Expand Down Expand Up @@ -286,7 +325,7 @@ impl AnnotateSnippetEmitter {
file_idx,
&mut report,
group,
&level,
&annotation_level,
);
}
}
Expand Down
15 changes: 15 additions & 0 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ impl DiagInner {
Level::ForceWarning
| Level::Warning
| Level::Note
| Level::BulletPoint
| Level::OnceNote
| Level::Help
| Level::OnceHelp
Expand Down Expand Up @@ -688,6 +689,20 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
self
}

with_fn! { with_bullet_point,
/// Add a bullet point to this diagnostic.
pub fn bullet_point(&mut self, msg: impl Into<DiagMessage>) -> &mut Self {
self.sub(Level::BulletPoint, msg, MultiSpan::new());
self
} }

with_fn! { with_span_bullet_point,
/// Add a bullet point to this diagnostic.
pub fn span_bullet_point(&mut self, sp: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) -> &mut Self {
self.sub(Level::BulletPoint, msg, sp.into());
self
} }

pub fn highlighted_span_note(
&mut self,
span: impl Into<MultiSpan>,
Expand Down
13 changes: 9 additions & 4 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,8 @@ impl<'a> DiagCtxtHandle<'a> {
DelayedBug => {
return self.inner.borrow_mut().emit_diagnostic(diag, self.tainted_with_errors);
}
ForceWarning | Warning | Note | OnceNote | Help | OnceHelp | FailureNote | Allow
| Expect => None,
ForceWarning | Warning | Note | BulletPoint | OnceNote | Help | OnceHelp
| FailureNote | Allow | Expect => None,
Comment on lines -603 to +604
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would have been nice to add BulletPoint to the end of the or-pattern to make the diff faster to skim :)

};

// FIXME(Centril, #69537): Consider reintroducing panic on overwriting a stashed diagnostic
Expand Down Expand Up @@ -1261,7 +1261,7 @@ impl DiagCtxtInner {
return None;
}
}
Note | Help | FailureNote => {}
Note | BulletPoint | Help | FailureNote => {}
OnceNote | OnceHelp => panic!("bad level: {:?}", diagnostic.level),
Allow => {
// Nothing emitted for allowed lints.
Expand Down Expand Up @@ -1531,6 +1531,7 @@ impl DelayedDiagInner {
/// | ForceWarning | - | () | yes | - | lint-only
/// | Warning | - | () | yes | yes | yes
/// | Note | - | () | rare | yes | -
/// | BulletPoint | - | () | - | yes | -
/// | OnceNote | - | () | - | yes | lint-only
/// | Help | - | () | rare | yes | -
/// | OnceHelp | - | () | - | yes | lint-only
Expand Down Expand Up @@ -1574,6 +1575,9 @@ pub enum Level {
/// A message giving additional context.
Note,

/// A message rendered as a bullet point.
BulletPoint,

/// A note that is only emitted once.
OnceNote,

Expand Down Expand Up @@ -1611,7 +1615,7 @@ impl Level {
AnsiColor::Yellow.on_default()
}
}
Note | OnceNote => AnsiColor::BrightGreen.on_default(),
Note | BulletPoint | OnceNote => AnsiColor::BrightGreen.on_default(),
Help | OnceHelp => AnsiColor::BrightCyan.on_default(),
FailureNote => anstyle::Style::new(),
Allow | Expect => unreachable!(),
Expand All @@ -1624,6 +1628,7 @@ impl Level {
Fatal | Error => "error",
ForceWarning | Warning => "warning",
Note | OnceNote => "note",
BulletPoint => "bullet-point",
Help | OnceHelp => "help",
FailureNote => "failure-note",
Allow | Expect => unreachable!(),
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ impl DiagnosticDeriveVariantBuilder {
let fn_ident = format_ident!("{}", subdiag);
match subdiag {
SubdiagnosticKind::Note
| SubdiagnosticKind::BulletPoint
| SubdiagnosticKind::NoteOnce
| SubdiagnosticKind::Help
| SubdiagnosticKind::HelpOnce
Expand Down Expand Up @@ -362,6 +363,7 @@ impl DiagnosticDeriveVariantBuilder {
Ok(self.add_spanned_subdiagnostic(binding, &fn_ident, message, variant))
}
SubdiagnosticKind::Note
| SubdiagnosticKind::BulletPoint
| SubdiagnosticKind::NoteOnce
| SubdiagnosticKind::Help
| SubdiagnosticKind::HelpOnce
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_macros/src/diagnostics/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,8 @@ pub(super) enum SubdiagnosticKind {
Label,
/// `#[note(...)]`
Note,
/// `#[bullet_point(...)]`
BulletPoint,
/// `#[note_once(...)]`
NoteOnce,
/// `#[help(...)]`
Expand Down Expand Up @@ -612,6 +614,7 @@ impl SubdiagnosticVariant {
let mut kind = match name {
"label" => SubdiagnosticKind::Label,
"note" => SubdiagnosticKind::Note,
"bullet_point" => SubdiagnosticKind::BulletPoint,
"note_once" => SubdiagnosticKind::NoteOnce,
"help" => SubdiagnosticKind::Help,
"help_once" => SubdiagnosticKind::HelpOnce,
Expand Down Expand Up @@ -672,6 +675,7 @@ impl SubdiagnosticVariant {
match kind {
SubdiagnosticKind::Label
| SubdiagnosticKind::Note
| SubdiagnosticKind::BulletPoint
| SubdiagnosticKind::NoteOnce
| SubdiagnosticKind::Help
| SubdiagnosticKind::HelpOnce
Expand Down Expand Up @@ -819,6 +823,7 @@ impl SubdiagnosticVariant {
}
SubdiagnosticKind::Label
| SubdiagnosticKind::Note
| SubdiagnosticKind::BulletPoint
| SubdiagnosticKind::NoteOnce
| SubdiagnosticKind::Help
| SubdiagnosticKind::HelpOnce
Expand All @@ -834,6 +839,7 @@ impl quote::IdentFragment for SubdiagnosticKind {
match self {
SubdiagnosticKind::Label => write!(f, "label"),
SubdiagnosticKind::Note => write!(f, "note"),
SubdiagnosticKind::BulletPoint => write!(f, "bullet_point"),
SubdiagnosticKind::NoteOnce => write!(f, "note_once"),
SubdiagnosticKind::Help => write!(f, "help"),
SubdiagnosticKind::HelpOnce => write!(f, "help_once"),
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ decl_derive!(
help,
help_once,
note,
bullet_point,
note_once,
warning,
// field attributes
Expand All @@ -203,6 +204,7 @@ decl_derive!(
help,
help_once,
note,
bullet_point,
note_once,
warning,
subdiagnostic,
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_query_impl/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) struct QueryOverflowNote {
}

#[derive(Subdiagnostic)]
#[note("...which requires {$desc}...")]
#[bullet_point("which requires {$desc}...")]
pub(crate) struct CycleStack {
#[primary_span]
pub span: Span,
Expand All @@ -34,9 +34,9 @@ pub(crate) struct CycleStack {

#[derive(Subdiagnostic)]
pub(crate) enum StackCount {
#[note("...which immediately requires {$stack_bottom} again")]
#[bullet_point("which immediately requires {$stack_bottom} again")]
Single { stack_bottom: String },
#[note("...which again requires {$stack_bottom}, completing the cycle")]
#[bullet_point("which again requires {$stack_bottom}, completing the cycle")]
Multiple { stack_bottom: String },
}

Expand Down
2 changes: 1 addition & 1 deletion src/tools/compiletest/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl ErrorKind {
match s {
"help" => ErrorKind::Help,
"error" | "error: internal compiler error" => ErrorKind::Error,
"note" | "failure-note" => ErrorKind::Note,
"note" | "failure-note" | "bullet-point" => ErrorKind::Note,
"warning" => ErrorKind::Warning,
_ => panic!("unexpected compiler diagnostic kind `{s}`"),
}
Expand Down
3 changes: 2 additions & 1 deletion tests/rustdoc-ui/private-type-cycle-dyn-110629.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ error[E0391]: cycle detected when expanding type alias `Bar`
LL | type Bar<'a, 'b> = Box<dyn PartialEq<Bar<'a, 'b>>>;
| ^^^^^^^^^^^
|
= note: ...which immediately requires expanding type alias `Bar` again
- which immediately requires expanding type alias `Bar` again
|
= note: type aliases cannot be recursive
= help: consider using a struct, enum, or union instead to break the cycle
= help: see <https://doc.rust-lang.org/reference/types.html#recursive-types> for more information
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/associated-consts/defaults-cyclic-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ error[E0391]: cycle detected when simplifying constant for the type system `Tr::
LL | const A: u8 = Self::B;
| ^^^^^^^^^^^
|
note: ...which requires const-evaluating + checking `Tr::A`...
- which requires const-evaluating + checking `Tr::A`...
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm intrigued how this will render under human-unicode or annotate-snippets equivalent. (Need to do a proper review of the main logic still.)

--> $DIR/defaults-cyclic-fail.rs:5:19
|
LL | const A: u8 = Self::B;
| ^^^^^^^
note: ...which requires simplifying constant for the type system `Tr::B`...
- which requires simplifying constant for the type system `Tr::B`...
--> $DIR/defaults-cyclic-fail.rs:8:5
|
LL | const B: u8 = Self::A;
| ^^^^^^^^^^^
note: ...which requires const-evaluating + checking `Tr::B`...
- which requires const-evaluating + checking `Tr::B`...
--> $DIR/defaults-cyclic-fail.rs:8:19
|
LL | const B: u8 = Self::A;
| ^^^^^^^
= note: ...which again requires simplifying constant for the type system `Tr::A`, completing the cycle
- which again requires simplifying constant for the type system `Tr::A`, completing the cycle
note: cycle used when optimizing promoted MIR for `main`
--> $DIR/defaults-cyclic-fail.rs:16:16
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ error[E0391]: cycle detected when checking if `IMPL_REF_BAR` is a trivial const
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
note: ...which requires building MIR for `IMPL_REF_BAR`...
- which requires building MIR for `IMPL_REF_BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
|
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^
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...
- which requires checking if `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR` is a trivial const...
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:5
|
LL | const BAR: u32 = IMPL_REF_BAR;
| ^^^^^^^^^^^^^^
note: ...which requires building MIR for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR`...
- which requires building MIR for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:5
|
LL | const BAR: u32 = IMPL_REF_BAR;
| ^^^^^^^^^^^^^^
= note: ...which again requires checking if `IMPL_REF_BAR` is a trivial const, completing the cycle
- which again requires checking if `IMPL_REF_BAR` is a trivial const, completing the cycle
note: cycle used when simplifying constant for the type system `IMPL_REF_BAR`
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@ error[E0391]: cycle detected when caching mir of `FooDefault::BAR` for CTFE
LL | const BAR: u32 = DEFAULT_REF_BAR;
| ^^^^^^^^^^^^^^
|
note: ...which requires elaborating drops for `FooDefault::BAR`...
- which requires elaborating drops for `FooDefault::BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:22
|
LL | const BAR: u32 = DEFAULT_REF_BAR;
| ^^^^^^^^^^^^^^^
note: ...which requires simplifying constant for the type system `DEFAULT_REF_BAR`...
- which requires simplifying constant for the type system `DEFAULT_REF_BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
|
LL | const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires const-evaluating + checking `DEFAULT_REF_BAR`...
- which requires const-evaluating + checking `DEFAULT_REF_BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:30
|
LL | const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires simplifying constant for the type system `FooDefault::BAR`...
- which requires simplifying constant for the type system `FooDefault::BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:5
|
LL | const BAR: u32 = DEFAULT_REF_BAR;
| ^^^^^^^^^^^^^^
note: ...which requires const-evaluating + checking `FooDefault::BAR`...
- which requires const-evaluating + checking `FooDefault::BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:5
|
LL | const BAR: u32 = DEFAULT_REF_BAR;
| ^^^^^^^^^^^^^^
= note: ...which again requires caching mir of `FooDefault::BAR` for CTFE, completing the cycle
- which again requires caching mir of `FooDefault::BAR` for CTFE, completing the cycle
note: cycle used when const-evaluating + checking `FooDefault::BAR`
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:5
|
Expand Down
Loading
Loading