Skip to content

Commit 95deb33

Browse files
nmiyakedmitshur
authored andcommitted
Ensure that sorting of variables is stable. (gopherjs#681)
If names are equal, sort ties by position. Fixes gopherjs#679.
1 parent 2b1d432 commit 95deb33

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

compiler/utils.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,12 @@ func (c *funcContext) handleEscapingVars(n ast.Node) {
369369

370370
var names []string
371371
objs := analysis.EscapingObjects(n, c.p.Info.Info)
372-
sort.Sort(varsByName(objs))
372+
sort.Slice(objs, func(i, j int) bool {
373+
if objs[i].Name() == objs[j].Name() {
374+
return objs[i].Pos() < objs[j].Pos()
375+
}
376+
return objs[i].Name() < objs[j].Name()
377+
})
373378
for _, obj := range objs {
374379
names = append(names, c.objectName(obj))
375380
c.p.escapingVars[obj] = true
@@ -638,17 +643,3 @@ func endsWithReturn(stmts []ast.Stmt) bool {
638643
func encodeIdent(name string) string {
639644
return strings.Replace(url.QueryEscape(name), "%", "$", -1)
640645
}
641-
642-
type varsByName []*types.Var
643-
644-
func (s varsByName) Len() int {
645-
return len(s)
646-
}
647-
648-
func (s varsByName) Swap(i, j int) {
649-
s[i], s[j] = s[j], s[i]
650-
}
651-
652-
func (s varsByName) Less(i, j int) bool {
653-
return s[i].Name() < s[j].Name()
654-
}

0 commit comments

Comments
 (0)