The assignment type-checking code previously bounced around a lot
between the LHS and RHS sides of the assignment. But there's actually
a very simple, consistent pattern to how to type check assignments:
1. Check the RHS expression.
2. If the LHS expression is an identifier that was declared in this
statement and it doesn't have an explicit type, give it the RHS
expression's default type.
3. Check the LHS expression.
4. Try assigning the RHS expression to the LHS expression, adding
implicit conversions as needed.
This CL implements this algorithm, and refactors tcAssign and
tcAssignList to use a common implementation. It also fixes the error
messages to consistently say just "1 variable" or "1 value", rather
than occasionally "1 variables" or "1 values".
Fixes#43348.
Passes toolstash -cmp.
Change-Id: I749cb8d6ccbc7d22cd7cb0a381f58a39fc2696b5
Reviewed-on: https://go-review.googlesource.com/c/go/+/280112
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
The following files had merge conflicts and were merged manually:
src/cmd/compile/fmtmap_test.go
src/cmd/compile/internal/gc/noder.go
src/go/parser/error_test.go
test/assign.go
test/chan/perm.go
test/fixedbugs/issue22822.go
test/fixedbugs/issue4458.go
test/init.go
test/interface/explicit.go
test/map1.go
test/method2.go
The following files had manual changes to make tests pass:
test/run.go
test/used.go
src/cmd/compile/internal/types2/stdlib_test.go
Change-Id: Ia495aaaa80ce321ee4ec2a9105780fbe913dbd4c
Ending typecheck1 in the switch makes it safe for each case
to do an appropriate type assertion. The main change is dropping
the computation of "ok" and using the syntax nodes themselves
to decide what's OK.
Passes buildall w/ toolstash -cmp.
Change-Id: I2a1873a51e3f1194d74bb87a6653cb9857a02a1b
Reviewed-on: https://go-review.googlesource.com/c/go/+/275444
Trust: Russ Cox <rsc@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>