mirror of https://github.com/golang/go.git
go/types, types2: improve error message for init without body
Change-Id: I8a684965e88e0e33a6ff33a16e08d136e3267f7e Reviewed-on: https://go-review.googlesource.com/c/go/+/663636 TryBot-Bypass: Mark Freeman <mark@golang.org> Auto-Submit: Mark Freeman <mark@golang.org> Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
parent
2c929d6f4c
commit
bc5aa2f7d3
|
|
@ -436,10 +436,8 @@ func (check *Checker) collectObjects() {
|
||||||
if name == "init" {
|
if name == "init" {
|
||||||
obj.parent = pkg.scope
|
obj.parent = pkg.scope
|
||||||
check.recordDef(s.Name, obj)
|
check.recordDef(s.Name, obj)
|
||||||
// init functions must have a body
|
|
||||||
if s.Body == nil {
|
if s.Body == nil {
|
||||||
// TODO(gri) make this error message consistent with the others above
|
check.softErrorf(obj.pos, MissingInitBody, "func init must have a body")
|
||||||
check.softErrorf(obj.pos, MissingInitBody, "missing function body")
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
check.declare(pkg.scope, s.Name, obj, nopos)
|
check.declare(pkg.scope, s.Name, obj, nopos)
|
||||||
|
|
|
||||||
|
|
@ -192,7 +192,7 @@ func TestIssue20855(t *testing.T) {
|
||||||
testenv.MustHaveSource(t)
|
testenv.MustHaveSource(t)
|
||||||
|
|
||||||
pkg, err := importer.ImportFrom("go/internal/srcimporter/testdata/issue20855", ".", 0)
|
pkg, err := importer.ImportFrom("go/internal/srcimporter/testdata/issue20855", ".", 0)
|
||||||
if err == nil || !strings.Contains(err.Error(), "missing function body") {
|
if err == nil || !strings.Contains(err.Error(), "func init must have a body") {
|
||||||
t.Fatalf("got unexpected or no error: %v", err)
|
t.Fatalf("got unexpected or no error: %v", err)
|
||||||
}
|
}
|
||||||
if pkg == nil {
|
if pkg == nil {
|
||||||
|
|
|
||||||
|
|
@ -4,4 +4,4 @@
|
||||||
|
|
||||||
package issue20855
|
package issue20855
|
||||||
|
|
||||||
func init() // "missing function body" is a soft error
|
func init() // "func init must have a body" is a soft error
|
||||||
|
|
|
||||||
|
|
@ -425,10 +425,8 @@ func (check *Checker) collectObjects() {
|
||||||
// don't declare init functions in the package scope - they are invisible
|
// don't declare init functions in the package scope - they are invisible
|
||||||
obj.parent = pkg.scope
|
obj.parent = pkg.scope
|
||||||
check.recordDef(d.decl.Name, obj)
|
check.recordDef(d.decl.Name, obj)
|
||||||
// init functions must have a body
|
|
||||||
if d.decl.Body == nil {
|
if d.decl.Body == nil {
|
||||||
// TODO(gri) make this error message consistent with the others above
|
check.softErrorf(obj, MissingInitBody, "func init must have a body")
|
||||||
check.softErrorf(obj, MissingInitBody, "missing function body")
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
check.declare(pkg.scope, d.decl.Name, obj, nopos)
|
check.declare(pkg.scope, d.decl.Name, obj, nopos)
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ type init /* ERROR "cannot declare init" */ struct{}
|
||||||
var _, init /* ERROR "cannot declare init" */ int
|
var _, init /* ERROR "cannot declare init" */ int
|
||||||
|
|
||||||
func init() {}
|
func init() {}
|
||||||
func init /* ERROR "missing function body" */ ()
|
func init /* ERROR "func init must have a body" */ ()
|
||||||
|
|
||||||
func _() { const init = 0 }
|
func _() { const init = 0 }
|
||||||
func _() { type init int }
|
func _() { type init int }
|
||||||
|
|
|
||||||
|
|
@ -6,4 +6,4 @@
|
||||||
|
|
||||||
package p
|
package p
|
||||||
|
|
||||||
func init() // ERROR "missing function body|cannot declare init"
|
func init() // ERROR "func init must have a body|cannot declare init"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue