From b33df7e76ac34951b978123d8494d32e7e494732 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 5 Nov 2013 10:51:13 -0800 Subject: [PATCH] go.tools/go/types: track init cycles through closures R=adonovan CC=golang-dev https://golang.org/cl/21790044 --- go/types/expr.go | 7 +++++-- go/types/stdlib_test.go | 2 +- go/types/testdata/init0.src | 12 ++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/go/types/expr.go b/go/types/expr.go index 066b633da97..0702d36039b 100644 --- a/go/types/expr.go +++ b/go/types/expr.go @@ -969,8 +969,11 @@ func (check *checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind { if sig, ok := check.typ(e.Type, nil, false).(*Signature); ok { x.mode = value x.typ = sig - // TODO(gri) provide correct *declInfo here - check.later(nil, nil, sig, e.Body) + // Anonymous functions are considered part of the + // init expression/func declaration which contains + // them: use the current package-level declaration + // info. + check.later(nil, check.decl, sig, e.Body) } else { check.invalidAST(e.Pos(), "invalid function literal %s", e) goto Error diff --git a/go/types/stdlib_test.go b/go/types/stdlib_test.go index 05a23c0f48a..beee1ef4ad6 100644 --- a/go/types/stdlib_test.go +++ b/go/types/stdlib_test.go @@ -124,7 +124,7 @@ func TestStdtest(t *testing.T) { func TestStdfixed(t *testing.T) { testTestDir(t, filepath.Join(runtime.GOROOT(), "test", "fixedbugs"), - "bug223.go", "bug413.go", "bug459.go", // TODO(gri) complete initialization checks + "bug459.go", // TODO(gri) complete initialization checks "bug248.go", "bug302.go", "bug369.go", // complex test instructions - ignore "issue3924.go", // incorrect test - see issue 6671 "issue4847.go", // TODO(gri) initialization cycle error not found diff --git a/go/types/testdata/init0.src b/go/types/testdata/init0.src index c4d9bfe5f5f..36dbf7dfaac 100644 --- a/go/types/testdata/init0.src +++ b/go/types/testdata/init0.src @@ -44,3 +44,15 @@ var x6, x7 /* ERROR initialization cycle */ = f2() var x8 = x7 func f2() (int, int) { return f3() + f3(), 0 } func f3() int { return x8 } + +// cycles via closures +var x9 /* ERROR initialization cycle */ = func() int { return x9 }() + +var x10 /* ERROR initialization cycle */ = f4() + +func f4() int { + _ = func() { + _ = x10 + } + return 0 +} \ No newline at end of file