proc: correctly mark closure variables as shadowed (#1674)

If a closure captures a variable but also defines a variable of the
same name in its root scope the shadowed flag would, sometimes, not be
appropriately applied to the captured variable.

This change:

1. sorts the variable list by depth *and* declaration line, so that
closure captured variables always appear before other root-scope
variables, regardless of the order used by the compiler

2. marks variable with the same name as shadowed even if there is only
one scope at play.

This fixes the problem but as a side effect:

1. programs compiled with Go prior to version 1.9 will have the
shadowed flag applied arbitrarily (previously the shadowed flag was not
applied at all)
2. programs compiled with Go prior to versoin 1.11 will still exhibit
the bug, as they do not have DeclLine information.

Fixes #1672
This commit is contained in:
Alessandro Arzilli
2019-09-15 20:40:35 +02:00
committed by Derek Parker
parent 223eb4cb5f
commit e994047355
5 changed files with 44 additions and 28 deletions

View File

@ -1946,16 +1946,21 @@ func (ctyp *constantType) describe(n int64) string {
return ""
}
type variablesByDepth struct {
type variablesByDepthAndDeclLine struct {
vars []*Variable
depths []int
}
func (v *variablesByDepth) Len() int { return len(v.vars) }
func (v *variablesByDepthAndDeclLine) Len() int { return len(v.vars) }
func (v *variablesByDepth) Less(i int, j int) bool { return v.depths[i] < v.depths[j] }
func (v *variablesByDepthAndDeclLine) Less(i int, j int) bool {
if v.depths[i] == v.depths[j] {
return v.vars[i].DeclLine < v.vars[j].DeclLine
}
return v.depths[i] < v.depths[j]
}
func (v *variablesByDepth) Swap(i int, j int) {
func (v *variablesByDepthAndDeclLine) Swap(i int, j int) {
v.depths[i], v.depths[j] = v.depths[j], v.depths[i]
v.vars[i], v.vars[j] = v.vars[j], v.vars[i]
}