Skip to content

Commit 0d64d53

Browse files
adonovangopherbot
authored andcommitted
Revert "gopls/internal/util/moreslices: add Reversed iterator"
This reverts commit b4768b8. Reason for revert: the generated code is not inlined in all cases, and is significantly more complex (allocations, dynamic calls). Change-Id: Id7d599bd37bb492800b59ca387b584a2027ce046 Reviewed-on: https://go-review.googlesource.com/c/tools/+/680437 Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 5efcbbf commit 0d64d53

File tree

11 files changed

+30
-42
lines changed

11 files changed

+30
-42
lines changed

gopls/internal/analysis/infertypeargs/infertypeargs.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"golang.org/x/tools/go/analysis"
1313
"golang.org/x/tools/go/analysis/passes/inspect"
1414
"golang.org/x/tools/go/ast/inspector"
15-
"golang.org/x/tools/gopls/internal/util/moreslices"
1615
"golang.org/x/tools/internal/typeparams"
1716
)
1817

@@ -75,7 +74,7 @@ func diagnose(fset *token.FileSet, inspect *inspector.Inspector, start, end toke
7574
// Start removing argument expressions from the right, and check if we can
7675
// still infer the call expression.
7776
required := len(indices) // number of type expressions that are required
78-
for i, _ := range moreslices.Reversed(indices) {
77+
for i := len(indices) - 1; i >= 0; i-- {
7978
var fun ast.Expr
8079
if i == 0 {
8180
// No longer an index expression: just use the parameterized operand.

gopls/internal/cache/check.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"golang.org/x/tools/gopls/internal/protocol"
3535
"golang.org/x/tools/gopls/internal/util/bug"
3636
"golang.org/x/tools/gopls/internal/util/moremaps"
37-
"golang.org/x/tools/gopls/internal/util/moreslices"
3837
"golang.org/x/tools/gopls/internal/util/safetoken"
3938
"golang.org/x/tools/internal/analysisinternal"
4039
"golang.org/x/tools/internal/event"
@@ -1871,7 +1870,8 @@ func depsErrors(ctx context.Context, snapshot *Snapshot, mp *metadata.Package) (
18711870
// we reach the workspace.
18721871
var errors []*Diagnostic
18731872
for _, depErr := range relevantErrors {
1874-
for _, item := range moreslices.Reversed(depErr.ImportStack) {
1873+
for i := len(depErr.ImportStack) - 1; i >= 0; i-- {
1874+
item := depErr.ImportStack[i]
18751875
if snapshot.IsWorkspacePackage(PackageID(item)) {
18761876
break
18771877
}
@@ -1909,7 +1909,8 @@ func depsErrors(ctx context.Context, snapshot *Snapshot, mp *metadata.Package) (
19091909
// Add a diagnostic to the module that contained the lowest-level import of
19101910
// the missing package.
19111911
for _, depErr := range relevantErrors {
1912-
for _, item := range moreslices.Reversed(depErr.ImportStack) {
1912+
for i := len(depErr.ImportStack) - 1; i >= 0; i-- {
1913+
item := depErr.ImportStack[i]
19131914
mp := snapshot.Metadata(PackageID(item))
19141915
if mp == nil || mp.Module == nil {
19151916
continue

gopls/internal/cache/mod.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"golang.org/x/tools/gopls/internal/label"
1919
"golang.org/x/tools/gopls/internal/protocol"
2020
"golang.org/x/tools/gopls/internal/protocol/command"
21-
"golang.org/x/tools/gopls/internal/util/moreslices"
2221
"golang.org/x/tools/internal/event"
2322
"golang.org/x/tools/internal/memoize"
2423
)
@@ -381,8 +380,8 @@ func (s *Snapshot) matchErrorToModule(pm *ParsedModule, goCmdError string) (prot
381380
var reference *modfile.Line
382381
matches := moduleVersionInErrorRe.FindAllStringSubmatch(goCmdError, -1)
383382

384-
for _, match := range moreslices.Reversed(matches) {
385-
ver := module.Version{Path: match[1], Version: match[2]}
383+
for i := len(matches) - 1; i >= 0; i-- {
384+
ver := module.Version{Path: matches[i][1], Version: matches[i][2]}
386385
if err := module.Check(ver.Path, ver.Version); err != nil {
387386
continue
388387
}
@@ -413,8 +412,8 @@ func (s *Snapshot) matchErrorToModule(pm *ParsedModule, goCmdError string) (prot
413412
func (s *Snapshot) goCommandDiagnostic(pm *ParsedModule, loc protocol.Location, goCmdError string) (*Diagnostic, error) {
414413
matches := moduleVersionInErrorRe.FindAllStringSubmatch(goCmdError, -1)
415414
var innermost *module.Version
416-
for _, match := range moreslices.Reversed(matches) {
417-
ver := module.Version{Path: match[1], Version: match[2]}
415+
for i := len(matches) - 1; i >= 0; i-- {
416+
ver := module.Version{Path: matches[i][1], Version: matches[i][2]}
418417
if err := module.Check(ver.Path, ver.Version); err != nil {
419418
continue
420419
}

gopls/internal/cmd/semantictokens.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"golang.org/x/tools/gopls/internal/protocol"
1717
"golang.org/x/tools/gopls/internal/protocol/semtok"
1818
"golang.org/x/tools/gopls/internal/settings"
19-
"golang.org/x/tools/gopls/internal/util/moreslices"
2019
)
2120

2221
// generate semantic tokens and interpolate them in the file
@@ -148,7 +147,8 @@ func decorate(legend protocol.SemanticTokensLegend, file *cmdFile, data []uint32
148147
return nil
149148
}
150149
lines := bytes.Split(file.mapper.Content, []byte{'\n'})
151-
for _, mx := range moreslices.Reversed(marks) {
150+
for i := len(marks) - 1; i >= 0; i-- {
151+
mx := marks[i]
152152
markLine(mx, lines)
153153
}
154154
os.Stdout.Write(bytes.Join(lines, []byte{'\n'}))

gopls/internal/golang/completion/util.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"golang.org/x/tools/go/types/typeutil"
1313
"golang.org/x/tools/gopls/internal/golang"
1414
"golang.org/x/tools/gopls/internal/protocol"
15-
"golang.org/x/tools/gopls/internal/util/moreslices"
1615
"golang.org/x/tools/gopls/internal/util/safetoken"
1716
"golang.org/x/tools/internal/diff"
1817
"golang.org/x/tools/internal/typeparams"
@@ -272,9 +271,9 @@ func prevStmt(pos token.Pos, path []ast.Node) ast.Stmt {
272271
}
273272
}
274273

275-
for _, line := range moreslices.Reversed(blockLines) {
276-
if line.End() < pos {
277-
return line
274+
for i := len(blockLines) - 1; i >= 0; i-- {
275+
if blockLines[i].End() < pos {
276+
return blockLines[i]
278277
}
279278
}
280279

gopls/internal/golang/extract.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"golang.org/x/tools/gopls/internal/cache/parsego"
2727
goplsastutil "golang.org/x/tools/gopls/internal/util/astutil"
2828
"golang.org/x/tools/gopls/internal/util/bug"
29-
"golang.org/x/tools/gopls/internal/util/moreslices"
3029
"golang.org/x/tools/gopls/internal/util/safetoken"
3130
"golang.org/x/tools/internal/typesinternal"
3231
)
@@ -2116,7 +2115,8 @@ func isFreeBranchStmt(stack []ast.Node) bool {
21162115
}
21172116
case token.BREAK, token.CONTINUE:
21182117
// Find innermost relevant ancestor for break/continue.
2119-
for _, n := range moreslices.Reversed(stack) {
2118+
for i := len(stack) - 2; i >= 0; i-- {
2119+
n := stack[i]
21202120
if isLabeled {
21212121
l, ok := n.(*ast.LabeledStmt)
21222122
if !(ok && l.Label.Name == node.Label.Name) {

gopls/internal/golang/hover.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"golang.org/x/tools/gopls/internal/settings"
3838
gastutil "golang.org/x/tools/gopls/internal/util/astutil"
3939
"golang.org/x/tools/gopls/internal/util/bug"
40-
"golang.org/x/tools/gopls/internal/util/moreslices"
4140
"golang.org/x/tools/gopls/internal/util/safetoken"
4241
internalastutil "golang.org/x/tools/internal/astutil"
4342
"golang.org/x/tools/internal/event"
@@ -1562,8 +1561,8 @@ func findDeclInfo(files []*ast.File, pos token.Pos) (decl ast.Decl, spec ast.Spe
15621561
switch n := n.(type) {
15631562
case *ast.Field:
15641563
findEnclosingDeclAndSpec := func() {
1565-
for _, n := range moreslices.Reversed(stack) {
1566-
switch n := n.(type) {
1564+
for i := len(stack) - 1; i >= 0; i-- {
1565+
switch n := stack[i].(type) {
15671566
case ast.Spec:
15681567
spec = n
15691568
case ast.Decl:

gopls/internal/golang/semtok.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"golang.org/x/tools/gopls/internal/protocol"
3030
"golang.org/x/tools/gopls/internal/protocol/semtok"
3131
"golang.org/x/tools/gopls/internal/util/bug"
32-
"golang.org/x/tools/gopls/internal/util/moreslices"
3332
"golang.org/x/tools/gopls/internal/util/safetoken"
3433
"golang.org/x/tools/internal/astutil"
3534
"golang.org/x/tools/internal/event"
@@ -278,7 +277,8 @@ func (tv *tokenVisitor) token(start token.Pos, length int, typ semtok.Type, modi
278277
// strStack converts the stack to a string, for debugging and error messages.
279278
func (tv *tokenVisitor) strStack() string {
280279
msg := []string{"["}
281-
for _, n := range moreslices.Reversed(tv.stack) {
280+
for i := len(tv.stack) - 1; i >= 0; i-- {
281+
n := tv.stack[i]
282282
msg = append(msg, strings.TrimPrefix(fmt.Sprintf("%T", n), "*ast."))
283283
}
284284
if len(tv.stack) > 0 {
@@ -649,8 +649,8 @@ func (tv *tokenVisitor) ident(id *ast.Ident) {
649649
// isParam reports whether the position is that of a parameter name of
650650
// an enclosing function.
651651
func (tv *tokenVisitor) isParam(pos token.Pos) bool {
652-
for _, n := range moreslices.Reversed(tv.stack) {
653-
switch n := n.(type) {
652+
for i := len(tv.stack) - 1; i >= 0; i-- {
653+
switch n := tv.stack[i].(type) {
654654
case *ast.FuncDecl:
655655
for _, f := range n.Type.Params.List {
656656
for _, id := range f.Names {

gopls/internal/server/general.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,13 @@ func (s *server) checkViewGoVersions() {
284284
// version, it returns -1.
285285
//
286286
// Copied from the testenv package.
287-
func go1Point() (version int) {
288-
for _, tag := range moreslices.Reversed(build.Default.ReleaseTags) {
289-
if _, err := fmt.Sscanf(tag, "go1.%d", &version); err == nil {
290-
return
287+
func go1Point() int {
288+
for i := len(build.Default.ReleaseTags) - 1; i >= 0; i-- {
289+
var version int
290+
if _, err := fmt.Sscanf(build.Default.ReleaseTags[i], "go1.%d", &version); err != nil {
291+
continue
291292
}
293+
return version
292294
}
293295
return -1
294296
}

gopls/internal/template/parse_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ package template
77
import (
88
"strings"
99
"testing"
10-
11-
"golang.org/x/tools/gopls/internal/util/moreslices"
1210
)
1311

1412
type datum struct {
@@ -137,7 +135,8 @@ func TestLineCol(t *testing.T) {
137135
t.Errorf("expected %d, %d, got %d, %d for case %d", len(lines)-1, mxlen, lastl, lastc, n)
138136
}
139137
// backwards
140-
for j, s := range moreslices.Reversed(saved) {
138+
for j := len(saved) - 1; j >= 0; j-- {
139+
s := saved[j]
141140
xl, xc := p.LineCol(s.offset)
142141
if xl != s.l || xc != s.c {
143142
t.Errorf("at offset %d(%d), got (%d,%d), expected (%d,%d)", s.offset, j, xl, xc, s.l, s.c)

0 commit comments

Comments
 (0)