blob: 1776313f05cea2de56c2cba1e61b7de4d5635d4b [file] [log] [blame]
Russ Cox78ebe2b2009-09-24 17:54:47 -07001// 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// Test that basic operations on named types are valid
8// and preserve the type.
9
10package main
11
12type Bool bool
13
14type Map map[int]int
Russ Cox565b5dc2010-06-08 18:50:02 -070015
Russ Cox78ebe2b2009-09-24 17:54:47 -070016func (Map) M() {}
17
Russ Cox565b5dc2010-06-08 18:50:02 -070018type Slice []byte
19
20var slice Slice
21
22func asBool(Bool) {}
23func asString(String) {}
24
25type String string
Russ Cox78ebe2b2009-09-24 17:54:47 -070026
27func main() {
28 var (
Russ Cox565b5dc2010-06-08 18:50:02 -070029 b Bool = true
30 i, j int
31 c = make(chan int)
32 m = make(Map)
Russ Cox78ebe2b2009-09-24 17:54:47 -070033 )
34
Russ Cox565b5dc2010-06-08 18:50:02 -070035 asBool(b)
36 asBool(!b)
37 asBool(true)
38 asBool(*&b)
39 asBool(Bool(true))
40 asBool(1 != 2) // ERROR "cannot use.*type bool.*as type Bool"
41 asBool(i < j) // ERROR "cannot use.*type bool.*as type Bool"
Russ Cox78ebe2b2009-09-24 17:54:47 -070042
Russ Cox565b5dc2010-06-08 18:50:02 -070043 _, b = m[2] // ERROR "cannot .* bool.*type Bool"
44 m[2] = 1, b // ERROR "cannot use.*type Bool.*as type bool"
Russ Cox78ebe2b2009-09-24 17:54:47 -070045
Russ Coxf4e76d82011-01-31 18:36:28 -050046 ////TODO(rsc): uncomment when this syntax is valid for receive+check closed
47 //// _, b = <-c // ERROR "cannot .* bool.*type Bool"
48 //// _ = b
Russ Cox78ebe2b2009-09-24 17:54:47 -070049
Russ Cox565b5dc2010-06-08 18:50:02 -070050 var inter interface{}
51 _, b = inter.(Map) // ERROR "cannot .* bool.*type Bool"
52 _ = b
Russ Cox78ebe2b2009-09-24 17:54:47 -070053
Russ Cox565b5dc2010-06-08 18:50:02 -070054 var minter interface {
55 M()
56 }
57 _, b = minter.(Map) // ERROR "cannot .* bool.*type Bool"
58 _ = b
Russ Cox78ebe2b2009-09-24 17:54:47 -070059
Russ Cox565b5dc2010-06-08 18:50:02 -070060 asBool(closed(c)) // ERROR "cannot use.*type bool.*as type Bool"
61 b = closed(c) // ERROR "cannot use.*type bool.*type Bool"
62 _ = b
63
Ian Lance Taylorc23657e2010-09-08 21:03:51 -070064 asString(String(slice)) // ERROR "cannot .*type Slice.*type String"
Russ Cox78ebe2b2009-09-24 17:54:47 -070065}