diff options
| author | Robert Griesemer <gri@golang.org> | 2024-06-10 10:27:26 -0700 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2024-06-10 17:49:19 +0000 |
| commit | 4256ec16616827b8ef5a19f74599aa44ffe6111a (patch) | |
| tree | d26dae0ca80e03ff3d939cd640c98d33a4462571 /src/cmd/compile/internal/syntax | |
| parent | f515c1bac708c46f3bb4811439fc75bd013300c9 (diff) | |
| download | go-4256ec16616827b8ef5a19f74599aa44ffe6111a.tar.xz | |
cmd/compile/internal/syntax: return correct start pos for KeyValueExprs
Fixes #67866.
Change-Id: Id9d345aab87e493b8ed94319c5acaa1900362648
Reviewed-on: https://go-review.googlesource.com/c/go/+/591695
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/cmd/compile/internal/syntax')
| -rw-r--r-- | src/cmd/compile/internal/syntax/issues_test.go | 51 | ||||
| -rw-r--r-- | src/cmd/compile/internal/syntax/positions.go | 3 |
2 files changed, 53 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/syntax/issues_test.go b/src/cmd/compile/internal/syntax/issues_test.go new file mode 100644 index 0000000000..d72890ca5e --- /dev/null +++ b/src/cmd/compile/internal/syntax/issues_test.go @@ -0,0 +1,51 @@ +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file holds test cases for individual issues +// for which there is (currently) no better location. + +package syntax + +import ( + "strings" + "testing" +) + +func TestIssue67866(t *testing.T) { + var tests = []string{ + "package p; var _ = T{@0: 0}", + "package p; var _ = T{@1 + 2: 0}", + "package p; var _ = T{@x[i]: 0}", + "package p; var _ = T{@f(1, 2, 3): 0}", + "package p; var _ = T{@a + f(b) + <-ch: 0}", + } + + for _, src := range tests { + // identify column position of @ and remove it from src + i := strings.Index(src, "@") + if i < 0 { + t.Errorf("%s: invalid test case (missing @)", src) + continue + } + src = src[:i] + src[i+1:] + want := colbase + uint(i) + + f, err := Parse(nil, strings.NewReader(src), nil, nil, 0) + if err != nil { + t.Errorf("%s: %v", src, err) + continue + } + + // locate KeyValueExpr + Inspect(f, func(n Node) bool { + _, ok := n.(*KeyValueExpr) + if ok { + if got := StartPos(n).Col(); got != want { + t.Errorf("%s: got col = %d, want %d", src, got, want) + } + } + return !ok + }) + } +} diff --git a/src/cmd/compile/internal/syntax/positions.go b/src/cmd/compile/internal/syntax/positions.go index 93596559a0..419855f70f 100644 --- a/src/cmd/compile/internal/syntax/positions.go +++ b/src/cmd/compile/internal/syntax/positions.go @@ -36,7 +36,8 @@ func StartPos(n Node) Pos { continue } return n.Pos() - // case *KeyValueExpr: + case *KeyValueExpr: + m = n.Key // case *FuncLit: // case *ParenExpr: case *SelectorExpr: |
