Skip to content

Commit b2137e7

Browse files
griesemergopherbot
authored andcommitted
go/types, types2: use 2nd operand position for comparison type mismatch errors
When a comparison is invalid due to mismatched types, we only know when we see the 2nd operand; so use that operand's position for the error message. This matches compiler behavior. For #55326. Change-Id: I79450756bbdd2b4bb90ed4e960a451be0197b186 Reviewed-on: https://go-review.googlesource.com/c/go/+/435555 Run-TryBot: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent fa13731 commit b2137e7

File tree

5 files changed

+30
-40
lines changed

5 files changed

+30
-40
lines changed

src/cmd/compile/internal/types2/expr.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -788,11 +788,6 @@ func (check *Checker) comparison(x, y *operand, op syntax.Operator, switchCase b
788788
// know after seeing the 2nd operand whether we have
789789
// a type mismatch.
790790
errOp = y
791-
// For now, if we're not running the compiler, use the
792-
// position of x to minimize changes to existing tests.
793-
if !check.conf.CompilerErrorMessages {
794-
errOp = x
795-
}
796791
cause = check.sprintf("mismatched types %s and %s", x.typ, y.typ)
797792
goto Error
798793
}

src/go/types/expr.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -765,11 +765,6 @@ func (check *Checker) comparison(x, y *operand, op token.Token, switchCase bool)
765765
// know after seeing the 2nd operand whether we have
766766
// a type mismatch.
767767
errOp = y
768-
// For now, if we're not running the compiler, use the
769-
// position of x to minimize changes to existing tests.
770-
if !compilerErrorMessages {
771-
errOp = x
772-
}
773768
cause = check.sprintf("mismatched types %s and %s", x.typ, y.typ)
774769
goto Error
775770
}

