...
Run Format

Text file src/go/types/testdata/gotos.src

Documentation: go/types/testdata

     1// Copyright 2011 The Go Authors. All rights reserved.
     2// Use of this source code is governed by a BSD-style
     3// license that can be found in the LICENSE file.
     4
     5// This file is a modified copy of $GOROOT/test/goto.go.
     6
     7package gotos
     8
     9var (
    10	i, n int
    11	x    []int
    12	c    chan int
    13	m    map[int]int
    14	s    string
    15)
    16
    17// goto after declaration okay
    18func _() {
    19	x := 1
    20	goto L
    21L:
    22	_ = x
    23}
    24
    25// goto before declaration okay
    26func _() {
    27	goto L
    28L:
    29	x := 1
    30	_ = x
    31}
    32
    33// goto across declaration not okay
    34func _() {
    35	goto L /* ERROR "goto L jumps over variable declaration at line 36" */
    36	x := 1
    37	_ = x
    38L:
    39}
    40
    41// goto across declaration in inner scope okay
    42func _() {
    43	goto L
    44	{
    45		x := 1
    46		_ = x
    47	}
    48L:
    49}
    50
    51// goto across declaration after inner scope not okay
    52func _() {
    53	goto L /* ERROR "goto L jumps over variable declaration at line 58" */
    54	{
    55		x := 1
    56		_ = x
    57	}
    58	x := 1
    59	_ = x
    60L:
    61}
    62
    63// goto across declaration in reverse okay
    64func _() {
    65L:
    66	x := 1
    67	_ = x
    68	goto L
    69}
    70
    71func _() {
    72L: L1:
    73	x := 1
    74	_ = x
    75	goto L
    76	goto L1
    77}
    78
    79// error shows first offending variable
    80func _() {
    81	goto L /* ERROR "goto L jumps over variable declaration at line 84" */
    82	x := 1
    83	_ = x
    84	y := 1
    85	_ = y
    86L:
    87}
    88
    89// goto not okay even if code path is dead
    90func _() {
    91	goto L /* ERROR "goto L jumps over variable declaration" */
    92	x := 1
    93	_ = x
    94	y := 1
    95	_ = y
    96	return
    97L:
    98}
    99
   100// goto into outer block okay
   101func _() {
   102	{
   103		goto L
   104	}
   105L:
   106}
   107
   108func _() {
   109	{
   110		goto L
   111		goto L1
   112	}
   113L: L1:
   114}
   115
   116// goto backward into outer block okay
   117func _() {
   118L:
   119	{
   120		goto L
   121	}
   122}
   123
   124func _() {
   125L: L1:
   126	{
   127		goto L
   128		goto L1
   129	}
   130}
   131
   132// goto into inner block not okay
   133func _() {
   134	goto L /* ERROR "goto L jumps into block" */
   135	{
   136	L:
   137	}
   138}
   139
   140func _() {
   141	goto L /* ERROR "goto L jumps into block" */
   142	goto L1 /* ERROR "goto L1 jumps into block" */
   143	{
   144	L: L1:
   145	}
   146}
   147
   148// goto backward into inner block still not okay
   149func _() {
   150	{
   151	L:
   152	}
   153	goto L /* ERROR "goto L jumps into block" */
   154}
   155
   156func _() {
   157	{
   158	L: L1:
   159	}
   160	goto L /* ERROR "goto L jumps into block" */
   161	goto L1 /* ERROR "goto L1 jumps into block" */
   162}
   163
   164// error shows first (outermost) offending block
   165func _() {
   166	goto L /* ERROR "goto L jumps into block" */
   167	{
   168		{
   169			{
   170			L:
   171			}
   172		}
   173	}
   174}
   175
   176// error prefers block diagnostic over declaration diagnostic
   177func _() {
   178	goto L /* ERROR "goto L jumps into block" */
   179	x := 1
   180	_ = x
   181	{
   182	L:
   183	}
   184}
   185
   186// many kinds of blocks, all invalid to jump into or among,
   187// but valid to jump out of
   188
   189// if
   190
   191func _() {
   192L:
   193	if true {
   194		goto L
   195	}
   196}
   197
   198func _() {
   199L:
   200	if true {
   201		goto L
   202	} else {
   203	}
   204}
   205
   206func _() {
   207L:
   208	if false {
   209	} else {
   210		goto L
   211	}
   212}
   213
   214func _() {
   215	goto L /* ERROR "goto L jumps into block" */
   216	if true {
   217	L:
   218	}
   219}
   220
   221func _() {
   222	goto L /* ERROR "goto L jumps into block" */
   223	if true {
   224	L:
   225	} else {
   226	}
   227}
   228
   229func _() {
   230	goto L /* ERROR "goto L jumps into block" */
   231	if true {
   232	} else {
   233	L:
   234	}
   235}
   236
   237func _() {
   238	if false {
   239	L:
   240	} else {
   241		goto L /* ERROR "goto L jumps into block" */
   242	}
   243}
   244
   245func _() {
   246	if true {
   247		goto L /* ERROR "goto L jumps into block" */
   248	} else {
   249	L:
   250	}
   251}
   252
   253func _() {
   254	if true {
   255		goto L /* ERROR "goto L jumps into block" */
   256	} else if false {
   257	L:
   258	}
   259}
   260
   261func _() {
   262	if true {
   263		goto L /* ERROR "goto L jumps into block" */
   264	} else if false {
   265	L:
   266	} else {
   267	}
   268}
   269
   270func _() {
   271	if true {
   272		goto L /* ERROR "goto L jumps into block" */
   273	} else if false {
   274	} else {
   275	L:
   276	}
   277}
   278
   279func _() {
   280	if true {
   281		goto L /* ERROR "goto L jumps into block" */
   282	} else {
   283		L:
   284	}
   285}
   286
   287func _() {
   288	if true {
   289		L:
   290	} else {
   291		goto L /* ERROR "goto L jumps into block" */
   292	}
   293}
   294
   295// for
   296
   297func _() {
   298	for {
   299		goto L
   300	}
   301L:
   302}
   303
   304func _() {
   305	for {
   306		goto L
   307	L:
   308	}
   309}
   310
   311func _() {
   312	for {
   313	L:
   314	}
   315	goto L /* ERROR "goto L jumps into block" */
   316}
   317
   318func _() {
   319	for {
   320		goto L
   321	L1:
   322	}
   323L:
   324	goto L1 /* ERROR "goto L1 jumps into block" */
   325}
   326
   327func _() {
   328	for i < n {
   329	L:
   330	}
   331	goto L /* ERROR "goto L jumps into block" */
   332}
   333
   334func _() {
   335	for i = 0; i < n; i++ {
   336	L:
   337	}
   338	goto L /* ERROR "goto L jumps into block" */
   339}
   340
   341func _() {
   342	for i = range x {
   343	L:
   344	}
   345	goto L /* ERROR "goto L jumps into block" */
   346}
   347
   348func _() {
   349	for i = range c {
   350	L:
   351	}
   352	goto L /* ERROR "goto L jumps into block" */
   353}
   354
   355func _() {
   356	for i = range m {
   357	L:
   358	}
   359	goto L /* ERROR "goto L jumps into block" */
   360}
   361
   362func _() {
   363	for i = range s {
   364	L:
   365	}
   366	goto L /* ERROR "goto L jumps into block" */
   367}
   368
   369// switch
   370
   371func _() {
   372L:
   373	switch i {
   374	case 0:
   375		goto L
   376	}
   377}
   378
   379func _() {
   380L:
   381	switch i {
   382	case 0:
   383
   384	default:
   385		goto L
   386	}
   387}
   388
   389func _() {
   390	switch i {
   391	case 0:
   392
   393	default:
   394	L:
   395		goto L
   396	}
   397}
   398
   399func _() {
   400	switch i {
   401	case 0:
   402
   403	default:
   404		goto L
   405	L:
   406	}
   407}
   408
   409func _() {
   410	switch i {
   411	case 0:
   412		goto L
   413	L:
   414		;
   415	default:
   416	}
   417}
   418
   419func _() {
   420	goto L /* ERROR "goto L jumps into block" */
   421	switch i {
   422	case 0:
   423	L:
   424	}
   425}
   426
   427func _() {
   428	goto L /* ERROR "goto L jumps into block" */
   429	switch i {
   430	case 0:
   431	L:
   432		;
   433	default:
   434	}
   435}
   436
   437func _() {
   438	goto L /* ERROR "goto L jumps into block" */
   439	switch i {
   440	case 0:
   441	default:
   442	L:
   443	}
   444}
   445
   446func _() {
   447	switch i {
   448	default:
   449		goto L /* ERROR "goto L jumps into block" */
   450	case 0:
   451	L:
   452	}
   453}
   454
   455func _() {
   456	switch i {
   457	case 0:
   458	L:
   459		;
   460	default:
   461		goto L /* ERROR "goto L jumps into block" */
   462	}
   463}
   464
   465// select
   466// different from switch.  the statement has no implicit block around it.
   467
   468func _() {
   469L:
   470	select {
   471	case <-c:
   472		goto L
   473	}
   474}
   475
   476func _() {
   477L:
   478	select {
   479	case c <- 1:
   480
   481	default:
   482		goto L
   483	}
   484}
   485
   486func _() {
   487	select {
   488	case <-c:
   489
   490	default:
   491	L:
   492		goto L
   493	}
   494}
   495
   496func _() {
   497	select {
   498	case c <- 1:
   499
   500	default:
   501		goto L
   502	L:
   503	}
   504}
   505
   506func _() {
   507	select {
   508	case <-c:
   509		goto L
   510	L:
   511		;
   512	default:
   513	}
   514}
   515
   516func _() {
   517	goto L /* ERROR "goto L jumps into block" */
   518	select {
   519	case c <- 1:
   520	L:
   521	}
   522}
   523
   524func _() {
   525	goto L /* ERROR "goto L jumps into block" */
   526	select {
   527	case c <- 1:
   528	L:
   529		;
   530	default:
   531	}
   532}
   533
   534func _() {
   535	goto L /* ERROR "goto L jumps into block" */
   536	select {
   537	case <-c:
   538	default:
   539	L:
   540	}
   541}
   542
   543func _() {
   544	select {
   545	default:
   546		goto L /* ERROR "goto L jumps into block" */
   547	case <-c:
   548	L:
   549	}
   550}
   551
   552func _() {
   553	select {
   554	case <-c:
   555	L:
   556		;
   557	default:
   558		goto L /* ERROR "goto L jumps into block" */
   559	}
   560}

View as plain text