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; }