diff --git a/src/validator/validator_function_test.cc b/src/validator/validator_function_test.cc index e939da4de8..aa4706036e 100644 --- a/src/validator/validator_function_test.cc +++ b/src/validator/validator_function_test.cc @@ -122,6 +122,24 @@ TEST_F(ValidateFunctionTest, FunctionTypeMustMatchReturnStatementType_fail) { "return type, returned '__i32', expected '__void'"); } +TEST_F(ValidateFunctionTest, FunctionTypeMustMatchReturnStatementTypeF32_pass) { + // fn func -> f32 { return 2.0; } + Func("func", ast::VariableList{}, ty.f32(), + ast::StatementList{ + create(Source{Source::Location{12, 34}}, + Expr(2.f)), + }, + ast::DecorationList{}); + Func("main", ast::VariableList{}, ty.void_(), ast::StatementList{}, + ast::DecorationList{ + create(ast::PipelineStage::kVertex), + }); + + ValidatorImpl& v = Build(); + + EXPECT_TRUE(v.Validate()); +} + TEST_F(ValidateFunctionTest, FunctionTypeMustMatchReturnStatementTypeF32_fail) { // fn func -> f32 { return 2; } Func("func", ast::VariableList{}, ty.f32(), @@ -140,6 +158,52 @@ TEST_F(ValidateFunctionTest, FunctionTypeMustMatchReturnStatementTypeF32_fail) { "return type, returned '__i32', expected '__f32'"); } +TEST_F(ValidateFunctionTest, + FunctionTypeMustMatchReturnStatementTypeF32Alias_pass) { + // type myf32 = f32; + // fn func -> myf32 { return 2.0; } + auto* myf32 = ty.alias("myf32", ty.f32()); + Func("func", ast::VariableList{}, myf32, + ast::StatementList{ + create(Source{Source::Location{12, 34}}, + Expr(2.f)), + }, + ast::DecorationList{}); + Func("main", ast::VariableList{}, ty.void_(), ast::StatementList{}, + ast::DecorationList{ + create(ast::PipelineStage::kVertex), + }); + + ValidatorImpl& v = Build(); + + EXPECT_TRUE(v.Validate()); +} + +TEST_F(ValidateFunctionTest, + FunctionTypeMustMatchReturnStatementTypeF32Alias_fail) { + // type myf32 = f32; + // fn func -> myf32 { return 2; } + auto* myf32 = ty.alias("myf32", ty.f32()); + Func("func", ast::VariableList{}, myf32, + ast::StatementList{ + create(Source{Source::Location{12, 34}}, + Expr(2u)), + }, + ast::DecorationList{}); + Func("main", ast::VariableList{}, ty.void_(), ast::StatementList{}, + ast::DecorationList{ + create(ast::PipelineStage::kVertex), + }); + + ValidatorImpl& v = Build(); + + EXPECT_FALSE(v.Validate()); + EXPECT_EQ( + v.error(), + "12:34 v-000y: return statement type must match its function " + "return type, returned '__u32', expected '__alias_tint_symbol_1__f32'"); +} + TEST_F(ValidateFunctionTest, FunctionNamesMustBeUnique_fail) { // fn func -> i32 { return 2; } // fn func -> i32 { return 2; } diff --git a/src/validator/validator_impl.cc b/src/validator/validator_impl.cc index 3a957d4ee8..add2fc1c41 100644 --- a/src/validator/validator_impl.cc +++ b/src/validator/validator_impl.cc @@ -287,7 +287,7 @@ bool ValidatorImpl::ValidateReturnStatement(const ast::ReturnStatement* ret) { ? program_->Sem().Get(ret->value())->Type()->UnwrapAll() : &void_type; - if (func_type->type_name() != ret_type->type_name()) { + if (func_type->UnwrapAll()->type_name() != ret_type->type_name()) { add_error(ret->source(), "v-000y", "return statement type must match its function return " "type, returned '" +