Skip to content

Commit 3218b1a

Browse files
ALTreegriesemer
authored andcommitted
cmd/compile: only print one error for bad-type literal in assignment
Fixes golang#8438 Change-Id: Ib43cdcdc962a8d9e14faf984bc859a92ba1eb517 Reviewed-on: https://go-review.googlesource.com/40531 Reviewed-by: Robert Griesemer <gri@golang.org>
1 parent 405a280 commit 3218b1a

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

src/cmd/compile/internal/gc/subr.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,9 @@ func assignconvfn(n *Node, t *types.Type, context func() string) *Node {
982982
var why string
983983
op := assignop(n.Type, t, &why)
984984
if op == 0 {
985-
yyerror("cannot use %L as type %v in %s%s", n, t, context(), why)
985+
if !old.Diag() {
986+
yyerror("cannot use %L as type %v in %s%s", n, t, context(), why)
987+
}
986988
op = OCONV
987989
}
988990

test/fixedbugs/issue8438.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// errorcheck
2+
3+
// Copyright 2017 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Check that we don't print duplicate errors for string ->
8+
// array-literal conversion
9+
10+
package main
11+
12+
func main() {
13+
_ = []byte{"foo"} // ERROR "cannot convert"
14+
_ = []int{"foo"} // ERROR "cannot convert"
15+
_ = []rune{"foo"} // ERROR "cannot convert"
16+
_ = []string{"foo"} // OK
17+
}

test/interface/explicit.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func main() {
5353
i2 = I2(i) // ERROR "invalid|missing N method"
5454

5555
e = E(t) // ok
56-
t = T(e) // ERROR "need explicit|need type assertion|incompatible" "as type [*]T"
56+
t = T(e) // ERROR "need explicit|need type assertion|incompatible"
5757
}
5858

5959
type M interface {
@@ -81,7 +81,6 @@ var m2 M = jj // ERROR "incompatible|wrong type for M method"
8181
var m3 = M(ii) // ERROR "invalid|missing"
8282
var m4 = M(jj) // ERROR "invalid|wrong type for M method"
8383

84-
8584
type B1 interface {
8685
_() // ERROR "methods must have a unique non-blank name"
8786
}

0 commit comments

Comments
 (0)