Skip to main content

Annotation

Struct Annotation 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn get_message(&self) -> Option<&str>

Returns the message attached to this annotation, if one exists.

Source

pub fn get_span(&self) -> &Span

Returns the Span associated with this annotation.

Source

pub fn set_span(&mut self, span: Span)

Sets the span on this annotation.

Source

pub fn push_tag(&mut self, tag: DiagnosticTag)

Attaches an additional tag to this annotation.

Source

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.

Source

pub fn is_primary(&self) -> bool

Trait Implementations§

Source§

impl Clone for Annotation

Source§

fn clone(&self) -> Annotation

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Annotation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Annotation

Source§

impl GetSize for Annotation

Source§

fn get_heap_size(&self) -> usize

Determines how many bytes this object occupies inside the heap. Read more
Source§

fn get_heap_size_with_tracker<TRACKER: GetSizeTracker>( &self, tracker: TRACKER, ) -> (usize, TRACKER)

Determines how many bytes this object occupies inside the heap while using a tracker. Read more
Source§

fn get_stack_size() -> usize

Determines how may bytes this object occupies inside the stack. Read more
Source§

fn get_size(&self) -> usize

Determines the total size of the object. Read more
Source§

fn get_size_with_tracker<T>(&self, tracker: T) -> (usize, T)
where T: GetSizeTracker,

Determines the total size of the object while using a tracker. Read more
Source§

impl Hash for Annotation

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Annotation

Source§

fn eq(&self, other: &Annotation) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Annotation

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> HashEqLike<&T> for T
where T: Hash + Eq,

Source§

fn hash<H>(&self, h: &mut H)
where H: Hasher,

Source§

fn eq(&self, data: &&T) -> bool

Source§

impl<T> HashEqLike<Cow<'_, T>> for T
where T: Hash + Eq + Clone,

Source§

fn hash<H>(&self, h: &mut H)
where H: Hasher,

Source§

fn eq(&self, data: &Cow<'_, T>) -> bool

Source§

impl<T> HashEqLike<T> for T
where T: Hash + Eq,

Source§

fn hash<H>(&self, h: &mut H)
where H: Hasher,

Source§

fn eq(&self, data: &T) -> bool

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Lookup<T> for T

Source§

fn into_owned(self) -> T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more