diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-06-10 23:53:20 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-06-10 23:54:16 +0200 |
| commit | ed6550fdb08eae92bffab6b6b137b1e0eebf62c6 (patch) | |
| tree | 74152a38f7aa2ed2ac2fa190e81494422700ca36 /src/diag.rs | |
| parent | 6aff11057bc88257c9383137952bb41b5b85c3dc (diff) | |
Bump dependencies
Diffstat (limited to 'src/diag.rs')
| -rw-r--r-- | src/diag.rs | 38 |
1 files changed, 10 insertions, 28 deletions
diff --git a/src/diag.rs b/src/diag.rs index b9fcebdf..76cad792 100644 --- a/src/diag.rs +++ b/src/diag.rs @@ -1,9 +1,9 @@ //! Diagnostics. use std::fmt::{self, Display, Formatter}; -use std::ops::Range; use crate::syntax::{Span, Spanned}; +use crate::Context; /// Early-return with a [`TypError`]. #[macro_export] @@ -39,8 +39,6 @@ pub type StrResult<T> = Result<T, String>; pub struct Error { /// The erroneous node in the source code. pub span: Span, - /// Where in the node the error should be annotated. - pub pos: ErrorPos, /// A diagnostic message describing the problem. pub message: String, /// The trace of function calls leading to the error. @@ -52,35 +50,12 @@ impl Error { pub fn new(span: Span, message: impl Into<String>) -> Self { Self { span, - pos: ErrorPos::Full, trace: vec![], message: message.into(), } } } -/// Where in a node an error should be annotated. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub enum ErrorPos { - /// At the start of the node. - Start, - /// Over the full width of the node. - Full, - /// At the end of the node. - End, -} - -impl ErrorPos { - /// Apply this to a node's byte range. - pub fn apply(self, range: Range<usize>) -> Range<usize> { - match self { - ErrorPos::Start => range.start .. range.start, - ErrorPos::Full => range, - ErrorPos::End => range.end .. range.end, - } - } -} - /// A part of an error's [trace](Error::trace). #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)] pub enum Tracepoint { @@ -124,18 +99,25 @@ where /// Enrich a [`TypResult`] with a tracepoint. pub trait Trace<T> { /// Add the tracepoint to all errors that lie outside the `span`. - fn trace<F>(self, make_point: F, span: Span) -> Self + fn trace<F>(self, ctx: &Context, make_point: F, span: Span) -> Self where F: Fn() -> Tracepoint; } impl<T> Trace<T> for TypResult<T> { - fn trace<F>(self, make_point: F, span: Span) -> Self + fn trace<F>(self, ctx: &Context, make_point: F, span: Span) -> Self where F: Fn() -> Tracepoint, { self.map_err(|mut errors| { + let range = ctx.sources.range(span); for error in errors.iter_mut() { + // Skip traces that surround the error. + let error_range = ctx.sources.range(error.span); + if range.start <= error_range.start && range.end >= error_range.end { + continue; + } + error.trace.push(Spanned::new(make_point(), span)); } errors |
