You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
cmd/compile: use "cannot use %s as %s value in %s: %s" error message
This is close to what the compiler used to say, except now we say
"as T value" rather than "as type T" which is closer to the truth
(we cannot use a value as a type, after all). Also, place the primary
error and the explanation (cause) on a single line.
Make respective (single line) adjustment to the matching "cannot
convert" error.
Adjust various tests.
For #55326.
Change-Id: Ib646cf906b11f4129b7ed0c38cf16471f9266b88
Reviewed-on: https://go-review.googlesource.com/c/go/+/436176
Reviewed-by: Robert Griesemer <[email protected]>
Run-TryBot: Robert Griesemer <[email protected]>
Auto-Submit: Robert Griesemer <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Copy file name to clipboardExpand all lines: src/internal/types/testdata/spec/conversions.go
+14-14Lines changed: 14 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -34,10 +34,10 @@ func _[
34
34
T3~int|~float64|~bool,
35
35
T4~int|~string,
36
36
]() {
37
-
_=T1(0/* ERROR cannot convert 0 .* to type T1:\n\tT1 does not contain specific types */)
38
-
_=T2(1/* ERROR cannot convert 1 .* to type T2:\n\tT2 does not contain specific types */)
39
-
_=T3(2/* ERROR cannot convert 2 .* to type T3:\n\tcannot convert 2 .* to type bool \(in T3\) */)
40
-
_=T4(3.14/* ERROR cannot convert 3.14 .* to type T4:\n\tcannot convert 3.14 .* to type int \(in T4\) */)
37
+
_=T1(0/* ERROR cannot convert 0 .* to type T1: T1 does not contain specific types */)
38
+
_=T2(1/* ERROR cannot convert 1 .* to type T2: T2 does not contain specific types */)
39
+
_=T3(2/* ERROR cannot convert 2 .* to type T3: cannot convert 2 .* to type bool \(in T3\) */)
40
+
_=T4(3.14/* ERROR cannot convert 3.14 .* to type T4: cannot convert 3.14 .* to type int \(in T4\) */)
41
41
}
42
42
43
43
// "x is assignable to T"
@@ -66,7 +66,7 @@ func _[X Foo, T Bar](x X) T { return T(x) }
66
66
func_[XFoo|Bar, TBar](xX) T { returnT(x) }
67
67
func_[XFoo, TFoo|Bar](xX) T { returnT(x) }
68
68
func_[XFoo, TFar](xX) T {
69
-
returnT(x/* ERROR cannot convert x \(variable of type X constrained by Foo\) to type T:\n\tcannot convert Foo \(in X\) to type Far \(in T\) */)
69
+
returnT(x/* ERROR cannot convert x \(variable of type X constrained by Foo\) to type T: cannot convert Foo \(in X\) to type Far \(in T\) */)
70
70
}
71
71
72
72
// "x's type and T are unnamed pointer types and their pointer base types
@@ -76,7 +76,7 @@ func _[X ~*Foo, T ~*Bar](x X) T { return T(x) }
76
76
func_[X~*Foo|~*Bar, T~*Bar](xX) T { returnT(x) }
77
77
func_[X~*Foo, T~*Foo|~*Bar](xX) T { returnT(x) }
78
78
func_[X~*Foo, T~*Far](xX) T {
79
-
returnT(x/* ERROR cannot convert x \(variable of type X constrained by ~\*Foo\) to type T:\n\tcannot convert \*Foo \(in X\) to type \*Far \(in T\) */)
79
+
returnT(x/* ERROR cannot convert x \(variable of type X constrained by ~\*Foo\) to type T: cannot convert \*Foo \(in X\) to type \*Far \(in T\) */)
80
80
}
81
81
82
82
// Verify that the defined types in constraints are considered for the rule above.
@@ -109,14 +109,14 @@ func _[X Float, T Float](x X) T { return T(x) }
109
109
110
110
func_[X, TInteger|Unsigned|Float](xX) T { returnT(x) }
111
111
func_[X, TInteger|~string](xX) T {
112
-
returnT(x/* ERROR cannot convert x \(variable of type X constrained by Integer \| ~string\) to type T:\n\tcannot convert string \(in X\) to type int \(in T\) */)
112
+
returnT(x/* ERROR cannot convert x \(variable of type X constrained by Integer \| ~string\) to type T: cannot convert string \(in X\) to type int \(in T\) */)
113
113
}
114
114
115
115
// "x's type and T are both complex types"
116
116
117
117
func_[X, TComplex](xX) T { returnT(x) }
118
118
func_[X, TFloat|Complex](xX) T {
119
-
returnT(x/* ERROR cannot convert x \(variable of type X constrained by Float \| Complex\) to type T:\n\tcannot convert float32 \(in X\) to type complex64 \(in T\) */)
119
+
returnT(x/* ERROR cannot convert x \(variable of type X constrained by Float \| Complex\) to type T: cannot convert float32 \(in X\) to type complex64 \(in T\) */)
120
120
}
121
121
122
122
// "x is an integer or a slice of bytes or runes and T is a string type"
return (*string)(x/* ERROR cannot convert x \(variable of type X constrained by Integer\) to type \*string:\n\tcannot convert int \(in X\) to type \*string */)
132
+
return (*string)(x/* ERROR cannot convert x \(variable of type X constrained by Integer\) to type \*string: cannot convert int \(in X\) to type \*string */)
133
133
}
134
134
135
135
func_[T~string](x []byte) T { returnT(x) }
@@ -138,22 +138,22 @@ func _[X ~[]byte, T ~string](x X) T { return T(x) }
138
138
func_[X~[]rune, T~string](xX) T { returnT(x) }
139
139
func_[XInteger|~[]byte|~[]rune, T~string](xX) T { returnT(x) }
140
140
func_[XInteger|~[]byte|~[]rune, T~*string](xX) T {
141
-
returnT(x/* ERROR cannot convert x \(variable of type X constrained by Integer \| ~\[\]byte \| ~\[\]rune\) to type T:\n\tcannot convert int \(in X\) to type \*string \(in T\) */)
141
+
returnT(x/* ERROR cannot convert x \(variable of type X constrained by Integer \| ~\[\]byte \| ~\[\]rune\) to type T: cannot convert int \(in X\) to type \*string \(in T\) */)
142
142
}
143
143
144
144
// "x is a string and T is a slice of bytes or runes"
145
145
146
146
func_[T~[]byte](xstring) T { returnT(x) }
147
147
func_[T~[]rune](xstring) T { returnT(x) }
148
148
func_[T~[]rune](x*string) T {
149
-
returnT(x/* ERROR cannot convert x \(variable of type \*string\) to type T:\n\tcannot convert \*string to type \[\]rune \(in T\) */)
149
+
returnT(x/* ERROR cannot convert x \(variable of type \*string\) to type T: cannot convert \*string to type \[\]rune \(in T\) */)
150
150
}
151
151
152
152
func_[X~string, T~[]byte](xX) T { returnT(x) }
153
153
func_[X~string, T~[]rune](xX) T { returnT(x) }
154
154
func_[X~string, T~[]byte|~[]rune](xX) T { returnT(x) }
155
155
func_[X~*string, T~[]byte|~[]rune](xX) T {
156
-
returnT(x/* ERROR cannot convert x \(variable of type X constrained by ~\*string\) to type T:\n\tcannot convert \*string \(in X\) to type \[\]byte \(in T\) */)
156
+
returnT(x/* ERROR cannot convert x \(variable of type X constrained by ~\*string\) to type T: cannot convert \*string \(in X\) to type \[\]byte \(in T\) */)
returnint64(x/* ERROR cannot convert x \(variable of type X constrained by unsafe\.Pointer\) to type int64:\n\tcannot convert unsafe\.Pointer \(in X\) to type int64 */)
176
+
returnint64(x/* ERROR cannot convert x \(variable of type X constrained by unsafe\.Pointer\) to type int64: cannot convert unsafe\.Pointer \(in X\) to type int64 */)
177
177
}
178
178
179
179
// "x is a slice, T is an array or pointer-to-array type,
Copy file name to clipboardExpand all lines: test/alias2.go
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -46,8 +46,8 @@ var _ A0 = T0{}
46
46
var_T0=A0{}
47
47
48
48
// But aliases and original types cannot be used with new types based on them.
49
-
var_N0=T0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|cannot use T0{} \(value of type T0\) as type N0 in variable declaration"
50
-
var_N0=A0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|cannot use A0{} \(value of type T0\) as type N0 in variable declaration"
49
+
var_N0=T0{} // ERROR "cannot use T0{} \(value of type T0\) as N0 value in variable declaration"
50
+
var_N0=A0{} // ERROR "cannot use A0{} \(value of type T0\) as N0 value in variable declaration"
51
51
52
52
var_A5=Value{}
53
53
@@ -82,10 +82,10 @@ func _() {
82
82
var_A0=T0{}
83
83
var_T0=A0{}
84
84
85
-
var_N0=T0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|cannot use T0{} \(value of type T0\) as type N0 in variable declaration"
86
-
var_N0=A0{} // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|cannot use A0{} \(value of type T0\) as type N0 in variable declaration"
85
+
var_N0=T0{} // ERROR "cannot use T0{} \(value of type T0\) as N0 value in variable declaration"
86
+
var_N0=A0{} // ERROR "cannot use A0{} \(value of type T0\) as N0 value in variable declaration"
87
87
88
-
var_A5=Value{} // ERROR "cannot use reflect\.Value{} \(type reflect.Value\) as type A5 in assignment|cannot use Value{} \(value of type reflect.Value\) as type A5 in variable declaration"
88
+
var_A5=Value{} // ERROR "cannot use Value{} \(value of type reflect\.Value\) as A5 value in variable declaration"
Copy file name to clipboardExpand all lines: test/append1.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,6 @@ func main() {
17
17
_=append(s...) // ERROR "cannot use ... on first argument|not enough arguments in call to append"
18
18
_=append(s, 2, s...) // ERROR "too many arguments to append|too many arguments in call to append"
19
19
20
-
_=append(s, make([]int, 0)) // ERROR "cannot use make.* as type int in append|cannot use make.* \(value of type \[\]int\) as type int in argument to append"
20
+
_=append(s, make([]int, 0)) // ERROR "cannot use make\(\[\]int, 0\) \(value of type \[\]int\) as int value in argument to append"
21
21
_=append(s, make([]int, -1)...) // ERROR "negative len argument in make|index -1.* must not be negative"
Copy file name to clipboardExpand all lines: test/fixedbugs/bug389.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -9,4 +9,4 @@ package foo
9
9
10
10
funcfn(afloat32) {}
11
11
12
-
varffunc(argint) =fn// ERROR "cannot use fn .type func.float32.. as type func.int. in assignment|different parameter types|cannot use fn .*type func.*float32.. as type func.*int. in variable declaration"
12
+
varffunc(argint) =fn// ERROR "different parameter types|cannot use fn .*type func.*float32.. as func.*int. value in variable declaration"
Copy file name to clipboardExpand all lines: test/fixedbugs/issue17645.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -12,5 +12,5 @@ type Foo struct {
12
12
13
13
funcmain() {
14
14
vars []int
15
-
var_string=append(s, Foo{""}) // ERROR "cannot use .. \(.*untyped string.*\) as .*int.*|incompatible type" "cannot use Foo{.*} \(.*type Foo\) as type int in .*append" "cannot use append\(s\, Foo{.*}\) \(.*type \[\]int\) as type string in (assignment|variable declaration)"
15
+
var_string=append(s, Foo{""}) // ERROR "cannot use append\(s, Foo{…}\) .* as string value in variable declaration" "cannot use Foo{…} .* as int value in argument to append" "cannot use .* as int value in struct literal"
f(new(T)) // ERROR "cannot use new\(T\) \(.*type \*T\) as type I in argument to f:\n\t\*T does not implement I \(missing method M\)"
36
+
f(new(T)) // ERROR "cannot use new\(T\) \(.*type \*T\) as I value in argument to f:\*T does not implement I \(missing method M\)"
37
37
38
38
variI
39
-
i=new(T) // ERROR "cannot use new\(T\) \(.*type \*T\) as type I in assignment:\n\t\*T does not implement I \(missing method M\)"
40
-
i=I(new(T)) // ERROR "cannot convert new\(T\) \(.*type \*T\) to type I:\n\t\*T does not implement I \(missing method M\)"
41
-
i=new(T2) // ERROR "cannot use new\(T2\) \(.*type \*T2\) as type I in assignment:\n\t\*T2 does not implement I \(missing method M\)\n\t\thave m\(int\)\n\t\twant M\(int\)"
39
+
i=new(T) // ERROR "cannot use new\(T\) \(.*type \*T\) as I value in assignment:\*T does not implement I \(missing method M\)"
40
+
i=I(new(T)) // ERROR "cannot convert new\(T\) \(.*type \*T\) to type I:\*T does not implement I \(missing method M\)"
41
+
i=new(T2) // ERROR "cannot use new\(T2\) \(.*type \*T2\) as I value in assignment:\*T2 does not implement I \(missing method M\)\n\t\thave m\(int\)\n\t\twant M\(int\)"
42
42
43
-
i=new(T3) // ERROR "cannot use new\(T3\) \(.*type \*T3\) as type I in assignment:\n\t\*T3 does not implement I \(wrong type for method M\)\n\t\thave M\(string\)\n\t\twant M\(int\)"
43
+
i=new(T3) // ERROR "cannot use new\(T3\) \(.*type \*T3\) as I value in assignment:\*T3 does not implement I \(wrong type for method M\)\n\t\thave M\(string\)\n\t\twant M\(int\)"
44
44
45
-
i=T4{} // ERROR "cannot use T4\{\} \(.*type T4\) as type I in assignment:\n\tT4 does not implement I \(method M has pointer receiver\)"
46
-
i=new(I) // ERROR "cannot use new\(I\) \(.*type \*I\) as type I in assignment:\n\t\*I does not implement I \(type \*I is pointer to interface, not interface\)"
45
+
i=T4{} // ERROR "cannot use T4\{\} \(.*type T4\) as I value in assignment: T4 does not implement I \(method M has pointer receiver\)"
46
+
i=new(I) // ERROR "cannot use new\(I\) \(.*type \*I\) as I value in assignment:\*I does not implement I \(type \*I is pointer to interface, not interface\)"
47
47
48
48
_=i.(*T2) // ERROR "impossible type assertion: i.\(\*T2\)\n\t\*T2 does not implement I \(missing method M\)\n\t\thave m\(int\)\n\t\twant M\(int\)"
49
49
_=i.(*T3) // ERROR "impossible type assertion: i.\(\*T3\)\n\t\*T3 does not implement I \(wrong type for method M\)\n\t\thave M\(string\)\n\t\twant M\(int\)"
50
50
_=i.(T5) // ERROR ""impossible type assertion: i.\(T5\)\n\tT5 does not implement I \(missing method M\)\n\t\thave m\(int\)\n\t\twant M\(int\)"
51
51
_=i.(T6) // ERROR "impossible type assertion: i.\(T6\)\n\tT6 does not implement I \(missing method M\)\n\t\thave m\(int\) string\n\t\twant M\(int\)"
52
52
53
53
vart*T4
54
-
t=i// ERROR "cannot use i \(variable of type I\) as type \*T4 in assignment:\n\tneed type assertion"
54
+
t=i// ERROR "cannot use i \(variable of type I\) as \*T4 value in assignment: need type assertion"
0 commit comments