From 844f6327852922d0d9b310a173adb220a96fc836 Mon Sep 17 00:00:00 2001 From: Sarah Mashayekhi Date: Tue, 18 Aug 2020 02:10:03 +0000 Subject: [PATCH] [type-determiner] return false when type determining an undeclared function Change-Id: Ia7e43be64675528037f92026a6c239d1e5220fc0 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/26941 Commit-Queue: dan sinclair Reviewed-by: dan sinclair --- src/type_determiner.cc | 9 ++++++ src/type_determiner_test.cc | 28 +++++++++++++++++++ ...tion-must-be-declared-before-use-fail.wgsl | 23 +++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 test/function-must-be-declared-before-use-fail.wgsl diff --git a/src/type_determiner.cc b/src/type_determiner.cc index de26661300..6cf1791578 100644 --- a/src/type_determiner.cc +++ b/src/type_determiner.cc @@ -511,6 +511,15 @@ bool TypeDeterminer::DetermineCall(ast::CallExpression* expr) { return false; } } + + if (!expr->func()->result_type()) { + auto func_name = expr->func()->AsIdentifier()->name(); + set_error( + expr->source(), + "v-0005: function must be declared before use: '" + func_name + "'"); + return false; + } + expr->set_result_type(expr->func()->result_type()); return true; } diff --git a/src/type_determiner_test.cc b/src/type_determiner_test.cc index 87d0d738f5..092e947452 100644 --- a/src/type_determiner_test.cc +++ b/src/type_determiner_test.cc @@ -387,6 +387,34 @@ TEST_F(TypeDeterminerTest, Stmt_Call) { EXPECT_TRUE(expr_ptr->result_type()->IsF32()); } +TEST_F(TypeDeterminerTest, Stmt_Call_undeclared) { + // fn main() -> void {func(); return; } + // fn func() -> void { return; } + ast::type::F32Type f32; + ast::ExpressionList call_params; + auto call_expr = std::make_unique( + Source{12, 34}, std::make_unique("func"), + std::move(call_params)); + ast::VariableList params0; + auto func_main = + std::make_unique("main", std::move(params0), &f32); + auto main_body = std::make_unique(); + main_body->append(std::make_unique(std::move(call_expr))); + main_body->append(std::make_unique()); + func_main->set_body(std::move(main_body)); + mod()->AddFunction(std::move(func_main)); + + auto func = std::make_unique("func", std::move(params0), &f32); + auto body = std::make_unique(); + body->append(std::make_unique()); + func->set_body(std::move(body)); + mod()->AddFunction(std::move(func)); + + EXPECT_FALSE(td()->Determine()) << td()->error(); + EXPECT_EQ(td()->error(), + "12:34: v-0005: function must be declared before use: 'func'"); +} + TEST_F(TypeDeterminerTest, Stmt_VariableDecl) { ast::type::I32Type i32; auto var = diff --git a/test/function-must-be-declared-before-use-fail.wgsl b/test/function-must-be-declared-before-use-fail.wgsl new file mode 100644 index 0000000000..91cfaef041 --- /dev/null +++ b/test/function-must-be-declared-before-use-fail.wgsl @@ -0,0 +1,23 @@ +# Copyright 2020 The Tint Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +fn main() -> void { + var a:f32 = func1(); + return; +} + +fn func1() -> f32 { + var a:i32 = 2.1; + return a; +}