]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: improve error message for init without body
authorMark Freeman <mark@golang.org>
Fri, 4 Apr 2025 17:44:38 +0000 (10:44 -0700)
committerGopher Robot <gobot@golang.org>
Mon, 19 May 2025 18:00:10 +0000 (11:00 -0700)
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>
src/cmd/compile/internal/types2/resolver.go
src/go/internal/srcimporter/srcimporter_test.go
src/go/internal/srcimporter/testdata/issue20855/issue20855.go
src/go/types/resolver.go
src/internal/types/testdata/check/decls0.go
test/fixedbugs/issue3705.go

index b9ece5e69494df55220f2883f2e89ce436afdf85..9d8769b96f7c1defad785b52a4dc084ec7f6bd11 100644 (file)
@@ -436,10 +436,8 @@ func (check *Checker) collectObjects() {
                                        if name == "init" {
                                                obj.parent = pkg.scope
                                                check.recordDef(s.Name, obj)
-                                               // init functions must have a body
                                                if s.Body == nil {
-                                                       // TODO(gri) make this error message consistent with the others above
-                                                       check.softErrorf(obj.pos, MissingInitBody, "missing function body")
+                                                       check.softErrorf(obj.pos, MissingInitBody, "func init must have a body")
                                                }
                                        } else {
                                                check.declare(pkg.scope, s.Name, obj, nopos)
index 5adb8831a940d23df722bba15cf3f7c502db2320..ce1e5972866e1faaf64dd94e24de08e8791544bb 100644 (file)
@@ -192,7 +192,7 @@ func TestIssue20855(t *testing.T) {
        testenv.MustHaveSource(t)
 
        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)
        }
        if pkg == nil {
index d55448b44cedb0e2379b8c00799479c08fbacc8b..1c57a7c31b40d46bea53f934c2a4ccd26e7250ac 100644 (file)
@@ -4,4 +4,4 @@
 
 package issue20855
 
-func init() // "missing function body" is a soft error
+func init() // "func init must have a body" is a soft error
index f11a510c1f235060f8ea8696bdd134ec59c8d1e0..dcf863b029feb22bb4a2fd1790b6774993daca46 100644 (file)
@@ -425,10 +425,8 @@ func (check *Checker) collectObjects() {
                                                // don't declare init functions in the package scope - they are invisible
                                                obj.parent = pkg.scope
                                                check.recordDef(d.decl.Name, obj)
-                                               // init functions must have a body
                                                if d.decl.Body == nil {
-                                                       // TODO(gri) make this error message consistent with the others above
-                                                       check.softErrorf(obj, MissingInitBody, "missing function body")
+                                                       check.softErrorf(obj, MissingInitBody, "func init must have a body")
                                                }
                                        } else {
                                                check.declare(pkg.scope, d.decl.Name, obj, nopos)
index f9b0849dad7744db38c7a7db3298ff049a1bde9b..f5345135db4193ea7369d4da9e77479008e9733d 100644 (file)
@@ -43,7 +43,7 @@ type init /* ERROR "cannot declare init" */ struct{}
 var _, init /* ERROR "cannot declare init" */ int
 
 func init() {}
-func init /* ERROR "missing function body" */ ()
+func init /* ERROR "func init must have a body" */ ()
 
 func _() { const init = 0 }
 func _() { type init int }
index ed0a193dcfbb54579abf9b5500734ddd0a9cfedf..b75094288fa5762667844857e278bfa180199c42 100644 (file)
@@ -6,4 +6,4 @@
 
 package p
 
-func init() // ERROR "missing function body|cannot declare init"
+func init() // ERROR "func init must have a body|cannot declare init"