diff --git a/compiler/erg_common/codeobj.rs b/compiler/erg_common/codeobj.rs index 8e8b3e64..913581fe 100644 --- a/compiler/erg_common/codeobj.rs +++ b/compiler/erg_common/codeobj.rs @@ -317,7 +317,7 @@ impl CodeObj { ) -> std::io::Result<()> { let mut file = File::create(path)?; let mut bytes = Vec::with_capacity(16); - let python_ver = python_ver.unwrap_or_else(|| detect_magic_number()); + let python_ver = python_ver.unwrap_or_else(detect_magic_number); bytes.append(&mut get_magic_num_bytes(python_ver).to_vec()); bytes.append(&mut vec![0; 4]); // padding bytes.append(&mut get_timestamp_bytes().to_vec()); diff --git a/compiler/erg_common/color.rs b/compiler/erg_common/color.rs index 40052884..3fb6b402 100644 --- a/compiler/erg_common/color.rs +++ b/compiler/erg_common/color.rs @@ -1,9 +1,9 @@ //! Escape sequences change the color of the terminal -pub const RESET: &'static str = "\x1b[m"; -pub const DEEP_RED: &'static str = "\x1b[31m"; -pub const RED: &'static str = "\x1b[91m"; -pub const GREEN: &'static str = "\x1b[92m"; -pub const YELLOW: &'static str = "\x1b[93m"; -pub const BLUE: &'static str = "\x1b[94m"; -pub const CYAN: &'static str = "\x1b[96m"; +pub const RESET: &str = "\x1b[m"; +pub const DEEP_RED: &str = "\x1b[31m"; +pub const RED: &str = "\x1b[91m"; +pub const GREEN: &str = "\x1b[92m"; +pub const YELLOW: &str = "\x1b[93m"; +pub const BLUE: &str = "\x1b[94m"; +pub const CYAN: &str = "\x1b[96m"; diff --git a/compiler/erg_common/combinations.rs b/compiler/erg_common/combinations.rs index 6dc55cdd..1194ba2f 100644 --- a/compiler/erg_common/combinations.rs +++ b/compiler/erg_common/combinations.rs @@ -44,7 +44,7 @@ where type Item = Vec; fn next(&mut self) -> Option { if let Some(i) = self.combinations.next() { - return Some(i); + Some(i) } else { self.combinations.reset(self.combinations.k() + 1); self.len -= 1; diff --git a/compiler/erg_common/config.rs b/compiler/erg_common/config.rs index 8ac6238b..6445c552 100644 --- a/compiler/erg_common/config.rs +++ b/compiler/erg_common/config.rs @@ -96,7 +96,7 @@ impl Input { }, Self::Pipe(s) | Self::Str(s) => s.split('\n').collect::>() [ln_begin - 1..=ln_end - 1] - .into_iter() + .iter() .map(|s| Str::rc(*s)) .collect(), Self::REPL => stdin::reread_lines(ln_begin, ln_end), diff --git a/compiler/erg_common/deserialize.rs b/compiler/erg_common/deserialize.rs index 6d42aede..a339caec 100644 --- a/compiler/erg_common/deserialize.rs +++ b/compiler/erg_common/deserialize.rs @@ -84,6 +84,7 @@ impl DeserializeError { pub type DeserializeResult = Result; +#[derive(Default)] pub struct Deserializer { str_cache: Cache, arr_cache: Cache<[ValueObj]>, @@ -107,7 +108,7 @@ impl Deserializer { process::exit(1); }; let codeobj = - CodeObj::from_pyc(&filename[..]).expect(&format!("failed to deserialize {filename}")); + CodeObj::from_pyc(&filename[..]).unwrap_or_else(|_| panic!("failed to deserialize {filename}")); println!("{}", codeobj.code_info()); } @@ -241,7 +242,7 @@ impl Deserializer { ) -> DeserializeResult> { match self.deserialize_const(v, python_ver)? { ValueObj::Array(arr) => Ok(arr.to_vec()), - other => Err(DeserializeError::type_error(&Type::Str, &other.ref_t())), + other => Err(DeserializeError::type_error(&Type::Str, other.ref_t())), } } @@ -252,18 +253,18 @@ impl Deserializer { ) -> DeserializeResult> { match self.deserialize_const(v, python_ver)? { ValueObj::Array(arr) => Ok(arr), - other => Err(DeserializeError::type_error(&Type::Str, &other.ref_t())), + other => Err(DeserializeError::type_error(&Type::Str, other.ref_t())), } } pub fn array_into_const(&mut self, arr: &[ValueObj]) -> ValueObj { - self.get_cached_arr(&arr) + self.get_cached_arr(arr) } pub fn try_into_str(&mut self, c: ValueObj) -> DeserializeResult { match c { ValueObj::Str(s) => Ok(s), - other => Err(DeserializeError::type_error(&Type::Str, &other.ref_t())), + other => Err(DeserializeError::type_error(&Type::Str, other.ref_t())), } } @@ -275,14 +276,14 @@ impl Deserializer { match self.deserialize_const(v, python_ver)? { ValueObj::Array(arr) => { let mut strs = Vec::with_capacity(arr.len()); - for c in arr.to_vec().into_iter() { + for c in arr.iter().cloned() { strs.push(self.try_into_str(c)?); } Ok(strs) } other => Err(DeserializeError::type_error( &Type::array(Type::Str, TyParam::erased(Type::Nat)), - &other.ref_t(), + other.ref_t(), )), } } @@ -290,7 +291,7 @@ impl Deserializer { pub fn deserialize_str(&mut self, v: &mut Vec, python_ver: u32) -> DeserializeResult { match self.deserialize_const(v, python_ver)? { ValueObj::Str(s) => Ok(s), - other => Err(DeserializeError::type_error(&Type::Str, &other.ref_t())), + other => Err(DeserializeError::type_error(&Type::Str, other.ref_t())), } } diff --git a/compiler/erg_common/dict.rs b/compiler/erg_common/dict.rs index d0acb8fb..2cd4c824 100644 --- a/compiler/erg_common/dict.rs +++ b/compiler/erg_common/dict.rs @@ -84,6 +84,11 @@ impl Dict { self.dict.len() } + #[inline] + pub fn is_empty(&self) -> bool { + self.dict.is_empty() + } + #[inline] pub fn capacity(&self) -> usize { self.dict.capacity() diff --git a/compiler/erg_common/error.rs b/compiler/erg_common/error.rs index 20760019..31409fc0 100644 --- a/compiler/erg_common/error.rs +++ b/compiler/erg_common/error.rs @@ -359,7 +359,7 @@ pub trait ErrorDisplay { fn write_to_stderr(&self) { let mut writer = BufWriter::new(stderr()); writer - .write( + .write_all( format!( "{}{}{}: {}{}\n", self.format_header(), @@ -379,9 +379,9 @@ pub trait ErrorDisplay { /// fmt::Display実装用 fn format(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( + writeln!( f, - "{}{}{}: {}{}\n", + "{}{}{}: {}{}", self.format_header(), self.format_code_and_pointer(), self.core().kind, diff --git a/compiler/erg_common/levenshtein.rs b/compiler/erg_common/levenshtein.rs index 0ef1b671..385df8a1 100644 --- a/compiler/erg_common/levenshtein.rs +++ b/compiler/erg_common/levenshtein.rs @@ -26,5 +26,5 @@ pub fn levenshtein(lhs: &str, rhs: &str) -> usize { .unwrap(); } } - return table[l_len][r_len]; + table[l_len][r_len] } diff --git a/compiler/erg_common/lib.rs b/compiler/erg_common/lib.rs index 8b3132f6..ebd17ad3 100644 --- a/compiler/erg_common/lib.rs +++ b/compiler/erg_common/lib.rs @@ -85,7 +85,7 @@ pub fn fmt_iter_split_with>(i: I, splitte pub fn fmt_indent(s: String, depth: usize) -> String { let indent = " ".repeat(depth); - s.split("\n").map(|s| indent.clone() + s).collect() + s.split('\n').map(|s| indent.clone() + s).collect() } pub fn get_hash(t: &T) -> usize { diff --git a/compiler/erg_common/python_util.rs b/compiler/erg_common/python_util.rs index 3e964ea8..95ad997d 100644 --- a/compiler/erg_common/python_util.rs +++ b/compiler/erg_common/python_util.rs @@ -13,9 +13,9 @@ pub fn which_python() -> String { .expect("python not found"); let res = String::from_utf8(out.stdout) .unwrap() - .replace("\n", "") - .replace("\r", ""); - if res == "" { + .replace('\n', "") + .replace('\r', ""); + if res.is_empty() { panic!("python not found"); } res diff --git a/compiler/erg_common/str.rs b/compiler/erg_common/str.rs index 872ba230..654810e5 100644 --- a/compiler/erg_common/str.rs +++ b/compiler/erg_common/str.rs @@ -17,7 +17,7 @@ pub enum Str { impl PartialEq for Str { #[inline] fn eq(&self, other: &Str) -> bool { - &self[..] == &other[..] + self[..] == other[..] } } diff --git a/compiler/erg_common/traits.rs b/compiler/erg_common/traits.rs index a3150a4a..f98ef977 100644 --- a/compiler/erg_common/traits.rs +++ b/compiler/erg_common/traits.rs @@ -345,8 +345,8 @@ pub trait Runnable: Sized { let output = stdout(); let mut output = BufWriter::new(output.lock()); log!(f output, "{GREEN}[DEBUG] The REPL has started.{RESET}\n"); - output.write(instance.start_message().as_bytes()).unwrap(); - output.write(instance.ps1().as_bytes()).unwrap(); + output.write_all(instance.start_message().as_bytes()).unwrap(); + output.write_all(instance.ps1().as_bytes()).unwrap(); output.flush().unwrap(); let mut lines = String::new(); loop { @@ -359,20 +359,20 @@ pub trait Runnable: Sized { lines.push_str(&line); if expect_block(&line) || line.starts_with(' ') { lines += "\n"; - output.write(instance.ps2().as_bytes()).unwrap(); + output.write_all(instance.ps2().as_bytes()).unwrap(); output.flush().unwrap(); continue; } match instance.eval(mem::take(&mut lines).into()) { Ok(out) => { - output.write((out + "\n").as_bytes()).unwrap(); + output.write_all((out + "\n").as_bytes()).unwrap(); output.flush().unwrap(); } Err(e) => { e.fmt_all_stderr(); } } - output.write(instance.ps1().as_bytes()).unwrap(); + output.write_all(instance.ps1().as_bytes()).unwrap(); output.flush().unwrap(); instance.clear(); } diff --git a/compiler/erg_common/ty.rs b/compiler/erg_common/ty.rs index 9faf64ea..4ea2bc7b 100644 --- a/compiler/erg_common/ty.rs +++ b/compiler/erg_common/ty.rs @@ -293,14 +293,14 @@ impl Free { self.0 .borrow() .constraint() - .and_then(|c| c.typ().map(|t| t.clone())) + .and_then(|c| c.typ().cloned()) } pub fn subtype_of(&self) -> Option { self.0 .borrow() .constraint() - .and_then(|c| c.super_type().map(|t| t.clone())) + .and_then(|c| c.super_type().cloned()) } pub fn is_unbound(&self) -> bool { diff --git a/compiler/erg_common/value.rs b/compiler/erg_common/value.rs index 16ec77cf..bf966db7 100644 --- a/compiler/erg_common/value.rs +++ b/compiler/erg_common/value.rs @@ -17,7 +17,7 @@ use crate::{RcArray, Str}; /// 値オブジェクト /// コンパイル時評価ができ、シリアライズも可能 -#[derive(Clone, PartialEq)] +#[derive(Clone)] pub enum ValueObj { Int(i32), Nat(u64), @@ -92,6 +92,29 @@ impl Neg for ValueObj { } } +impl PartialEq for ValueObj { + fn eq(&self, other: &ValueObj) -> bool { + match (self, other) { + (Self::Int(i), Self::Int(j)) => i == j, + (Self::Nat(n), Self::Nat(m)) => n == m, + (Self::Float(fl), Self::Float(fr)) => fl == fr, + (Self::Str(s), Self::Str(t)) => s == t, + (Self::True, Self::True) => true, + (Self::False, Self::False) => true, + (Self::Array(arr), Self::Array(arr2)) => arr == arr2, + (Self::Dict(dict), Self::Dict(dict2)) => dict == dict2, + (Self::Code(code), Self::Code(code2)) => code == code2, + (Self::None, Self::None) => true, + (Self::Ellipsis, Self::Ellipsis) => true, + (Self::NotImplemented, Self::NotImplemented) => true, + (Self::NegInf, Self::NegInf) => true, + (Self::Inf, Self::Inf) => true, + (Self::Illegal, Self::Illegal) => true, + _ => false, + } + } +} + // FIXME: impl Hash for ValueObj { fn hash(&self, state: &mut H) {