Merge branch 'split-err-msg' of https://github.com/GreasySlug/erg into split-err-msg

This commit is contained in:
GreasySlug 2022-11-17 23:01:07 +09:00
commit 22652cb174
3 changed files with 8 additions and 129 deletions

View file

@ -229,37 +229,6 @@ impl From<&str> for ErrorKind {
/// ///
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Location { pub enum Location {
///
/// Error used when the error is caused by a discrepancy with a code on another line
///
/// # Example
///
/// Ownership error
///
/// ```erg
/// a: Nat = 1
/// a.consume_ownership() // move occurs
///
/// function(a) // borrowed after moved
/// ```
///
/// `a` moves ownership in a method(or function) that are defined and consume it.
///
/// ```erg
/// Location::RangePair {
/// ln_first: (2, 2),
/// col_first: (0, 1),
/// ln_second: (4, 4),
/// col_second: (9, 10),
/// }
/// ```
///
RangePair {
ln_first: (usize, usize),
col_first: (usize, usize),
ln_second: (usize, usize),
col_second: (usize, usize),
},
/// ///
/// Location used for basic errors /// Location used for basic errors
/// ```erg /// ```erg
@ -311,59 +280,34 @@ impl Location {
} }
} }
pub fn pair(lhs: Self, rhs: Self) -> Self {
Self::RangePair {
ln_first: (lhs.ln_begin().unwrap(), lhs.ln_end().unwrap()),
col_first: (lhs.col_begin().unwrap(), lhs.col_end().unwrap()),
ln_second: (rhs.ln_begin().unwrap(), rhs.ln_end().unwrap()),
col_second: (rhs.col_begin().unwrap(), rhs.col_end().unwrap()),
}
}
pub const fn ln_begin(&self) -> Option<usize> { pub const fn ln_begin(&self) -> Option<usize> {
match self { match self {
Self::RangePair { Self::Range { ln_begin, .. } | Self::LineRange(ln_begin, _) | Self::Line(ln_begin) => {
ln_first: (ln_begin, _), Some(*ln_begin)
..
} }
| Self::Range { ln_begin, .. }
| Self::LineRange(ln_begin, _)
| Self::Line(ln_begin) => Some(*ln_begin),
Self::Unknown => None, Self::Unknown => None,
} }
} }
pub const fn ln_end(&self) -> Option<usize> { pub const fn ln_end(&self) -> Option<usize> {
match self { match self {
Self::RangePair { Self::Range { ln_end, .. } | Self::LineRange(ln_end, _) | Self::Line(ln_end) => {
ln_second: (_, ln_end), Some(*ln_end)
..
} }
| Self::Range { ln_end, .. }
| Self::LineRange(ln_end, _)
| Self::Line(ln_end) => Some(*ln_end),
Self::Unknown => None, Self::Unknown => None,
} }
} }
pub const fn col_begin(&self) -> Option<usize> { pub const fn col_begin(&self) -> Option<usize> {
match self { match self {
Self::RangePair { Self::Range { col_begin, .. } => Some(*col_begin),
col_first: (col_begin, _),
..
}
| Self::Range { col_begin, .. } => Some(*col_begin),
_ => None, _ => None,
} }
} }
pub const fn col_end(&self) -> Option<usize> { pub const fn col_end(&self) -> Option<usize> {
match self { match self {
Self::RangePair { Self::Range { col_end, .. } => Some(*col_end),
col_second: (_, col_end),
..
}
| Self::Range { col_end, .. } => Some(*col_end),
_ => None, _ => None,
} }
} }
@ -659,11 +603,6 @@ pub trait ErrorDisplay {
ln_begin, ln_end, .. ln_begin, ln_end, ..
} }
| Location::LineRange(ln_begin, ln_end) => format!(", line {ln_begin}..{ln_end}"), | Location::LineRange(ln_begin, ln_end) => format!(", line {ln_begin}..{ln_end}"),
Location::RangePair {
ln_first: (l1, l2),
ln_second: (l3, l4),
..
} => format!(", line {l1}..{l2}, {l3}..{l4}"),
Location::Line(lineno) => format!(", line {lineno}"), Location::Line(lineno) => format!(", line {lineno}"),
Location::Unknown => "".to_string(), Location::Unknown => "".to_string(),
}; };
@ -686,38 +625,6 @@ pub trait ErrorDisplay {
chars: &Characters, chars: &Characters,
) -> String { ) -> String {
match self.core().loc { match self.core().loc {
// TODO: Current implementation does not allow for multiple descriptions of errors to be given at each location
// In the future, this will be implemented in a different structure that can handle multiple lines and files
Location::RangePair {
ln_first,
col_first,
ln_second,
col_second,
} => {
format_context(
self,
ln_first.0,
ln_first.1,
col_first.0,
col_first.1,
err_color,
gutter_color,
chars,
mark,
) +
"\n" // TODO: dealing with error chains
+ &format_context(
self,
ln_second.0,
ln_second.1,
col_second.0,
col_second.1,
err_color,
gutter_color,
chars,
mark,
)
}
Location::Range { Location::Range {
ln_begin, ln_begin,
col_begin, col_begin,

View file

@ -477,12 +477,7 @@ pub trait Locational {
fn ln_begin(&self) -> Option<usize> { fn ln_begin(&self) -> Option<usize> {
match self.loc() { match self.loc() {
Location::RangePair { Location::Range { ln_begin, .. } | Location::LineRange(ln_begin, _) => Some(ln_begin),
ln_first: (ln_begin, _),
..
}
| Location::Range { ln_begin, .. }
| Location::LineRange(ln_begin, _) => Some(ln_begin),
Location::Line(lineno) => Some(lineno), Location::Line(lineno) => Some(lineno),
Location::Unknown => None, Location::Unknown => None,
} }
@ -490,12 +485,7 @@ pub trait Locational {
fn ln_end(&self) -> Option<usize> { fn ln_end(&self) -> Option<usize> {
match self.loc() { match self.loc() {
Location::RangePair { Location::Range { ln_end, .. } | Location::LineRange(_, ln_end) => Some(ln_end),
ln_second: (_, ln_end),
..
}
| Location::Range { ln_end, .. }
| Location::LineRange(_, ln_end) => Some(ln_end),
Location::Line(lineno) => Some(lineno), Location::Line(lineno) => Some(lineno),
Location::Unknown => None, Location::Unknown => None,
} }

View file

@ -1895,24 +1895,6 @@ if True:
); );
print!("{}", err); print!("{}", err);
let loc = Location::RangePair {
ln_first: (1, 2),
col_first: (0, 1),
ln_second: (4, 4),
col_second: (9, 10),
};
let input = Input::Pipe(
"\
a: Nat = 1
a.ownership_is_moved()
function(a)
"
.to_string(),
);
let err = TyCheckError::checker_bug(input, 0, loc, "file_name", 0);
print!("{}", err);
let loc = Location::Range { let loc = Location::Range {
ln_begin: 1, ln_begin: 1,
col_begin: 0, col_begin: 0,