Russ Cox | 78ebe2b | 2009-09-24 17:54:47 -0700 | [diff] [blame] | 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 | // Test that basic operations on named types are valid |
| 8 | // and preserve the type. |
| 9 | |
| 10 | package main |
| 11 | |
| 12 | type Bool bool |
| 13 | |
| 14 | type Map map[int]int |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 15 | |
Russ Cox | 78ebe2b | 2009-09-24 17:54:47 -0700 | [diff] [blame] | 16 | func (Map) M() {} |
| 17 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 18 | type Slice []byte |
| 19 | |
| 20 | var slice Slice |
| 21 | |
| 22 | func asBool(Bool) {} |
| 23 | func asString(String) {} |
| 24 | |
| 25 | type String string |
Russ Cox | 78ebe2b | 2009-09-24 17:54:47 -0700 | [diff] [blame] | 26 | |
| 27 | func main() { |
| 28 | var ( |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 29 | b Bool = true |
| 30 | i, j int |
| 31 | c = make(chan int) |
| 32 | m = make(Map) |
Russ Cox | 78ebe2b | 2009-09-24 17:54:47 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 35 | 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 Cox | 78ebe2b | 2009-09-24 17:54:47 -0700 | [diff] [blame] | 42 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 43 | _, b = m[2] // ERROR "cannot .* bool.*type Bool" |
| 44 | m[2] = 1, b // ERROR "cannot use.*type Bool.*as type bool" |
Russ Cox | 78ebe2b | 2009-09-24 17:54:47 -0700 | [diff] [blame] | 45 | |
Russ Cox | f4e76d8 | 2011-01-31 18:36:28 -0500 | [diff] [blame] | 46 | ////TODO(rsc): uncomment when this syntax is valid for receive+check closed |
| 47 | //// _, b = <-c // ERROR "cannot .* bool.*type Bool" |
| 48 | //// _ = b |
Russ Cox | 78ebe2b | 2009-09-24 17:54:47 -0700 | [diff] [blame] | 49 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 50 | var inter interface{} |
| 51 | _, b = inter.(Map) // ERROR "cannot .* bool.*type Bool" |
| 52 | _ = b |
Russ Cox | 78ebe2b | 2009-09-24 17:54:47 -0700 | [diff] [blame] | 53 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 54 | var minter interface { |
| 55 | M() |
| 56 | } |
| 57 | _, b = minter.(Map) // ERROR "cannot .* bool.*type Bool" |
| 58 | _ = b |
Russ Cox | 78ebe2b | 2009-09-24 17:54:47 -0700 | [diff] [blame] | 59 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 60 | 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 Taylor | c23657e | 2010-09-08 21:03:51 -0700 | [diff] [blame] | 64 | asString(String(slice)) // ERROR "cannot .*type Slice.*type String" |
Russ Cox | 78ebe2b | 2009-09-24 17:54:47 -0700 | [diff] [blame] | 65 | } |