Improve binary expression validation error message

Example:
```
  var a : i32;
  var b : f32;
  if (a == b) {
    return vec4<f32>(0.4, 0.4, 0.8, 1.0);
  }
```

Outputs:
```
error: test7.wgsl:6:9 error: Binary expression operand types are invalid for this operation: i32 equal f32
  if (a == b) {
        ^^
```

Bug: tint:663
Change-Id: Idd2bb5a248b3c7d652483931d7dd58d5123e9ee8
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/46640
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Antonio Maiorano 2021-04-01 19:40:37 +00:00 committed by Commit Bot service account
parent 15c6ed048c
commit f1b643ee70
3 changed files with 33 additions and 41 deletions

View File

@ -182,66 +182,52 @@ inline bool BinaryExpression::IsBitshift() const {
} }
} }
inline std::ostream& operator<<(std::ostream& out, BinaryOp op) { constexpr const char* FriendlyName(BinaryOp op) {
switch (op) { switch (op) {
case BinaryOp::kNone: case BinaryOp::kNone:
out << "none"; return "none";
break;
case BinaryOp::kAnd: case BinaryOp::kAnd:
out << "and"; return "and";
break;
case BinaryOp::kOr: case BinaryOp::kOr:
out << "or"; return "or";
break;
case BinaryOp::kXor: case BinaryOp::kXor:
out << "xor"; return "xor";
break;
case BinaryOp::kLogicalAnd: case BinaryOp::kLogicalAnd:
out << "logical_and"; return "logical_and";
break;
case BinaryOp::kLogicalOr: case BinaryOp::kLogicalOr:
out << "logical_or"; return "logical_or";
break;
case BinaryOp::kEqual: case BinaryOp::kEqual:
out << "equal"; return "equal";
break;
case BinaryOp::kNotEqual: case BinaryOp::kNotEqual:
out << "not_equal"; return "not_equal";
break;
case BinaryOp::kLessThan: case BinaryOp::kLessThan:
out << "less_than"; return "less_than";
break;
case BinaryOp::kGreaterThan: case BinaryOp::kGreaterThan:
out << "greater_than"; return "greater_than";
break;
case BinaryOp::kLessThanEqual: case BinaryOp::kLessThanEqual:
out << "less_than_equal"; return "less_than_equal";
break;
case BinaryOp::kGreaterThanEqual: case BinaryOp::kGreaterThanEqual:
out << "greater_than_equal"; return "greater_than_equal";
break;
case BinaryOp::kShiftLeft: case BinaryOp::kShiftLeft:
out << "shift_left"; return "shift_left";
break;
case BinaryOp::kShiftRight: case BinaryOp::kShiftRight:
out << "shift_right"; return "shift_right";
break;
case BinaryOp::kAdd: case BinaryOp::kAdd:
out << "add"; return "add";
break;
case BinaryOp::kSubtract: case BinaryOp::kSubtract:
out << "subtract"; return "subtract";
break;
case BinaryOp::kMultiply: case BinaryOp::kMultiply:
out << "multiply"; return "multiply";
break;
case BinaryOp::kDivide: case BinaryOp::kDivide:
out << "divide"; return "divide";
break;
case BinaryOp::kModulo: case BinaryOp::kModulo:
out << "modulo"; return "modulo";
break;
} }
return "INVALID";
}
inline std::ostream& operator<<(std::ostream& out, BinaryOp op) {
out << FriendlyName(op);
return out; return out;
} }

View File

@ -1174,7 +1174,10 @@ bool Resolver::ValidateBinary(ast::BinaryExpression* expr) {
} }
diagnostics_.add_error( diagnostics_.add_error(
"Binary expression operand types are invalid for this operation", "Binary expression operand types are invalid for this operation: " +
lhs_type->FriendlyName(builder_->Symbols()) + " " +
FriendlyName(expr->op()) + " " +
rhs_type->FriendlyName(builder_->Symbols()),
expr->source()); expr->source());
return false; return false;
} }

View File

@ -1270,7 +1270,10 @@ TEST_P(Expr_Binary_Test_Invalid, All) {
ASSERT_FALSE(r()->Resolve()) << r()->error(); ASSERT_FALSE(r()->Resolve()) << r()->error();
ASSERT_EQ(r()->error(), ASSERT_EQ(r()->error(),
"12:34 error: Binary expression operand types are invalid for " "12:34 error: Binary expression operand types are invalid for "
"this operation"); "this operation: " +
lhs_type->FriendlyName(Symbols()) + " " +
FriendlyName(expr->op()) + " " +
rhs_type->FriendlyName(Symbols()));
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
ResolverTest, ResolverTest,