mirror of https://github.com/zkat/miette.git
Fix: edit error on checking out bounds spans
This commit is contained in:
parent
70911c504b
commit
9071750c32
|
|
@ -4,14 +4,14 @@ use syn::{
|
||||||
parenthesized,
|
parenthesized,
|
||||||
parse::{Parse, ParseStream},
|
parse::{Parse, ParseStream},
|
||||||
spanned::Spanned,
|
spanned::Spanned,
|
||||||
Token, Member,
|
Token,
|
||||||
};
|
};
|
||||||
|
|
||||||
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}, source_code::SourceCode,
|
utils::{display_pat_members, gen_all_variants_with},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Labels(Vec<Label>);
|
pub struct Labels(Vec<Label>);
|
||||||
|
|
@ -105,13 +105,6 @@ 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;
|
||||||
|
|
@ -120,15 +113,11 @@ 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| {
|
.map(|#var| miette::LabeledSpan::new_with_span(
|
||||||
if #source.len() < #var.len() + #var.offset() {
|
|
||||||
panic!("invalid span, not shadowing the error message");
|
|
||||||
};
|
|
||||||
miette::LabeledSpan::new_with_span(
|
|
||||||
std::option::Option::Some(format!(#fmt #args)),
|
std::option::Option::Some(format!(#fmt #args)),
|
||||||
#var.clone(),
|
#var.clone(),
|
||||||
)
|
)
|
||||||
})
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
quote! {
|
quote! {
|
||||||
|
|
|
||||||
|
|
@ -315,7 +315,13 @@ impl GraphicalReportHandler {
|
||||||
source.read_span(label.inner(), self.context_lines, self.context_lines)
|
source.read_span(label.inner(), self.context_lines, self.context_lines)
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<Box<dyn SpanContents<'_>>>, MietteError>>()
|
.collect::<Result<Vec<Box<dyn SpanContents<'_>>>, MietteError>>()
|
||||||
.map_err(|_| fmt::Error)?;
|
.map_err(|e| {
|
||||||
|
if matches!(e,MietteError::OutOfBounds) {
|
||||||
|
panic!("invalid span , OutOfBounds");
|
||||||
|
} else {
|
||||||
|
fmt::Error
|
||||||
|
}
|
||||||
|
})?;
|
||||||
let mut contexts = Vec::new();
|
let mut contexts = Vec::new();
|
||||||
for (right, right_conts) in labels.iter().cloned().zip(contents.iter()) {
|
for (right, right_conts) in labels.iter().cloned().zip(contents.iter()) {
|
||||||
if contexts.is_empty() {
|
if contexts.is_empty() {
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,7 @@ fn context_info<'a>(
|
||||||
offset += 1;
|
offset += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if offset >= (span.offset() + span.len()).saturating_sub(1) {
|
if offset >= (span.offset() + span.len()) {
|
||||||
let starting_offset = before_lines_starts.first().copied().unwrap_or_else(|| {
|
let starting_offset = before_lines_starts.first().copied().unwrap_or_else(|| {
|
||||||
if context_lines_before == 0 {
|
if context_lines_before == 0 {
|
||||||
span.offset()
|
span.offset()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue