add Into<String> arg for error constructors

This commit is contained in:
greg 2019-02-20 02:05:24 -08:00
parent bcf48d0ecb
commit 2be55958f4
2 changed files with 14 additions and 14 deletions

View File

@ -17,8 +17,8 @@ pub struct ParseError {
}
impl ParseError {
fn new_with_token<T>(msg: &str, token: Token) -> ParseResult<T>{
Err(ParseError { msg: msg.to_string(), token })
fn new_with_token<T, M>(msg: M, token: Token) -> ParseResult<T> where M: Into<String> {
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::<f64>() {
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::<u64>() {
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),
}
}
}

View File

@ -22,8 +22,8 @@ type InferResult<T> = Result<T, TypeError>;
pub struct TypeError { pub msg: String }
impl TypeError {
fn new<A>(msg: &str) -> InferResult<A> { //TODO make these kinds of error-producing functions CoW-ready
Err(TypeError { msg: msg.to_string() })
fn new<A, T>(msg: T) -> InferResult<A> where T: Into<String> { //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<String>) -> InferResult<Type> {
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)),
})
}
}