]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: add untyped test cases for AssignableTo API
authorRob Findley <rfindley@google.com>
Mon, 24 Aug 2020 14:43:43 +0000 (10:43 -0400)
committerRobert Findley <rfindley@google.com>
Thu, 27 Aug 2020 16:05:57 +0000 (16:05 +0000)
The AssignableTo API is specifically for non-constant values, but is
currently called by gopls for constant completions. Add a test to ensure
that we handle this edge case correctly.

Change-Id: I83115cbca2443a783df1c3090b5741260dffb78e
Reviewed-on: https://go-review.googlesource.com/c/go/+/250258
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/types/api_test.go

index 798c09bbffd1dece1a7330f153850559b8643846..6c129cd01b6c7fd7e53d179620420150926be051 100644 (file)
@@ -1243,6 +1243,11 @@ func TestConvertibleTo(t *testing.T) {
                {newDefined(new(Struct)), new(Struct), true},
                {newDefined(Typ[Int]), new(Struct), false},
                {Typ[UntypedInt], Typ[Int], true},
+               // TODO (rFindley): the below behavior is undefined as non-constant untyped
+               // string values are not permitted by the spec. But we should consider
+               // changing this case to return 'true', to have more reasonable behavior in
+               // cases where the API is used for constant expressions.
+               {Typ[UntypedString], Typ[String], false},
        } {
                if got := ConvertibleTo(test.v, test.t); got != test.want {
                        t.Errorf("ConvertibleTo(%v, %v) = %t, want %t", test.v, test.t, got, test.want)
@@ -1260,6 +1265,14 @@ func TestAssignableTo(t *testing.T) {
                {newDefined(Typ[Int]), Typ[Int], false},
                {newDefined(new(Struct)), new(Struct), true},
                {Typ[UntypedBool], Typ[Bool], true},
+               {Typ[UntypedString], Typ[Bool], false},
+               // TODO (rFindley): the below behavior is undefined as AssignableTo is
+               // intended for non-constant values (and neither UntypedString or
+               // UntypedInt assignments arise during normal type checking).  But as
+               // described in TestConvertibleTo above, we should consider changing this
+               // behavior.
+               {Typ[UntypedString], Typ[String], false},
+               {Typ[UntypedInt], Typ[Int], false},
        } {
                if got := AssignableTo(test.v, test.t); got != test.want {
                        t.Errorf("AssignableTo(%v, %v) = %t, want %t", test.v, test.t, got, test.want)