-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
text/template: avoid a global map to help the linker's deadcode elimi…
…nation Fixes golang#36021 Updates golang#2559 Updates golang#26775 Change-Id: I2e6708691311035b63866f25d5b4b3977a118290 Reviewed-on: https://go-review.googlesource.com/c/go/+/210284 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Rob Pike <[email protected]>
- Loading branch information
Showing
4 changed files
with
111 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// 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" | ||
"io/ioutil" | ||
"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, err := ioutil.TempDir("", "text_template_TestDeadCodeElimination") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.RemoveAll(td) | ||
|
||
if err := ioutil.WriteFile(filepath.Join(td, "x.go"), []byte(prog), 0644); err != nil { | ||
t.Fatal(err) | ||
} | ||
cmd := exec.Command("go", "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 := ioutil.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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters