aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/types2/assignments.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2021-03-17 22:13:16 -0700
committerRobert Griesemer <gri@golang.org>2021-03-23 05:11:09 +0000
commit8f19394b62a5b1651cd66eefb1bfeacc278966b9 (patch)
treeaf9e7e1c97239a9283eedd30c49f437e4acb054d /src/cmd/compile/internal/types2/assignments.go
parent0265b6475f08f2c23a742132db87c357fcbfa458 (diff)
downloadgo-8f19394b62a5b1651cd66eefb1bfeacc278966b9.tar.gz
go-8f19394b62a5b1651cd66eefb1bfeacc278966b9.zip
cmd/compile/internal/types2: refactor untyped conversions
Based on https://golang.org/cl/284256 for go/types. Brings this code more in line with go/types. Adjusted various tests to match new error messages which generally are now better: for assignment errors, instead of a generic "cannot convert" we now say "cannot use" followed by a clearer reason as to why not. Major differences to go/types with respect to the changed files: - Some of the new code now returns error codes, but they are only used internally for now, and not reported with errors. - go/types does not "convert" untyped nil values to target types, but here we do. This is unchanged from how types2 handled this before this CL. Change-Id: If45336d7ee679ece100f6d9d9f291a6ea55004d8 Reviewed-on: https://go-review.googlesource.com/c/go/+/302757 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'src/cmd/compile/internal/types2/assignments.go')
-rw-r--r--src/cmd/compile/internal/types2/assignments.go28
1 files changed, 23 insertions, 5 deletions
diff --git a/src/cmd/compile/internal/types2/assignments.go b/src/cmd/compile/internal/types2/assignments.go
index 18ae0ddf62..272e2fee6e 100644
--- a/src/cmd/compile/internal/types2/assignments.go
+++ b/src/cmd/compile/internal/types2/assignments.go
@@ -33,8 +33,8 @@ func (check *Checker) assignment(x *operand, T Type, context string) {
// spec: "If an untyped constant is assigned to a variable of interface
// type or the blank identifier, the constant is first converted to type
// bool, rune, int, float64, complex128 or string respectively, depending
- // on whether the value is a boolean, rune, integer, floating-point, complex,
- // or string constant."
+ // on whether the value is a boolean, rune, integer, floating-point,
+ // complex, or string constant."
if x.isNil() {
if T == nil {
check.errorf(x, "use of untyped nil in %s", context)
@@ -44,10 +44,27 @@ func (check *Checker) assignment(x *operand, T Type, context string) {
} else if T == nil || IsInterface(T) {
target = Default(x.typ)
}
- check.convertUntyped(x, target)
- if x.mode == invalid {
+ newType, val, code := check.implicitTypeAndValue(x, target)
+ if code != 0 {
+ msg := check.sprintf("cannot use %s as %s value in %s", x, target, context)
+ switch code {
+ case _TruncatedFloat:
+ msg += " (truncated)"
+ case _NumericOverflow:
+ msg += " (overflows)"
+ }
+ check.error(x, msg)
+ x.mode = invalid
return
}
+ if val != nil {
+ x.val = val
+ check.updateExprVal(x.expr, val)
+ }
+ if newType != x.typ {
+ x.typ = newType
+ check.updateExprType(x.expr, newType, false)
+ }
}
// x.typ is typed
@@ -63,7 +80,8 @@ func (check *Checker) assignment(x *operand, T Type, context string) {
return
}
- if reason := ""; !x.assignableTo(check, T, &reason) {
+ reason := ""
+ if ok, _ := x.assignableTo(check, T, &reason); !ok {
if check.conf.CompilerErrorMessages {
check.errorf(x, "incompatible type: cannot use %s as %s value", x, T)
} else {