blob: b6a582fffba8700287add0f3f9bf01f3c868ced2 [file] [log] [blame]
Russ Coxa2a7d472010-06-09 11:00:55 -07001// errchk $G -e $D/$F.go
Russ Coxe5124812009-01-08 18:06:06 -08002
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
Russ Coxc7d30bc2009-05-12 16:09:47 -07007// Static error messages about interface conversions.
8
Russ Coxe5124812009-01-08 18:06:06 -08009package main
10
Russ Cox565b5dc2010-06-08 18:50:02 -070011type T struct {
12 a int
13}
14
Russ Coxe5124812009-01-08 18:06:06 -080015var t *T
16
Russ Cox565b5dc2010-06-08 18:50:02 -070017type I interface {
18 M()
19}
20
Russ Coxe5124812009-01-08 18:06:06 -080021var i I
22
Russ Cox565b5dc2010-06-08 18:50:02 -070023type I2 interface {
24 M()
25 N()
26}
27
Russ Coxd436a702009-07-06 17:20:48 -070028var i2 I2
Russ Cox49e20872009-02-11 17:55:16 -080029
Russ Cox565b5dc2010-06-08 18:50:02 -070030type E interface{}
31
Russ Coxd436a702009-07-06 17:20:48 -070032var e E
Russ Cox49e20872009-02-11 17:55:16 -080033
Russ Coxe5124812009-01-08 18:06:06 -080034func main() {
Russ Cox565b5dc2010-06-08 18:50:02 -070035 e = t // ok
36 t = e // ERROR "need explicit|need type assertion"
Russ Cox49e20872009-02-11 17:55:16 -080037
Russ Coxe5124812009-01-08 18:06:06 -080038 // neither of these can work,
39 // because i has an extra method
40 // that t does not, so i cannot contain a t.
Russ Cox565b5dc2010-06-08 18:50:02 -070041 i = t // ERROR "incompatible|missing M method"
42 t = i // ERROR "incompatible|need type assertion"
Russ Cox49e20872009-02-11 17:55:16 -080043
Russ Cox565b5dc2010-06-08 18:50:02 -070044 i = i2 // ok
Ian Lance Taylor2d8433a2010-09-08 21:03:24 -070045 i2 = i // ERROR "incompatible|missing N method"
Russ Coxd436a702009-07-06 17:20:48 -070046
Russ Cox565b5dc2010-06-08 18:50:02 -070047 i = I(i2) // ok
Ian Lance Taylor2d8433a2010-09-08 21:03:24 -070048 i2 = I2(i) // ERROR "invalid|missing N method"
Russ Cox565b5dc2010-06-08 18:50:02 -070049
50 e = E(t) // ok
51 t = T(e) // ERROR "need explicit|need type assertion|incompatible"
Russ Coxe5124812009-01-08 18:06:06 -080052}
Russ Coxa2a7d472010-06-09 11:00:55 -070053
Russ Coxf2b5a072011-01-19 23:09:00 -050054type M interface {
55 M()
56}
57
Russ Coxa2a7d472010-06-09 11:00:55 -070058var m M
59
Russ Coxf2b5a072011-01-19 23:09:00 -050060var _ = m.(int) // ERROR "impossible type assertion"
Russ Coxa2a7d472010-06-09 11:00:55 -070061
62type Int int
Russ Coxa2a7d472010-06-09 11:00:55 -070063
Russ Coxf2b5a072011-01-19 23:09:00 -050064func (Int) M(float64) {}
65
66var _ = m.(Int) // ERROR "impossible type assertion"
Russ Coxa2a7d472010-06-09 11:00:55 -070067
68var ii int
69var jj Int
70
Russ Coxf2b5a072011-01-19 23:09:00 -050071var m1 M = ii // ERROR "incompatible|missing"
72var m2 M = jj // ERROR "incompatible|wrong type for M method"
Russ Coxa2a7d472010-06-09 11:00:55 -070073
Russ Coxf2b5a072011-01-19 23:09:00 -050074var m3 = M(ii) // ERROR "invalid|missing"
75var m4 = M(jj) // ERROR "invalid|wrong type for M method"