target = Default(x.typ)
}
if err := check.canConvertUntyped(x, target); err != nil {
- var internalErr Error
- msg := err.Error()
+ msg := check.sprintf("cannot use %s as %s value in %s", x, target, context)
code := _IncompatibleAssign
- if errors.As(err, &internalErr) {
- msg = internalErr.Msg
- code = internalErr.go116code
+ var ierr Error
+ if errors.As(err, &ierr) {
+ // Preserve these inner errors, as they are informative.
+ switch ierr.go116code {
+ case _TruncatedFloat:
+ msg += " (truncated)"
+ code = ierr.go116code
+ case _NumericOverflow:
+ msg += " (overflows)"
+ code = ierr.go116code
+ }
}
- check.errorf(x.pos(), code, "cannot use %s as %s value in %s: %v", x, target, context, msg)
+ check.error(x.pos(), code, msg)
x.mode = invalid
return
}
// _InvalidConstVal occurs when a const value cannot be converted to its
// target type.
//
+ // TODO(findleyr): this error code and example are not very clear. Consider
+ // removing it.
+ //
// Example:
- // var x string = 1
+ // const _ = 1 << "hello"
_InvalidConstVal
// _InvalidConstType occurs when the underlying type in a const declaration
type S []byte
type T string
var t T
- _ = append(s, "foo" /* ERROR cannot convert */ )
+ _ = append(s, "foo" /* ERROR cannot use .* in argument to append */ )
_ = append(s, "foo"...)
- _ = append(S(s), "foo" /* ERROR cannot convert */ )
+ _ = append(S(s), "foo" /* ERROR cannot use .* in argument to append */ )
_ = append(S(s), "foo"...)
_ = append(s, t /* ERROR cannot use t */ )
_ = append(s, t...)
v11 = xx/yy*yy - xx
v12 = true && false
v13 = nil /* ERROR "use of untyped nil" */
+ v14 string = 257 // ERROR cannot use 257 .* as string value in variable declaration$
+ v15 int8 = 257 // ERROR cannot use 257 .* as int8 value in variable declaration .*overflows
)
// Multiple assignment expressions
// Verify by checking that errors are reported.
func (T /* ERROR "undeclared" */ ) _() {}
func (T1) _(undeclared /* ERROR "undeclared" */ ) {}
-func (T1) _() int { return "foo" /* ERROR "cannot convert" */ }
+func (T1) _() int { return "foo" /* ERROR "cannot use .* in return statement" */ }
// Methods with undeclared receiver type can still be checked.
// Verify by checking that errors are reported.
func (Foo /* ERROR "undeclared" */ ) m() {}
func (Foo /* ERROR "undeclared" */ ) m(undeclared /* ERROR "undeclared" */ ) {}
-func (Foo /* ERROR "undeclared" */ ) m() int { return "foo" /* ERROR "cannot convert" */ }
+func (Foo /* ERROR "undeclared" */ ) m() int { return "foo" /* ERROR "cannot use .* in return statement" */ }
func (Foo /* ERROR "undeclared" */ ) _() {}
func (Foo /* ERROR "undeclared" */ ) _(undeclared /* ERROR "undeclared" */ ) {}
-func (Foo /* ERROR "undeclared" */ ) _() int { return "foo" /* ERROR "cannot convert" */ }
+func (Foo /* ERROR "undeclared" */ ) _() int { return "foo" /* ERROR "cannot use .* in return statement" */ }
// Receiver declarations are regular parameter lists;
// receiver types may use parentheses, and the list
_ = (*T7).m4
_ = (*T7).m5
_ = (*T7).m6
-)
\ No newline at end of file
+)
_ = &s /* ERROR "cannot take address" */ [:10]
var m map[string]int
- _ = m[0 /* ERROR "cannot convert" */ ]
+ _ = m[0 /* ERROR "cannot use .* in map index" */ ]
_ = m /* ERROR "cannot slice" */ ["foo" : "bar"]
_ = m["foo"]
// ok is of type bool
_ = T1{aa /* ERROR "unknown field" */ : 0}
_ = T1{1 /* ERROR "invalid field name" */ : 0}
_ = T1{a: 0, s: "foo", u: 0, a /* ERROR "duplicate field" */: 10}
- _ = T1{a: "foo" /* ERROR "cannot convert" */ }
+ _ = T1{a: "foo" /* ERROR "cannot use .* in struct literal" */ }
_ = T1{c /* ERROR "unknown field" */ : 0}
_ = T1{T0: { /* ERROR "missing type" */ }} // struct literal element type may not be elided
_ = T1{T0: T0{}}
_ = T0{1, b /* ERROR "mixture" */ : 2, 3}
_ = T0{1, 2} /* ERROR "too few values" */
_ = T0{1, 2, 3, 4 /* ERROR "too many values" */ }
- _ = T0{1, "foo" /* ERROR "cannot convert" */, 3.4 /* ERROR "truncated" */}
+ _ = T0{1, "foo" /* ERROR "cannot use .* in struct literal" */, 3.4 /* ERROR "cannot use .*\(truncated\)" */}
// invalid type
type P *struct{
_ = A1{5: 5, 6, 7, 4: 4, 1 /* ERROR "overflows" */ <<100: 4}
_ = A1{2.0}
_ = A1{2.1 /* ERROR "truncated" */ }
- _ = A1{"foo" /* ERROR "cannot convert" */ }
+ _ = A1{"foo" /* ERROR "cannot use .* in array or slice literal" */ }
// indices must be integer constants
i := 1
_ = S0{5: 5, 6, 7, 4: 4, 1 /* ERROR "overflows" */ <<100: 4}
_ = S0{2.0}
_ = S0{2.1 /* ERROR "truncated" */ }
- _ = S0{"foo" /* ERROR "cannot convert" */ }
+ _ = S0{"foo" /* ERROR "cannot use .* in array or slice literal" */ }
// indices must be resolved correctly
const index1 = 1
_ = M0{}
_ = M0{1 /* ERROR "missing key" */ }
- _ = M0{1 /* ERROR "cannot convert" */ : 2}
- _ = M0{"foo": "bar" /* ERROR "cannot convert" */ }
+ _ = M0{1 /* ERROR "cannot use .* in map literal" */ : 2}
+ _ = M0{"foo": "bar" /* ERROR "cannot use .* in map literal" */ }
_ = M0{"foo": 1, "bar": 2, "foo" /* ERROR "duplicate key" */ : 3 }
_ = map[interface{}]int{2: 1, 2 /* ERROR "duplicate key" */ : 1}
func issue35895() {
// T is defined in this package, don't qualify its name with the package name.
- var _ T = 0 // ERROR cannot convert 0 \(untyped int constant\) to T
+ var _ T = 0 // ERROR cannot use 0 \(untyped int constant\) as T
// There is only one package with name syntax imported, only use the (global) package name in error messages.
- var _ *syn.File = 0 // ERROR cannot convert 0 \(untyped int constant\) to \*syntax.File
+ var _ *syn.File = 0 // ERROR cannot use 0 \(untyped int constant\) as \*syntax.File
// Because both t1 and t2 have the same global package name (template),
// qualify packages with full path name in this case.
var x int
x <- /* ERROR "cannot send" */ x
rch <- /* ERROR "cannot send" */ x
- ch <- "foo" /* ERROR "cannot convert" */
+ ch <- "foo" /* ERROR "cannot use .* in send" */
ch <- x
}
func returns1(x float64) (int, *float64) {
return 0, &x
return /* ERROR wrong number of return values */
- return "foo" /* ERROR "cannot convert" */, x /* ERROR "cannot use .* in return statement" */
+ return "foo" /* ERROR "cannot .* in return statement" */, x /* ERROR "cannot use .* in return statement" */
return /* ERROR wrong number of return values */ 0, &x, 1
}
func returns2() (a, b int) {
return
- return 1, "foo" /* ERROR cannot convert */
+ return 1, "foo" /* ERROR cannot use .* in return statement */
return /* ERROR wrong number of return values */ 1, 2, 3
{
type a int
// untyped constants are converted to default types
switch 1<<63-1 {
}
- switch 1 /* ERROR "overflows int" */ << 63 {
+ switch 1 /* ERROR "cannot use .* as int value.*\(overflows\)" */ << 63 {
}
var x int
switch 1.0 {
}
func issue11667() {
- switch 9223372036854775808 /* ERROR "overflows int" */ {
+ switch 9223372036854775808 /* ERROR "cannot use .* as int value.*\(overflows\)" */ {
}
- switch 9223372036854775808 /* ERROR "overflows int" */ {
+ switch 9223372036854775808 /* ERROR "cannot use .* as int value.*\(overflows\)" */ {
case 9223372036854775808:
}
var x int