diff options
author | wszqkzqk <wszqkzqk@qq.com> | 2022-12-13 21:56:53 +0800 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2022-12-22 17:13:52 +0100 |
commit | c5679d09cdc8b49c9b886fc6617db901350c301d (patch) | |
tree | a75445dfdeaef656e8c03140f793028c3e746369 /vala | |
parent | a67d19f51082944e3751760d6c0ae09efc35b860 (diff) | |
download | vala-c5679d09cdc8b49c9b886fc6617db901350c301d.tar.gz |
parser: Properly handle chained equality expressions
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1385
Diffstat (limited to 'vala')
-rw-r--r-- | vala/valabinaryexpression.vala | 44 | ||||
-rw-r--r-- | vala/valaparser.vala | 27 |
2 files changed, 39 insertions, 32 deletions
diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala index 6545e1cff..d627e2942 100644 --- a/vala/valabinaryexpression.vala +++ b/vala/valabinaryexpression.vala @@ -467,7 +467,12 @@ public class Vala.BinaryExpression : Expression { DataType resulting_type; if (is_chained) { - var lbe = (BinaryExpression) left; + unowned BinaryExpression lbe = (BinaryExpression) left; + if (lbe.right.value_type.compatible (context.analyzer.string_type) + && right.value_type.compatible (context.analyzer.string_type)) { + value_type = context.analyzer.bool_type; + break; + } resulting_type = context.analyzer.get_arithmetic_result_type (lbe.right.target_type, right.target_type); } else { resulting_type = context.analyzer.get_arithmetic_result_type (left.target_type, right.target_type); @@ -475,7 +480,14 @@ public class Vala.BinaryExpression : Expression { if (resulting_type == null) { error = true; - Report.error (source_reference, "Relational operation not supported for types `%s' and `%s'", left.value_type.to_string (), right.value_type.to_string ()); + unowned DataType left_type; + if (is_chained) { + unowned BinaryExpression lbe = (BinaryExpression) left; + left_type = lbe.right.value_type; + } else { + left_type = left.value_type; + } + Report.error (source_reference, "Relational operation not supported for types `%s' and `%s'", left_type.to_string (), right.value_type.to_string ()); return false; } @@ -515,17 +527,31 @@ public class Vala.BinaryExpression : Expression { } } - if (!right.value_type.compatible (left.value_type) - && !left.value_type.compatible (right.value_type)) { - Report.error (source_reference, "Equality operation: `%s' and `%s' are incompatible", right.value_type.to_string (), left.value_type.to_string ()); - error = true; - return false; + DataType resulting_type; + if (is_chained) { + unowned BinaryExpression lbe = (BinaryExpression) left; + resulting_type = context.analyzer.get_arithmetic_result_type (lbe.right.target_type, right.target_type); + if (!right.value_type.compatible (lbe.right.value_type) + && !lbe.right.value_type.compatible (right.value_type)) { + Report.error (source_reference, "Equality operation: `%s' and `%s' are incompatible", right.value_type.to_string (), lbe.right.value_type.to_string ()); + error = true; + return false; + } + } else { + resulting_type = context.analyzer.get_arithmetic_result_type (left.target_type, right.target_type); + if (!right.value_type.compatible (left.value_type) + && !left.value_type.compatible (right.value_type)) { + Report.error (source_reference, "Equality operation: `%s' and `%s' are incompatible", right.value_type.to_string (), left.value_type.to_string ()); + error = true; + return false; + } } - var resulting_type = context.analyzer.get_arithmetic_result_type (left.target_type, right.target_type); if (resulting_type != null) { // numeric operation - left.target_type = resulting_type.copy (); + if (!is_chained) { + left.target_type = resulting_type.copy (); + } right.target_type = resulting_type.copy (); } diff --git a/vala/valaparser.vala b/vala/valaparser.vala index d932b8b2d..25769ec54 100644 --- a/vala/valaparser.vala +++ b/vala/valaparser.vala @@ -1452,6 +1452,8 @@ public class Vala.Parser : CodeVisitor { case BinaryOperator.LESS_THAN: case BinaryOperator.LESS_THAN_OR_EQUAL: case BinaryOperator.GREATER_THAN_OR_EQUAL: + case BinaryOperator.EQUALITY: + case BinaryOperator.INEQUALITY: next (); var right = parse_type_check_expression (); if (first) { @@ -1485,32 +1487,11 @@ public class Vala.Parser : CodeVisitor { return left; } - Expression parse_equality_expression () throws ParseError { - var begin = get_location (); - var left = parse_relational_expression (); - bool found = true; - while (found) { - var operator = get_binary_operator (current ()); - switch (operator) { - case BinaryOperator.EQUALITY: - case BinaryOperator.INEQUALITY: - next (); - var right = parse_relational_expression (); - left = new BinaryExpression (operator, left, right, get_src (begin)); - break; - default: - found = false; - break; - } - } - return left; - } - Expression parse_and_expression () throws ParseError { var begin = get_location (); - var left = parse_equality_expression (); + var left = parse_relational_expression (); while (accept (TokenType.BITWISE_AND)) { - var right = parse_equality_expression (); + var right = parse_relational_expression (); left = new BinaryExpression (BinaryOperator.BITWISE_AND, left, right, get_src (begin)); } return left; |