mirror of https://github.com/zkat/miette.git
Fix: invalid span shadowing err message
This commit is contained in:
parent
3497508aa9
commit
70911c504b
|
|
@ -4,14 +4,14 @@ use syn::{
|
||||||
parenthesized,
|
parenthesized,
|
||||||
parse::{Parse, ParseStream},
|
parse::{Parse, ParseStream},
|
||||||
spanned::Spanned,
|
spanned::Spanned,
|
||||||
Token,
|
Token, Member,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
diagnostic::{DiagnosticConcreteArgs, DiagnosticDef},
|
diagnostic::{DiagnosticConcreteArgs, DiagnosticDef},
|
||||||
fmt::{self, Display},
|
fmt::{self, Display},
|
||||||
forward::WhichFn,
|
forward::WhichFn,
|
||||||
utils::{display_pat_members, gen_all_variants_with},
|
utils::{display_pat_members, gen_all_variants_with}, source_code::SourceCode,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Labels(Vec<Label>);
|
pub struct Labels(Vec<Label>);
|
||||||
|
|
@ -105,6 +105,13 @@ impl Labels {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn gen_struct(&self, fields: &syn::Fields) -> Option<TokenStream> {
|
pub(crate) fn gen_struct(&self, fields: &syn::Fields) -> Option<TokenStream> {
|
||||||
|
let sources = SourceCode::from_fields(fields).unwrap_or_else(|_| None);
|
||||||
|
let source : Option<Member>;
|
||||||
|
if let Some(SourceCode { source_code }) = sources {
|
||||||
|
source = Some(source_code);
|
||||||
|
} else {
|
||||||
|
source = None;
|
||||||
|
}
|
||||||
let (display_pat, display_members) = display_pat_members(fields);
|
let (display_pat, display_members) = display_pat_members(fields);
|
||||||
let labels = self.0.iter().map(|highlight| {
|
let labels = self.0.iter().map(|highlight| {
|
||||||
let Label { span, label, ty } = highlight;
|
let Label { span, label, ty } = highlight;
|
||||||
|
|
@ -113,10 +120,15 @@ impl Labels {
|
||||||
let (fmt, args) = display.expand_shorthand_cloned(&display_members);
|
let (fmt, args) = display.expand_shorthand_cloned(&display_members);
|
||||||
quote! {
|
quote! {
|
||||||
miette::macro_helpers::OptionalWrapper::<#ty>::new().to_option(&self.#span)
|
miette::macro_helpers::OptionalWrapper::<#ty>::new().to_option(&self.#span)
|
||||||
.map(|#var| miette::LabeledSpan::new_with_span(
|
.map(|#var| {
|
||||||
std::option::Option::Some(format!(#fmt #args)),
|
if #source.len() < #var.len() + #var.offset() {
|
||||||
#var.clone(),
|
panic!("invalid span, not shadowing the error message");
|
||||||
))
|
};
|
||||||
|
miette::LabeledSpan::new_with_span(
|
||||||
|
std::option::Option::Some(format!(#fmt #args)),
|
||||||
|
#var.clone(),
|
||||||
|
)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
quote! {
|
quote! {
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct SourceCode {
|
pub struct SourceCode {
|
||||||
source_code: syn::Member,
|
pub source_code: syn::Member,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SourceCode {
|
impl SourceCode {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue