From 6a5f35047f8c69301bc05ee4359bb20bd71dcad9 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 18 Apr 2023 10:20:15 -0700 Subject: [PATCH] go/types, types2: consistently set x.mode in Checker.assignment Per the doc string, Checker.assignment must set x.mode to invalid in case of failure. (It may be simpler to return a bool, but the operand x may be tested by callers several stack frames above.) Change-Id: Ia1789b0396e8338103c0e707761c46f8d253fd31 Reviewed-on: https://go-review.googlesource.com/c/go/+/485875 Reviewed-by: Robert Findley Run-TryBot: Robert Griesemer Auto-Submit: Robert Griesemer TryBot-Result: Gopher Robot Reviewed-by: Robert Griesemer --- src/cmd/compile/internal/types2/assignments.go | 3 +++ src/go/types/assignments.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/cmd/compile/internal/types2/assignments.go b/src/cmd/compile/internal/types2/assignments.go index 02823fed2c6..30ed9ae701c 100644 --- a/src/cmd/compile/internal/types2/assignments.go +++ b/src/cmd/compile/internal/types2/assignments.go @@ -30,6 +30,7 @@ func (check *Checker) assignment(x *operand, T Type, context string) { // we may get here because of other problems (go.dev/issue/39634, crash 12) // TODO(gri) do we need a new "generic" error code here? check.errorf(x, IncompatibleAssign, "cannot assign %s to %s in %s", x, T, context) + x.mode = invalid return } @@ -78,6 +79,8 @@ func (check *Checker) assignment(x *operand, T Type, context string) { // A generic (non-instantiated) function value cannot be assigned to a variable. if sig, _ := under(x.typ).(*Signature); sig != nil && sig.TypeParams().Len() > 0 { check.errorf(x, WrongTypeArgCount, "cannot use generic function %s without instantiation in %s", x, context) + x.mode = invalid + return } // spec: "If a left-hand side is the blank identifier, any typed or diff --git a/src/go/types/assignments.go b/src/go/types/assignments.go index 68b07a71722..71fbbea46ff 100644 --- a/src/go/types/assignments.go +++ b/src/go/types/assignments.go @@ -30,6 +30,7 @@ func (check *Checker) assignment(x *operand, T Type, context string) { // we may get here because of other problems (go.dev/issue/39634, crash 12) // TODO(gri) do we need a new "generic" error code here? check.errorf(x, IncompatibleAssign, "cannot assign %s to %s in %s", x, T, context) + x.mode = invalid return } @@ -76,6 +77,8 @@ func (check *Checker) assignment(x *operand, T Type, context string) { // A generic (non-instantiated) function value cannot be assigned to a variable. if sig, _ := under(x.typ).(*Signature); sig != nil && sig.TypeParams().Len() > 0 { check.errorf(x, WrongTypeArgCount, "cannot use generic function %s without instantiation in %s", x, context) + x.mode = invalid + return } // spec: "If a left-hand side is the blank identifier, any typed or