~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to test/interface/explicit.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-04-20 17:36:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110420173648-ifergoxyrm832trd
Tags: upstream-2011.03.07.1
Import upstream version 2011.03.07.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// errchk $G -e $D/$F.go
 
2
 
 
3
// Copyright 2009 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
// Static error messages about interface conversions.
 
8
 
 
9
package main
 
10
 
 
11
type T struct {
 
12
        a int
 
13
}
 
14
 
 
15
var t *T
 
16
 
 
17
type I interface {
 
18
        M()
 
19
}
 
20
 
 
21
var i I
 
22
 
 
23
type I2 interface {
 
24
        M()
 
25
        N()
 
26
}
 
27
 
 
28
var i2 I2
 
29
 
 
30
type E interface{}
 
31
 
 
32
var e E
 
33
 
 
34
func main() {
 
35
        e = t // ok
 
36
        t = e // ERROR "need explicit|need type assertion"
 
37
 
 
38
        // neither of these can work,
 
39
        // because i has an extra method
 
40
        // that t does not, so i cannot contain a t.
 
41
        i = t // ERROR "incompatible|missing M method"
 
42
        t = i // ERROR "incompatible|need type assertion"
 
43
 
 
44
        i = i2 // ok
 
45
        i2 = i // ERROR "incompatible|missing N method"
 
46
 
 
47
        i = I(i2)  // ok
 
48
        i2 = I2(i) // ERROR "invalid|missing N method"
 
49
 
 
50
        e = E(t) // ok
 
51
        t = T(e) // ERROR "need explicit|need type assertion|incompatible"
 
52
}
 
53
 
 
54
type M interface {
 
55
        M()
 
56
}
 
57
 
 
58
var m M
 
59
 
 
60
var _ = m.(int) // ERROR "impossible type assertion"
 
61
 
 
62
type Int int
 
63
 
 
64
func (Int) M(float64) {}
 
65
 
 
66
var _ = m.(Int) // ERROR "impossible type assertion"
 
67
 
 
68
var ii int
 
69
var jj Int
 
70
 
 
71
var m1 M = ii // ERROR "incompatible|missing"
 
72
var m2 M = jj // ERROR "incompatible|wrong type for M method"
 
73
 
 
74
var m3 = M(ii) // ERROR "invalid|missing"
 
75
var m4 = M(jj) // ERROR "invalid|wrong type for M method"