From cb0309ed6c05db4636ca1c51ded1a65209c9b104 Mon Sep 17 00:00:00 2001 From: Sarah Date: Mon, 28 Jun 2021 20:44:31 +0000 Subject: [PATCH] validation: fix error msg for assign to 'let' and 'function parameter' Bug: tint:903 Change-Id: Ie2220d533f2227be717f61fa702514e2aeb052c8 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/56300 Reviewed-by: Antonio Maiorano Kokoro: Kokoro --- src/resolver/assignment_validation_test.cc | 3 ++- src/resolver/function_validation_test.cc | 4 +++- src/resolver/resolver.cc | 21 ++++++++++++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/resolver/assignment_validation_test.cc b/src/resolver/assignment_validation_test.cc index c3109fe97d..0e0ad423e1 100644 --- a/src/resolver/assignment_validation_test.cc +++ b/src/resolver/assignment_validation_test.cc @@ -173,7 +173,8 @@ TEST_F(ResolverAssignmentValidationTest, AssignToConstant_Fail) { WrapInFunction(var, Assign(Expr(Source{{12, 34}}, "a"), 2)); EXPECT_FALSE(r()->Resolve()); - EXPECT_EQ(r()->error(), "12:34 error: cannot assign to value of type 'i32'"); + EXPECT_EQ(r()->error(), + "12:34 error: cannot assign to const\nnote: 'a' is declared here:"); } TEST_F(ResolverAssignmentValidationTest, AssignNonStorable_Fail) { diff --git a/src/resolver/function_validation_test.cc b/src/resolver/function_validation_test.cc index 0771eeb8a3..6ec8c1f847 100644 --- a/src/resolver/function_validation_test.cc +++ b/src/resolver/function_validation_test.cc @@ -354,7 +354,9 @@ TEST_F(ResolverFunctionValidationTest, FunctionParamsConst) { {Assign(Expr(Source{{12, 34}}, "arg"), Expr(1)), Return()}); EXPECT_FALSE(r()->Resolve()); - EXPECT_EQ(r()->error(), "12:34 error: cannot assign to value of type 'i32'"); + EXPECT_EQ(r()->error(), + "12:34 error: cannot assign to function parameter\nnote: 'arg' is " + "declared here:"); } TEST_F(ResolverFunctionValidationTest, WorkgroupSize_Literal_BadType) { diff --git a/src/resolver/resolver.cc b/src/resolver/resolver.cc index 9c302e42f9..65b34bd0a0 100644 --- a/src/resolver/resolver.cc +++ b/src/resolver/resolver.cc @@ -3670,12 +3670,31 @@ bool Resolver::ValidateAssignment(const ast::AssignmentStatement* a) { auto const* lhs_type = TypeOf(a->lhs()); auto const* rhs_type = TypeOf(a->rhs()); + if (auto* ident = a->lhs()->As()) { + VariableInfo* var; + if (variable_stack_.get(ident->symbol(), &var)) { + if (var->kind == VariableKind::kParameter) { + AddError("cannot assign to function parameter", a->lhs()->source()); + AddNote("'" + builder_->Symbols().NameFor(ident->symbol()) + + "' is declared here:", + var->declaration->source()); + return false; + } + if (var->declaration->is_const()) { + AddError("cannot assign to const", a->lhs()->source()); + AddNote("'" + builder_->Symbols().NameFor(ident->symbol()) + + "' is declared here:", + var->declaration->source()); + return false; + } + } + } + auto* lhs_ref = lhs_type->As(); if (!lhs_ref) { // LHS is not a reference, so it has no storage. AddError("cannot assign to value of type '" + TypeNameOf(a->lhs()) + "'", a->lhs()->source()); - return false; }