Skip to content

Commit c761052

Browse files
committed
Always check the posibility for implicit casting before explicit one
1 parent a4360d9 commit c761052

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

crates/gitql-parser/src/parse_cast.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub(crate) fn parse_cast_operator_expression(
2020
position: &mut usize,
2121
) -> Result<Box<dyn Expr>, Box<Diagnostic>> {
2222
let expr = parse_index_or_slice_expression(context, env, tokens, position)?;
23-
2423
if *position < tokens.len() && tokens[*position].kind == TokenKind::ColonColon {
2524
// Consume `::` Token
2625
let colon_colon_token = &tokens[*position];
@@ -29,7 +28,6 @@ pub(crate) fn parse_cast_operator_expression(
2928
let target_type = parse_type(env, tokens, position)?;
3029
return cast_expression_or_error(expr, target_type, colon_colon_token.location);
3130
}
32-
3331
Ok(expr)
3432
}
3533

@@ -76,18 +74,25 @@ fn cast_expression_or_error(
7674
target_type: Box<dyn DataType>,
7775
location: SourceLocation,
7876
) -> Result<Box<dyn Expr>, Box<Diagnostic>> {
79-
let value_type = expr.expr_type();
80-
let value_expected_types = value_type.can_perform_explicit_cast_op_to();
77+
// Check if it's possiable to perform implicit cast directly
78+
if target_type.has_implicit_cast_from(&expr) {
79+
return Ok(Box::new(CastExpr {
80+
value: expr,
81+
result_type: target_type,
82+
}));
83+
}
8184

82-
// If it's supported to cast this value to result type, just return CastExpr
85+
// Check if it's supported to cast this value to result type, just return CastExpr
86+
let value_type: Box<dyn DataType> = expr.expr_type();
87+
let value_expected_types = value_type.can_perform_explicit_cast_op_to();
8388
if value_expected_types.contains(&target_type) {
8489
return Ok(Box::new(CastExpr {
8590
value: expr,
8691
result_type: target_type,
8792
}));
8893
}
8994

90-
// Check if it possible to implicit cast the value to one of the expected type of result type
95+
// Check if it's possible to implicit cast the value to one of the expected type of result type
9196
// then Cast from expected type to the result type
9297
// Examples: Cast("true" as Int) can be casted as Text -> Bool -> Int
9398
let expected_types = target_type.can_perform_explicit_cast_op_to();

0 commit comments

Comments
 (0)