compiler: do not emit code for unnamed unused variables
If variable is unnamed and does not contain function call then it's treated as unused and code generation may be omitted for it initialization/declaration.
This commit is contained in:
parent
1dcbdb011a
commit
91b36657d6
8 changed files with 151 additions and 18 deletions
|
@ -596,15 +596,32 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
|
|||
}
|
||||
}
|
||||
}
|
||||
for i := range t.Names {
|
||||
if len(t.Values) != 0 {
|
||||
if i == 0 || !multiRet {
|
||||
ast.Walk(c, t.Values[i])
|
||||
for i, id := range t.Names {
|
||||
if id.Name != "_" {
|
||||
if len(t.Values) != 0 {
|
||||
if i == 0 || !multiRet {
|
||||
ast.Walk(c, t.Values[i])
|
||||
}
|
||||
} else {
|
||||
c.emitDefault(c.typeOf(t.Type))
|
||||
}
|
||||
} else {
|
||||
c.emitDefault(c.typeOf(t.Type))
|
||||
c.emitStoreVar("", t.Names[i].Name)
|
||||
continue
|
||||
}
|
||||
// If var decl contains call then the code should be emitted for it, otherwise - do not evaluate.
|
||||
if len(t.Values) == 0 {
|
||||
continue
|
||||
}
|
||||
var hasCall bool
|
||||
if i == 0 || !multiRet {
|
||||
hasCall = containsCall(t.Values[i])
|
||||
}
|
||||
if hasCall {
|
||||
ast.Walk(c, t.Values[i])
|
||||
}
|
||||
if hasCall || i != 0 && multiRet {
|
||||
c.emitStoreVar("", "_") // drop unused after walk
|
||||
}
|
||||
c.emitStoreVar("", t.Names[i].Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue