Skip to content

Commit

Permalink
chore: improve overload error msg
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Jun 15, 2024
1 parent 5cf3acb commit 22cd07a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
19 changes: 16 additions & 3 deletions crates/erg_compiler/context/initialize/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions crates/erg_compiler/context/initialize/const_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,16 @@ pub(crate) fn as_record(mut args: ValueArgs, ctx: &Context) -> EvalValueResult<T
Ok(ValueObj::builtin_type(Type::Record(dict)).into())
}

pub(crate) fn int_abs(mut args: ValueArgs, _ctx: &Context) -> EvalValueResult<TyParam> {
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<TyParam> {
let slf = args
.remove_left_or_key("self")
Expand Down
14 changes: 11 additions & 3 deletions crates/erg_compiler/context/inquire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
))
}
Expand Down Expand Up @@ -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::<Vec<_>>(),
namespace.caused_by(),
))
Expand Down
12 changes: 6 additions & 6 deletions crates/erg_compiler/error/tycheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,18 +1346,18 @@ passed keyword args: {kw_args_len}"
errno: usize,
loc: Location,
caused_by: String,
pos_args: Vec<ParamTy>,
kw_args: Vec<ParamTy>,
pos_args: impl Iterator<Item = ParamTy>,
kw_args: impl Iterator<Item = ParamTy>,
found: impl Iterator<Item = &'a Type>,
) -> 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,
Expand Down
7 changes: 7 additions & 0 deletions crates/erg_compiler/lib/pystd/string.d.er
Original file line number Diff line number Diff line change
Expand Up @@ -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; _]
Expand Down
3 changes: 2 additions & 1 deletion crates/erg_compiler/ty/typaram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 22cd07a

Please sign in to comment.