Rémy Oudompheng | 2ece2f5 | 2012-02-18 22:15:42 +0100 | [diff] [blame] | 1 | // compile |
Robert Griesemer | 8c20787 | 2011-05-25 10:26:06 -0700 | [diff] [blame] | 2 | |
| 3 | // Copyright 2011 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 | // Issue 1708, legal cases. |
| 8 | |
| 9 | package p |
| 10 | |
| 11 | func f(x int) int { return 0 } |
| 12 | func g(x interface{}) int { return 0 } |
| 13 | func h(x float64) int { return 0 } |
| 14 | |
| 15 | // from the spec |
| 16 | var ( |
| 17 | s uint = 33 |
| 18 | i = 1 << s // 1 has type int |
| 19 | j int32 = 1 << s // 1 has type int32; j == 0 |
| 20 | k = uint64(1 << s) // 1 has type uint64; k == 1<<33 |
| 21 | m int = 1.0 << s // legal: 1.0 has type int |
| 22 | w int64 = 1.0 << 33 // legal: 1.0<<33 is a constant shift expression |
| 23 | ) |
| 24 | |
| 25 | // non-constant shift expressions |
| 26 | var ( |
| 27 | a1 int = 2.0 << s // typeof(2.0) is int in this context => legal shift |
| 28 | d1 = f(2.0 << s) // typeof(2.0) is int in this context => legal shift |
| 29 | ) |
| 30 | |
| 31 | // constant shift expressions |
| 32 | const c uint = 5 |
| 33 | |
| 34 | var ( |
| 35 | a2 int = 2.0 << c // a2 == 64 (type int) |
| 36 | b2 = 2.0 << c // b2 == 64 (untyped integer) |
| 37 | _ = f(b2) // verify b2 has type int |
| 38 | c2 float64 = 2 << c // c2 == 64.0 (type float64) |
| 39 | d2 = f(2.0 << c) // == f(64) |
| 40 | e2 = g(2.0 << c) // == g(int(64)) |
| 41 | f2 = h(2 << c) // == h(float64(64.0)) |
| 42 | ) |