diff options
author | Rob Findley <rfindley@google.com> | 2020-07-12 22:36:34 -0400 |
---|---|---|
committer | Robert Findley <rfindley@google.com> | 2020-08-28 16:45:21 +0000 |
commit | 42e09dc1ba1e820af44b2cbd4db0d60abb5559a2 (patch) | |
tree | 14142f321ddb8ad835fe457bf1eb5fbbd7dfc329 /src/go/types/api_test.go | |
parent | ae7b6a3b779c4d6de96f59efbfed0b899c3ff6df (diff) | |
download | go-42e09dc1ba1e820af44b2cbd4db0d60abb5559a2.tar.gz go-42e09dc1ba1e820af44b2cbd4db0d60abb5559a2.zip |
go/types: factor out usage of implicit type
There was some duplication of logic interpreting the implicit type of
an operand in assignableTo and convertUntyped. Factor out this logic to
a new 'implicitType' function, which returns the implicit type of an
untyped operand when used in a context where a target type is expected.
I believe this resolves some comments about code duplication. There is
other similar code in assignable, assignableTo, and convertUntypes, but
I found it to to be sufficiently semantically distinct to not warrant
factoring out.
Change-Id: I199298a2e58fcf05344318fca0226b460c57867d
Reviewed-on: https://go-review.googlesource.com/c/go/+/242084
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/go/types/api_test.go')
-rw-r--r-- | src/go/types/api_test.go | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/src/go/types/api_test.go b/src/go/types/api_test.go index 6c129cd01b..75cebc9826 100644 --- a/src/go/types/api_test.go +++ b/src/go/types/api_test.go @@ -1243,11 +1243,9 @@ 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}, + // Untyped string values are not permitted by the spec, so the below + // behavior is undefined. + {Typ[UntypedString], Typ[String], true}, } { 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) @@ -1266,13 +1264,11 @@ func TestAssignableTo(t *testing.T) { {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}, + // Neither untyped string nor untyped numeric assignments arise during + // normal type checking, so the below behavior is technically undefined by + // the spec. + {Typ[UntypedString], Typ[String], true}, + {Typ[UntypedInt], Typ[Int], true}, } { 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) |