From: Rob Findley Date: Thu, 13 Apr 2023 14:22:46 +0000 (-0400) Subject: go/types,types2: add a test for const initializer panic X-Git-Tag: go1.21rc1~912 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1312d9e6da8d5d41bf8f2238c166deb1c5db10c3;p=gostls13.git go/types,types2: add a test for const initializer panic Updates #59603 Change-Id: Iff99f45a72a259b57b2ebbc6c0f9ed710add3ae3 Reviewed-on: https://go-review.googlesource.com/c/go/+/484376 Run-TryBot: Robert Findley TryBot-Result: Gopher Robot Reviewed-by: Robert Griesemer --- diff --git a/src/cmd/compile/internal/types2/api_test.go b/src/cmd/compile/internal/types2/api_test.go index 41ee641f59..e824f56fae 100644 --- a/src/cmd/compile/internal/types2/api_test.go +++ b/src/cmd/compile/internal/types2/api_test.go @@ -1564,6 +1564,33 @@ var _ = a.C2 makePkg("main", mainSrc) // don't crash when type-checking this package } +func TestIssue59603(t *testing.T) { + imports := make(testImporter) + conf := Config{ + Error: func(err error) { t.Log(err) }, // don't exit after first error + Importer: imports, + } + makePkg := func(path, src string) { + f := mustParse(path, src) + pkg, _ := conf.Check(path, []*syntax.File{f}, nil) // errors logged via conf.Error + imports[path] = pkg + } + + const libSrc = ` +package a +const C = foo +` + + const mainSrc = ` +package main +import "a" +const _ = a.C +` + + makePkg("a", libSrc) + makePkg("main", mainSrc) // don't crash when type-checking this package +} + func TestLookupFieldOrMethodOnNil(t *testing.T) { // LookupFieldOrMethod on a nil type is expected to produce a run-time panic. defer func() { diff --git a/src/go/types/api_test.go b/src/go/types/api_test.go index 7cfad422e9..7a8c63a43b 100644 --- a/src/go/types/api_test.go +++ b/src/go/types/api_test.go @@ -1562,6 +1562,34 @@ var _ = a.C2 makePkg("main", mainSrc) // don't crash when type-checking this package } +func TestIssue59603(t *testing.T) { + fset := token.NewFileSet() + imports := make(testImporter) + conf := Config{ + Error: func(err error) { t.Log(err) }, // don't exit after first error + Importer: imports, + } + makePkg := func(path, src string) { + f := mustParse(fset, path, src) + pkg, _ := conf.Check(path, fset, []*ast.File{f}, nil) // errors logged via conf.Error + imports[path] = pkg + } + + const libSrc = ` +package a +const C = foo +` + + const mainSrc = ` +package main +import "a" +const _ = a.C +` + + makePkg("a", libSrc) + makePkg("main", mainSrc) // don't crash when type-checking this package +} + func TestLookupFieldOrMethodOnNil(t *testing.T) { // LookupFieldOrMethod on a nil type is expected to produce a run-time panic. defer func() {