]> Cypherpunks repositories - gostls13.git/commitdiff
go/ast: add Range token.Pos to RangeStmt
authorcuiweixie <cuiweixie@gmail.com>
Sat, 27 Aug 2022 07:21:29 +0000 (15:21 +0800)
committerDaniel Martí <mvdan@mvdan.cc>
Mon, 5 Sep 2022 08:12:37 +0000 (08:12 +0000)
For #50429

Change-Id: Idb027244f901d9f482c894b5b979a054d0f07de5
Reviewed-on: https://go-review.googlesource.com/c/go/+/426091
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>

api/next/50429.txt [new file with mode: 0644]
src/go/ast/ast.go
src/go/parser/parser.go
src/go/parser/parser_test.go

diff --git a/api/next/50429.txt b/api/next/50429.txt
new file mode 100644 (file)
index 0000000..558937d
--- /dev/null
@@ -0,0 +1 @@
+pkg go/ast, type RangeStmt struct, Range token.Pos #50429
\ No newline at end of file
index 1e089b9e70f03fd050f5049697e54388aac890a6..8d138fc72af6f282645d554eb783827ce2f41166 100644 (file)
@@ -754,6 +754,7 @@ type (
                Key, Value Expr        // Key, Value may be nil
                TokPos     token.Pos   // position of Tok; invalid if Key == nil
                Tok        token.Token // ILLEGAL if Key == nil, ASSIGN, DEFINE
+               Range      token.Pos   // position of "range" keyword
                X          Expr        // value to range over
                Body       *BlockStmt
        }
index 159834172aca688cf5eecc024a1216b0017549ba..1edc2e9a4ddeeb8d1320f152d0481398e537c209 100644 (file)
@@ -2338,6 +2338,7 @@ func (p *parser) parseForStmt() ast.Stmt {
                        Value:  value,
                        TokPos: as.TokPos,
                        Tok:    as.Tok,
+                       Range:  as.Rhs[0].Pos(),
                        X:      x,
                        Body:   body,
                }
index a62dd553ce63a0ac4e9b476a4eede8e5abe34d00..ddb244902c3a7db7754fc46de99035d0b454de71 100644 (file)
@@ -697,3 +697,33 @@ func TestScopeDepthLimit(t *testing.T) {
                }
        }
 }
+
+// proposal #50429
+func TestRangePos(t *testing.T) {
+       testcases := []string{
+               "package p; func _() { for range x {} }",
+               "package p; func _() { for i = range x {} }",
+               "package p; func _() { for i := range x {} }",
+               "package p; func _() { for k, v = range x {} }",
+               "package p; func _() { for k, v := range x {} }",
+       }
+
+       for _, src := range testcases {
+               fset := token.NewFileSet()
+               f, err := ParseFile(fset, src, src, 0)
+               if err != nil {
+                       t.Fatal(err)
+               }
+
+               ast.Inspect(f, func(x ast.Node) bool {
+                       switch s := x.(type) {
+                       case *ast.RangeStmt:
+                               pos := fset.Position(s.Range)
+                               if pos.Offset != strings.Index(src, "range") {
+                                       t.Errorf("%s: got offset %v, want %v", src, pos.Offset, strings.Index(src, "range"))
+                               }
+                       }
+                       return true
+               })
+       }
+}