Skip to content

Commit

Permalink
go/types: match types2 errors for missing index expressions
Browse files Browse the repository at this point in the history
Use "middle" and "final" rather than "2nd" and "3rd" in error messages
for invalid slice expressions. This is the original compiler error
message and many tests check for this specific message.

For #54511.

Change-Id: I86eb739aa7218b7f393fab1ab402732cb9e9a1f6
Reviewed-on: https://go-review.googlesource.com/c/go/+/424906
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
  • Loading branch information
griesemer committed Aug 19, 2022
1 parent 6bedf4a commit 84232b0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/go/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1511,14 +1511,14 @@ func (p *parser) parseIndexOrSliceOrInstance(x ast.Expr) ast.Expr {
slice3 := false
if ncolons == 2 {
slice3 = true
// Check presence of 2nd and 3rd index here rather than during type-checking
// Check presence of middle and final index here rather than during type-checking
// to prevent erroneous programs from passing through gofmt (was issue 7305).
if index[1] == nil {
p.error(colons[0], "2nd index required in 3-index slice")
p.error(colons[0], "middle index required in 3-index slice")
index[1] = &ast.BadExpr{From: colons[0] + 1, To: colons[1]}
}
if index[2] == nil {
p.error(colons[1], "3rd index required in 3-index slice")
p.error(colons[1], "final index required in 3-index slice")
index[2] = &ast.BadExpr{From: colons[1] + 1, To: rbrack}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/go/parser/short_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ var invalids = []string{
`package p; func f() { _ = x = /* ERROR "expected '=='" */ 0 {}};`,
`package p; func f() { _ = 1 == func()int { var x bool; x = x = /* ERROR "expected '=='" */ true; return x }() };`,
`package p; func f() { var s []int; _ = s[] /* ERROR "expected operand" */ };`,
`package p; func f() { var s []int; _ = s[i:j: /* ERROR "3rd index required" */ ] };`,
`package p; func f() { var s []int; _ = s[i: /* ERROR "2nd index required" */ :k] };`,
`package p; func f() { var s []int; _ = s[i: /* ERROR "2nd index required" */ :] };`,
`package p; func f() { var s []int; _ = s[: /* ERROR "2nd index required" */ :] };`,
`package p; func f() { var s []int; _ = s[: /* ERROR "2nd index required" */ ::] };`,
`package p; func f() { var s []int; _ = s[i:j: /* ERROR "final index required" */ ] };`,
`package p; func f() { var s []int; _ = s[i: /* ERROR "middle index required" */ :k] };`,
`package p; func f() { var s []int; _ = s[i: /* ERROR "middle index required" */ :] };`,
`package p; func f() { var s []int; _ = s[: /* ERROR "middle index required" */ :] };`,
`package p; func f() { var s []int; _ = s[: /* ERROR "middle index required" */ ::] };`,
`package p; func f() { var s []int; _ = s[i:j:k: /* ERROR "expected ']'" */ l] };`,
`package p; func f() { for x /* ERROR "boolean or range expression" */ = []string {} }`,
`package p; func f() { for x /* ERROR "boolean or range expression" */ := []string {} }`,
Expand Down
8 changes: 4 additions & 4 deletions src/go/types/testdata/check/expr3.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func indexes() {
_ = a[- /* ERROR "negative" */ 1]
_ = a[- /* ERROR "negative" */ 1 :]
_ = a[: - /* ERROR "negative" */ 1]
_ = a[: /* ERROR "2nd index required" */ : /* ERROR "3rd index required" */ ]
_ = a[0: /* ERROR "2nd index required" */ : /* ERROR "3rd index required" */ ]
_ = a[0: /* ERROR "2nd index required" */ :10]
_ = a[: /* ERROR "middle index required" */ : /* ERROR "final index required" */ ]
_ = a[0: /* ERROR "middle index required" */ : /* ERROR "final index required" */ ]
_ = a[0: /* ERROR "middle index required" */ :10]
_ = a[:10:10]

var a0 int
Expand Down Expand Up @@ -87,7 +87,7 @@ func indexes() {
_ = s[: 1 /* ERROR "overflows" */ <<100]
_ = s[1 /* ERROR "overflows" */ <<100 :]
_ = s[1 /* ERROR "overflows" */ <<100 : 1 /* ERROR "overflows" */ <<100]
_ = s[: /* ERROR "2nd index required" */ : /* ERROR "3rd index required" */ ]
_ = s[: /* ERROR "middle index required" */ : /* ERROR "final index required" */ ]
_ = s[:10:10]
_ = s[10:0 /* ERROR "invalid slice indices" */ :10]
_ = s[0:10:0 /* ERROR "invalid slice indices" */ ]
Expand Down

0 comments on commit 84232b0

Please sign in to comment.