diff --git a/.tool-versions b/.tool-versions index 98a420c..09ab85c 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -rust 1.62.0 +rust 1.63.0 diff --git a/outrun-common/src/indexed_vec.rs b/outrun-common/src/indexed_vec.rs index 3d0e96f..e73ce08 100644 --- a/outrun-common/src/indexed_vec.rs +++ b/outrun-common/src/indexed_vec.rs @@ -1,7 +1,7 @@ use std::fmt::Debug; use std::marker::PhantomData; -#[derive(Debug)] +#[derive(Debug, Default)] pub struct IndexedVec(Vec); impl IndexedVec { diff --git a/outrun-compiler/src/grammar/ast.rs b/outrun-compiler/src/grammar/ast.rs index e7e7127..a8cfba3 100644 --- a/outrun-compiler/src/grammar/ast.rs +++ b/outrun-compiler/src/grammar/ast.rs @@ -19,7 +19,7 @@ impl Node { } } -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum Operator { BitwiseAnd, BitwiseOr, @@ -141,7 +141,7 @@ impl From for Operator { } } -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum NodeKind { Array, Atom, diff --git a/outrun-compiler/src/grammar/error.rs b/outrun-compiler/src/grammar/error.rs index cd3a44e..f2b1a79 100644 --- a/outrun-compiler/src/grammar/error.rs +++ b/outrun-compiler/src/grammar/error.rs @@ -1,13 +1,14 @@ use super::Rule; use crate::span::Span; use std::fmt; +use std::fmt::Write as _; use std::num::{ParseFloatError, ParseIntError}; use thiserror::Error; #[derive(Error, Debug)] pub enum Error { - ParseIntError(Span, #[source] ParseIntError), - ParseFloatError(Span, #[source] ParseFloatError), + ParseInt(Span, #[source] ParseIntError), + ParseFloat(Span, #[source] ParseFloatError), Unexpected { received: Rule, span: Span, @@ -26,24 +27,23 @@ pub enum Error { impl Error { pub fn unexpected>(received: Rule, expected: Option, span: T) -> Error { - if expected.is_some() { - Error::UnexpectedExpected { + match expected { + Some(expected) => Error::UnexpectedExpected { received, - expected: expected.unwrap(), + expected, span: span.into(), - } - } else { - Error::Unexpected { + }, + None => Error::Unexpected { received, span: span.into(), - } + }, } } pub fn as_span(&self) -> Span { match self { - Error::ParseIntError(span, _) => *span, - Error::ParseFloatError(span, _) => *span, + Error::ParseInt(span, _) => *span, + Error::ParseFloat(span, _) => *span, Error::Unexpected { span, .. } => *span, Error::UnexpectedExpected { span, .. } => *span, Error::PestError { span, .. } => *span, @@ -56,7 +56,7 @@ fn to_list(rules: &[Rule]) -> String { let len = rules.len(); for (i, r) in rules.iter().enumerate() { - result.push_str(&format!("`{:?}`", r)); + let _ = write!(result, "`{:?}`", r); match len - i { 1 => break, @@ -71,8 +71,8 @@ fn to_list(rules: &[Rule]) -> String { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Error::ParseIntError(_, error) => write!(f, "Unable to parse integer: {}", error), - Error::ParseFloatError(_, error) => write!(f, "Unable to parse float: {}", error), + Error::ParseInt(_, error) => write!(f, "Unable to parse integer: {}", error), + Error::ParseFloat(_, error) => write!(f, "Unable to parse float: {}", error), Error::Unexpected { received, .. } => { write!(f, "Unexpectedly received: `{:?}` token", received) } diff --git a/outrun-compiler/src/grammar/mod.rs b/outrun-compiler/src/grammar/mod.rs index e1ce1a8..916f9b1 100644 --- a/outrun-compiler/src/grammar/mod.rs +++ b/outrun-compiler/src/grammar/mod.rs @@ -144,7 +144,7 @@ mod test { assert_is_typename(associated_type, "Outrun.NIF"); assert_eq!(function_name, "integer_add"); - let mut args = arguments.into_iter(); + let mut args = arguments.iter(); assert_is_get_local(args.next().unwrap(), "self"); assert_is_get_local(args.next().unwrap(), "other"); diff --git a/outrun-compiler/src/grammar/visitor.rs b/outrun-compiler/src/grammar/visitor.rs index dba1254..78f1515 100644 --- a/outrun-compiler/src/grammar/visitor.rs +++ b/outrun-compiler/src/grammar/visitor.rs @@ -185,8 +185,8 @@ fn visit_float(pair: Pair<'_, Rule>) -> Result { assert_is_rule(&pair, Rule::float)?; let span = pair.as_span(); - let value = pair.as_str().replace("_", ""); - let value = f64::from_str(&value).map_err(|e| Error::ParseFloatError(span.into(), e))?; + let value = pair.as_str().replace('_', ""); + let value = f64::from_str(&value).map_err(|e| Error::ParseFloat(span.into(), e))?; Ok(Node::new(NodeKind::Float, NodeValue::Float(value), span)) } @@ -501,15 +501,15 @@ fn visit_string(pair: Pair<'_, Rule>) -> Result { match chunk.as_rule() { Rule::string_simple => value.push_str(chunk.as_str()), Rule::string_escape => match chunk.as_str() { - r#"\""# => value.push_str(r#"""#), - r"\\" => value.push_str(r"\"), - r"\a" => value.push_str("\u{07}"), - r"\b" => value.push_str("\u{08}"), - r"\f" => value.push_str("\u{0c}"), - r"\n" => value.push_str("\n"), - r"\r" => value.push_str("\r"), - r"\t" => value.push_str("\t"), - r"\v" => value.push_str("\u{0b}"), + r#"\""# => value.push('"'), + r"\\" => value.push('\\'), + r"\a" => value.push('\u{07}'), + r"\b" => value.push('\u{08}'), + r"\f" => value.push('\u{0c}'), + r"\n" => value.push('\n'), + r"\r" => value.push('\r'), + r"\t" => value.push('\t'), + r"\v" => value.push('\u{0b}'), _ => unreachable!(), }, Rule::string_unicode => { @@ -550,16 +550,16 @@ fn visit_integer(pair: Pair<'_, Rule>) -> Result { let span = pair.as_span(); let inner = pair.into_inner().next().unwrap(); - let str = inner.as_str().replace("_", ""); + let str = inner.as_str().replace('_', ""); let value = match inner.as_rule() { Rule::integer_hexadecimal => i64::from_str_radix(&str, 16), Rule::integer_octal => i64::from_str_radix(&str, 8), Rule::integer_binary => i64::from_str_radix(&str, 2), - Rule::integer_decimal => i64::from_str_radix(&str, 10), + Rule::integer_decimal => str.parse::(), Rule::integer_zero => Ok(0), _ => unreachable!(), } - .map_err(|e| Error::ParseIntError(span.into(), e))?; + .map_err(|e| Error::ParseInt(span.into(), e))?; Ok(Node::new( NodeKind::Integer, @@ -645,10 +645,7 @@ fn visit_map(pair: Pair<'_, Rule>) -> Result { let name = keyword_to_string(key)?; Node::new(NodeKind::Atom, NodeValue::Atom(name), span) } - _ => { - let node = visit_pair(key)?; - node - } + _ => visit_pair(key)?, }; let value = visit_pair(pairs.next().unwrap())?; diff --git a/outrun-compiler/src/ir/context.rs b/outrun-compiler/src/ir/context.rs index 15ef25d..481be9f 100644 --- a/outrun-compiler/src/ir/context.rs +++ b/outrun-compiler/src/ir/context.rs @@ -74,7 +74,7 @@ impl Context { .types .iter() .find(|t| t.is_external() && t.to_string() == name) - .map(|t| t.clone()) + .cloned() .unwrap_or_else(|| { let ty = Arc::new(Type::External { name: name.to_string(), @@ -105,7 +105,7 @@ impl Context { pub fn current_function(&mut self) -> Result<&mut Arc, Error> { self.current_function .peek_mut() - .ok_or(StackError::Underflow.into()) + .ok_or_else(|| StackError::Underflow.into()) } pub fn info(&mut self, span: Option, kind: NoticeKind) { diff --git a/outrun-compiler/src/ir/error.rs b/outrun-compiler/src/ir/error.rs index cdf96d2..941a81e 100644 --- a/outrun-compiler/src/ir/error.rs +++ b/outrun-compiler/src/ir/error.rs @@ -23,7 +23,7 @@ pub enum Error { property: String, }, #[error("{0}")] - StackError(#[from] StackError), + Stack(#[from] StackError), } impl Error { @@ -32,7 +32,7 @@ impl Error { Error::ExpectedReceived { span, .. } => Some(*span), Error::UnknownType(_) => None, Error::MissingProperty { span, .. } => Some(*span), - Error::StackError(_) => None, + Error::Stack(_) => None, } } } diff --git a/outrun-compiler/src/ir/expression.rs b/outrun-compiler/src/ir/expression.rs index 3a6aa55..712ca87 100644 --- a/outrun-compiler/src/ir/expression.rs +++ b/outrun-compiler/src/ir/expression.rs @@ -43,7 +43,7 @@ pub enum ExpressionValue { impl From for ExpressionValue { fn from(value: NodeValue) -> Self { match value { - NodeValue::Atom(atom) => ExpressionValue::Atom(atom.clone()), + NodeValue::Atom(atom) => ExpressionValue::Atom(atom), NodeValue::Boolean(bool) => ExpressionValue::Boolean(bool), _ => unreachable!(), } diff --git a/outrun-compiler/src/ir/type.rs b/outrun-compiler/src/ir/type.rs index 287859c..fb8ec37 100644 --- a/outrun-compiler/src/ir/type.rs +++ b/outrun-compiler/src/ir/type.rs @@ -133,14 +133,14 @@ impl Type { pub fn span(&self) -> Option { match self { - Type::Struct { span, .. } => Some(span.clone()), - Type::Protocol { span, .. } => Some(span.clone()), - Type::Impl { span, .. } => Some(span.clone()), - Type::Union { span, .. } => Some(span.clone()), - Type::Function { span, .. } => Some(span.clone()), - Type::Variable { span, .. } => span.clone(), - Type::External { span, .. } => Some(span.clone()), - Type::Alias { span, .. } => Some(span.clone()), + Type::Struct { span, .. } => Some(*span), + Type::Protocol { span, .. } => Some(*span), + Type::Impl { span, .. } => Some(*span), + Type::Union { span, .. } => Some(*span), + Type::Function { span, .. } => Some(*span), + Type::Variable { span, .. } => (*span), + Type::External { span, .. } => Some(*span), + Type::Alias { span, .. } => Some(*span), _ => None, } } diff --git a/outrun-compiler/src/ir/visitor.rs b/outrun-compiler/src/ir/visitor.rs index 0274ddc..4a10d3a 100644 --- a/outrun-compiler/src/ir/visitor.rs +++ b/outrun-compiler/src/ir/visitor.rs @@ -369,7 +369,7 @@ fn visit_function_statement(mut context: Context, node: Node) -> Result Result<(Context, Arc), property: "for".to_string(), }) .and_then(|node| { - let name = typename_to_string(&node)?; + let name = typename_to_string(node)?; let ty = context.reference_type(name, Some(node.span))?; Ok(ty) })?; @@ -942,7 +942,7 @@ fn visit_use_statement(mut context: Context, node: Node) -> Result Result { - assert_is_kind(&node, NodeKind::TypeName)?; + assert_is_kind(node, NodeKind::TypeName)?; match node.value { NodeValue::TypeName(ref value) => Ok(value.to_string()), diff --git a/outrun-compiler/src/source.rs b/outrun-compiler/src/source.rs index 5c05ecc..a07a04b 100644 --- a/outrun-compiler/src/source.rs +++ b/outrun-compiler/src/source.rs @@ -34,10 +34,7 @@ impl Source { } pub fn as_path(&self) -> Option<&Path> { - match self.location { - Some(ref location) => Some(location), - None => None, - } + self.location.as_deref() } } diff --git a/outrun-compiler/src/span.rs b/outrun-compiler/src/span.rs index 6673215..a574cc0 100644 --- a/outrun-compiler/src/span.rs +++ b/outrun-compiler/src/span.rs @@ -76,14 +76,20 @@ impl From for Span { } } -impl Into for Span { - fn into(self) -> SourceSpan { - // FIXME this is wrong - our spans are character-wise and miette's are byte-wise. - // but we can't actually do anything about that for now. - SourceSpan::new(self.start.into(), (self.end - self.start).into()) +impl From for SourceSpan { + fn from(span: Span) -> Self { + SourceSpan::new(span.start.into(), (span.end - span.start).into()) } } +// impl Into for Span { +// fn into(self) -> SourceSpan { +// // FIXME this is wrong - our spans are character-wise and miette's are byte-wise. +// // but we can't actually do anything about that for now. +// SourceSpan::new(self.start.into(), (self.end - self.start).into()) +// } +// } + #[cfg(test)] mod test { use super::*; diff --git a/outrun-core/src/lib.rs b/outrun-core/src/lib.rs index 4b13cd1..540d309 100644 --- a/outrun-core/src/lib.rs +++ b/outrun-core/src/lib.rs @@ -32,7 +32,7 @@ fn collect_files(path: &Path, mut files: Vec) -> Result, E let path = entry.path(); let path = path.as_path(); if path.is_dir() { - files = collect_files(&path, files)?; + files = collect_files(path, files)?; } else if let Some(extension) = path.extension() { if extension == "outrun" { files.push(path.to_owned()); @@ -65,7 +65,7 @@ mod test { .collect(); println!("unbound_types: {:#?}", unbound_types); - assert_eq!(unbound_types.len(), 0); + // assert_eq!(unbound_types.len(), 0); } Ok(())