Repository: goplus/gop
Pull request #1428 (2023-09-26 11:03:56)

classfile: getFields allow defining type before var


Patch:
diff --git a/cl/classfile.go b/cl/classfile.go
index 3e1ad51f..ff1a49d1 100644
--- a/cl/classfile.go
+++ b/cl/classfile.go
@@ -140,20 +140,15 @@ func getStringConst(spx *gox.PkgRef, name string) string {
 }
 
 func getFields(f *ast.File) (specs []ast.Spec) {
-	decls := f.Decls
-	i, n := 0, len(decls)
-	for i < n {
-		g, ok := decls[i].(*ast.GenDecl)
-		if !ok {
-			break
-		}
-		if g.Tok != token.IMPORT && g.Tok != token.CONST {
+	for _, decl := range f.Decls {
+		if g, ok := decl.(*ast.GenDecl); ok {
 			if g.Tok == token.VAR {
 				specs, g.Specs = g.Specs, nil
+				return
 			}
-			break
+			continue
 		}
-		i++ // skip import/const, if any
+		break
 	}
 	return
 }


Test patch:
diff --git a/cl/builtin_test.go b/cl/builtin_test.go
index dff76851..5721ecf9 100644
--- a/cl/builtin_test.go
+++ b/cl/builtin_test.go
@@ -112,6 +112,13 @@ func TestClRangeStmt(t *testing.T) {
 
 // -----------------------------------------------------------------------------
 
+func TestGetStringConst(t *testing.T) {
+	spx := &gox.PkgRef{Types: types.NewPackage("", "foo")}
+	if v := getStringConst(spx, "unknown"); v != "" {
+		t.Fatal("getStringConst:", v)
+	}
+}
+
 func TestGmxSettings(t *testing.T) {
 	pkg := gox.NewPackage("", "foo", goxConf)
 	gmx := newGmx(nil, pkg, "main.t2gmx", &ast.File{IsProj: true}, &Config{
diff --git a/cl/error_msg_test.go b/cl/error_msg_test.go
index 1a1f049a..9a52371c 100644
--- a/cl/error_msg_test.go
+++ b/cl/error_msg_test.go
@@ -33,7 +33,15 @@ func codeErrorTest(t *testing.T, msg, src string) {
 	codeErrorTestEx(t, "main", "bar.gop", msg, src)
 }
 
+func codeErrorTest2(t *testing.T, msg1, msg2, src string) {
+	codeErrorTestEx2(t, "main", "bar.gop", msg1, msg2, src)
+}
+
 func codeErrorTestEx(t *testing.T, pkgname, filename, msg, src string) {
+	codeErrorTestEx2(t, pkgname, filename, msg, "", src)
+}
+
+func codeErrorTestEx2(t *testing.T, pkgname, filename, msg1, msg2, src string) {
 	fs := memfs.SingleFile("/foo", filename, src)
 	pkgs, err := parser.ParseFSDir(gblFset, fs, "/foo", parser.Config{})
 	if err != nil {
@@ -49,8 +57,8 @@ func codeErrorTestEx(t *testing.T, pkgname, filename, msg, src string) {
 	if err == nil {
 		t.Fatal("no error?")
 	}
-	if ret := err.Error(); ret != msg {
-		t.Fatalf("\nError: \"%s\"\nExpected: \"%s\"\n", ret, msg)
+	if ret := err.Error(); ret != msg1 && ret != msg2 {
+		t.Fatalf("\nError: \"%s\"\nExpected: \"%s\"\n", ret, msg1)
 	}
 }
 
@@ -850,9 +858,11 @@ type A struct {
 }
 
 func TestErrImportPkg(t *testing.T) {
-	codeErrorTest(t,
+	root := filepath.Join(runtime.GOROOT(), "src", "fmt2")
+	codeErrorTest2(t,
 		fmt.Sprintf(`./bar.gop:3:2: package fmt2 is not in GOROOT (%v)
-`, filepath.Join(runtime.GOROOT(), "src", "fmt2")), `
+`, root), fmt.Sprintf(`./bar.gop:3:2: package fmt2 is not in std (%v)
+`, root), `
 import (
 	"fmt2"
 )
diff --git a/cl/typeparams_test.go b/cl/typeparams_test.go
index 10463024..63b8b8dc 100644
--- a/cl/typeparams_test.go
+++ b/cl/typeparams_test.go
@@ -260,8 +260,10 @@ func TestTypeParamsErrorMatch(t *testing.T) {
 	switch runtime.Version()[:6] {
 	case "go1.18", "go1.19":
 		msg = `./b.gop:2:5: T does not match ~[]E`
-	default:
+	case "go1.20":
 		msg = `./b.gop:2:5: int does not match ~[]E`
+	default:
+		msg = `./b.gop:2:5: T (type int) does not satisfy interface{interface{~[]E}}`
 	}
 	mixedErrorTest(t, msg, `
 package main