rustc_errors::diagnostic

Macro with_fn

source
macro_rules! with_fn {
    {
        $with_f:ident,
        $(#[$attrs:meta])*
        pub fn $f:ident(&mut $self:ident, $($name:ident: $ty:ty),* $(,)?) -> &mut Self {
            $($body:tt)*
        }
    } => { ... };
}
Expand description

Diag impls many &mut self -> &mut Self methods. Each one modifies an existing diagnostic, either in a standalone fashion, e.g. err.code(code);, or in a chained fashion to make multiple modifications, e.g. err.code(code).span(span);.

This macro creates an equivalent self -> Self method, with a with_ prefix. This can be used in a chained fashion when making a new diagnostic, e.g. let err = struct_err(msg).with_code(code);, or emitting a new diagnostic, e.g. struct_err(msg).with_code(code).emit();.

Although the latter method can be used to modify an existing diagnostic, e.g. err = err.with_code(code);, this should be avoided because the former method gives shorter code, e.g. err.code(code);.

Note: the with_ methods are added only when needed. If you want to use one and it’s not defined, feel free to add it.

Note: any doc comments must be within the with_fn! call.