resolver: Always validate return statements

Previously we were only validating return statements that had values,
which meant we were not catching issues when the return value was
omitted in a non-void function.

Fixed: chromium:1219037
Change-Id: If68f67a4a79e46894eee08322726000074c9095b
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/54561
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: James Price <jrprice@google.com>
Auto-Submit: James Price <jrprice@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
James Price 2021-06-15 15:00:57 +00:00 committed by Tint LUCI CQ
parent d80bb9d997
commit 7e22196a35
2 changed files with 21 additions and 5 deletions

View File

@ -200,6 +200,21 @@ TEST_F(ResolverFunctionValidationTest,
"return type, returned 'i32', expected 'void'"); "return type, returned 'i32', expected 'void'");
} }
TEST_F(ResolverFunctionValidationTest,
FunctionTypeMustMatchReturnStatementTypeMissing_fail) {
// fn func -> f32 { return; }
Func("func", ast::VariableList{}, ty.f32(),
ast::StatementList{
Return(Source{Source::Location{12, 34}}, nullptr),
},
ast::DecorationList{});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error v-000y: return statement type must match its function "
"return type, returned 'void', expected 'f32'");
}
TEST_F(ResolverFunctionValidationTest, TEST_F(ResolverFunctionValidationTest,
FunctionTypeMustMatchReturnStatementTypeF32_pass) { FunctionTypeMustMatchReturnStatementTypeF32_pass) {
// fn func -> f32 { return 2.0; } // fn func -> f32 { return 2.0; }

View File

@ -3189,13 +3189,14 @@ bool Resolver::Return(ast::ReturnStatement* ret) {
if (auto* value = ret->value()) { if (auto* value = ret->value()) {
Mark(value); Mark(value);
if (!Expression(value)) {
// Validate after processing the return value expression so that its type return false;
// is available for validation }
return Expression(value) && ValidateReturn(ret);
} }
return true; // Validate after processing the return value expression so that its type is
// available for validation.
return ValidateReturn(ret);
} }
bool Resolver::ValidateSwitch(const ast::SwitchStatement* s) { bool Resolver::ValidateSwitch(const ast::SwitchStatement* s) {