| 1 |
// Copyright 2011 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"./p" |
| 9 |
) |
| 10 |
|
| 11 |
type Exported interface { |
| 12 |
private() |
| 13 |
} |
| 14 |
|
| 15 |
type Implementation struct{} |
| 16 |
|
| 17 |
func (p *Implementation) private() {} |
| 18 |
|
| 19 |
|
| 20 |
func main() { |
| 21 |
// nothing unusual here |
| 22 |
var x Exported |
| 23 |
x = new(Implementation) |
| 24 |
x.private() // main.Implementation.private() |
| 25 |
|
| 26 |
// same here - should be and is legal |
| 27 |
var px p.Exported |
| 28 |
px = p.X |
| 29 |
|
| 30 |
// this assignment is correctly illegal: |
| 31 |
// px.private undefined (cannot refer to unexported field or method private) |
| 32 |
// px.private() |
| 33 |
|
| 34 |
// this assignment is correctly illegal: |
| 35 |
// *Implementation does not implement p.Exported (missing p.private method) |
| 36 |
// px = new(Implementation) |
| 37 |
|
| 38 |
// this assignment is correctly illegal: |
| 39 |
// p.Exported does not implement Exported (missing private method) |
| 40 |
// x = px |
| 41 |
|
| 42 |
// this assignment unexpectedly compiles and then executes |
| 43 |
defer func() { |
| 44 |
recover() |
| 45 |
}() |
| 46 |
x = px.(Exported) |
| 47 |
|
| 48 |
println("should not get this far") |
| 49 |
|
| 50 |
// this is a legitimate call, but because of the previous assignment, |
| 51 |
// it invokes the method private in p! |
| 52 |
x.private() // p.Implementation.private() |
| 53 |
} |