| // Copyright 2013 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| // initialization cycles |
| |
| package init0 |
| |
| // type-checking, not initialization cycles (we don't know the types) |
| // (avoid duplicate errors) |
| var ( |
| x0 /* ERROR cycle */ = x0 |
| |
| x1 /* ERROR cycle */ = y1 |
| y1 = x1 |
| |
| a1 = b1 |
| b1 /* ERROR cycle */ = c1 |
| c1 = d1 |
| d1 = b1 |
| ) |
| |
| // initialization cycles (we know the types) |
| var ( |
| x00 /* ERROR initialization cycle */ int = x00 |
| |
| x2 /* ERROR initialization cycle */ int = y2 |
| y2 = x2 |
| |
| a2 = b2 |
| b2 /* ERROR initialization cycle */ int = c2 |
| c2 = d2 |
| d2 = b2 |
| ) |
| |
| // cycles via struct fields |
| |
| type S1 struct { |
| f int |
| } |
| var x3 /* ERROR initialization cycle */ S1 = S1{x3.f} |
| |
| // cycles via functions |
| |
| var x4 = x5 |
| var x5 /* ERROR initialization cycle */ = f1() |
| func f1() int { return x5*10 } |
| |
| var x6 /* ERROR initialization cycle */ , x7 = f2() |
| var x8 = x7 |
| func f2() (int, int) { return f3() + f3(), 0 } |
| func f3() int { return x8 } |
| |
| // cycles via closures |
| |
| var x9 /* ERROR illegal cycle */ = func() int { return x9 }() |
| |
| var x10 /* ERROR initialization cycle */ = f4() |
| |
| func f4() int { |
| _ = func() { |
| _ = x10 |
| } |
| return 0 |
| } |
| |
| // cycles via method expressions |
| |
| type T1 struct{} |
| |
| func (T1) m() bool { _ = x11; return false } |
| |
| var x11 /* ERROR initialization cycle */ = T1.m(T1{}) |
| |
| // no cycles via method values |
| |
| type T2 struct{} |
| |
| func (T2) m() bool { _ = x12; return false } |
| |
| var t1 T2 |
| var x12 = t1.m |