From 3ff0eb8f656b64ef27e8e659fbe5a7c348b95b83 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Fri, 17 Mar 2023 00:13:43 +0900 Subject: [PATCH] fix: improve `match` error messages --- crates/erg_compiler/context/inquire.rs | 4 ++++ crates/erg_compiler/error/tycheck.rs | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/erg_compiler/context/inquire.rs b/crates/erg_compiler/context/inquire.rs index cc858818..12a558cf 100644 --- a/crates/erg_compiler/context/inquire.rs +++ b/crates/erg_compiler/context/inquire.rs @@ -304,6 +304,7 @@ impl Context { let match_target_expr_t = pos_args[0].expr.ref_t(); // Never or T => T let mut union_pat_t = Type::Never; + let mut arm_ts = vec![]; for (i, pos_arg) in pos_args.iter().skip(1).enumerate() { let lambda = erg_common::enum_unwrap!(&pos_arg.expr, hir::Expr::Lambda); // already checked if !lambda.params.defaults.is_empty() { @@ -334,6 +335,7 @@ impl Context { ParamKind::NonDefault, )?; union_pat_t = self.union(&union_pat_t, &rhs); + arm_ts.push(rhs); } // NG: expr_t: Nat, union_pat_t: {1, 2} // OK: expr_t: Int, union_pat_t: {1} or 'T @@ -347,6 +349,8 @@ impl Context { pos_args[0].loc(), self.caused_by(), match_target_expr_t, + &union_pat_t, + arm_ts, ))); } let branch_ts = pos_args diff --git a/crates/erg_compiler/error/tycheck.rs b/crates/erg_compiler/error/tycheck.rs index 67c66c67..e2f2eb40 100644 --- a/crates/erg_compiler/error/tycheck.rs +++ b/crates/erg_compiler/error/tycheck.rs @@ -409,15 +409,23 @@ impl TyCheckError { loc: Location, caused_by: String, expr_t: &Type, + union_pat_t: &Type, + arm_ts: Vec, ) -> Self { + let arms = arm_ts + .into_iter() + .enumerate() + .fold("".to_string(), |acc, (i, t)| { + acc + &format!("{} arm type: {t}\n", ordinal_num(i + 1)) + }); Self::new( ErrorCore::new( vec![SubMessage::only_loc(loc)], switch_lang!( - "japanese" => format!("{expr_t}型の全パターンを網羅していません"), - "simplified_chinese" => format!("并非所有{expr_t}类型的模式都被涵盖"), - "traditional_chinese" => format!("並非所有{expr_t}類型的模式都被涵蓋"), - "english" => format!("not all patterns of type {expr_t} are covered"), + "japanese" => format!("{expr_t}型の全パターンを網羅していません\nunion type: {union_pat_t}\n{arms}"), + "simplified_chinese" => format!("并非所有{expr_t}类型的模式都被涵盖\nunion type: {union_pat_t}\n{arms}"), + "traditional_chinese" => format!("並非所有{expr_t}類型的模式都被涵蓋\nunion type: {union_pat_t}\n{arms}"), + "english" => format!("not all patterns of type {expr_t} are covered\nunion type: {union_pat_t}\n{arms}"), ), errno, TypeError,