aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobert Findley <rfindley@google.com>2021-11-02 18:42:51 -0400
committerRobert Findley <rfindley@google.com>2021-11-03 00:07:38 +0000
commit49a00631b118834010e4d0124ccc04eeaf2cd7ac (patch)
tree12d7a079769c85eeb29485ebd9473655f635d68f /src
parent2b81b863a258decaaca26ab4c01bfd070cc01ebe (diff)
downloadgo-49a00631b118834010e4d0124ccc04eeaf2cd7ac.tar.xz
go/types: report cause for failing const conversions
This is a port of CL 360795 to go/types. Error messages were adjusted accordingly, with a TODO to fix the discrepancy. Change-Id: Ifd7d8248fa11a31fde391021f3c5f1840877892f Reviewed-on: https://go-review.googlesource.com/c/go/+/360937 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/go/types/conversions.go11
-rw-r--r--src/go/types/testdata/spec/conversions.go214
2 files changed, 23 insertions, 2 deletions
diff --git a/src/go/types/conversions.go b/src/go/types/conversions.go
index 9baad98e09..a6f0714ba0 100644
--- a/src/go/types/conversions.go
+++ b/src/go/types/conversions.go
@@ -49,8 +49,15 @@ func (check *Checker) conversion(x *operand, T Type) {
// converted.
ok = under(T).(*TypeParam).underIs(func(u Type) bool {
// t is nil if there are no specific type terms
- // TODO(gri) add a cause in case of failure
- return u != nil && constConvertibleTo(u, nil)
+ if u == nil {
+ cause = check.sprintf("%s does not contain specific types", T)
+ return false
+ }
+ if !constConvertibleTo(u, nil) {
+ cause = check.sprintf("cannot convert %s to %s (in %s)", x, u, T)
+ return false
+ }
+ return true
})
x.mode = value // type parameters are not constants
case x.convertibleTo(check, T, &cause):
diff --git a/src/go/types/testdata/spec/conversions.go2 b/src/go/types/testdata/spec/conversions.go2
index 47b1f07d87..e54403cea9 100644
--- a/src/go/types/testdata/spec/conversions.go2
+++ b/src/go/types/testdata/spec/conversions.go2
@@ -27,6 +27,20 @@ func _[T ~string]() {
var _ T = 0 // ERROR cannot use .* as T value
}
+// failing const conversions of constants to type parameters report a cause
+func _[
+ T1 any,
+ T2 interface{ m() },
+ T3 ~int | ~float64 | ~bool,
+ T4 ~int | ~string,
+]() {
+ // TODO(rfindley): align the error formatting here with types2.
+ _ = T1(0 /* ERROR cannot convert 0 .* to T1.*T1 does not contain specific types */ )
+ _ = T2(1 /* ERROR cannot convert 1 .* to T2.*T2 does not contain specific types */ )
+ _ = T3(2 /* ERROR cannot convert 2 .* to T3.*cannot convert 2 .* to bool \(in T3\) */ )
+ _ = T4(3.14 /* ERROR cannot convert 3.14 .* to T4.*cannot convert 3.14 .* to int \(in T4\) */ )
+}
+
// "x is assignable to T"
// - tested via assignability tests