From 2be55958f429b3d9c818a71310ee4bcda1a02197 Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 20 Feb 2019 02:05:24 -0800 Subject: [PATCH] add Into arg for error constructors --- schala-lang/language/src/parsing.rs | 16 ++++++++-------- schala-lang/language/src/typechecking.rs | 12 ++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index f2e4313..bc96350 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -17,8 +17,8 @@ pub struct ParseError { } impl ParseError { - fn new_with_token(msg: &str, token: Token) -> ParseResult{ - Err(ParseError { msg: msg.to_string(), token }) + fn new_with_token(msg: M, token: Token) -> ParseResult where M: Into { + Err(ParseError { msg: msg.into(), token }) } } @@ -112,7 +112,7 @@ macro_rules! expect { $expected_kind if $cond => $self.token_handler.next(), actual_kind => { let msg = format!("Expected {}, got {:?}", print_token_pattern!($expected_kind), actual_kind); - return ParseError::new_with_token(&msg, tok); + return ParseError::new_with_token(msg, tok); } } } @@ -864,7 +864,7 @@ impl Parser { self.token_handler.next(); Pattern::Ignored }, - other => return ParseError::new_with_token(&format!("{:?} is not a valid Pattern", other), tok) + other => return ParseError::new_with_token(format!("{:?} is not a valid Pattern", other), tok) } }) } @@ -982,7 +982,7 @@ impl Parser { let tok = self.token_handler.next(); match tok.get_kind() { Identifier(s) => Ok(s), - p => ParseError::new_with_token(&format!("Expected an identifier, got {:?}", p), tok), + p => ParseError::new_with_token(format!("Expected an identifier, got {:?}", p), tok), } } @@ -1005,7 +1005,7 @@ impl Parser { self.token_handler.next(); Ok(Expression(StringLiteral(s.clone()), None)) } - e => ParseError::new_with_token(&format!("Expected a literal expression, got {:?}", e), tok), + e => ParseError::new_with_token(format!("Expected a literal expression, got {:?}", e), tok), } } @@ -1047,13 +1047,13 @@ impl Parser { digits.push_str(&self.digits()?); match digits.parse::() { Ok(f) => Ok(Expression(FloatLiteral(f), None)), - Err(e) => ParseError::new_with_token(&format!("Float failed to parse with error: {}", e), tok), + Err(e) => ParseError::new_with_token(format!("Float failed to parse with error: {}", e), tok), } } else { match digits.parse::() { Ok(d) => Ok(Expression(NatLiteral(d), None)), - Err(e) => ParseError::new_with_token(&format!("Integer failed to parse with error: {}", e), tok), + Err(e) => ParseError::new_with_token(format!("Integer failed to parse with error: {}", e), tok), } } } diff --git a/schala-lang/language/src/typechecking.rs b/schala-lang/language/src/typechecking.rs index e31db7a..b2d1888 100644 --- a/schala-lang/language/src/typechecking.rs +++ b/schala-lang/language/src/typechecking.rs @@ -22,8 +22,8 @@ type InferResult = Result; pub struct TypeError { pub msg: String } impl TypeError { - fn new(msg: &str) -> InferResult { //TODO make these kinds of error-producing functions CoW-ready - Err(TypeError { msg: msg.to_string() }) + fn new(msg: T) -> InferResult where T: Into { //TODO make these kinds of error-producing functions CoW-ready + Err(TypeError { msg: msg.into() }) } } @@ -267,7 +267,7 @@ impl<'a> TypeContext<'a> { let _ = self.unify(*t1.clone(), tx)?; *t2.clone() }, - _ => return TypeError::new(&format!("Not a function")) + _ => return TypeError::new(format!("Not a function")) }) } @@ -275,7 +275,7 @@ impl<'a> TypeContext<'a> { use self::Discriminator::*; use self::IfExpressionBody::*; match (discriminator, body) { (Simple(expr), SimpleConditional(then_clause, else_clause)) => self.handle_simple_if(expr, then_clause, else_clause), - _ => TypeError::new(&format!("Complex conditionals not supported")) + _ => TypeError::new(format!("Complex conditionals not supported")) } } @@ -307,7 +307,7 @@ impl<'a> TypeContext<'a> { fn handle_value(&mut self, val: &Rc) -> InferResult { match self.variable_map.lookup(val) { Some(ty) => Ok(ty.clone()), - None => TypeError::new(&format!("Couldn't find variable: {}", val)) + None => TypeError::new(format!("Couldn't find variable: {}", val)) } } @@ -315,7 +315,7 @@ impl<'a> TypeContext<'a> { use self::Type::*; use self::TypeConst::*; Ok(match (t1, t2) { (Const(ref c1), Const(ref c2)) if c1 == c2 => Const(c1.clone()), //choice of c1 is arbitrary I *think* - (a, b) => return TypeError::new(&format!("{:?} and {:?} do not unify", a, b)), + (a, b) => return TypeError::new(format!("{:?} and {:?} do not unify", a, b)), }) } }