1 // Regression: a parenthesised CONSTANT used as a method receiver must be
2 // materialised at the type the method was resolved on.
3 //
4 // resolveMethodCallWithRecv picks the builtin Stringer from the operand's
5 // DEFAULT type (UntypedToTyped maps an untyped int to int64), but
6 // resolveBuiltinMethod allocated its receiver temporary at the OPERAND's type -
7 // i32 for a folded constant. The runtime body then read 8 bytes out of a 4-byte
8 // alloca and reported the low half twice:
9 //
10 // (42).String() -> 180388626474 want 42
11 // (40+2).String() -> 180388626474 want 42
12 // (1<<26).String() -> 180455735296 want 67108864
13 // (1<<26-1).String() -> 288230376218820607 want 67108863
14 //
15 // 180388626474 is exactly 42<<32|42, and the others follow the same shape, so
16 // the bug is silent and its signature is unmistakable. A typed variable was
17 // always correct (`a := 42; a.String()` printed 42), which is why nothing in the
18 // suite caught it: every fixture stored the constant in a variable first.
19 //
20 // The temporary is now allocated at the RESOLVED type and emitStore widens the
21 // operand into it.
22 //
23 package main
24 25 type small int32
26 27 func (s *small) String() (out string) { return "small" }
28 29 func main() {
30 // Termination: a batch program must still be stoppable, so the entry
31 // handles the channel the runtime closes on SIGINT/SIGTERM.
32 select {
33 case <-shutdown:
34 return
35 default:
36 }
37 38 // a typed variable is the reference the inline forms must match
39 a := 42
40 if a.String() != "42" {
41 panic("typed variable: " | a.String())
42 }
43 44 if (42).String() != "42" {
45 panic("(42).String() = " | (42).String())
46 }
47 if (40 + 2).String() != "42" {
48 panic("(40+2).String() = " | (40 + 2).String())
49 }
50 if (1 << 26).String() != "67108864" {
51 panic("(1<<26).String() = " | (1 << 26).String())
52 }
53 if (1<<26 - 1).String() != "67108863" {
54 panic("(1<<26-1).String() = " | (1<<26 - 1).String())
55 }
56 if (1<<26 + 1).String() != "67108865" {
57 panic("(1<<26+1).String() = " | (1<<26 + 1).String())
58 }
59 if (-7).String() != "-7" {
60 panic("(-7).String() = " | (-7).String())
61 }
62 63 // A constant receiver of a string and a bool take the same path.
64 if ("hi").String() != "hi" {
65 panic("(hi).String()")
66 }
67 if (true).String() != "true" {
68 panic("(true).String()")
69 }
70 71 // A named type with its own Stringer must still win over the builtin.
72 if (small(5)).String() != "small" {
73 panic("(small(5)).String()")
74 }
75 76 // And the value must still be usable as its default type.
77 n := (1 << 26)
78 if n != 67108864 {
79 panic("default type value")
80 }
81 println("ok")
82 }
83