| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2017 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 |
// This test makes sure that itabs are unique. |
| 8 |
// More explicitly, we require that only one itab structure exists for the pair of |
| 9 |
// a given compile-time interface type and underlying concrete type. |
| 10 |
// Ensuring this invariant enables fixes for 18492 (improve type switch code). |
| 11 |
|
| 12 |
package main |
| 13 |
|
| 14 |
type I interface { |
| 15 |
M() |
| 16 |
} |
| 17 |
type J interface { |
| 18 |
M() |
| 19 |
} |
| 20 |
|
| 21 |
type T struct{} |
| 22 |
|
| 23 |
func (*T) M() {} |
| 24 |
|
| 25 |
func main() { |
| 26 |
test1() |
| 27 |
test2() |
| 28 |
} |
| 29 |
|
| 30 |
func test1() { |
| 31 |
t := new(T) |
| 32 |
var i1, i2 I |
| 33 |
var j interface { |
| 34 |
M() |
| 35 |
} |
| 36 |
i1 = t |
| 37 |
j = t |
| 38 |
i2 = j |
| 39 |
if i1 != i2 { |
| 40 |
panic("interfaces not equal") |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func test2() { |
| 45 |
t := new(T) |
| 46 |
i1 := (I)(t) |
| 47 |
i2 := (I)((interface { |
| 48 |
M() |
| 49 |
})((J)(t))) |
| 50 |
if i1 != i2 { |
| 51 |
panic("interfaces not equal") |
| 52 |
} |
| 53 |
} |