| 1 |
|
| 2 |
|
| 3 |
-- go.mod -- |
| 4 |
module example.com |
| 5 |
go 1.18 |
| 6 |
|
| 7 |
-- template/template.go -- |
| 8 |
package template |
| 9 |
|
| 10 |
// Basic test of type-aware expression refactoring. |
| 11 |
|
| 12 |
import ( |
| 13 |
"errors" |
| 14 |
"fmt" |
| 15 |
) |
| 16 |
|
| 17 |
func before(s string) error { return fmt.Errorf("%s", s) } |
| 18 |
func after(s string) error { return errors.New(s) } |
| 19 |
|
| 20 |
-- in/a1/a1.go -- |
| 21 |
package a1 |
| 22 |
|
| 23 |
import ( |
| 24 |
. "fmt" |
| 25 |
myfmt "fmt" |
| 26 |
"os" |
| 27 |
"strings" |
| 28 |
) |
| 29 |
|
| 30 |
func example(n int) { |
| 31 |
x := "foo" + strings.Repeat("\t", n) |
| 32 |
// Match, despite named import. |
| 33 |
myfmt.Errorf("%s", x) |
| 34 |
|
| 35 |
// Match, despite dot import. |
| 36 |
Errorf("%s", x) |
| 37 |
|
| 38 |
// Match: multiple matches in same function are possible. |
| 39 |
myfmt.Errorf("%s", x) |
| 40 |
|
| 41 |
// No match: wildcarded operand has the wrong type. |
| 42 |
myfmt.Errorf("%s", 3) |
| 43 |
|
| 44 |
// No match: function operand doesn't match. |
| 45 |
myfmt.Printf("%s", x) |
| 46 |
|
| 47 |
// No match again, dot import. |
| 48 |
Printf("%s", x) |
| 49 |
|
| 50 |
// Match. |
| 51 |
myfmt.Fprint(os.Stderr, myfmt.Errorf("%s", x+"foo")) |
| 52 |
|
| 53 |
// No match: though this literally matches the template, |
| 54 |
// fmt doesn't resolve to a package here. |
| 55 |
var fmt struct{ Errorf func(string, string) } |
| 56 |
fmt.Errorf("%s", x) |
| 57 |
|
| 58 |
// Recursive matching: |
| 59 |
|
| 60 |
// Match: both matches are well-typed, so both succeed. |
| 61 |
myfmt.Errorf("%s", myfmt.Errorf("%s", x+"foo").Error()) |
| 62 |
|
| 63 |
// Outer match succeeds, inner doesn't: 3 has wrong type. |
| 64 |
myfmt.Errorf("%s", myfmt.Errorf("%s", 3).Error()) |
| 65 |
|
| 66 |
// Inner match succeeds, outer doesn't: the inner replacement |
| 67 |
// has the wrong type (error not string). |
| 68 |
myfmt.Errorf("%s", myfmt.Errorf("%s", x+"foo")) |
| 69 |
} |
| 70 |
|
| 71 |
-- out/a1/a1.go -- |
| 72 |
package a1 |
| 73 |
|
| 74 |
import ( |
| 75 |
"errors" |
| 76 |
. "fmt" |
| 77 |
myfmt "fmt" |
| 78 |
"os" |
| 79 |
"strings" |
| 80 |
) |
| 81 |
|
| 82 |
func example(n int) { |
| 83 |
x := "foo" + strings.Repeat("\t", n) |
| 84 |
// Match, despite named import. |
| 85 |
errors.New(x) |
| 86 |
|
| 87 |
// Match, despite dot import. |
| 88 |
errors.New(x) |
| 89 |
|
| 90 |
// Match: multiple matches in same function are possible. |
| 91 |
errors.New(x) |
| 92 |
|
| 93 |
// No match: wildcarded operand has the wrong type. |
| 94 |
myfmt.Errorf("%s", 3) |
| 95 |
|
| 96 |
// No match: function operand doesn't match. |
| 97 |
myfmt.Printf("%s", x) |
| 98 |
|
| 99 |
// No match again, dot import. |
| 100 |
Printf("%s", x) |
| 101 |
|
| 102 |
// Match. |
| 103 |
myfmt.Fprint(os.Stderr, errors.New(x+"foo")) |
| 104 |
|
| 105 |
// No match: though this literally matches the template, |
| 106 |
// fmt doesn't resolve to a package here. |
| 107 |
var fmt struct{ Errorf func(string, string) } |
| 108 |
fmt.Errorf("%s", x) |
| 109 |
|
| 110 |
// Recursive matching: |
| 111 |
|
| 112 |
// Match: both matches are well-typed, so both succeed. |
| 113 |
errors.New(errors.New(x + "foo").Error()) |
| 114 |
|
| 115 |
// Outer match succeeds, inner doesn't: 3 has wrong type. |
| 116 |
errors.New(myfmt.Errorf("%s", 3).Error()) |
| 117 |
|
| 118 |
// Inner match succeeds, outer doesn't: the inner replacement |
| 119 |
// has the wrong type (error not string). |
| 120 |
myfmt.Errorf("%s", errors.New(x+"foo")) |
| 121 |
} |
| 122 |
-- a2/a2.go -- |
| 123 |
package a2 |
| 124 |
|
| 125 |
// This refactoring causes addition of "errors" import. |
| 126 |
// TODO(adonovan): fix: it should also remove "fmt". |
| 127 |
|
| 128 |
import myfmt "fmt" |
| 129 |
|
| 130 |
func example(n int) { |
| 131 |
myfmt.Errorf("%s", "") |
| 132 |
} |
| 133 |
|
| 134 |
-- out/a2/a2.go -- |
| 135 |
package a2 |
| 136 |
|
| 137 |
// This refactoring causes addition of "errors" import. |
| 138 |
// TODO(adonovan): fix: it should also remove "fmt". |
| 139 |
|
| 140 |
import ( |
| 141 |
"errors" |
| 142 |
myfmt "fmt" |
| 143 |
) |
| 144 |
|
| 145 |
func example(n int) { |
| 146 |
errors.New("") |
| 147 |
} |