From 73a5c372410adb272b6a63484c9a9de5e93e986c Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Mon, 24 Aug 2020 10:43:43 -0400 Subject: [PATCH] go/types: add untyped test cases for AssignableTo API 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 TryBot-Result: Gobot Gobot Reviewed-by: Robert Griesemer --- src/go/types/api_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/go/types/api_test.go b/src/go/types/api_test.go index 798c09bbff..6c129cd01b 100644 --- a/src/go/types/api_test.go +++ b/src/go/types/api_test.go @@ -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) -- 2.48.1