blob: adbfb773073844ba5228fdfefa9b9ecaf130cb01 [file] [log] [blame]
Rémy Oudompheng2ece2f52012-02-18 22:15:42 +01001// compile
Robert Griesemer8c207872011-05-25 10:26:06 -07002
Emmanuel Odeke53fd5222016-04-10 14:32:26 -07003// Copyright 2011 The Go Authors. All rights reserved.
Robert Griesemer8c207872011-05-25 10:26:06 -07004// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
Rob Pike80a97832012-02-24 11:48:19 +11007// Test legal shifts.
Robert Griesemer8c207872011-05-25 10:26:06 -07008// Issue 1708, legal cases.
Rob Pike80a97832012-02-24 11:48:19 +11009// Compiles but does not run.
Robert Griesemer8c207872011-05-25 10:26:06 -070010
11package p
12
13func f(x int) int { return 0 }
14func g(x interface{}) int { return 0 }
15func h(x float64) int { return 0 }
16
17// from the spec
18var (
19 s uint = 33
20 i = 1 << s // 1 has type int
21 j int32 = 1 << s // 1 has type int32; j == 0
22 k = uint64(1 << s) // 1 has type uint64; k == 1<<33
Rémy Oudompheng05cf6fe2013-06-25 08:06:34 +020023 l = g(1 << s) // 1 has type int
Robert Griesemer8c207872011-05-25 10:26:06 -070024 m int = 1.0 << s // legal: 1.0 has type int
25 w int64 = 1.0 << 33 // legal: 1.0<<33 is a constant shift expression
26)
27
28// non-constant shift expressions
29var (
30 a1 int = 2.0 << s // typeof(2.0) is int in this context => legal shift
31 d1 = f(2.0 << s) // typeof(2.0) is int in this context => legal shift
32)
33
34// constant shift expressions
35const c uint = 5
36
37var (
38 a2 int = 2.0 << c // a2 == 64 (type int)
39 b2 = 2.0 << c // b2 == 64 (untyped integer)
40 _ = f(b2) // verify b2 has type int
41 c2 float64 = 2 << c // c2 == 64.0 (type float64)
42 d2 = f(2.0 << c) // == f(64)
43 e2 = g(2.0 << c) // == g(int(64))
44 f2 = h(2 << c) // == h(float64(64.0))
45)