1
0
mirror of https://github.com/golang/go synced 2024-11-18 00:54:45 -07:00

[dev.typeparams] go/types: refactor untyped conversion for typeparams

Some logic was missing in the merge from dev.go2go to deal with untyped
conversion of generic types. Part of this was due to the complexity of
the merge, as untyped conversion had been refactored on master.

Rather than back out the refactoring of untyped conversion, in this CL I
have decided to take it one step further. It was always problematic that
isRepresentable and canConvertUntyped mutated their arguments. In
retrospect the refactoring was perhaps too conservative.

This CL performs the following refactoring:
 + Replace 'isRepresentable' with 'representation': a Checker method
   produces the rounded representation of an untyped constant operand as
   a target type.
 + Make some functions return error codes rather than errors, and factor
   out the construction of the error message for invalid conversion.
   This avoided some indirect code.
 + Replace implicitType with implicitTypeAndValue, and have it handle
   the case of a constant basic operand, returning the rounded value.
 + Eliminate canConvertUntyped, lifting the logic to update expr types
   and values to the two callers.
 + Add handling for Sum types in implicitTypeAndValue. Here, the
   decision was made to depart from dev.go2go (and types2), and produce
   a Sum type as output. This seemed most correct on first principles,
   and tests still passed (though some logic for recording types had to
   be updated to allow for Sum types).

Change-Id: Ic93901f69e6671b83b14ee2bf185a4ed767e31ee
Reviewed-on: https://go-review.googlesource.com/c/go/+/284256
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: Robert Griesemer <gri@golang.org>
Trust: Robert Findley <rfindley@google.com>
This commit is contained in:
Rob Findley 2021-01-15 11:42:10 -05:00 committed by Robert Findley
parent d8796b5670
commit 734cb8be0a
4 changed files with 97 additions and 91 deletions

View File

