blob: 7feae13b9d7045f9cd405ad13dbc559df56a7695 [file] [log] [blame]
Russ Cox57eb06f2012-02-16 23:51:04 -05001// errorcheck
Russ Cox78ebe2b2009-09-24 17:54:47 -07002
Emmanuel Odeke53fd5222016-04-10 14:32:26 -07003// Copyright 2009 The Go Authors. All rights reserved.
Russ Cox78ebe2b2009-09-24 17:54:47 -07004// 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.
Rob Pike80a97832012-02-24 11:48:19 +11009// Does not compile.
Russ Cox78ebe2b2009-09-24 17:54:47 -070010
11package main
12
13type Bool bool
14
15type Map map[int]int
Russ Cox565b5dc2010-06-08 18:50:02 -070016
Russ Cox78ebe2b2009-09-24 17:54:47 -070017func (Map) M() {}
18
Russ Cox565b5dc2010-06-08 18:50:02 -070019type Slice []byte
20
21var slice Slice
22
23func asBool(Bool) {}
24func asString(String) {}
25
26type String string
Russ Cox78ebe2b2009-09-24 17:54:47 -070027
28func main() {
29 var (
Russ Coxa457fa52012-02-21 22:54:07 -050030 b Bool = true
Russ Cox565b5dc2010-06-08 18:50:02 -070031 i, j int
32 c = make(chan int)
33 m = make(Map)
Russ Cox78ebe2b2009-09-24 17:54:47 -070034 )
35
Russ Cox565b5dc2010-06-08 18:50:02 -070036 asBool(b)
37 asBool(!b)
Russ Coxa457fa52012-02-21 22:54:07 -050038 asBool(true)
Russ Cox565b5dc2010-06-08 18:50:02 -070039 asBool(*&b)
40 asBool(Bool(true))
Russ Coxe29d3df2012-02-22 00:29:37 -050041 asBool(1 != 2) // ok now
42 asBool(i < j) // ok now
Russ Cox78ebe2b2009-09-24 17:54:47 -070043
Chris Manghane897f7a32014-08-11 16:11:55 -070044 _, b = m[2] // ok now
Russ Cox78ebe2b2009-09-24 17:54:47 -070045
Russ Cox565b5dc2010-06-08 18:50:02 -070046 var inter interface{}
Chris Manghane897f7a32014-08-11 16:11:55 -070047 _, b = inter.(Map) // ok now
Russ Cox565b5dc2010-06-08 18:50:02 -070048 _ = b
Russ Cox78ebe2b2009-09-24 17:54:47 -070049
Russ Cox565b5dc2010-06-08 18:50:02 -070050 var minter interface {
51 M()
52 }
Chris Manghane897f7a32014-08-11 16:11:55 -070053 _, b = minter.(Map) // ok now
Russ Cox565b5dc2010-06-08 18:50:02 -070054 _ = b
Russ Cox78ebe2b2009-09-24 17:54:47 -070055
Russ Cox3f915f52011-03-11 14:47:44 -050056 _, bb := <-c
57 asBool(bb) // ERROR "cannot use.*type bool.*as type Bool"
Chris Manghane897f7a32014-08-11 16:11:55 -070058 _, b = <-c // ok now
Russ Cox565b5dc2010-06-08 18:50:02 -070059 _ = b
60
Russ Cox6e3e3802011-11-22 12:30:02 -050061 asString(String(slice)) // ok
Russ Cox78ebe2b2009-09-24 17:54:47 -070062}