Clean: into() convert to_string() or to_owned()

This commit is contained in:
GreasySlug 2022-11-22 14:29:24 +09:00
parent c100f344ab
commit dad6fd1996
2 changed files with 62 additions and 61 deletions

View file

@ -147,11 +147,11 @@ impl ErrorDisplay for CompileError {
// found, error // found, error
const ERR: Color = THEME.colors.error; const ERR: Color = THEME.colors.error;
// name // var name, lhs, rhs
const WARN: Color = THEME.colors.warning; const WARN: Color = THEME.colors.warning;
// expect, hint // expect, hint
const HINT: Color = THEME.colors.hint; const HINT: Color = THEME.colors.hint;
// url and var name // url
const ACCENT: Color = THEME.colors.accent; const ACCENT: Color = THEME.colors.accent;
// url and feature = pretty // url and feature = pretty
const UNDERLINE: Attribute = Attribute::Underline; const UNDERLINE: Attribute = Attribute::Underline;
@ -197,7 +197,7 @@ impl CompileError {
loc, loc,
), ),
input, input,
"".into(), "".to_owned(),
) )
} }
@ -230,7 +230,7 @@ impl CompileError {
loc, loc,
), ),
input, input,
"".into(), "".to_owned(),
) )
} }
@ -268,7 +268,7 @@ impl CompileError {
Location::Unknown, Location::Unknown,
), ),
Input::Dummy, Input::Dummy,
"".into(), "".to_owned(),
) )
} }
} }
@ -277,11 +277,11 @@ pub type TyCheckError = CompileError;
impl TyCheckError { impl TyCheckError {
pub fn dummy(input: Input, errno: usize) -> Self { pub fn dummy(input: Input, errno: usize) -> Self {
Self::new(ErrorCore::dummy(errno), input, "".into()) Self::new(ErrorCore::dummy(errno), input, "".to_string())
} }
pub fn unreachable(input: Input, fn_name: &str, line: u32) -> Self { pub fn unreachable(input: Input, fn_name: &str, line: u32) -> Self {
Self::new(ErrorCore::unreachable(fn_name, line), input, "".into()) Self::new(ErrorCore::unreachable(fn_name, line), input, "".to_string())
} }
pub fn checker_bug( pub fn checker_bug(
@ -305,7 +305,7 @@ impl TyCheckError {
loc, loc,
), ),
input, input,
"".into(), "".to_string(),
) )
} }
@ -385,7 +385,7 @@ impl TyCheckError {
"traditional_chinese" => format!("(第{pos}個參數)"), "traditional_chinese" => format!("(第{pos}個參數)"),
"english" => format!(" (the {} argument)", ordinal_num(pos)), "english" => format!(" (the {} argument)", ordinal_num(pos)),
), ),
None => "".into(), None => "".to_owned(),
}; };
let name = StyledString::new(format!("{}{}", name, ord), Some(WARN), Some(ATTR)); let name = StyledString::new(format!("{}{}", name, ord), Some(WARN), Some(ATTR));
let mut expct = StyledStrings::default(); let mut expct = StyledStrings::default();
@ -601,11 +601,11 @@ impl TyCheckError {
} }
pub fn dummy_infer_error(input: Input, fn_name: &str, line: u32) -> Self { pub fn dummy_infer_error(input: Input, fn_name: &str, line: u32) -> Self {
Self::new(ErrorCore::unreachable(fn_name, line), input, "".into()) Self::new(ErrorCore::unreachable(fn_name, line), input, "".to_owned())
} }
pub fn not_relation(input: Input, fn_name: &str, line: u32) -> Self { pub fn not_relation(input: Input, fn_name: &str, line: u32) -> Self {
Self::new(ErrorCore::unreachable(fn_name, line), input, "".into()) Self::new(ErrorCore::unreachable(fn_name, line), input, "".to_owned())
} }
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
@ -855,7 +855,7 @@ passed keyword args: {kw_args_len}"
"simplified_chinese" =>sub_type.push_str("超類型: "), "simplified_chinese" =>sub_type.push_str("超類型: "),
"english" => sub_type.push_str("subtype: "), "english" => sub_type.push_str("subtype: "),
); );
sub_type.push_str_with_color_and_attribute(format!("{}", sub_t), WARN, ATTR); sub_type.push_str_with_color_and_attribute(format!("{}", sub_t), HINT, ATTR);
let mut sup_type = StyledStrings::default(); let mut sup_type = StyledStrings::default();
switch_lang!( switch_lang!(
@ -864,7 +864,7 @@ passed keyword args: {kw_args_len}"
"simplified_chinese" => sup_type.push_str("超類型: "), "simplified_chinese" => sup_type.push_str("超類型: "),
"english" =>sup_type.push_str("supertype: "), "english" =>sup_type.push_str("supertype: "),
); );
sup_type.push_str_with_color_and_attribute(format!("{}", sup_t), WARN, ATTR); sup_type.push_str_with_color_and_attribute(format!("{}", sup_t), ERR, ATTR);
Self::new( Self::new(
ErrorCore::new( ErrorCore::new(
@ -902,7 +902,7 @@ passed keyword args: {kw_args_len}"
"traditional_chinese" => lhs_uni.push_str("左邊: "), "traditional_chinese" => lhs_uni.push_str("左邊: "),
"english" => lhs_uni.push_str("lhs: "), "english" => lhs_uni.push_str("lhs: "),
); );
lhs_uni.push_str_with_color_and_attribute(format!("{}", lhs), WARN, ATTR); lhs_uni.push_str_with_color_and_attribute(format!("{}", lhs), HINT, ATTR);
let mut rhs_uni = StyledStrings::default(); let mut rhs_uni = StyledStrings::default();
switch_lang!( switch_lang!(
"japanese" => rhs_uni.push_str("右辺: "), "japanese" => rhs_uni.push_str("右辺: "),
@ -910,7 +910,7 @@ passed keyword args: {kw_args_len}"
"traditional_chinese" => rhs_uni.push_str("右邊: "), "traditional_chinese" => rhs_uni.push_str("右邊: "),
"english" => rhs_uni.push_str("rhs: "), "english" => rhs_uni.push_str("rhs: "),
); );
rhs_uni.push_str_with_color_and_attribute(format!("{}", rhs), WARN, ATTR); rhs_uni.push_str_with_color_and_attribute(format!("{}", rhs), ERR, ATTR);
Self::new( Self::new(
ErrorCore::new( ErrorCore::new(
vec![SubMessage::ambiguous_new( vec![SubMessage::ambiguous_new(
@ -1228,7 +1228,7 @@ passed keyword args: {kw_args_len}"
ErrorCore::new( ErrorCore::new(
vec![SubMessage::ambiguous_new( vec![SubMessage::ambiguous_new(
expr.loc(), expr.loc(),
vec![candidate.into()], vec![candidate.to_string()],
hint, hint,
)], )],
switch_lang!( switch_lang!(
@ -1302,12 +1302,7 @@ pub type EffectError = TyCheckError;
pub type EffectErrors = TyCheckErrors; pub type EffectErrors = TyCheckErrors;
impl EffectError { impl EffectError {
pub fn has_effect<S: Into<String>>( pub fn has_effect(input: Input, errno: usize, expr: &Expr, caused_by: String) -> Self {
input: Input,
errno: usize,
expr: &Expr,
caused_by: S,
) -> Self {
Self::new( Self::new(
ErrorCore::new( ErrorCore::new(
vec![SubMessage::only_loc(expr.loc())], vec![SubMessage::only_loc(expr.loc())],
@ -1322,15 +1317,15 @@ impl EffectError {
expr.loc(), expr.loc(),
), ),
input, input,
caused_by.into(), caused_by,
) )
} }
pub fn proc_assign_error<S: Into<String>>( pub fn proc_assign_error(
input: Input, input: Input,
errno: usize, errno: usize,
sig: &Signature, sig: &Signature,
caused_by: S, caused_by: String,
) -> Self { ) -> Self {
let hint = Some( let hint = Some(
switch_lang!( switch_lang!(
@ -1378,7 +1373,7 @@ impl EffectError {
sig.loc(), sig.loc(),
), ),
input, input,
caused_by.into(), caused_by,
) )
} }
} }
@ -1387,13 +1382,13 @@ pub type OwnershipError = TyCheckError;
pub type OwnershipErrors = TyCheckErrors; pub type OwnershipErrors = TyCheckErrors;
impl OwnershipError { impl OwnershipError {
pub fn move_error<S: Into<String>>( pub fn move_error(
input: Input, input: Input,
errno: usize, errno: usize,
name: &str, name: &str,
name_loc: Location, name_loc: Location,
moved_loc: Location, moved_loc: Location,
caused_by: S, caused_by: String,
) -> Self { ) -> Self {
let found = StyledString::new(name, Some(ERR), Some(ATTR)); let found = StyledString::new(name, Some(ERR), Some(ATTR));
Self::new( Self::new(
@ -1422,7 +1417,7 @@ impl OwnershipError {
name_loc, name_loc,
), ),
input, input,
caused_by.into(), caused_by,
) )
} }
} }
@ -1435,18 +1430,18 @@ pub type LowerResult<T> = TyCheckResult<T>;
pub type SingleLowerResult<T> = SingleTyCheckResult<T>; pub type SingleLowerResult<T> = SingleTyCheckResult<T>;
impl LowerError { impl LowerError {
pub fn syntax_error<S: Into<String>>( pub fn syntax_error(
input: Input, input: Input,
errno: usize, errno: usize,
loc: Location, loc: Location,
caused_by: String, caused_by: String,
desc: S, desc: String,
hint: Option<String>, hint: Option<String>,
) -> Self { ) -> Self {
Self::new( Self::new(
ErrorCore::new( ErrorCore::new(
vec![SubMessage::ambiguous_new(loc, vec![], hint)], vec![SubMessage::ambiguous_new(loc, vec![], hint)],
desc.into(), desc,
errno, errno,
SyntaxError, SyntaxError,
loc, loc,
@ -2007,7 +2002,7 @@ impl LowerError {
None None
} }
(None, Some(py)) => { (None, Some(py)) => {
py_str.push_str("similar name python module: "); py_str.push_str("similar name python module exits: ");
py_str.push_str_with_color_and_attribute(py, WARN, ATTR); py_str.push_str_with_color_and_attribute(py, WARN, ATTR);
Some("to import python modules, use `pyimport`".to_string()) Some("to import python modules, use `pyimport`".to_string())
} }
@ -2155,7 +2150,7 @@ pub type CompileWarnings = CompileErrors;
mod test { mod test {
use super::TyCheckError; use super::TyCheckError;
use crate::{ use crate::{
error::CompileError, error::{CompileError, LowerError},
ty::{Predicate, Type}, ty::{Predicate, Type},
}; };
use erg_common::{config::Input, error::Location}; use erg_common::{config::Input, error::Location};
@ -2164,12 +2159,12 @@ mod test {
// This test make sure sub_msg and hint are displayed correctly. // This test make sure sub_msg and hint are displayed correctly.
#[test] #[test]
fn default_error_format_confirmation() { fn default_error_format_confirmation() {
let input = Input::Pipe("line error".into()); let input = Input::Pipe("line error".to_owned());
let loc = Location::Line(1); let loc = Location::Line(1);
let err = CompileError::stack_bug(input, loc, 0, 0, "FileName"); let err = CompileError::stack_bug(input, loc, 0, 0, "FileName");
print!("{}", err); print!("{}", err);
let input = Input::Pipe("a: Nat = -1".into()); let input = Input::Pipe("a: Nat = -1".to_owned());
let err = TyCheckError::checker_bug(input, 0, Location::Unknown, "name", 1); let err = TyCheckError::checker_bug(input, 0, Location::Unknown, "name", 1);
print!("{}", err); print!("{}", err);
@ -2233,7 +2228,7 @@ mod test {
&expect, &expect,
&found, &found,
None, None,
Some("hint message here".into()), Some("hint message here".to_owned()),
); );
print!("{}", err); print!("{}", err);
@ -2279,21 +2274,6 @@ mod test {
let err = TyCheckError::subtyping_error(input, errno, sub_t, sup_t, loc, caused_by); let err = TyCheckError::subtyping_error(input, errno, sub_t, sup_t, loc, caused_by);
print!("{}", err); print!("{}", err);
/*
let input = Input::Pipe("".to_string());
let errno = 0;
let expr = Location::Range {
ln_begin: 1,
col_begin: 1,
ln_end: 1,
col_end: 1,
};
let candidates = &[Type::Int, Type::Nat, Type::Bool];
let caused_by = "".to_string();
let err = TyCheckError::ambiguous_type_error(input, errno, expr, candidates, caused_by);
println!("{}", err);
*/
let input = Input::Pipe("pred unification error".to_string()); let input = Input::Pipe("pred unification error".to_string());
let errno = 0; let errno = 0;
let loc = Location::Range { let loc = Location::Range {
@ -2344,6 +2324,24 @@ mod test {
hint, hint,
); );
print!("{}", err); print!("{}", err);
let input = Input::Pipe("singular no attribute error".to_string());
let caused_by = "<caused by>".to_string();
let obj_name = "ojb name";
let obj_t = Type::Bool;
let name = "name";
let similar_name = Some("object name");
let err = LowerError::singular_no_attr_error(
input,
errno,
loc,
caused_by,
obj_name,
&obj_t,
name,
similar_name,
);
print!("{err}");
} }
#[test] #[test]

View file

@ -174,7 +174,7 @@ impl ASTLowerer {
"traditional_chinese" => "如果您不想使用該值請使用discard函數", "traditional_chinese" => "如果您不想使用該值請使用discard函數",
"english" => "if you don't use the value, use discard function", "english" => "if you don't use the value, use discard function",
) )
.into(), .to_owned(),
), ),
)) ))
} else { } else {
@ -231,7 +231,8 @@ impl ASTLowerer {
"simplified_chinese" => "数组元素必须全部是相同类型", "simplified_chinese" => "数组元素必须全部是相同类型",
"traditional_chinese" => "數組元素必須全部是相同類型", "traditional_chinese" => "數組元素必須全部是相同類型",
"english" => "all elements of an array must be of the same type", "english" => "all elements of an array must be of the same type",
), )
.to_owned(),
Some( Some(
switch_lang!( switch_lang!(
"japanese" => "Int or Strなど明示的に型を指定してください", "japanese" => "Int or Strなど明示的に型を指定してください",
@ -239,7 +240,7 @@ impl ASTLowerer {
"traditional_chinese" => "請明確指定類型,例如: Int or Str", "traditional_chinese" => "請明確指定類型,例如: Int or Str",
"english" => "please specify the type explicitly, e.g. Int or Str", "english" => "please specify the type explicitly, e.g. Int or Str",
) )
.into(), .to_owned(),
), ),
))); )));
} }
@ -377,7 +378,8 @@ impl ASTLowerer {
"simplified_chinese" => "集合元素必须全部是相同类型", "simplified_chinese" => "集合元素必须全部是相同类型",
"traditional_chinese" => "集合元素必須全部是相同類型", "traditional_chinese" => "集合元素必須全部是相同類型",
"english" => "all elements of a set must be of the same type", "english" => "all elements of a set must be of the same type",
), )
.to_owned(),
Some( Some(
switch_lang!( switch_lang!(
"japanese" => "Int or Strなど明示的に型を指定してください", "japanese" => "Int or Strなど明示的に型を指定してください",
@ -385,7 +387,7 @@ impl ASTLowerer {
"traditional_chinese" => "明確指定類型,例如: Int or Str", "traditional_chinese" => "明確指定類型,例如: Int or Str",
"english" => "please specify the type explicitly, e.g. Int or Str", "english" => "please specify the type explicitly, e.g. Int or Str",
) )
.into(), .to_owned(),
), ),
))); )));
} }
@ -503,7 +505,8 @@ impl ASTLowerer {
"simplified_chinese" => "Dict的值必须是同一类型", "simplified_chinese" => "Dict的值必须是同一类型",
"traditional_chinese" => "Dict的值必須是同一類型", "traditional_chinese" => "Dict的值必須是同一類型",
"english" => "Values of Dict must be the same type", "english" => "Values of Dict must be the same type",
), )
.to_owned(),
Some( Some(
switch_lang!( switch_lang!(
"japanese" => "Int or Strなど明示的に型を指定してください", "japanese" => "Int or Strなど明示的に型を指定してください",
@ -511,7 +514,7 @@ impl ASTLowerer {
"traditional_chinese" => "明確指定類型,例如: Int or Str", "traditional_chinese" => "明確指定類型,例如: Int or Str",
"english" => "please specify the type explicitly, e.g. Int or Str", "english" => "please specify the type explicitly, e.g. Int or Str",
) )
.into(), .to_owned(),
), ),
))); )));
} }
@ -657,7 +660,7 @@ impl ASTLowerer {
line!() as usize, line!() as usize,
call.args.loc(), call.args.loc(),
self.ctx.caused_by(), self.ctx.caused_by(),
"invalid assert casting type", "invalid assert casting type".to_owned(),
None, None,
))); )));
} }
@ -716,7 +719,7 @@ impl ASTLowerer {
line!() as usize, line!() as usize,
other.loc(), other.loc(),
self.ctx.caused_by(), self.ctx.caused_by(),
"", "".to_owned(),
None, None,
))) )))
} }