Rémy Oudompheng | 2ece2f5 | 2012-02-18 22:15:42 +0100 | [diff] [blame] | 1 | // run |
Robert Griesemer | fb279e7 | 2010-06-09 16:05:00 -0700 | [diff] [blame] | 2 | |
Emmanuel Odeke | 53fd522 | 2016-04-10 14:32:26 -0700 | [diff] [blame] | 3 | // Copyright 2010 The Go Authors. All rights reserved. |
Robert Griesemer | fb279e7 | 2010-06-09 16:05:00 -0700 | [diff] [blame] | 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 | |
Robert Griesemer | fb279e7 | 2010-06-09 16:05:00 -0700 | [diff] [blame] | 15 | var callee string |
Russ Cox | 44526cd | 2011-11-01 22:06:05 -0400 | [diff] [blame] | 16 | var error_ bool |
Robert Griesemer | fb279e7 | 2010-06-09 16:05:00 -0700 | [diff] [blame] | 17 | |
| 18 | type T int |
| 19 | |
| 20 | func (t *T) f() { callee = "f" } |
| 21 | func (i *T) g() { callee = "g" } |
| 22 | |
Robert Griesemer | fb279e7 | 2010-06-09 16:05:00 -0700 | [diff] [blame] | 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) |
Russ Cox | 44526cd | 2011-11-01 22:06:05 -0400 | [diff] [blame] | 37 | error_ = true |
Robert Griesemer | fb279e7 | 2010-06-09 16:05:00 -0700 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
Robert Griesemer | fb279e7 | 2010-06-09 16:05:00 -0700 | [diff] [blame] | 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) |
Russ Cox | 44526cd | 2011-11-01 22:06:05 -0400 | [diff] [blame] | 49 | error_ = true |
Robert Griesemer | fb279e7 | 2010-06-09 16:05:00 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Robert Griesemer | fb279e7 | 2010-06-09 16:05:00 -0700 | [diff] [blame] | 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) |
Russ Cox | 44526cd | 2011-11-01 22:06:05 -0400 | [diff] [blame] | 62 | error_ = true |
Robert Griesemer | fb279e7 | 2010-06-09 16:05:00 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | func main() { |
| 67 | x := new(T) |
| 68 | test1(x) |
| 69 | test2(x) |
| 70 | test3(x) |
Russ Cox | 44526cd | 2011-11-01 22:06:05 -0400 | [diff] [blame] | 71 | if error_ { |
Robert Griesemer | fb279e7 | 2010-06-09 16:05:00 -0700 | [diff] [blame] | 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 | */ |