From: Rob Findley Date: Sun, 12 Jul 2020 20:57:01 +0000 (-0400) Subject: go/types: better error when converting untyped values in assignments X-Git-Tag: go1.16beta1~1189 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=346efc28508dc358cba7e246adeb40bde99cfb2a;p=gostls13.git go/types: better error when converting untyped values in assignments The error returned by convertUntyped is 'cannot convert _ to _', which can be misleading in contexts where an explicit conversion would be allowed. Arguably the error message from convertUntyped should just be 'cannot use _ as _', as 'convert' has an explicit meaning within the spec. Making that change caused a large number of test failures, so for now we just fix this for assignments by interpreting the error. For #22070 Change-Id: I4eed6f39d1a991e8df7e035ec301d28a05150eb5 Reviewed-on: https://go-review.googlesource.com/c/go/+/242083 Run-TryBot: Robert Findley TryBot-Result: Gobot Gobot Reviewed-by: Robert Griesemer --- diff --git a/src/go/types/assignments.go b/src/go/types/assignments.go index 34a9d7843d..9697e504cd 100644 --- a/src/go/types/assignments.go +++ b/src/go/types/assignments.go @@ -7,6 +7,7 @@ package types import ( + "errors" "go/ast" "go/token" ) @@ -43,8 +44,16 @@ func (check *Checker) assignment(x *operand, T Type, context string) { } target = Default(x.typ) } - check.convertUntyped(x, target) - if x.mode == invalid { + if err := check.canConvertUntyped(x, target); err != nil { + var internalErr Error + var msg string + if errors.As(err, &internalErr) { + msg = internalErr.Msg + } else { + msg = err.Error() + } + check.errorf(x.pos(), "cannot use %s as %s value in %s: %v", x, target, context, msg) + x.mode = invalid return } }