pub struct Annotation { /* private fields */ }Expand description
A pointer to a subsequence in the end user’s input.
Also known as an annotation, the pointer can optionally contain a short message, typically describing in general terms what is being pointed to.
An annotation is either primary or secondary, depending on whether it was
constructed via Annotation::primary or Annotation::secondary.
Semantically, a primary annotation is meant to point to the “locus” of a
diagnostic. Visually, the difference between a primary and a secondary
annotation is usually just a different form of highlighting on the
corresponding span.
§Advice
The span on an annotation should be as specific as possible. For example, if there is a problem with a function call because one of its arguments has an invalid type, then the span should point to the specific argument and not to the entire function call.
Messages attached to annotations should also be as brief and specific as possible. Long messages could negative impact the quality of rendering.
Implementations§
Source§impl Annotation
impl Annotation
Sourcepub fn primary(span: Span) -> Annotation
pub fn primary(span: Span) -> Annotation
Create a “primary” annotation.
A primary annotation is meant to highlight the “locus” of a diagnostic. That is, it should point to something in the end user’s input that is the subject or “point” of a diagnostic.
A diagnostic may have many primary annotations. A diagnostic may not have any annotations, but if it does, at least one ought to be primary.
Sourcepub fn secondary(span: Span) -> Annotation
pub fn secondary(span: Span) -> Annotation
Create a “secondary” annotation.
A secondary annotation is meant to highlight relevant context for a diagnostic, but not to point to the “locus” of the diagnostic.
A diagnostic with only secondary annotations is usually not sensible, but it is allowed and will produce a reasonable rendering.
Sourcepub fn message<'a>(self, message: impl IntoDiagnosticMessage + 'a) -> Annotation
pub fn message<'a>(self, message: impl IntoDiagnosticMessage + 'a) -> Annotation
Attach a message to this annotation.
An annotation without a message will still have a presence in rendering. In particular, it will highlight the span association with this annotation in some way.
When a message is attached to an annotation, then it will be associated with the highlighted span in some way during rendering.
§Types implementing IntoDiagnosticMessage
Callers can pass anything that implements std::fmt::Display
directly. If callers want or need to avoid cloning the diagnostic
message, then they can also pass a DiagnosticMessage directly.
Sourcepub fn set_message<'a>(&mut self, message: impl IntoDiagnosticMessage + 'a)
pub fn set_message<'a>(&mut self, message: impl IntoDiagnosticMessage + 'a)
Sets the message on this annotation.
If one was already set, then this overwrites it.
This is useful if one needs to set the message on an annotation,
and all one has is a &mut Annotation. For example, via
Diagnostic::primary_annotation_mut.
Sourcepub fn get_message(&self) -> Option<&str>
pub fn get_message(&self) -> Option<&str>
Returns the message attached to this annotation, if one exists.
Sourcepub fn push_tag(&mut self, tag: DiagnosticTag)
pub fn push_tag(&mut self, tag: DiagnosticTag)
Attaches an additional tag to this annotation.
Sourcepub fn hide_snippet(&mut self, yes: bool)
pub fn hide_snippet(&mut self, yes: bool)
Set whether or not the snippet on this annotation should be suppressed when rendering.
Such annotations are only rendered with their file name and range, if available. This is
intended for backwards compatibility with Ruff diagnostics, which historically used
TextRange::default to indicate a file-level diagnostic. In the new diagnostic model, a
Span with a range of None should be used instead, as mentioned in the Span
documentation.
TODO(brent) update this usage in Ruff and remove is_file_level entirely. See
https://github.com/astral-sh/ruff/issues/19688, especially my first comment, for more
details. As of 2025-09-26 we also use this to suppress snippet rendering for formatter
diagnostics, which also need to have a range, so we probably can’t eliminate this entirely.
pub fn is_primary(&self) -> bool
Trait Implementations§
Source§impl Clone for Annotation
impl Clone for Annotation
Source§fn clone(&self) -> Annotation
fn clone(&self) -> Annotation
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Annotation
impl Debug for Annotation
impl Eq for Annotation
Source§impl GetSize for Annotation
impl GetSize for Annotation
Source§fn get_heap_size(&self) -> usize
fn get_heap_size(&self) -> usize
Source§fn get_heap_size_with_tracker<TRACKER: GetSizeTracker>(
&self,
tracker: TRACKER,
) -> (usize, TRACKER)
fn get_heap_size_with_tracker<TRACKER: GetSizeTracker>( &self, tracker: TRACKER, ) -> (usize, TRACKER)
tracker. Read moreSource§fn get_stack_size() -> usize
fn get_stack_size() -> usize
Source§fn get_size_with_tracker<T>(&self, tracker: T) -> (usize, T)where
T: GetSizeTracker,
fn get_size_with_tracker<T>(&self, tracker: T) -> (usize, T)where
T: GetSizeTracker,
tracker. Read moreSource§impl Hash for Annotation
impl Hash for Annotation
Source§impl PartialEq for Annotation
impl PartialEq for Annotation
impl StructuralPartialEq for Annotation
Auto Trait Implementations§
impl Freeze for Annotation
impl RefUnwindSafe for Annotation
impl Send for Annotation
impl Sync for Annotation
impl Unpin for Annotation
impl UnsafeUnpin for Annotation
impl UnwindSafe for Annotation
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> HashEqLike<&T> for T
impl<T> HashEqLike<&T> for T
Source§impl<T> HashEqLike<Cow<'_, T>> for T
impl<T> HashEqLike<Cow<'_, T>> for T
Source§impl<T> HashEqLike<T> for T
impl<T> HashEqLike<T> for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more