aboutsummaryrefslogtreecommitdiff
path: root/test/interface
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2010-06-08 18:50:02 -0700
committerRuss Cox <rsc@golang.org>2010-06-08 18:50:02 -0700
commit565b5dc0760baf556f83adf847f578718a1c571f (patch)
treee2dc094314df75a900a88adc71677452545d74ca /test/interface
parent6aaef044698bf9da21e270188f281321de98a391 (diff)
downloadgo-565b5dc0760baf556f83adf847f578718a1c571f.tar.gz
go-565b5dc0760baf556f83adf847f578718a1c571f.zip
gc: new typechecking rules
* Code for assignment, conversions now mirrors spec. * Changed some snprint -> smprint. * Renamed runtime functions to separate interface conversions from type assertions: convT2I, assertI2T, etc. * Correct checking of \U sequences. Fixes #840. Fixes #830. Fixes #778. R=ken2 CC=golang-dev https://golang.org/cl/1303042
Diffstat (limited to 'test/interface')
-rw-r--r--test/interface/explicit.go41
-rw-r--r--test/interface/pointer.go16
-rw-r--r--test/interface/receiver1.go71
3 files changed, 74 insertions, 54 deletions
diff --git a/test/interface/explicit.go b/test/interface/explicit.go
index bd1bd19a96..797cec80e4 100644
--- a/test/interface/explicit.go
+++ b/test/interface/explicit.go
@@ -8,34 +8,45 @@
package main
-type T struct { a int }
+type T struct {
+ a int
+}
+
var t *T
-type I interface { M() }
+type I interface {
+ M()
+}
+
var i I
-type I2 interface { M(); N(); }
+type I2 interface {
+ M()
+ N()
+}
+
var i2 I2
-type E interface { }
+type E interface{}
+
var e E
func main() {
- e = t; // ok
- t = e; // ERROR "need explicit|need type assertion"
+ e = t // ok
+ t = e // ERROR "need explicit|need type assertion"
// neither of these can work,
// because i has an extra method
// that t does not, so i cannot contain a t.
- i = t; // ERROR "missing|incompatible|is not"
- t = i; // ERROR "missing|incompatible|is not"
+ i = t // ERROR "incompatible|missing M method"
+ t = i // ERROR "incompatible|need type assertion"
+
+ i = i2 // ok
+ i2 = i // ERROR "missing N method"
- i = i2; // ok
- i2 = i; // ERROR "need explicit|need type assertion"
-
- i = I(i2); // ok
- i2 = I2(i); // ERROR "need explicit|need type assertion"
+ i = I(i2) // ok
+ i2 = I2(i) // ERROR "missing N method"
- e = E(t); // ok
- t = T(e); // ERROR "need explicit|need type assertion|incompatible"
+ e = E(t) // ok
+ t = T(e) // ERROR "need explicit|need type assertion|incompatible"
}
diff --git a/test/interface/pointer.go b/test/interface/pointer.go
index be24952ffb..e628b558ea 100644
--- a/test/interface/pointer.go
+++ b/test/interface/pointer.go
@@ -9,28 +9,28 @@
package main
type Inst interface {
- Next() *Inst;
+ Next() *Inst
}
type Regexp struct {
- code []Inst;
- start Inst;
+ code []Inst
+ start Inst
}
type Start struct {
- foo *Inst;
+ foo *Inst
}
func (start *Start) Next() *Inst { return nil }
func AddInst(Inst) *Inst {
- print("ok in addinst\n");
+ print("ok in addinst\n")
return nil
}
func main() {
- print("call addinst\n");
- var x Inst = AddInst(new(Start)); // ERROR "illegal|incompatible|is not"
- print("return from addinst\n");
+ print("call addinst\n")
+ var x Inst = AddInst(new(Start)) // ERROR "pointer to interface"
+ print("return from addinst\n")
}
diff --git a/test/interface/receiver1.go b/test/interface/receiver1.go
index 8ce96424e3..51312d0002 100644
--- a/test/interface/receiver1.go
+++ b/test/interface/receiver1.go
@@ -9,41 +9,50 @@
package main
type T int
+
func (t T) V()
func (t *T) P()
-type V interface { V() }
-type P interface { P(); V() }
+type V interface {
+ V()
+}
+type P interface {
+ P()
+ V()
+}
-type S struct { T; }
-type SP struct { *T; }
+type S struct {
+ T
+}
+type SP struct {
+ *T
+}
func main() {
- var t T;
- var v V;
- var p P;
- var s S;
- var sp SP;
-
- v = t;
- p = t; // ERROR "is not|requires a pointer"
- _, _= v, p;
- v = &t;
- p = &t;
- _, _= v, p;
-
- v = s;
- p = s; // ERROR "is not|requires a pointer"
- _, _= v, p;
- v = &s;
- p = &s;
- _, _= v, p;
-
- v = sp;
- p = sp; // no error!
- _, _= v, p;
- v = &sp;
- p = &sp;
- _, _= v, p;
+ var t T
+ var v V
+ var p P
+ var s S
+ var sp SP
+
+ v = t
+ p = t // ERROR "does not implement|requires a pointer"
+ _, _ = v, p
+ v = &t
+ p = &t
+ _, _ = v, p
+
+ v = s
+ p = s // ERROR "does not implement|requires a pointer"
+ _, _ = v, p
+ v = &s
+ p = &s
+ _, _ = v, p
+
+ v = sp
+ p = sp // no error!
+ _, _ = v, p
+ v = &sp
+ p = &sp
+ _, _ = v, p
}
-