From 22cd07a414c3f604db0f18d2a3538a719126c20f Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Sat, 15 Jun 2024 14:08:48 +0900 Subject: [PATCH] chore: improve overload error msg --- .../context/initialize/classes.rs | 19 ++++++++++++++++--- .../context/initialize/const_func.rs | 10 ++++++++++ crates/erg_compiler/context/inquire.rs | 14 +++++++++++--- crates/erg_compiler/error/tycheck.rs | 12 ++++++------ crates/erg_compiler/lib/pystd/string.d.er | 7 +++++++ crates/erg_compiler/ty/typaram.rs | 3 ++- 6 files changed, 52 insertions(+), 13 deletions(-) diff --git a/crates/erg_compiler/context/initialize/classes.rs b/crates/erg_compiler/context/initialize/classes.rs index 689e1246c..3c04e8f6f 100644 --- a/crates/erg_compiler/context/initialize/classes.rs +++ b/crates/erg_compiler/context/initialize/classes.rs @@ -494,7 +494,20 @@ impl Context { int.register_trait(self, mono(NUM)).unwrap(); // class("Rational"), // class("Integral"), - int.register_py_builtin(FUNC_ABS, fn0_met(Int, Nat), Some(OP_ABS), 11); + let i_abs = ValueObj::Subr(ConstSubr::Builtin(BuiltinConstSubr::new( + FUNC_ABS, + int_abs, + fn0_met(Int, Nat), + None, + ))); + int.register_py_builtin_const( + FUNC_ABS, + Visibility::BUILTIN_PUBLIC, + Some(fn0_met(Int, Nat)), + i_abs, + Some(OP_ABS), + Some(11), + ); int.register_py_builtin(FUNC_SUCC, fn0_met(Int, Int), Some(FUNC_SUCC), 54); int.register_py_builtin(FUNC_PRED, fn0_met(Int, Int), Some(FUNC_PRED), 47); int.register_py_builtin( @@ -1732,12 +1745,12 @@ impl Context { Predicate::le(var, N.clone() - value(1usize)), ); // __getitem__: |T, N|(self: [T; N], _: {I: Nat | I <= N}) -> T - // and (self: [T; N], _: Range(Int)) -> [T; _] + // and (self: [T; N], _: Range(Int) | Slice) -> [T; _] let list_getitem_t = (fn1_kw_met(list_t(T.clone(), N.clone()), anon(input.clone()), T.clone()) & fn1_kw_met( list_t(T.clone(), N.clone()), - anon(poly(RANGE, vec![ty_tp(Int)])), + anon(poly(RANGE, vec![ty_tp(Int)]) | mono(SLICE)), unknown_len_list_t(T.clone()), )) .quantify(); diff --git a/crates/erg_compiler/context/initialize/const_func.rs b/crates/erg_compiler/context/initialize/const_func.rs index 3e5d1239a..5e65a4fb9 100644 --- a/crates/erg_compiler/context/initialize/const_func.rs +++ b/crates/erg_compiler/context/initialize/const_func.rs @@ -985,6 +985,16 @@ pub(crate) fn as_record(mut args: ValueArgs, ctx: &Context) -> EvalValueResult EvalValueResult { + let slf = args + .remove_left_or_key("self") + .ok_or_else(|| not_passed("self"))?; + let Some(slf) = slf.as_int() else { + return Err(type_mismatch("Int", slf, "self")); + }; + Ok(ValueObj::Int(slf.abs()).into()) +} + pub(crate) fn str_endswith(mut args: ValueArgs, _ctx: &Context) -> EvalValueResult { let slf = args .remove_left_or_key("self") diff --git a/crates/erg_compiler/context/inquire.rs b/crates/erg_compiler/context/inquire.rs index 2b9c6f011..f196f9c01 100644 --- a/crates/erg_compiler/context/inquire.rs +++ b/crates/erg_compiler/context/inquire.rs @@ -1203,13 +1203,21 @@ impl Context { let Type::Subr(subr_t) = input_t else { unreachable!() }; + let non_default_params = subr_t + .non_default_params + .iter() + .map(|pt| pt.clone().map_type(|t| self.readable_type(t))); + let default_params = subr_t + .default_params + .iter() + .map(|pt| pt.clone().map_type(|t| self.readable_type(t))); Err(TyCheckError::overload_error( self.cfg.input.clone(), line!() as usize, loc.loc(), self.caused_by(), - subr_t.non_default_params, - subr_t.default_params, + non_default_params, + default_params, intersecs.iter(), )) } @@ -3530,7 +3538,7 @@ impl Context { attr, &candidates .iter() - .map(|mp| mp.definition_type.clone()) + .map(|mp| self.readable_type(mp.definition_type.clone())) .collect::>(), namespace.caused_by(), )) diff --git a/crates/erg_compiler/error/tycheck.rs b/crates/erg_compiler/error/tycheck.rs index 834428aa5..1527a4be9 100644 --- a/crates/erg_compiler/error/tycheck.rs +++ b/crates/erg_compiler/error/tycheck.rs @@ -1346,18 +1346,18 @@ passed keyword args: {kw_args_len}" errno: usize, loc: Location, caused_by: String, - pos_args: Vec, - kw_args: Vec, + pos_args: impl Iterator, + kw_args: impl Iterator, found: impl Iterator, ) -> Self { Self::new( ErrorCore::new( vec![], switch_lang!( - "japanese" => format!("オーバーロード解決に失敗しました\nオーバーロード型:\n* {}\n渡された位置引数: {}\n渡された名前付き引数: {}", fmt_iter_split_with(found, "\n* "), fmt_vec(&pos_args), fmt_vec(&kw_args)), - "simplified_chinese" => format!("无法解析重载\n重载类型:\n* {}\n位置参数: {}\n命名参数: {}", fmt_iter_split_with(found, "\n* "), fmt_vec(&pos_args), fmt_vec(&kw_args)), - "traditional_chinese" => format!("無法解析重載\n重載類型:\n* {}\n位置參數: {}\n命名參數: {}", fmt_iter_split_with(found, "\n* "), fmt_vec(&pos_args), fmt_vec(&kw_args)), - "english" => format!("cannot resolve overload\noverloaded type:\n* {}\npassed positional arguments: {}\npassed named arguments: {}", fmt_iter_split_with(found, "\n* "), fmt_vec(&pos_args), fmt_vec(&kw_args)), + "japanese" => format!("オーバーロード解決に失敗しました\nオーバーロード型:\n* {}\n渡された位置引数: {}\n渡された名前付き引数: {}", fmt_iter_split_with(found, "\n* "), fmt_iter(pos_args), fmt_iter(kw_args)), + "simplified_chinese" => format!("无法解析重载\n重载类型:\n* {}\n位置参数: {}\n命名参数: {}", fmt_iter_split_with(found, "\n* "), fmt_iter(pos_args), fmt_iter(kw_args)), + "traditional_chinese" => format!("無法解析重載\n重載類型:\n* {}\n位置參數: {}\n命名參數: {}", fmt_iter_split_with(found, "\n* "), fmt_iter(pos_args), fmt_iter(kw_args)), + "english" => format!("cannot resolve overload\noverloaded type:\n* {}\npassed positional arguments: {}\npassed named arguments: {}", fmt_iter_split_with(found, "\n* "), fmt_iter(pos_args), fmt_iter(kw_args)), ), errno, TypeError, diff --git a/crates/erg_compiler/lib/pystd/string.d.er b/crates/erg_compiler/lib/pystd/string.d.er index 17b83947d..8721bc713 100644 --- a/crates/erg_compiler/lib/pystd/string.d.er +++ b/crates/erg_compiler/lib/pystd/string.d.er @@ -11,6 +11,13 @@ .capwords: (s: Str, sep := Str) -> Str .Template: ClassType +.Template. + template: Str + __call__: (template: Str) -> .Template + substitute: (self: .Template, mapping := {Str: Str}, **kws: Str) -> Str + safe_substitute: (self: .Template, mapping := {Str: Str}, **kws: Str) -> Str + is_valid: (self: .Template) -> Bool + get_identifiers: (self: .Template) -> [Str; _] FormatterIterator = 'formatteriterator': ClassType FormatterIterator <: Iterable [Str; _] diff --git a/crates/erg_compiler/ty/typaram.rs b/crates/erg_compiler/ty/typaram.rs index 3584efb98..9f7b21b5e 100644 --- a/crates/erg_compiler/ty/typaram.rs +++ b/crates/erg_compiler/ty/typaram.rs @@ -422,8 +422,9 @@ impl LimitedDisplay for TyParam { write!(f, "{attr}") } Self::ProjCall { obj, attr, args } => { + write!(f, "(")?; obj.limited_fmt(f, limit - 1)?; - write!(f, ".")?; + write!(f, ").")?; write!(f, "{attr}")?; write!(f, "(")?; for (i, arg) in args.iter().enumerate() {