3 // Copyright 2011 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Verify goto semantics.
10 // Each test is in a separate function just so that if the
11 // compiler stops processing after one error, we don't
24 // goto after declaration okay
32 // goto before declaration okay
40 // goto across declaration not okay
42 goto L // ERROR "goto L jumps over declaration of x at LINE+1|goto jumps over declaration"
43 x := 1 // GCCGO_ERROR "defined here"
48 // goto across declaration in inner scope okay
58 // goto across declaration after inner scope not okay
60 goto L // ERROR "goto L jumps over declaration of x at LINE+5|goto jumps over declaration"
65 x := 1 // GCCGO_ERROR "defined here"
70 // goto across declaration in reverse okay
78 // error shows first offending variable
80 goto L // ERROR "goto L jumps over declaration of y at LINE+3|goto jumps over declaration"
81 x := 1 // GCCGO_ERROR "defined here"
88 // goto not okay even if code path is dead
90 goto L // ERROR "goto L jumps over declaration of y at LINE+3|goto jumps over declaration"
91 x := 1 // GCCGO_ERROR "defined here"
99 // goto into outer block okay
107 // goto backward into outer block okay
115 // goto into inner block not okay
117 goto L // ERROR "goto L jumps into block starting at LINE+1|goto jumps into block"
118 { // GCCGO_ERROR "block starts here"
123 // goto backward into inner block still not okay
125 { // GCCGO_ERROR "block starts here"
128 goto L // ERROR "goto L jumps into block starting at LINE-3|goto jumps into block"
131 // error shows first (outermost) offending block
133 goto L // ERROR "goto L jumps into block starting at LINE+3|goto jumps into block"
136 { // GCCGO_ERROR "block starts here"
143 // error prefers block diagnostic over declaration diagnostic
145 goto L // ERROR "goto L jumps into block starting at LINE+3|goto jumps into block"
148 { // GCCGO_ERROR "block starts here"
153 // many kinds of blocks, all invalid to jump into or among,
154 // but valid to jump out of
182 goto L // ERROR "goto L jumps into block starting at LINE+1|goto jumps into block"
183 if true { // GCCGO_ERROR "block starts here"
189 goto L // ERROR "goto L jumps into block starting at LINE+1|goto jumps into block"
190 if true { // GCCGO_ERROR "block starts here"
197 goto L // ERROR "goto L jumps into block starting at LINE+2|goto jumps into block"
199 } else { // GCCGO_ERROR "block starts here"
205 if false { // GCCGO_ERROR "block starts here"
208 goto L // ERROR "goto L jumps into block starting at LINE-3|goto jumps into block"
214 goto L // ERROR "goto L jumps into block starting at LINE+1|goto jumps into block"
215 } else { // GCCGO_ERROR "block starts here"
222 goto L // ERROR "goto L jumps into block starting at LINE+1|goto jumps into block"
223 } else if false { // GCCGO_ERROR "block starts here"
230 goto L // ERROR "goto L jumps into block starting at LINE+1|goto jumps into block"
231 } else if false { // GCCGO_ERROR "block starts here"
238 // This one is tricky. There is an implicit scope
239 // starting at the second if statement, and it contains
240 // the final else, so the outermost offending scope
241 // really is LINE+1 (like in the previous test),
242 // even though it looks like it might be LINE+3 instead.
244 goto L // ERROR "goto L jumps into block starting at LINE+2|goto jumps into block"
246 } else { // GCCGO_ERROR "block starts here"
251 /* Want to enable these tests but gofmt mangles them. Issue 1972.
254 // This one is okay, because the else is in the
255 // implicit whole-if block and has no inner block
256 // (no { }) around it.
265 if true { //// GCCGO_ERROR "block starts here"
268 goto L //// ERROR "goto L jumps into block starting at LINE-3|goto jumps into block"
290 for { // GCCGO_ERROR "block starts here"
293 goto L // ERROR "goto L jumps into block starting at LINE-3|goto jumps into block"
297 for { // GCCGO_ERROR "block starts here"
302 goto L1 // ERROR "goto L1 jumps into block starting at LINE-5|goto jumps into block"
306 for i < n { // GCCGO_ERROR "block starts here"
309 goto L // ERROR "goto L jumps into block starting at LINE-3|goto jumps into block"
313 for i = 0; i < n; i++ { // GCCGO_ERROR "block starts here"
316 goto L // ERROR "goto L jumps into block starting at LINE-3|goto jumps into block"
320 for i = range x { // GCCGO_ERROR "block starts here"
323 goto L // ERROR "goto L jumps into block starting at LINE-3|goto jumps into block"
327 for i = range c { // GCCGO_ERROR "block starts here"
330 goto L // ERROR "goto L jumps into block starting at LINE-3|goto jumps into block"
334 for i = range m { // GCCGO_ERROR "block starts here"
337 goto L // ERROR "goto L jumps into block starting at LINE-3|goto jumps into block"
341 for i = range s { // GCCGO_ERROR "block starts here"
344 goto L // ERROR "goto L jumps into block starting at LINE-3|goto jumps into block"
398 goto L // ERROR "goto L jumps into block starting at LINE+2|goto jumps into block"
401 L: // GCCGO_ERROR "block starts here"
406 goto L // ERROR "goto L jumps into block starting at LINE+2|goto jumps into block"
409 L: // GCCGO_ERROR "block starts here"
416 goto L // ERROR "goto L jumps into block starting at LINE+3|goto jumps into block"
420 L: // GCCGO_ERROR "block starts here"
427 goto L // ERROR "goto L jumps into block starting at LINE+1|goto jumps into block"
429 L: // GCCGO_ERROR "block starts here"
436 L: // GCCGO_ERROR "block starts here"
439 goto L // ERROR "goto L jumps into block starting at LINE-4|goto jumps into block"
444 // different from switch. the statement has no implicit block around it.
495 goto L // ERROR "goto L jumps into block starting at LINE+2|goto jumps into block"
498 L: // GCCGO_ERROR "block starts here"
503 goto L // ERROR "goto L jumps into block starting at LINE+2|goto jumps into block"
506 L: // GCCGO_ERROR "block starts here"
513 goto L // ERROR "goto L jumps into block starting at LINE+3|goto jumps into block"
517 L: // GCCGO_ERROR "block starts here"
524 goto L // ERROR "goto L jumps into block starting at LINE+1|goto jumps into block"
526 L: // GCCGO_ERROR "block starts here"
533 L: // GCCGO_ERROR "block starts here"
536 goto L // ERROR "goto L jumps into block starting at LINE-4|goto jumps into block"