github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/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 f() {
    16  L1:
    17  	for {
    18  		if x == 0 {
    19  			break L1
    20  		}
    21  		if x == 1 {
    22  			continue L1
    23  		}
    24  		goto L1
    25  	}
    26  
    27  L2:
    28  	select {
    29  	default:
    30  		if x == 0 {
    31  			break L2
    32  		}
    33  		if x == 1 {
    34  			continue L2 // ERROR "invalid continue label .*L2|continue is not in a loop"
    35  		}
    36  		goto L2
    37  	}
    38  
    39  	for {
    40  		if x == 1 {
    41  			continue L2 // ERROR "invalid continue label .*L2"
    42  		}
    43  	}
    44  
    45  L3:
    46  	switch {
    47  	case x > 10:
    48  		if x == 11 {
    49  			break L3
    50  		}
    51  		if x == 12 {
    52  			continue L3 // ERROR "invalid continue label .*L3|continue is not in a loop"
    53  		}
    54  		goto L3
    55  	}
    56  
    57  L4:
    58  	if true {
    59  		if x == 13 {
    60  			break L4 // ERROR "invalid break label .*L4"
    61  		}
    62  		if x == 14 {
    63  			continue L4 // ERROR "invalid continue label .*L4|continue is not in a loop"
    64  		}
    65  		if x == 15 {
    66  			goto L4
    67  		}
    68  	}
    69  
    70  L5:
    71  	f()
    72  	if x == 16 {
    73  		break L5 // ERROR "invalid break label .*L5"
    74  	}
    75  	if x == 17 {
    76  		continue L5 // ERROR "invalid continue label .*L5|continue is not in a loop"
    77  	}
    78  	if x == 18 {
    79  		goto L5
    80  	}
    81  
    82  	for {
    83  		if x == 19 {
    84  			break L1 // ERROR "invalid break label .*L1"
    85  		}
    86  		if x == 20 {
    87  			continue L1 // ERROR "invalid continue label .*L1"
    88  		}
    89  		if x == 21 {
    90  			goto L1
    91  		}
    92  	}
    93  
    94  	continue // ERROR "continue is not in a loop"
    95  	for {
    96  		continue on // ERROR "continue label not defined: on"
    97  	}
    98  
    99  	break // ERROR "break is not in a loop"
   100  	for {
   101  		break dance // ERROR "break label not defined: dance"
   102  	}
   103  
   104  	for {
   105  		switch x {
   106  		case 1:
   107  			continue
   108  		}
   109  	}
   110  }