src/internal/types/testdata/check/const0.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const (
2727
ub1 = true
2828
ub2 = 2 < 1
2929
ub3 = ui1 == uf1
30-
ub4 = true /* ERROR "mismatched types untyped bool and untyped int" */ == 0
30+
ub4 = true == 0 /* ERROR "mismatched types untyped bool and untyped int" */
3131

3232
// integer values
3333
ui0 = 0
@@ -110,7 +110,7 @@ const (
110110
tb0 bool = false
111111
tb1 bool = true
112112
tb2 mybool = 2 < 1
113-
tb3 mybool = ti1 /* ERROR "mismatched types" */ == tf1
113+
tb3 mybool = ti1 == tf1 /* ERROR "mismatched types" */
114114

115115
// integer values
116116
ti0 int8 = ui0

src/internal/types/testdata/check/expr2.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func _bool() {
1010
const t = true == true
1111
const f = true == false
1212
_ = t /* ERROR cannot compare */ < f
13-
_ = 0 /* ERROR mismatched types untyped int and untyped bool */ == t
13+
_ = 0 == t /* ERROR mismatched types untyped int and untyped bool */
1414
var b bool
1515
var x, y float32
1616
b = x < y
@@ -29,15 +29,15 @@ func arrays() {
2929
_ = a == b
3030
_ = a != b
3131
_ = a /* ERROR < not defined */ < b
32-
_ = a /* ERROR cannot compare.*mismatched types */ == nil
32+
_ = a == nil /* ERROR cannot compare.*mismatched types */
3333

3434
type C [10]int
3535
var c C
3636
_ = a == c
3737

3838
type D [10]int
3939
var d D
40-
_ = c /* ERROR mismatched types */ == d
40+
_ = c == d /* ERROR mismatched types */
4141

4242
var e [10]func() int
4343
_ = e /* ERROR \[10\]func\(\) int cannot be compared */ == e
@@ -53,7 +53,7 @@ func structs() {
5353
_ = s == t
5454
_ = s != t
5555
_ = s /* ERROR < not defined */ < t
56-
_ = s /* ERROR cannot compare.*mismatched types */ == nil
56+
_ = s == nil /* ERROR cannot compare.*mismatched types */
5757

5858
type S struct {
5959
x int
@@ -68,7 +68,7 @@ func structs() {
6868
var ss S
6969
var tt T
7070
_ = s == ss
71-
_ = ss /* ERROR mismatched types */ == tt
71+
_ = ss == tt /* ERROR mismatched types */
7272

7373
var u struct {
7474
x int
@@ -115,11 +115,11 @@ func pointers() {
115115
p2 P2
116116
)
117117
_ = ps1 == ps1
118-
_ = ps1 /* ERROR mismatched types */ == ps2
119-
_ = ps2 /* ERROR mismatched types */ == ps1
118+
_ = ps1 == ps2 /* ERROR mismatched types */
119+
_ = ps2 == ps1 /* ERROR mismatched types */
120120

121121
_ = p1 == p1
122-
_ = p1 /* ERROR mismatched types */ == p2
122+
_ = p1 == p2 /* ERROR mismatched types */
123123

124124
_ = p1 == ps1
125125
}
@@ -147,13 +147,13 @@ func channels() {
147147
c2 C2
148148
)
149149
_ = c1 == c1
150-
_ = c1 /* ERROR mismatched types */ == c1r
151-
_ = c1 /* ERROR mismatched types */ == c1s
152-
_ = c1r /* ERROR mismatched types */ == c1s
150+
_ = c1 == c1r /* ERROR mismatched types */
151+
_ = c1 == c1s /* ERROR mismatched types */
152+
_ = c1r == c1s /* ERROR mismatched types */
153153
_ = c1 == c1a
154154
_ = c1a == c1
155-
_ = c1 /* ERROR mismatched types */ == c2
156-
_ = c1a /* ERROR mismatched types */ == c2
155+
_ = c1 == c2 /* ERROR mismatched types */
156+
_ = c1a == c2 /* ERROR mismatched types */
157157

158158
// various element types (unnamed types)
159159
var (
@@ -166,11 +166,11 @@ func channels() {
166166
_ = d1 == d1
167167
_ = d1 == d1r
168168
_ = d1 == d1s
169-
_ = d1r /* ERROR mismatched types */ == d1s
169+
_ = d1r == d1s /* ERROR mismatched types */
170170
_ = d1 == d1a
171171
_ = d1a == d1
172-
_ = d1 /* ERROR mismatched types */ == d2
173-
_ = d1a /* ERROR mismatched types */ == d2
172+
_ = d1 == d2 /* ERROR mismatched types */
173+
_ = d1a == d2 /* ERROR mismatched types */
174174
}
175175

176176
// for interfaces test
@@ -194,20 +194,20 @@ func interfaces() {
194194
var ii interface { m() int; n() }
195195
var k interface { m() float32 }
196196
_ = i == ii
197-
_ = i /* ERROR mismatched types */ == k
197+
_ = i == k /* ERROR mismatched types */
198198

199199
// interfaces vs values
200200
var s1 S1
201201
var s11 S11
202202
var s2 S2
203203

204204
_ = i == 0 /* ERROR cannot convert */
205-
_ = i /* ERROR mismatched types */ == s1
205+
_ = i == s1 /* ERROR mismatched types */
206206
_ = i == &s1
207207
_ = i == &s11
208208

209-
_ = i /* ERROR mismatched types */ == s2
210-
_ = i /* ERROR mismatched types */ == &s2
209+
_ = i == s2 /* ERROR mismatched types */
210+
_ = i == & /* ERROR mismatched types */ s2
211211

212212
// issue #28164
213213
// testcase from issue

src/internal/types/testdata/spec/comparisons.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ func _() {
4040
_ = m /* ERROR map can only be compared to nil */ == m
4141
_ = c == c
4242

43-
_ = b /* ERROR mismatched types */ == nil
44-
_ = a /* ERROR mismatched types */ == nil
43+
_ = b == nil /* ERROR mismatched types */
44+
_ = a == nil /* ERROR mismatched types */
4545
_ = l == nil
46-
_ = s /* ERROR mismatched types */ == nil
46+
_ = s == nil /* ERROR mismatched types */
4747
_ = p == nil
4848
_ = f == nil
4949
_ = i == nil
@@ -96,14 +96,14 @@ func _[
9696
_ = m /* ERROR incomparable types in type set */ == m
9797
_ = c == c
9898

99-
_ = b /* ERROR mismatched types */ == nil
100-
_ = a /* ERROR mismatched types */ == nil
99+
_ = b == nil /* ERROR mismatched types */
100+
_ = a == nil /* ERROR mismatched types */
101101
_ = l == nil
102-
_ = s /* ERROR mismatched types */ == nil
102+
_ = s == nil /* ERROR mismatched types */
103103
_ = p == nil
104104
_ = f == nil
105-
_ = i /* ERROR mismatched types */ == nil
106-
_ = j /* ERROR mismatched types */ == nil
105+
_ = i == nil /* ERROR mismatched types */
106+
_ = j == nil /* ERROR mismatched types */
107107
_ = m == nil
108108
_ = c == nil
109109

0 commit comments

Comments
 (0)