mirror of
https://github.com/golang/go
synced 2025-05-02 02:51:41 +00:00
cmd/compile: constant-fold switches early in compilation
So that the inliner knows all the other cases are dead and doesn't accumulate any cost for them. The canonical case for this is switching on runtime.GOOS, which occurs several places in the stdlib. Fixes #50253 Change-Id: I44823aaebb6c1b03c9b0c12d10086db81954350f Reviewed-on: https://go-review.googlesource.com/c/go/+/399694 Run-TryBot: Keith Randall <khr@golang.org> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
@ -160,6 +160,51 @@ func switchType(x interface{}) int { // ERROR "can inline switchType" "x does no
|
||||
}
|
||||
}
|
||||
|
||||
// Test that switches on constant things, with constant cases, only cost anything for
|
||||
// the case that matches. See issue 50253.
|
||||
func switchConst1(p func(string)) { // ERROR "can inline switchConst" "p does not escape"
|
||||
const c = 1
|
||||
switch c {
|
||||
case 0:
|
||||
p("zero")
|
||||
case 1:
|
||||
p("one")
|
||||
case 2:
|
||||
p("two")
|
||||
default:
|
||||
p("other")
|
||||
}
|
||||
}
|
||||
|
||||
func switchConst2() string { // ERROR "can inline switchConst2"
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
return "Leenooks"
|
||||
case "windows":
|
||||
return "Windoze"
|
||||
case "darwin":
|
||||
return "MackBone"
|
||||
case "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100":
|
||||
return "Numbers"
|
||||
default:
|
||||
return "oh nose!"
|
||||
}
|
||||
}
|
||||
func switchConst3() string { // ERROR "can inline switchConst3"
|
||||
switch runtime.GOOS {
|
||||
case "Linux":
|
||||
panic("Linux")
|
||||
case "Windows":
|
||||
panic("Windows")
|
||||
case "Darwin":
|
||||
panic("Darwin")
|
||||
case "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100":
|
||||
panic("Numbers")
|
||||
default:
|
||||
return "oh nose!"
|
||||
}
|
||||
}
|
||||
|
||||
func inlineRangeIntoMe(data []int) { // ERROR "can inline inlineRangeIntoMe" "data does not escape"
|
||||
rangeFunc(data, 12) // ERROR "inlining call to rangeFunc"
|
||||
}
|
||||
|
Reference in New Issue
Block a user