Resolver: validate else condition is boolean
Bug: tint:786 Change-Id: I5e4987e4eeb0608c640fc5c8ea035c8dca0ef187 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/50461 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:
parent
6a56744b55
commit
9fdfa1e323
|
@ -1196,6 +1196,15 @@ bool Resolver::IfStatement(ast::IfStatement* stmt) {
|
||||||
if (!Expression(cond)) {
|
if (!Expression(cond)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto* else_cond_type = TypeOf(cond)->UnwrapAll();
|
||||||
|
if (else_cond_type != builder_->ty.bool_()) {
|
||||||
|
diagnostics_.add_error(
|
||||||
|
"else statement condition must be bool, got " +
|
||||||
|
else_cond_type->FriendlyName(builder_->Symbols()),
|
||||||
|
cond->source());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Mark(else_stmt->body());
|
Mark(else_stmt->body());
|
||||||
if (!BlockStatement(else_stmt->body())) {
|
if (!BlockStatement(else_stmt->body())) {
|
||||||
|
|
|
@ -125,7 +125,7 @@ TEST_F(ResolverTest, Stmt_If) {
|
||||||
|
|
||||||
auto* else_body = Block(Assign(else_lhs, else_rhs));
|
auto* else_body = Block(Assign(else_lhs, else_rhs));
|
||||||
|
|
||||||
auto* else_cond = Expr(3);
|
auto* else_cond = Expr(true);
|
||||||
auto* else_stmt = create<ast::ElseStatement>(else_cond, else_body);
|
auto* else_stmt = create<ast::ElseStatement>(else_cond, else_body);
|
||||||
|
|
||||||
auto* lhs = Expr("v");
|
auto* lhs = Expr("v");
|
||||||
|
|
|
@ -147,6 +147,20 @@ TEST_F(ResolverValidationTest, Stmt_If_NonBool) {
|
||||||
"12:34 error: if statement condition must be bool, got f32");
|
"12:34 error: if statement condition must be bool, got f32");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(ResolverValidationTest, Stmt_Else_NonBool) {
|
||||||
|
// else (1.23f) {}
|
||||||
|
|
||||||
|
WrapInFunction(If(Expr(true), Block(),
|
||||||
|
Else(create<ast::ScalarConstructorExpression>(
|
||||||
|
Source{{12, 34}}, Literal(1.23f)),
|
||||||
|
Block())));
|
||||||
|
|
||||||
|
EXPECT_FALSE(r()->Resolve());
|
||||||
|
|
||||||
|
EXPECT_EQ(r()->error(),
|
||||||
|
"12:34 error: else statement condition must be bool, got f32");
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(ResolverValidationTest,
|
TEST_F(ResolverValidationTest,
|
||||||
Stmt_VariableDecl_MismatchedTypeScalarConstructor) {
|
Stmt_VariableDecl_MismatchedTypeScalarConstructor) {
|
||||||
u32 unsigned_value = 2u; // Type does not match variable type
|
u32 unsigned_value = 2u; // Type does not match variable type
|
||||||
|
|
Loading…
Reference in New Issue