From c8b161842ae142a0f757c5ac0c34b462027ba1d0 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Sun, 11 Dec 2022 22:58:14 +0900 Subject: [PATCH] Add Location::unknown_or --- compiler/erg_common/error.rs | 16 +++++++++++++++- compiler/erg_compiler/context/eval.rs | 4 +++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/compiler/erg_common/error.rs b/compiler/erg_common/error.rs index 8da09667..ef560356 100644 --- a/compiler/erg_common/error.rs +++ b/compiler/erg_common/error.rs @@ -279,6 +279,18 @@ impl Location { } } + pub const fn is_unknown(&self) -> bool { + matches!(self, Self::Unknown) + } + + pub const fn unknown_or(&self, other: Self) -> Self { + if self.is_unknown() { + other + } else { + *self + } + } + pub const fn ln_begin(&self) -> Option { match self { Self::Range { ln_begin, .. } | Self::LineRange(ln_begin, _) | Self::Line(ln_begin) => { @@ -477,7 +489,7 @@ impl SubMessage { mark: char, chars: &Characters, ) -> String { - match self.loc { + match self.loc.unknown_or(e.core().loc) { Location::Range { ln_begin, col_begin, @@ -752,6 +764,8 @@ pub trait ErrorDisplay { for sub_msg in &core.sub_messages { msg += &sub_msg.format_code_and_pointer(self, color, gutter_color, mark, chars); } + msg += &core.kind.to_string(); + msg += ": "; msg += &core.main_message; msg += "\n\n"; msg diff --git a/compiler/erg_compiler/context/eval.rs b/compiler/erg_compiler/context/eval.rs index aefbfeb2..c8a3b48d 100644 --- a/compiler/erg_compiler/context/eval.rs +++ b/compiler/erg_compiler/context/eval.rs @@ -262,7 +262,9 @@ impl Context { match subr { ConstSubr::User(_user) => todo!(), ConstSubr::Builtin(builtin) => builtin.call(args, self).map_err(|mut e| { - e.0.loc = loc; + if e.0.loc.is_unknown() { + e.0.loc = loc; + } EvalErrors::from(EvalError::new( *e.0, self.cfg.input.clone(),