modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/label1.go (about)

     1  // errorcheck
     2  
     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.
     6  
     7  // Verify that erroneous labels are caught by the compiler.
     8  // This set is caught by pass 2. That's why this file is label1.go.
     9  // Does not compile.
    10  
    11  package main
    12  
    13  var x int
    14  
    15  func f1() {
    16  	switch x {
    17  	case 1:
    18  		continue // ERROR "continue is not in a loop$"
    19  	}
    20  	select {
    21  	default:
    22  		continue // ERROR "continue is not in a loop$"
    23  	}
    24  
    25  }
    26  
    27  func f2() {
    28  L1:
    29  	for {
    30  		if x == 0 {
    31  			break L1
    32  		}
    33  		if x == 1 {
    34  			continue L1
    35  		}
    36  		goto L1
    37  	}
    38  
    39  L2:
    40  	select {
    41  	default:
    42  		if x == 0 {
    43  			break L2
    44  		}
    45  		if x == 1 {
    46  			continue L2 // ERROR "invalid continue label .*L2|continue is not in a loop$"
    47  		}
    48  		goto L2
    49  	}
    50  
    51  	for {
    52  		if x == 1 {
    53  			continue L2 // ERROR "invalid continue label .*L2"
    54  		}
    55  	}
    56  
    57  L3:
    58  	switch {
    59  	case x > 10:
    60  		if x == 11 {
    61  			break L3
    62  		}
    63  		if x == 12 {
    64  			continue L3 // ERROR "invalid continue label .*L3|continue is not in a loop$"
    65  		}
    66  		goto L3
    67  	}
    68  
    69  L4:
    70  	if true {
    71  		if x == 13 {
    72  			break L4 // ERROR "invalid break label .*L4"
    73  		}
    74  		if x == 14 {
    75  			continue L4 // ERROR "invalid continue label .*L4|continue is not in a loop$"
    76  		}
    77  		if x == 15 {
    78  			goto L4
    79  		}
    80  	}
    81  
    82  L5:
    83  	f2()
    84  	if x == 16 {
    85  		break L5 // ERROR "invalid break label .*L5"
    86  	}
    87  	if x == 17 {
    88  		continue L5 // ERROR "invalid continue label .*L5|continue is not in a loop$"
    89  	}
    90  	if x == 18 {
    91  		goto L5
    92  	}
    93  
    94  	for {
    95  		if x == 19 {
    96  			break L1 // ERROR "invalid break label .*L1"
    97  		}
    98  		if x == 20 {
    99  			continue L1 // ERROR "invalid continue label .*L1"
   100  		}
   101  		if x == 21 {
   102  			goto L1
   103  		}
   104  	}
   105  
   106  	continue // ERROR "continue is not in a loop$"
   107  	for {
   108  		continue on // ERROR "continue label not defined: on"
   109  	}
   110  
   111  	break // ERROR "break is not in a loop, switch, or select"
   112  	for {
   113  		break dance // ERROR "break label not defined: dance"
   114  	}
   115  
   116  	for {
   117  		switch x {
   118  		case 1:
   119  			continue
   120  		}
   121  	}
   122  }