case constArg && isConstType(T):
// constant conversion
ok = constConvertibleTo(T, &x.val)
+ // A conversion from an integer constant to an integer type
+ // can only fail if there's overflow. Give a concise error.
+ // (go.dev/issue/63563)
+ if !ok && isInteger(x.typ) && isInteger(T) {
+ check.errorf(x, InvalidConversion, "constant %s overflows %s", x.val, T)
+ x.mode = invalid
+ return
+ }
case constArg && isTypeParam(T):
// x is convertible to T if it is convertible
// to each specific type in the type set of T.
return true
}
if !constConvertibleTo(u, nil) {
- cause = check.sprintf("cannot convert %s to type %s (in %s)", x, u, T)
+ if isInteger(x.typ) && isInteger(u) {
+ // see comment above on constant conversion
+ cause = check.sprintf("constant %s overflows %s (in %s)", x.val, u, T)
+ } else {
+ cause = check.sprintf("cannot convert %s to type %s (in %s)", x, u, T)
+ }
return false
}
return true
case constArg && isConstType(T):
// constant conversion
ok = constConvertibleTo(T, &x.val)
+ // A conversion from an integer constant to an integer type
+ // can only fail if there's overflow. Give a concise error.
+ // (go.dev/issue/63563)
+ if !ok && isInteger(x.typ) && isInteger(T) {
+ check.errorf(x, InvalidConversion, "constant %s overflows %s", x.val, T)
+ x.mode = invalid
+ return
+ }
case constArg && isTypeParam(T):
// x is convertible to T if it is convertible
// to each specific type in the type set of T.
return true
}
if !constConvertibleTo(u, nil) {
- cause = check.sprintf("cannot convert %s to type %s (in %s)", x, u, T)
+ if isInteger(x.typ) && isInteger(u) {
+ // see comment above on constant conversion
+ cause = check.sprintf("constant %s overflows %s (in %s)", x.val, u, T)
+ } else {
+ cause = check.sprintf("cannot convert %s to type %s (in %s)", x, u, T)
+ }
return false
}
return true
_ int8 = maxInt8 /* ERROR "overflows" */ + 1
_ int8 = smallestFloat64 /* ERROR "truncated" */
- _ = int8(minInt8 /* ERROR "cannot convert" */ - 1)
+ _ = int8(minInt8 /* ERROR "overflows" */ - 1)
_ = int8(minInt8)
_ = int8(maxInt8)
- _ = int8(maxInt8 /* ERROR "cannot convert" */ + 1)
+ _ = int8(maxInt8 /* ERROR "overflows" */ + 1)
_ = int8(smallestFloat64 /* ERROR "cannot convert" */)
)
_ int16 = maxInt16 /* ERROR "overflows" */ + 1
_ int16 = smallestFloat64 /* ERROR "truncated" */
- _ = int16(minInt16 /* ERROR "cannot convert" */ - 1)
+ _ = int16(minInt16 /* ERROR "overflows" */ - 1)
_ = int16(minInt16)
_ = int16(maxInt16)
- _ = int16(maxInt16 /* ERROR "cannot convert" */ + 1)
+ _ = int16(maxInt16 /* ERROR "overflows" */ + 1)
_ = int16(smallestFloat64 /* ERROR "cannot convert" */)
)
_ int32 = maxInt32 /* ERROR "overflows" */ + 1
_ int32 = smallestFloat64 /* ERROR "truncated" */
- _ = int32(minInt32 /* ERROR "cannot convert" */ - 1)
+ _ = int32(minInt32 /* ERROR "overflows" */ - 1)
_ = int32(minInt32)
_ = int32(maxInt32)
- _ = int32(maxInt32 /* ERROR "cannot convert" */ + 1)
+ _ = int32(maxInt32 /* ERROR "overflows" */ + 1)
_ = int32(smallestFloat64 /* ERROR "cannot convert" */)
)
_ int64 = maxInt64 /* ERROR "overflows" */ + 1
_ int64 = smallestFloat64 /* ERROR "truncated" */
- _ = int64(minInt64 /* ERROR "cannot convert" */ - 1)
+ _ = int64(minInt64 /* ERROR "overflows" */ - 1)
_ = int64(minInt64)
_ = int64(maxInt64)
- _ = int64(maxInt64 /* ERROR "cannot convert" */ + 1)
+ _ = int64(maxInt64 /* ERROR "overflows" */ + 1)
_ = int64(smallestFloat64 /* ERROR "cannot convert" */)
)
_ int = maxInt /* ERROR "overflows" */ + 1
_ int = smallestFloat64 /* ERROR "truncated" */
- _ = int(minInt /* ERROR "cannot convert" */ - 1)
+ _ = int(minInt /* ERROR "overflows" */ - 1)
_ = int(minInt)
_ = int(maxInt)
- _ = int(maxInt /* ERROR "cannot convert" */ + 1)
+ _ = int(maxInt /* ERROR "overflows" */ + 1)
_ = int(smallestFloat64 /* ERROR "cannot convert" */)
)
_ uint8 = maxUint8 /* ERROR "overflows" */ + 1
_ uint8 = smallestFloat64 /* ERROR "truncated" */
- _ = uint8(0 /* ERROR "cannot convert" */ - 1)
+ _ = uint8(0 /* ERROR "overflows" */ - 1)
_ = uint8(0)
_ = uint8(maxUint8)
- _ = uint8(maxUint8 /* ERROR "cannot convert" */ + 1)
+ _ = uint8(maxUint8 /* ERROR "overflows" */ + 1)
_ = uint8(smallestFloat64 /* ERROR "cannot convert" */)
)
_ uint16 = maxUint16 /* ERROR "overflows" */ + 1
_ uint16 = smallestFloat64 /* ERROR "truncated" */
- _ = uint16(0 /* ERROR "cannot convert" */ - 1)
+ _ = uint16(0 /* ERROR "overflows" */ - 1)
_ = uint16(0)
_ = uint16(maxUint16)
- _ = uint16(maxUint16 /* ERROR "cannot convert" */ + 1)
+ _ = uint16(maxUint16 /* ERROR "overflows" */ + 1)
_ = uint16(smallestFloat64 /* ERROR "cannot convert" */)
)
_ uint32 = maxUint32 /* ERROR "overflows" */ + 1
_ uint32 = smallestFloat64 /* ERROR "truncated" */
- _ = uint32(0 /* ERROR "cannot convert" */ - 1)
+ _ = uint32(0 /* ERROR "overflows" */ - 1)
_ = uint32(0)
_ = uint32(maxUint32)
- _ = uint32(maxUint32 /* ERROR "cannot convert" */ + 1)
+ _ = uint32(maxUint32 /* ERROR "overflows" */ + 1)
_ = uint32(smallestFloat64 /* ERROR "cannot convert" */)
)
_ uint64 = maxUint64 /* ERROR "overflows" */ + 1
_ uint64 = smallestFloat64 /* ERROR "truncated" */
- _ = uint64(0 /* ERROR "cannot convert" */ - 1)
+ _ = uint64(0 /* ERROR "overflows" */ - 1)
_ = uint64(0)
_ = uint64(maxUint64)
- _ = uint64(maxUint64 /* ERROR "cannot convert" */ + 1)
+ _ = uint64(maxUint64 /* ERROR "overflows" */ + 1)
_ = uint64(smallestFloat64 /* ERROR "cannot convert" */)
)
_ uint = maxUint /* ERROR "overflows" */ + 1
_ uint = smallestFloat64 /* ERROR "truncated" */
- _ = uint(0 /* ERROR "cannot convert" */ - 1)
+ _ = uint(0 /* ERROR "overflows" */ - 1)
_ = uint(0)
_ = uint(maxUint)
- _ = uint(maxUint /* ERROR "cannot convert" */ + 1)
+ _ = uint(maxUint /* ERROR "overflows" */ + 1)
_ = uint(smallestFloat64 /* ERROR "cannot convert" */)
)
_ uintptr = maxUintptr /* ERROR "overflows" */ + 1
_ uintptr = smallestFloat64 /* ERROR "truncated" */
- _ = uintptr(0 /* ERROR "cannot convert" */ - 1)
+ _ = uintptr(0 /* ERROR "overflows" */ - 1)
_ = uintptr(0)
_ = uintptr(maxUintptr)
- _ = uintptr(maxUintptr /* ERROR "cannot convert" */ + 1)
+ _ = uintptr(maxUintptr /* ERROR "overflows" */ + 1)
_ = uintptr(smallestFloat64 /* ERROR "cannot convert" */)
)
--- /dev/null
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+var (
+ _ = int8(1 /* ERROR "constant 255 overflows int8" */ <<8 - 1)
+ _ = int16(1 /* ERROR "constant 65535 overflows int16" */ <<16 - 1)
+ _ = int32(1 /* ERROR "constant 4294967295 overflows int32" */ <<32 - 1)
+ _ = int64(1 /* ERROR "constant 18446744073709551615 overflows int64" */ <<64 - 1)
+
+ _ = uint8(1 /* ERROR "constant 256 overflows uint8" */ << 8)
+ _ = uint16(1 /* ERROR "constant 65536 overflows uint16" */ << 16)
+ _ = uint32(1 /* ERROR "constant 4294967296 overflows uint32" */ << 32)
+ _ = uint64(1 /* ERROR "constant 18446744073709551616 overflows uint64" */ << 64)
+)
+
+func _[P int8 | uint8]() {
+ _ = P(0)
+ _ = P(1 /* ERROR "constant 255 overflows int8 (in P)" */ <<8 - 1)
+}
+
+func _[P int16 | uint16]() {
+ _ = P(0)
+ _ = P(1 /* ERROR "constant 65535 overflows int16 (in P)" */ <<16 - 1)
+}
+
+func _[P int32 | uint32]() {
+ _ = P(0)
+ _ = P(1 /* ERROR "constant 4294967295 overflows int32 (in P)" */ <<32 - 1)
+}
+
+func _[P int64 | uint64]() {
+ _ = P(0)
+ _ = P(1 /* ERROR "constant 18446744073709551615 overflows int64 (in P)" */ <<64 - 1)
+}