mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
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 <gri@google.com> Auto-Submit: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
b16501c08b
commit
7398c3c0c6
@ -675,15 +675,11 @@ func (check *Checker) collectMethods(obj *TypeName) {
|
||||
// to it must be unique."
|
||||
assert(m.name != "_")
|
||||
if alt := mset.insert(m); alt != nil {
|
||||
var err error_
|
||||
err.code = _DuplicateMethod
|
||||
if check.conf.CompilerErrorMessages {
|
||||
err.errorf(m.pos, "%s.%s redeclared in this block", obj.Name(), m.name)
|
||||
if alt.Pos().IsKnown() {
|
||||
check.errorf(m.pos, _DuplicateMethod, "method %s.%s already declared at %s", obj.Name(), m.name, alt.Pos())
|
||||
} else {
|
||||
err.errorf(m.pos, "method %s already declared for %s", m.name, obj)
|
||||
check.errorf(m.pos, _DuplicateMethod, "method %s.%s already declared", obj.Name(), m.name)
|
||||
}
|
||||
err.recordAltDecl(alt)
|
||||
check.report(&err)
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -749,8 +749,11 @@ func (check *Checker) collectMethods(obj *TypeName) {
|
||||
// to it must be unique."
|
||||
assert(m.name != "_")
|
||||
if alt := mset.insert(m); alt != nil {
|
||||
check.errorf(m, _DuplicateMethod, "method %s already declared for %s", m.name, obj)
|
||||
check.reportAltDecl(alt)
|
||||
if alt.Pos().IsValid() {
|
||||
check.errorf(m, _DuplicateMethod, "method %s.%s already declared at %s", obj.Name(), m.name, alt.Pos())
|
||||
} else {
|
||||
check.errorf(m, _DuplicateMethod, "method %s.%s already declared", obj.Name(), m.name)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -36,9 +36,9 @@ type (
|
||||
|
||||
// Methods can be declared on the original named type and the alias.
|
||||
func (T0) m1() {} // GCCGO_ERROR "previous"
|
||||
func (*T0) m1() {} // ERROR "method redeclared: T0\.m1|T0\.m1 redeclared in this block|redefinition of .m1."
|
||||
func (A0) m1() {} // ERROR "T0\.m1 redeclared in this block|redefinition of .m1."
|
||||
func (A0) m1() {} // ERROR "T0\.m1 redeclared in this block|redefinition of .m1."
|
||||
func (*T0) m1() {} // ERROR "method redeclared: T0\.m1|T0\.m1 already declared|redefinition of .m1."
|
||||
func (A0) m1() {} // ERROR "T0\.m1 already declared|redefinition of .m1."
|
||||
func (A0) m1() {} // ERROR "T0\.m1 already declared|redefinition of .m1."
|
||||
func (A0) m2() {}
|
||||
|
||||
// Type aliases and the original type name can be used interchangeably.
|
||||
|
@ -9,7 +9,7 @@ package main
|
||||
type T int
|
||||
|
||||
func (T) m() {} // GCCGO_ERROR "previous"
|
||||
func (T) m() {} // ERROR "T[.]m redeclared|redefinition"
|
||||
func (T) m() {} // ERROR "T\.m already declared|redefinition"
|
||||
|
||||
func (*T) p() {} // GCCGO_ERROR "previous"
|
||||
func (*T) p() {} // ERROR "[(][*]T[)][.]p redeclared|redefinition|redeclared"
|
||||
func (*T) p() {} // ERROR "T\.p already declared|redefinition"
|
||||
|
@ -11,12 +11,12 @@ type A = T
|
||||
type B = T
|
||||
|
||||
func (T) m() {}
|
||||
func (T) m() {} // ERROR "redeclared|redefinition"
|
||||
func (A) m() {} // ERROR "redeclared|redefinition"
|
||||
func (A) m() {} // ERROR "redeclared|redefinition"
|
||||
func (B) m() {} // ERROR "redeclared|redefinition"
|
||||
func (B) m() {} // ERROR "redeclared|redefinition"
|
||||
func (T) m() {} // ERROR "already declared|redefinition"
|
||||
func (A) m() {} // ERROR "already declared|redefinition"
|
||||
func (A) m() {} // ERROR "already declared|redefinition"
|
||||
func (B) m() {} // ERROR "already declared|redefinition"
|
||||
func (B) m() {} // ERROR "already declared|redefinition"
|
||||
|
||||
func (*T) m() {} // ERROR "redeclared|redefinition"
|
||||
func (*A) m() {} // ERROR "redeclared|redefinition"
|
||||
func (*B) m() {} // ERROR "redeclared|redefinition"
|
||||
func (*T) m() {} // ERROR "already declared|redefinition"
|
||||
func (*A) m() {} // ERROR "already declared|redefinition"
|
||||
func (*B) m() {} // ERROR "already declared|redefinition"
|
||||
|
@ -12,10 +12,10 @@ package main
|
||||
type T struct{}
|
||||
|
||||
func (t *T) M(int, string) // GCCGO_ERROR "previous"
|
||||
func (t *T) M(int, float64) {} // ERROR "redeclared|redefinition"
|
||||
func (t *T) M(int, float64) {} // ERROR "already declared|redefinition"
|
||||
|
||||
func (t T) H() // GCCGO_ERROR "previous"
|
||||
func (t *T) H() {} // ERROR "redeclared|redefinition"
|
||||
func (t *T) H() {} // ERROR "already declared|redefinition"
|
||||
|
||||
func f(int, string) // GCCGO_ERROR "previous"
|
||||
func f(int, float64) {} // ERROR "redeclared|redefinition"
|
||||
|
Loading…
x
Reference in New Issue
Block a user