gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/go_generics/tests/all_stmts/input.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tests
    16  
    17  import (
    18  	"sync"
    19  )
    20  
    21  type T int
    22  
    23  func h(T) {
    24  }
    25  
    26  type s struct {
    27  	a, b int
    28  	c    []int
    29  }
    30  
    31  func g(T) *s {
    32  	return &s{}
    33  }
    34  
    35  func f() (T, []int) {
    36  	// Branch.
    37  	goto T
    38  	goto R
    39  
    40  	// Labeled.
    41  T:
    42  	_ = T(0)
    43  
    44  	// Empty.
    45  R:
    46  	;
    47  
    48  	// Assignment with definition.
    49  	a, b, c := T(1), T(2), T(3)
    50  	_, _, _ = a, b, c
    51  
    52  	// Assignment without definition.
    53  	g(T(0)).a, g(T(1)).b, c = int(T(1)), int(T(2)), T(3)
    54  	_, _, _ = a, b, c
    55  
    56  	// Block.
    57  	{
    58  		var T T
    59  		T = 0
    60  		_ = T
    61  	}
    62  
    63  	// Declarations.
    64  	type Type T
    65  	const Const T = 10
    66  	var g1 func(T, int, ...T) (int, T)
    67  	var v T
    68  	var w = T(0)
    69  	{
    70  		var T struct {
    71  			f []T
    72  		}
    73  		_ = T
    74  	}
    75  
    76  	// Defer.
    77  	defer g1(T(0), 1)
    78  
    79  	// Expression.
    80  	h(v + w + T(1))
    81  
    82  	// For statements.
    83  	for i := T(0); i < T(10); i++ {
    84  		var T func(int) T
    85  		v := T(0)
    86  		_ = v
    87  	}
    88  
    89  	for {
    90  		var T func(int) T
    91  		v := T(0)
    92  		_ = v
    93  	}
    94  
    95  	// Go.
    96  	go g1(T(0), 1)
    97  
    98  	// If statements.
    99  	if a != T(1) {
   100  		var T func(int) T
   101  		v := T(0)
   102  		_ = v
   103  	}
   104  
   105  	if a := T(0); a != T(1) {
   106  		var T func(int) T
   107  		v := T(0)
   108  		_ = v
   109  	}
   110  
   111  	if a := T(0); a != T(1) {
   112  		var T func(int) T
   113  		v := T(0)
   114  		_ = v
   115  	} else if b := T(0); b != T(1) {
   116  		var T func(int) T
   117  		v := T(0)
   118  		_ = v
   119  	} else if T := T(0); T != 1 {
   120  		T++
   121  		_ = T
   122  	} else {
   123  		T--
   124  		_ = T
   125  	}
   126  
   127  	if a := T(0); a != T(1) {
   128  		var T func(int) T
   129  		v := T(0)
   130  		_ = v
   131  	} else {
   132  		var T func(int) T
   133  		v := T(0)
   134  		_ = v
   135  	}
   136  
   137  	// Inc/Dec statements.
   138  	(*(*T)(nil))++
   139  	(*(*T)(nil))--
   140  
   141  	// Range statements.
   142  	for g(T(0)).a, g(T(1)).b = range g(T(10)).c {
   143  		var d T
   144  		_ = d
   145  	}
   146  
   147  	for T, b := range g(T(10)).c {
   148  		_ = T
   149  		_ = b
   150  	}
   151  
   152  	// Select statement.
   153  	{
   154  		var fch func(T) chan int
   155  
   156  		select {
   157  		case <-fch(T(30)):
   158  			var T T
   159  			T = 0
   160  			_ = T
   161  		default:
   162  			var T T
   163  			T = 0
   164  			_ = T
   165  		case T := <-fch(T(30)):
   166  			T = 0
   167  			_ = T
   168  		case g(T(0)).a = <-fch(T(30)):
   169  			var T T
   170  			T = 0
   171  			_ = T
   172  		case fch(T(30)) <- int(T(0)):
   173  			var T T
   174  			T = 0
   175  			_ = T
   176  		}
   177  	}
   178  
   179  	// Send statements.
   180  	{
   181  		var ch chan T
   182  		var fch func(T) chan int
   183  
   184  		ch <- T(0)
   185  		fch(T(1)) <- g(T(10)).a
   186  	}
   187  
   188  	// Switch statements.
   189  	{
   190  		var a T
   191  		var b int
   192  		switch {
   193  		case a == T(0):
   194  			var T T
   195  			T = 0
   196  			_ = T
   197  		case a < T(0), b < g(T(10)).a:
   198  			var T T
   199  			T = 0
   200  			_ = T
   201  		default:
   202  			var T T
   203  			T = 0
   204  			_ = T
   205  		}
   206  	}
   207  
   208  	switch T(g(T(10)).a) {
   209  	case T(0):
   210  		var T T
   211  		T = 0
   212  		_ = T
   213  	case T(1), T(g(T(10)).a):
   214  		var T T
   215  		T = 0
   216  		_ = T
   217  	default:
   218  		var T T
   219  		T = 0
   220  		_ = T
   221  	}
   222  
   223  	switch b := g(T(10)); T(b.a) + T(10) {
   224  	case T(0):
   225  		var T T
   226  		T = 0
   227  		_ = T
   228  	case T(1), T(g(T(10)).a):
   229  		var T T
   230  		T = 0
   231  		_ = T
   232  	default:
   233  		var T T
   234  		T = 0
   235  		_ = T
   236  	}
   237  
   238  	// Type switch statements.
   239  	{
   240  		var interfaceFunc func(T) any
   241  
   242  		switch interfaceFunc(T(0)).(type) {
   243  		case *T, T, int:
   244  			var T T
   245  			T = 0
   246  			_ = T
   247  		case sync.Mutex, **T:
   248  			var T T
   249  			T = 0
   250  			_ = T
   251  		default:
   252  			var T T
   253  			T = 0
   254  			_ = T
   255  		}
   256  
   257  		switch x := interfaceFunc(T(0)).(type) {
   258  		case *T, T, int:
   259  			var T T
   260  			T = 0
   261  			_ = T
   262  			_ = x
   263  		case sync.Mutex, **T:
   264  			var T T
   265  			T = 0
   266  			_ = T
   267  		default:
   268  			var T T
   269  			T = 0
   270  			_ = T
   271  		}
   272  
   273  		switch t := T(0); x := interfaceFunc(T(0) + t).(type) {
   274  		case *T, T, int:
   275  			var T T
   276  			T = 0
   277  			_ = T
   278  			_ = x
   279  		case sync.Mutex, **T:
   280  			var T T
   281  			T = 0
   282  			_ = T
   283  		default:
   284  			var T T
   285  			T = 0
   286  			_ = T
   287  		}
   288  	}
   289  
   290  	// Return statement.
   291  	return T(10), g(T(11)).c
   292  }