Or is it just doing the bounds check elimination (and debug messages about it) before the dead code elimination?
Or is it just doing the bounds check elimination (and debug messages about it) before the dead code elimination?
In go, out-of-bounds array/slice access is equivalent to calling panic. So _ = a[i] isn't dead code unless you can be sure that a[i] is in bounds. So yes, I imagine BCE should come before dead code elimination.
char x[4];
printf("%c", x[4]);
That's pretty easy to detect.We can look at the example given above and clearly know/prove that the access is out of bounds. ie: a C compiler could emit a diagnostic (or decide it's undefined behavior and treat it as unreachable).
There aren't any bounds checks above to eliminate.
x := true
var y = 0
if x {
y = 10
} else {
y = 20
}
In that case dead code elimination might require a little more analysis than checking for unassigned expressions and unused variables.So the question might be: what kind of dead code elimination exist in the compiler, apart from what you describe which has existed since the beginning of Go? (if I understand correctly)
This is obviously not true and is easily disproved.
Here's a program that compiles but has statically provably dead code on line 15 https://play.golang.org/p/_XcxezHV-6.
But even if I couldn't give you an example like that where the dead code is obvious, dead code is produced by the compiler itself as it optimises, and by code generation tools.
`_` mainly exists to allow ignoring single values, within multiple. Eg, dropping an error: `val, _ := MightError()`
_ serves multiple purposes, not just for ignoring a return value. It's often used when you care about the side effect but not the return value, such as importation a driver for a database . You'll never use the library, so you import it as _ "db.driver.name/path/".