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 <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Sarah 2021-06-28 20:44:31 +00:00 committed by Sarah Mashayekhi
parent e37044a6cc
commit cb0309ed6c
3 changed files with 25 additions and 3 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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<ast::IdentifierExpression>()) {
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<sem::Reference>();
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;
}