]> Cypherpunks.ru repositories - gostls13.git/commit
go/types, types2: better error messages for comparisons
authorRobert Griesemer <gri@golang.org>
Tue, 1 Feb 2022 05:25:14 +0000 (21:25 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 4 Feb 2022 23:42:25 +0000 (23:42 +0000)
commite052044d6b25a76603cafbfb1099cf4196528556
treef66a228ec75c6821ce3b68de5d59556fa2147efe
parentf9763a648bbe4468118b95e147bc5e81268d0341
go/types, types2: better error messages for comparisons

Refactor Checker.comparison such that its logic is easier to reason
about and so that special cases can be handled more directly.

Use the appropriate operand (of 1st or 2nd operand) for error
reporting (position and type), rather than always using the
first operand.

Use an extra parameter to indicate a switch case
comparison; in this case the error is always reported at
the position of the first operand. (The error messages are
not yet adjusted for switches; see next CL.)

Introduce a new kindString function which is used to print simplified
types in error messages (related to comparisons only): instead of
printing the details of a struct type, we just print "struct" where
the details are not relevant. This matches the 1.17 compiler behavior.

Added a "reportf" parameter to the internal comparable function so we
can report an error cause in addition to the boolean result. Rather
than passing a *string for cause, we pass a function to record the
cause so that we can use the *Checker context for printing (needed
for proper type qualification). This mechanism reports the same
details now as the 1.17 compiler.

Adjusted various tests as needed added new test files.

Fixes #50918.

Change-Id: I1f0e7af22f09db4d31679c667c71a9038a8dc9d2
Reviewed-on: https://go-review.googlesource.com/c/go/+/381964
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
17 files changed:
src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/predicates.go
src/cmd/compile/internal/types2/stmt.go
src/cmd/compile/internal/types2/testdata/check/expr2.src
src/cmd/compile/internal/types2/testdata/fixedbugs/issue48712.go2
src/cmd/compile/internal/types2/testdata/fixedbugs/issue50918.go [new file with mode: 0644]
src/cmd/compile/internal/types2/testdata/spec/comparisons.go2 [new file with mode: 0644]
src/cmd/compile/internal/types2/typeset.go
src/go/types/expr.go
src/go/types/predicates.go
src/go/types/stmt.go
src/go/types/testdata/check/expr2.src
src/go/types/testdata/fixedbugs/issue48712.go2
src/go/types/testdata/fixedbugs/issue50918.go [new file with mode: 0644]
src/go/types/testdata/spec/comparisons.go2 [new file with mode: 0644]
src/go/types/typeset.go
test/fixedbugs/issue11737.go