diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:23:18 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:23:18 +0000 |
commit | 43a123c1ae6613b3efeed291fa552ecd909d3acf (patch) | |
tree | fd92518b7024bc74031f78a1cf9e454b65e73665 /src/text/template/link_test.go | |
parent | Initial commit. (diff) | |
download | golang-1.20-43a123c1ae6613b3efeed291fa552ecd909d3acf.tar.xz golang-1.20-43a123c1ae6613b3efeed291fa552ecd909d3acf.zip |
Adding upstream version 1.20.14.upstream/1.20.14upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/text/template/link_test.go')
-rw-r--r-- | src/text/template/link_test.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/text/template/link_test.go b/src/text/template/link_test.go new file mode 100644 index 0000000..e1d3136 --- /dev/null +++ b/src/text/template/link_test.go @@ -0,0 +1,59 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package template_test + +import ( + "bytes" + "internal/testenv" + "os" + "os/exec" + "path/filepath" + "testing" +) + +// Issue 36021: verify that text/template doesn't prevent the linker from removing +// unused methods. +func TestLinkerGC(t *testing.T) { + if testing.Short() { + t.Skip("skipping in short mode") + } + testenv.MustHaveGoBuild(t) + const prog = `package main + +import ( + _ "text/template" +) + +type T struct{} + +func (t *T) Unused() { println("THIS SHOULD BE ELIMINATED") } +func (t *T) Used() {} + +var sink *T + +func main() { + var t T + sink = &t + t.Used() +} +` + td := t.TempDir() + + if err := os.WriteFile(filepath.Join(td, "x.go"), []byte(prog), 0644); err != nil { + t.Fatal(err) + } + cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "x.exe", "x.go") + cmd.Dir = td + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("go build: %v, %s", err, out) + } + slurp, err := os.ReadFile(filepath.Join(td, "x.exe")) + if err != nil { + t.Fatal(err) + } + if bytes.Contains(slurp, []byte("THIS SHOULD BE ELIMINATED")) { + t.Error("binary contains code that should be deadcode eliminated") + } +} |