@ -7,7 +7,6 @@
package types package types
import ( import (
"errors"
"go/ast" "go/ast"
"go/token" "go/token"
) )
@ -46,27 +45,30 @@ func (check *Checker) assignment(x *operand, T Type, context string) {
} }
target = Default(x.typ) target = Default(x.typ)
} }
if err := check.canConvertUntyped(x, target); err != nil { newType, val, code := check.implicitTypeAndValue(x, target)
if code != 0 {
msg := check.sprintf("cannot use %s as %s value in %s", x, target, context) msg := check.sprintf("cannot use %s as %s value in %s", x, target, context)
code := _IncompatibleAssign switch code {
var ierr Error case _TruncatedFloat:
if errors.As(err, &ierr) { msg += " (truncated)"
// Preserve these inner errors, as they are informative. case _NumericOverflow:
switch ierr.go116code { msg += " (overflows)"
case _TruncatedFloat: default:
msg += " (truncated)" code = _IncompatibleAssign
code = ierr.go116code
case _NumericOverflow:
msg += " (overflows)"
code = ierr.go116code
}
} }
check.error(x, code, msg) check.error(x, code, msg)
x.mode = invalid x.mode = invalid
return 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
// A generic (non-instantiated) function value cannot be assigned to a variable. // A generic (non-instantiated) function value cannot be assigned to a variable.
if sig := asSignature(x.typ); sig != nil && len(sig.tparams) > 0 { if sig := asSignature(x.typ); sig != nil && len(sig.tparams) > 0 {

View File

@ -338,17 +338,18 @@ func representableConst(x constant.Value, check *Checker, typ *Basic, rounded *c
// representable checks that a constant operand is representable in the given // representable checks that a constant operand is representable in the given
// basic type. // basic type.
func (check *Checker) representable(x *operand, typ *Basic) { func (check *Checker) representable(x *operand, typ *Basic) {
if err := check.isRepresentable(x, typ); err != nil { if v, code := check.representation(x, typ); code != 0 {
check.invalidConversion(code, x, typ)
x.mode = invalid x.mode = invalid
check.err(err) } else if v != nil {
x.val = v
} }
} }
func (check *Checker) isRepresentable(x *operand, typ *Basic) error { func (check *Checker) representation(x *operand, typ *Basic) (constant.Value, errorCode) {
assert(x.mode == constant_) assert(x.mode == constant_)
if !representableConst(x.val, check, typ, &x.val) { v := x.val
var msg string if !representableConst(x.val, check, typ, &v) {
var code errorCode
if isNumeric(x.typ) && isNumeric(typ) { if isNumeric(x.typ) && isNumeric(typ) {
// numeric conversion : error msg // numeric conversion : error msg
// //
@ -358,19 +359,25 @@ func (check *Checker) isRepresentable(x *operand, typ *Basic) error {
// float -> float : overflows // float -> float : overflows
// //
if !isInteger(x.typ) && isInteger(typ) { if !isInteger(x.typ) && isInteger(typ) {
msg = "%s truncated to %s" return nil, _TruncatedFloat
code = _TruncatedFloat
} else { } else {
msg = "%s overflows %s" return nil, _NumericOverflow
code = _NumericOverflow
} }
} else {
msg = "cannot convert %s to %s"
code = _InvalidConstVal
} }
return check.newErrorf(x, code, false, msg, x, typ) return nil, _InvalidConstVal
} }
return nil return v, 0
}
func (check *Checker) invalidConversion(code errorCode, x *operand, target Type) {
msg := "cannot convert %s to %s"
switch code {
case _TruncatedFloat:
msg = "%s truncated to %s"
case _NumericOverflow:
msg = "%s overflows %s"
}
check.errorf(x, code, msg, x, target)
} }
// updateExprType updates the type of x to typ and invokes itself // updateExprType updates the type of x to typ and invokes itself
@ -506,16 +513,29 @@ func (check *Checker) updateExprVal(x ast.Expr, val constant.Value) {
// convertUntyped attempts to set the type of an untyped value to the target type. // convertUntyped attempts to set the type of an untyped value to the target type.
func (check *Checker) convertUntyped(x *operand, target Type) { func (check *Checker) convertUntyped(x *operand, target Type) {
if err := check.canConvertUntyped(x, target); err != nil { newType, val, code := check.implicitTypeAndValue(x, target)
if code != 0 {
check.invalidConversion(code, x, target.Underlying())
x.mode = invalid x.mode = invalid
check.err(err) 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)
} }
} }
func (check *Checker) canConvertUntyped(x *operand, target Type) error { // implicitTypeAndValue returns the implicit type of x when used in a context
// where the target type is expected. If no such implicit conversion is
// possible, it returns a nil Type.
func (check *Checker) implicitTypeAndValue(x *operand, target Type) (Type, constant.Value, errorCode) {
target = expand(target) target = expand(target)
if x.mode == invalid || isTyped(x.typ) || target == Typ[Invalid] { if x.mode == invalid || isTyped(x.typ) || target == Typ[Invalid] {
return nil return x.typ, nil, 0
} }
if isUntyped(target) { if isUntyped(target) {
@ -524,43 +544,23 @@ func (check *Checker) canConvertUntyped(x *operand, target Type) error {
tkind := target.(*Basic).kind tkind := target.(*Basic).kind
if isNumeric(x.typ) && isNumeric(target) { if isNumeric(x.typ) && isNumeric(target) {
if xkind < tkind { if xkind < tkind {
x.typ = target return target, nil, 0
check.updateExprType(x.expr, target, false)
} }
} else if xkind != tkind { } else if xkind != tkind {
return check.newErrorf(x, _InvalidUntypedConversion, false, "cannot convert %s to %s", x, target) return nil, nil, _InvalidUntypedConversion
} }
return nil return x.typ, nil, 0
} }
if t, ok := target.Underlying().(*Basic); ok && x.mode == constant_ { switch t := optype(target).(type) {
if err := check.isRepresentable(x, t); err != nil {
return err
}
// Expression value may have been rounded - update if needed.
check.updateExprVal(x.expr, x.val)
} else {
newTarget := check.implicitType(x, target)
if newTarget == nil {
return check.newErrorf(x, _InvalidUntypedConversion, false, "cannot convert %s to %s", x, target)
}
target = newTarget
}
x.typ = target
// Even though implicitType can return UntypedNil, this value is final: the
// predeclared identifier nil has no type.
check.updateExprType(x.expr, target, true)
return nil
}
// implicitType returns the implicit type of x when used in a context where the
// target type is expected. If no such implicit conversion is possible, it
// returns nil.
func (check *Checker) implicitType(x *operand, target Type) Type {
assert(isUntyped(x.typ))
switch t := target.Underlying().(type) {
case *Basic: case *Basic:
assert(x.mode != constant_) if x.mode == constant_ {
v, code := check.representation(x, t)
if code != 0 {
return nil, nil, code
}
return target, v, code
}
// Non-constant untyped values may appear as the // Non-constant untyped values may appear as the
// result of comparisons (untyped bool), intermediate // result of comparisons (untyped bool), intermediate
// (delayed-checked) rhs operands of shifts, and as // (delayed-checked) rhs operands of shifts, and as
@ -568,26 +568,39 @@ func (check *Checker) implicitType(x *operand, target Type) Type {
switch x.typ.(*Basic).kind { switch x.typ.(*Basic).kind {
case UntypedBool: case UntypedBool:
if !isBoolean(target) { if !isBoolean(target) {
return nil return nil, nil, _InvalidUntypedConversion
} }
case UntypedInt, UntypedRune, UntypedFloat, UntypedComplex: case UntypedInt, UntypedRune, UntypedFloat, UntypedComplex:
if !isNumeric(target) { if !isNumeric(target) {
return nil return nil, nil, _InvalidUntypedConversion
} }
case UntypedString: case UntypedString:
// Non-constant untyped string values are not permitted by the spec and // Non-constant untyped string values are not permitted by the spec and
// should not occur during normal typechecking passes, but this path is // should not occur during normal typechecking passes, but this path is
// reachable via the AssignableTo API. // reachable via the AssignableTo API.
if !isString(target) { if !isString(target) {
return nil return nil, nil, _InvalidUntypedConversion
} }
case UntypedNil: case UntypedNil:
// Unsafe.Pointer is a basic type that includes nil. // Unsafe.Pointer is a basic type that includes nil.
if !hasNil(target) { if !hasNil(target) {
return nil return nil, nil, _InvalidUntypedConversion
} }
// TODO(rFindley) return UntypedNil here (golang.org/issues/13061).
default: default:
return nil return nil, nil, _InvalidUntypedConversion
}
case *Sum:
ok := t.is(func(t Type) bool {
target, _, _ := check.implicitTypeAndValue(x, t)
return target != nil
})
if !ok {
return nil, nil, _InvalidUntypedConversion
}
// keep nil untyped (was bug #39755)
if x.isNil() {
return Typ[UntypedNil], nil, 0
} }
case *Interface: case *Interface:
// Values must have concrete dynamic types. If the value is nil, // Values must have concrete dynamic types. If the value is nil,
@ -595,24 +608,24 @@ func (check *Checker) implicitType(x *operand, target Type) Type {
// need the dynamic type for argument checking of say, print // need the dynamic type for argument checking of say, print
// functions) // functions)
if x.isNil() { if x.isNil() {
return Typ[UntypedNil] return Typ[UntypedNil], nil, 0
} }
// cannot assign untyped values to non-empty interfaces // cannot assign untyped values to non-empty interfaces
check.completeInterface(token.NoPos, t) check.completeInterface(token.NoPos, t)
if !t.Empty() { if !t.Empty() {
return nil return nil, nil, _InvalidUntypedConversion
} }
return Default(x.typ) return Default(x.typ), nil, 0
case *Pointer, *Signature, *Slice, *Map, *Chan: case *Pointer, *Signature, *Slice, *Map, *Chan:
if !x.isNil() { if !x.isNil() {
return nil return nil, nil, _InvalidUntypedConversion
} }
// Keep nil untyped - see comment for interfaces, above. // Keep nil untyped - see comment for interfaces, above.
return Typ[UntypedNil] return Typ[UntypedNil], nil, 0
default: default:
return nil return nil, nil, _InvalidUntypedConversion
} }
return target return target, nil, 0
} }
func (check *Checker) comparison(x, y *operand, op token.Token) { func (check *Checker) comparison(x, y *operand, op token.Token) {

View File

@ -242,20 +242,15 @@ func (x *operand) assignableTo(check *Checker, T Type, reason *string) (bool, er
// x is an untyped value representable by a value of type T. // x is an untyped value representable by a value of type T.
if isUntyped(Vu) { if isUntyped(Vu) {
// TODO(rFindley) synchronize this block of code with types2 if t, ok := Tu.(*Sum); ok {
switch t := Tu.(type) {
case *Basic:
if x.mode == constant_ {
return representableConst(x.val, check, t, nil), _IncompatibleAssign
}
case *Sum:
return t.is(func(t Type) bool { return t.is(func(t Type) bool {
// TODO(gri) this could probably be more efficient // TODO(gri) this could probably be more efficient
ok, _ := x.assignableTo(check, t, reason) ok, _ := x.assignableTo(check, t, reason)
return ok return ok
}), _IncompatibleAssign }), _IncompatibleAssign
} }
return check.implicitType(x, Tu) != nil, _IncompatibleAssign newType, _, _ := check.implicitTypeAndValue(x, Tu)
return newType != nil, _IncompatibleAssign
} }
// Vu is typed // Vu is typed

View File

@ -74,12 +74,8 @@ func isUntyped(typ Type) bool {
return !isTyped(typ) return !isTyped(typ)
} }
func isOrdered(typ Type) bool { return is(typ, IsOrdered) } func isOrdered(typ Type) bool { return is(typ, IsOrdered) }
func isConstType(typ Type) bool { return is(typ, IsConstType) }
func isConstType(typ Type) bool {
t := asBasic(typ)
return t != nil && t.info&IsConstType != 0
}
// IsInterface reports whether typ is an interface type. // IsInterface reports whether typ is an interface type.
func IsInterface(typ Type) bool { func IsInterface(typ Type) bool {