diff options
author | Robert Griesemer <gri@golang.org> | 2022-01-31 21:25:14 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2022-02-04 23:42:25 +0000 |
commit | e052044d6b25a76603cafbfb1099cf4196528556 (patch) | |
tree | f66a228ec75c6821ce3b68de5d59556fa2147efe /src/cmd/compile/internal/types2/predicates.go | |
parent | f9763a648bbe4468118b95e147bc5e81268d0341 (diff) | |
download | go-e052044d6b25a76603cafbfb1099cf4196528556.tar.gz go-e052044d6b25a76603cafbfb1099cf4196528556.zip |
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>
Diffstat (limited to 'src/cmd/compile/internal/types2/predicates.go')
-rw-r--r-- | src/cmd/compile/internal/types2/predicates.go | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/cmd/compile/internal/types2/predicates.go b/src/cmd/compile/internal/types2/predicates.go index 003e58db38..279d0775bd 100644 --- a/src/cmd/compile/internal/types2/predicates.go +++ b/src/cmd/compile/internal/types2/predicates.go @@ -102,10 +102,11 @@ func isGeneric(t Type) bool { // Comparable reports whether values of type T are comparable. func Comparable(T Type) bool { - return comparable(T, nil) + return comparable(T, nil, nil) } -func comparable(T Type, seen map[Type]bool) bool { +// If reportf != nil, it may be used to report why T is not comparable. +func comparable(T Type, seen map[Type]bool, reportf func(string, ...interface{})) bool { if seen[T] { return true } @@ -123,13 +124,22 @@ func comparable(T Type, seen map[Type]bool) bool { return true case *Struct: for _, f := range t.fields { - if !comparable(f.typ, seen) { + if !comparable(f.typ, seen, nil) { + if reportf != nil { + reportf("struct containing %s cannot be compared", f.typ) + } return false } } return true case *Array: - return comparable(t.elem, seen) + if !comparable(t.elem, seen, nil) { + if reportf != nil { + reportf("%s cannot be compared", t) + } + return false + } + return true case *Interface: return !isTypeParam(T) || t.typeSet().IsComparable(seen) } |