From 3a472bc5c2428cd4836f6b7a239adea9d78ef2a9 Mon Sep 17 00:00:00 2001 From: Sarah Date: Tue, 8 Jun 2021 14:19:01 +0000 Subject: [PATCH] [validation] v-2000: Add disabled test for module-scope variable and function name intersect v-2000: Function names must not have any intersect with module-scope variable names. A declaration must not introduce a name when that identifier is already in scope with the same end scope as another instance of that name. A module-scope variable and a function have the same end scope ie. end of the program. bug: tint:260 Change-Id: I95321d3e6c04b0344c3acfb7b77cb483b3659728 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/52820 Kokoro: Kokoro Reviewed-by: Ben Clayton Reviewed-by: Sarah Mashayekhi Commit-Queue: Sarah Mashayekhi --- src/resolver/function_validation_test.cc | 73 ++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/resolver/function_validation_test.cc b/src/resolver/function_validation_test.cc index a542b9d13b..58ea6fba27 100644 --- a/src/resolver/function_validation_test.cc +++ b/src/resolver/function_validation_test.cc @@ -59,6 +59,79 @@ TEST_F(ResolverFunctionValidationTest, EXPECT_TRUE(r()->Resolve()) << r()->error(); } +TEST_F(ResolverFunctionValidationTest, + DISABLED_FunctionNameSameAsGlobalVariableName_Fail) { + // var foo:f32 = 3.14; + // fn foo() -> void {} + + auto* global_var = Var(Source{Source::Location{56, 78}}, "foo", ty.f32(), + ast::StorageClass::kPrivate, Expr(3.14f)); + AST().AddGlobalVariable(global_var); + + Func(Source{Source::Location{12, 34}}, "foo", ast::VariableList{}, ty.void_(), + ast::StatementList{}, ast::DecorationList{}); + + EXPECT_FALSE(r()->Resolve()) << r()->error(); + EXPECT_EQ(r()->error(), + "12:34 error v-2000: duplicate declaration 'foo'\n56:78 note: " + "'foo' first declared here:"); +} + +TEST_F(ResolverFunctionValidationTest, + DISABLED_GlobalVariableNameSameAFunctionName_Fail) { + // fn foo() -> void {} + // var foo:f32 = 3.14; + + Func(Source{Source::Location{12, 34}}, "foo", ast::VariableList{}, ty.void_(), + ast::StatementList{}, ast::DecorationList{}); + auto* global_var = + Var("foo", ty.f32(), ast::StorageClass::kPrivate, Expr(3.14f)); + AST().AddGlobalVariable(global_var); + + EXPECT_FALSE(r()->Resolve()) << r()->error(); + EXPECT_EQ(r()->error(), + "error v-2000: duplicate declaration 'foo'\n12:34 note: 'foo' " + "first declared here:"); +} + +TEST_F(ResolverFunctionValidationTest, FunctionUsingSameVariableName_Pass) { + // fn func() -> i32 { + // var func:i32 = 0; + // return func; + // } + + auto* var = Var("func", ty.i32(), ast::StorageClass::kNone, Expr(0)); + Func("func", ast::VariableList{}, ty.i32(), + ast::StatementList{ + Decl(var), + Return(Source{Source::Location{12, 34}}, Expr("func")), + }, + ast::DecorationList{}); + + EXPECT_TRUE(r()->Resolve()) << r()->error(); +} + +TEST_F(ResolverFunctionValidationTest, + FunctionNameSameAsFunctionScopeVariableName_Pass) { + // fn a() -> void { var b:i32 = 0; } + // fn b() -> i32 { return 2; } + + auto* var = Var("b", ty.i32(), ast::StorageClass::kNone, Expr(0)); + Func("a", ast::VariableList{}, ty.void_(), + ast::StatementList{ + Decl(var), + }, + ast::DecorationList{}); + + Func(Source{Source::Location{12, 34}}, "b", ast::VariableList{}, ty.i32(), + ast::StatementList{ + Return(2), + }, + ast::DecorationList{}); + + EXPECT_TRUE(r()->Resolve()) << r()->error(); +} + TEST_F(ResolverFunctionValidationTest, FunctionEndWithoutReturnStatement_Fail) { // fn func -> int { var a:i32 = 2; }