Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/type-checker/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ pub enum TypeCheckError {
location: Location,
},

#[error("{location}: combined unary operators are prohibited")]
CombinedUnaryOperators { location: Location },

#[error(
"{location}: cannot apply operator `{operator:?}` to operands of different types: `{left}` and `{right}`"
)]
Expand Down Expand Up @@ -357,6 +360,7 @@ impl TypeCheckError {
| TypeCheckError::MissingTypeParameters { location, .. }
| TypeCheckError::InvalidBinaryOperand { location, .. }
| TypeCheckError::InvalidUnaryOperand { location, .. }
| TypeCheckError::CombinedUnaryOperators { location }
| TypeCheckError::BinaryOperandTypeMismatch { location, .. }
| TypeCheckError::SelfReferenceInFunction { location, .. }
| TypeCheckError::SelfReferenceOutsideMethod { location }
Expand Down
17 changes: 17 additions & 0 deletions core/type-checker/src/type_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,23 @@ impl TypeChecker {
None
}
Expression::PrefixUnary(prefix_unary_expression) => {
fn is_prefix_unary(expr: &Expression) -> bool {
match expr {
Expression::PrefixUnary(_) => true,
Expression::Parenthesized(inner) => {
is_prefix_unary(&inner.expression.borrow())
}
Expression::Literal(Literal::Number(num)) => num.value.starts_with('-'),
_ => false,
}
}

if is_prefix_unary(&prefix_unary_expression.expression.borrow()) {
self.errors.push(TypeCheckError::CombinedUnaryOperators {
location: prefix_unary_expression.location,
});
return None;
}
match prefix_unary_expression.operator {
UnaryOperatorKind::Not => {
let expression_type_op = self
Expand Down
14 changes: 9 additions & 5 deletions tests/src/type_checker/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,11 +751,15 @@ mod expression_coverage {
fn test_unary_neg_nested() {
let source = r#"fn test() -> i32 { return --42; }"#;
let result = try_type_check(source);
assert!(
result.is_ok(),
"Double unary neg should work, got: {:?}",
result.err()
);
assert!(result.is_err(), "Double unary neg should be prohibited");
if let Err(error) = result {
let error_msg = error.to_string();
assert!(
error_msg.contains("combined unary operators"),
"Error should mention combined unary operators: {}",
error_msg
);
}
}

// FIXME: Test disabled due to parser or type checker limitation
Expand Down
45 changes: 33 additions & 12 deletions tests/src/type_checker/type_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2166,10 +2166,12 @@ mod unary_operator_tests {
fn test_double_negate() {
let source = r#"fn test(x: i32) -> i32 { return --(x); }"#;
let result = try_type_check(source);
assert!(result.is_err(), "Double negation should be prohibited");
let err_msg = result.err().unwrap().to_string();
assert!(
result.is_ok(),
"Double negation should succeed, got: {:?}",
result.err()
err_msg.contains("combined unary operators"),
"Error should mention combined unary operators, got: {}",
err_msg
);
}
}
Expand Down Expand Up @@ -2282,21 +2284,38 @@ mod unary_operator_tests {
fn test_bitnot_combined_with_negate() {
let source = r#"fn test(x: i32) -> i32 { return ~-(x); }"#;
let result = try_type_check(source);
assert!(result.is_err(), "Combined unary operators should be prohibited");
let err_msg = result.err().unwrap().to_string();
assert!(
result.is_ok(),
"Combining BitNot and Neg should succeed, got: {:?}",
result.err()
err_msg.contains("combined unary operators"),
"Error should mention combined unary operators, got: {}",
err_msg
);
}

#[test]
fn test_negate_combined_with_bitnot() {
let source = r#"fn test(x: i32) -> i32 { return -(~x); }"#;
let result = try_type_check(source);
assert!(result.is_err(), "Combined unary operators should be prohibited");
let err_msg = result.err().unwrap().to_string();
assert!(
result.is_ok(),
"Combining Neg and BitNot should succeed, got: {:?}",
result.err()
err_msg.contains("combined unary operators"),
"Error should mention combined unary operators, got: {}",
err_msg
);
}

#[test]
fn test_bitnot_then_neg_literal_combination_errors() {
let source = r#"fn test() -> i32 { return -~42; }"#;
let result = try_type_check(source);
assert!(result.is_err(), "Combined unary operators should be prohibited");
let err_msg = result.err().unwrap().to_string();
assert!(
err_msg.contains("combined unary operators"),
"Error should mention combined unary operators, got: {}",
err_msg
);
}
}
Expand Down Expand Up @@ -2332,10 +2351,12 @@ mod unary_operator_tests {
fn test_double_logical_not() {
let source = r#"fn test(x: bool) -> bool { return !!x; }"#;
let result = try_type_check(source);
assert!(result.is_err(), "Double logical NOT should be prohibited");
let err_msg = result.err().unwrap().to_string();
assert!(
result.is_ok(),
"Double logical NOT should succeed, got: {:?}",
result.err()
err_msg.contains("combined unary operators"),
"Error should mention combined unary operators, got: {}",
err_msg
);
}
}
Expand Down