| 1 |
|
| 2 |
-- go.mod -- |
| 3 |
module example.com |
| 4 |
go 1.18 |
| 5 |
|
| 6 |
-- template/template.go -- |
| 7 |
package template |
| 8 |
|
| 9 |
// Test of repeated use of wildcard in pattern. |
| 10 |
|
| 11 |
// NB: multiple patterns would be required to handle variants such as |
| 12 |
// s[:len(s)], s[x:len(s)], etc, since a wildcard can't match nothing at all. |
| 13 |
// TODO(adonovan): support multiple templates in a single pass. |
| 14 |
|
| 15 |
func before(s string) string { return s[:len(s)] } |
| 16 |
func after(s string) string { return s } |
| 17 |
|
| 18 |
-- in/c1/c1.go -- |
| 19 |
package C1 |
| 20 |
|
| 21 |
import "strings" |
| 22 |
|
| 23 |
func example() { |
| 24 |
x := "foo" |
| 25 |
println(x[:len(x)]) |
| 26 |
|
| 27 |
// Match, but the transformation is not sound w.r.t. possible side effects. |
| 28 |
println(strings.Repeat("*", 3)[:len(strings.Repeat("*", 3))]) |
| 29 |
|
| 30 |
// No match, since second use of wildcard doesn't match first. |
| 31 |
println(strings.Repeat("*", 3)[:len(strings.Repeat("*", 2))]) |
| 32 |
|
| 33 |
// Recursive match demonstrating bottom-up rewrite: |
| 34 |
// only after the inner replacement occurs does the outer syntax match. |
| 35 |
println((x[:len(x)])[:len(x[:len(x)])]) |
| 36 |
// -> (x[:len(x)]) |
| 37 |
// -> x |
| 38 |
} |
| 39 |
|
| 40 |
-- out/c1/c1.go -- |
| 41 |
package C1 |
| 42 |
|
| 43 |
import "strings" |
| 44 |
|
| 45 |
func example() { |
| 46 |
x := "foo" |
| 47 |
println(x) |
| 48 |
|
| 49 |
// Match, but the transformation is not sound w.r.t. possible side effects. |
| 50 |
println(strings.Repeat("*", 3)) |
| 51 |
|
| 52 |
// No match, since second use of wildcard doesn't match first. |
| 53 |
println(strings.Repeat("*", 3)[:len(strings.Repeat("*", 2))]) |
| 54 |
|
| 55 |
// Recursive match demonstrating bottom-up rewrite: |
| 56 |
// only after the inner replacement occurs does the outer syntax match. |
| 57 |
println(x) |
| 58 |
// -> (x[:len(x)]) |
| 59 |
// -> x |
| 60 |
} |