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

     1  // run
     2  
     3  // Copyright 2010 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  // Test case for issue 849.
     8  
     9  package main
    10  
    11  type I interface {
    12  	f()
    13  }
    14  
    15  var callee string
    16  var error_ bool
    17  
    18  type T int
    19  
    20  func (t *T) f() { callee = "f" }
    21  func (i *T) g() { callee = "g" }
    22  
    23  // test1 and test2 are the same except that in the interface J
    24  // the entries are swapped. test2 and test3 are the same except
    25  // that in test3 the interface J is declared outside the function.
    26  //
    27  // Error: test2 calls g instead of f
    28  
    29  func test1(x I) {
    30  	type J interface {
    31  		I
    32  		g()
    33  	}
    34  	x.(J).f()
    35  	if callee != "f" {
    36  		println("test1 called", callee)
    37  		error_ = true
    38  	}
    39  }
    40  
    41  func test2(x I) {
    42  	type J interface {
    43  		g()
    44  		I
    45  	}
    46  	x.(J).f()
    47  	if callee != "f" {
    48  		println("test2 called", callee)
    49  		error_ = true
    50  	}
    51  }
    52  
    53  type J interface {
    54  	g()
    55  	I
    56  }
    57  
    58  func test3(x I) {
    59  	x.(J).f()
    60  	if callee != "f" {
    61  		println("test3 called", callee)
    62  		error_ = true
    63  	}
    64  }
    65  
    66  func main() {
    67  	x := new(T)
    68  	test1(x)
    69  	test2(x)
    70  	test3(x)
    71  	if error_ {
    72  		panic("wrong method called")
    73  	}
    74  }
    75  
    76  /*
    77  6g bug286.go && 6l bug286.6 && 6.out
    78  test2 called g
    79  panic: wrong method called
    80  
    81  panic PC=0x24e040
    82  runtime.panic+0x7c /home/gri/go1/src/pkg/runtime/proc.c:1012
    83  	runtime.panic(0x0, 0x24e0a0)
    84  main.main+0xef /home/gri/go1/test/bugs/bug286.go:76
    85  	main.main()
    86  mainstart+0xf /home/gri/go1/src/pkg/runtime/amd64/asm.s:60
    87  	mainstart()
    88  goexit /home/gri/go1/src/pkg/runtime/proc.c:145
    89  	goexit()
    90  */