diff --git a/go/types/expr.go b/go/types/expr.go index 066b633da9..0702d36039 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 05a23c0f48..beee1ef4ad 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 c4d9bfe5f5..36dbf7dfaa 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