blob: 7280edf333cdc841b3d762d68a5684b66f6a0c3d [file] [log] [blame]
Russ Cox0b477ef2012-02-16 23:48:57 -05001// run
Russ Cox8f194bf2009-03-12 19:04:38 -07002
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
Rob Pikefc0dc042012-02-19 13:19:43 +11007// Test types of constant expressions, using reflect.
8
Russ Cox8f194bf2009-03-12 19:04:38 -07009package main
10
Russ Cox1af3edc2009-07-03 09:45:15 -070011import "reflect"
Russ Cox8f194bf2009-03-12 19:04:38 -070012
Russ Cox07abf1c2011-04-25 13:39:36 -040013func typeof(x interface{}) string { return reflect.TypeOf(x).String() }
Russ Cox8f194bf2009-03-12 19:04:38 -070014
Rob Pike325cf8e2010-03-24 16:46:53 -070015func f() int { return 0 }
Russ Cox8f194bf2009-03-12 19:04:38 -070016
Rob Pike325cf8e2010-03-24 16:46:53 -070017func g() int { return 0 }
Russ Cox8f194bf2009-03-12 19:04:38 -070018
19type T func() int
20
Rob Pike325cf8e2010-03-24 16:46:53 -070021var m = map[string]T{"f": f}
Russ Cox8f194bf2009-03-12 19:04:38 -070022
23type A int
24type B int
25
Rob Pike325cf8e2010-03-24 16:46:53 -070026var a A = 1
27var b B = 2
28var x int
Russ Cox8f194bf2009-03-12 19:04:38 -070029
30func main() {
Rob Pike325cf8e2010-03-24 16:46:53 -070031 want := typeof(g)
Russ Cox8f194bf2009-03-12 19:04:38 -070032 if t := typeof(f); t != want {
Rob Pike325cf8e2010-03-24 16:46:53 -070033 println("type of f is", t, "want", want)
34 panic("fail")
Russ Cox8f194bf2009-03-12 19:04:38 -070035 }
36
Rob Pike325cf8e2010-03-24 16:46:53 -070037 want = typeof(a)
Russ Cox8f194bf2009-03-12 19:04:38 -070038 if t := typeof(+a); t != want {
Rob Pike325cf8e2010-03-24 16:46:53 -070039 println("type of +a is", t, "want", want)
40 panic("fail")
Russ Cox8f194bf2009-03-12 19:04:38 -070041 }
Rob Pike325cf8e2010-03-24 16:46:53 -070042 if t := typeof(a + 0); t != want {
43 println("type of a+0 is", t, "want", want)
44 panic("fail")
Russ Cox8f194bf2009-03-12 19:04:38 -070045 }
Russ Cox8f194bf2009-03-12 19:04:38 -070046}