Skip to content

Commit 7398c3c

Browse files
griesemerRobert Griesemer
authored and
Robert Griesemer
committed
cmd/compile: use "method T.m already declared" for method redeclaration errors
Compromise between old compiler error "T.m redeclared in this block" (where the "in this block" is not particularly helpful) and the old type-checker error "method m already declared for type T ...". In the case where we have position information for the original declaration, the error message is "method T.m already declared at <position>". The new message is both shorter and more precise. For #55326. Change-Id: Id4a7f326fe631b11db9e8030eccb417c72d6c7db Reviewed-on: https://go-review.googlesource.com/c/go/+/435016 Run-TryBot: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent b16501c commit 7398c3c

File tree

6 files changed

+23
-24
lines changed

6 files changed

+23
-24
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -675,15 +675,11 @@ func (check *Checker) collectMethods(obj *TypeName) {
675675
// to it must be unique."
676676
assert(m.name != "_")
677677
if alt := mset.insert(m); alt != nil {
678-
var err error_
679-
err.code = _DuplicateMethod
680-
if check.conf.CompilerErrorMessages {
681-
err.errorf(m.pos, "%s.%s redeclared in this block", obj.Name(), m.name)
678+
if alt.Pos().IsKnown() {
679+
check.errorf(m.pos, _DuplicateMethod, "method %s.%s already declared at %s", obj.Name(), m.name, alt.Pos())
682680
} else {
683-
err.errorf(m.pos, "method %s already declared for %s", m.name, obj)
681+
check.errorf(m.pos, _DuplicateMethod, "method %s.%s already declared", obj.Name(), m.name)
684682
}
685-
err.recordAltDecl(alt)
686-
check.report(&err)
687683
continue
688684
}
689685

src/go/types/decl.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,11 @@ func (check *Checker) collectMethods(obj *TypeName) {
749749
// to it must be unique."
750750
assert(m.name != "_")
751751
if alt := mset.insert(m); alt != nil {
752-
check.errorf(m, _DuplicateMethod, "method %s already declared for %s", m.name, obj)
753-
check.reportAltDecl(alt)
752+
if alt.Pos().IsValid() {
753+
check.errorf(m, _DuplicateMethod, "method %s.%s already declared at %s", obj.Name(), m.name, alt.Pos())
754+
} else {
755+
check.errorf(m, _DuplicateMethod, "method %s.%s already declared", obj.Name(), m.name)
756+
}
754757
continue
755758
}
756759

test/alias2.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ type (
3636

3737
// Methods can be declared on the original named type and the alias.
3838
func (T0) m1() {} // GCCGO_ERROR "previous"
39-
func (*T0) m1() {} // ERROR "method redeclared: T0\.m1|T0\.m1 redeclared in this block|redefinition of .m1."
40-
func (A0) m1() {} // ERROR "T0\.m1 redeclared in this block|redefinition of .m1."
41-
func (A0) m1() {} // ERROR "T0\.m1 redeclared in this block|redefinition of .m1."
39+
func (*T0) m1() {} // ERROR "method redeclared: T0\.m1|T0\.m1 already declared|redefinition of .m1."
40+
func (A0) m1() {} // ERROR "T0\.m1 already declared|redefinition of .m1."
41+
func (A0) m1() {} // ERROR "T0\.m1 already declared|redefinition of .m1."
4242
func (A0) m2() {}
4343

4444
// Type aliases and the original type name can be used interchangeably.

test/fixedbugs/bug350.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package main
99
type T int
1010

1111
func (T) m() {} // GCCGO_ERROR "previous"
12-
func (T) m() {} // ERROR "T[.]m redeclared|redefinition"
12+
func (T) m() {} // ERROR "T\.m already declared|redefinition"
1313

1414
func (*T) p() {} // GCCGO_ERROR "previous"
15-
func (*T) p() {} // ERROR "[(][*]T[)][.]p redeclared|redefinition|redeclared"
15+
func (*T) p() {} // ERROR "T\.p already declared|redefinition"

test/fixedbugs/issue18655.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ type A = T
1111
type B = T
1212

1313
func (T) m() {}
14-
func (T) m() {} // ERROR "redeclared|redefinition"
15-
func (A) m() {} // ERROR "redeclared|redefinition"
16-
func (A) m() {} // ERROR "redeclared|redefinition"
17-
func (B) m() {} // ERROR "redeclared|redefinition"
18-
func (B) m() {} // ERROR "redeclared|redefinition"
14+
func (T) m() {} // ERROR "already declared|redefinition"
15+
func (A) m() {} // ERROR "already declared|redefinition"
16+
func (A) m() {} // ERROR "already declared|redefinition"
17+
func (B) m() {} // ERROR "already declared|redefinition"
18+
func (B) m() {} // ERROR "already declared|redefinition"
1919

20-
func (*T) m() {} // ERROR "redeclared|redefinition"
21-
func (*A) m() {} // ERROR "redeclared|redefinition"
22-
func (*B) m() {} // ERROR "redeclared|redefinition"
20+
func (*T) m() {} // ERROR "already declared|redefinition"
21+
func (*A) m() {} // ERROR "already declared|redefinition"
22+
func (*B) m() {} // ERROR "already declared|redefinition"

test/method1.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ package main
1212
type T struct{}
1313

1414
func (t *T) M(int, string) // GCCGO_ERROR "previous"
15-
func (t *T) M(int, float64) {} // ERROR "redeclared|redefinition"
15+
func (t *T) M(int, float64) {} // ERROR "already declared|redefinition"
1616

1717
func (t T) H() // GCCGO_ERROR "previous"
18-
func (t *T) H() {} // ERROR "redeclared|redefinition"
18+
func (t *T) H() {} // ERROR "already declared|redefinition"
1919

2020
func f(int, string) // GCCGO_ERROR "previous"
2121
func f(int, float64) {} // ERROR "redeclared|redefinition"

0 commit comments

Comments
 (0)