Skip to content

Commit

Permalink
Check type in unary operator expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp authored and David Peter committed Sep 11, 2023
1 parent c087a25 commit ca2b470
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions numbat/src/typechecker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,18 +395,18 @@ impl TypeChecker {
ast::Expression::UnaryOperator { op, expr, span_op } => {
let checked_expr = self.check_expression(expr)?;
let type_ = checked_expr.get_type();
let dtype = match &type_ {
Type::Dimension(d) => d.clone(),
_ => {
return Err(TypeCheckError::ExpectedDimensionType(
checked_expr.full_span(),
type_.clone(),
))
}
};

match *op {
ast::UnaryOperator::Factorial => {
let dtype = match &type_ {
Type::Dimension(d) => d.clone(),
_ => {
return Err(TypeCheckError::ExpectedDimensionType(
checked_expr.full_span(),
type_.clone(),
))
}
};
if dtype != DType::unity() {
return Err(TypeCheckError::NonScalarFactorialArgument(
expr.full_span(),
Expand Down Expand Up @@ -1503,6 +1503,14 @@ mod tests {
));
}

#[test]
fn boolean_values() {
assert!(matches!(
get_typecheck_error("-true"),
TypeCheckError::ExpectedDimensionType(_, _)
));
}

#[test]
fn conditionals() {
assert_successful_typecheck("if true then 1 else 2");
Expand Down

0 comments on commit ca2b470

Please sign in to comment.