blob: 44f05153f48e4b4922f8f4926284144e1da0159f [file] [log] [blame]
Rémy Oudompheng2ece2f52012-02-18 22:15:42 +01001// run
Robert Griesemerfb279e72010-06-09 16:05:00 -07002
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
9package main
10
11type I interface {
12 f()
13}
14
Robert Griesemerfb279e72010-06-09 16:05:00 -070015var callee string
Russ Cox44526cd2011-11-01 22:06:05 -040016var error_ bool
Robert Griesemerfb279e72010-06-09 16:05:00 -070017
18type T int
19
20func (t *T) f() { callee = "f" }
21func (i *T) g() { callee = "g" }
22
Robert Griesemerfb279e72010-06-09 16:05:00 -070023// 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
29func 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)
Russ Cox44526cd2011-11-01 22:06:05 -040037 error_ = true
Robert Griesemerfb279e72010-06-09 16:05:00 -070038 }
39}
40
Robert Griesemerfb279e72010-06-09 16:05:00 -070041func 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)
Russ Cox44526cd2011-11-01 22:06:05 -040049 error_ = true
Robert Griesemerfb279e72010-06-09 16:05:00 -070050 }
51}
52
Robert Griesemerfb279e72010-06-09 16:05:00 -070053type J interface {
54 g()
55 I
56}
57
58func test3(x I) {
59 x.(J).f()
60 if callee != "f" {
61 println("test3 called", callee)
Russ Cox44526cd2011-11-01 22:06:05 -040062 error_ = true
Robert Griesemerfb279e72010-06-09 16:05:00 -070063 }
64}
65
66func main() {
67 x := new(T)
68 test1(x)
69 test2(x)
70 test3(x)
Russ Cox44526cd2011-11-01 22:06:05 -040071 if error_ {
Robert Griesemerfb279e72010-06-09 16:05:00 -070072 panic("wrong method called")
73 }
74}
75
76/*
776g bug286.go && 6l bug286.6 && 6.out
78test2 called g
79panic: wrong method called
80
81panic PC=0x24e040
82runtime.panic+0x7c /home/gri/go1/src/pkg/runtime/proc.c:1012
83 runtime.panic(0x0, 0x24e0a0)
84main.main+0xef /home/gri/go1/test/bugs/bug286.go:76
85 main.main()
86mainstart+0xf /home/gri/go1/src/pkg/runtime/amd64/asm.s:60
87 mainstart()
88goexit /home/gri/go1/src/pkg/runtime/proc.c:145
89 goexit()
90*/