Fix: edit error on checking out bounds spans

This commit is contained in:
Mehrbod Akhlaghpour 2023-02-15 21:26:26 +03:30
parent 70911c504b
commit 9071750c32
3 changed files with 12 additions and 17 deletions

View File

@ -4,14 +4,14 @@ use syn::{
parenthesized,
parse::{Parse, ParseStream},
spanned::Spanned,
Token, Member,
Token,
};
use crate::{
diagnostic::{DiagnosticConcreteArgs, DiagnosticDef},
fmt::{self, Display},
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>);
@ -105,13 +105,6 @@ impl Labels {
}
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 labels = self.0.iter().map(|highlight| {
let Label { span, label, ty } = highlight;
@ -120,15 +113,11 @@ impl Labels {
let (fmt, args) = display.expand_shorthand_cloned(&display_members);
quote! {
miette::macro_helpers::OptionalWrapper::<#ty>::new().to_option(&self.#span)
.map(|#var| {
if #source.len() < #var.len() + #var.offset() {
panic!("invalid span, not shadowing the error message");
};
miette::LabeledSpan::new_with_span(
.map(|#var| miette::LabeledSpan::new_with_span(
std::option::Option::Some(format!(#fmt #args)),
#var.clone(),
)
})
)
}
} else {
quote! {

View File

@ -315,7 +315,13 @@ impl GraphicalReportHandler {
source.read_span(label.inner(), self.context_lines, self.context_lines)
})
.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();
for (right, right_conts) in labels.iter().cloned().zip(contents.iter()) {
if contexts.is_empty() {

View File

@ -72,7 +72,7 @@ fn context_info<'a>(
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(|| {
if context_lines_before == 0 {
span.offset()