Skip to content

Commit b7a695b

Browse files
griesemergopherbot
authored andcommitted
cmd/compile/internal/syntax: better error messages for incorrect type parameter list
When parsing a declaration of the form type a [b[c]]d where a, b, c, d stand for identifiers, b[c] is parsed as a type constraint (because an array length must be constant and an index expression b[c] is never constant, even if b is a constant string and c a constant index - this is crucial for disambiguation of the various possibilities). As a result, the error message referred to a missing type parameter name and not an invalid array declaration. Recognize this special case and report both possibilities (because we can't be sure without type information) with the new error: "missing type parameter name or invalid array length" ALso, change the previous error message "type parameter must be named" to "missing type parameter name" which is more fitting as the error refers to an absent type parameter (rather than a type parameter that's somehow invisibly present but unnamed). Fixes #60812. Change-Id: Iaad3b3a9aeff9dfe2184779f3d799f16c7500b34 Reviewed-on: https://go-review.googlesource.com/c/go/+/538856 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 34a5830 commit b7a695b

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

src/cmd/compile/internal/syntax/parser.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2057,7 +2057,11 @@ func (p *parser) paramList(name *Name, typ Expr, close token, requireNames bool)
20572057
pos = end // position error at closing ]
20582058
msg = "missing type constraint"
20592059
} else {
2060-
msg = "type parameters must be named"
2060+
msg = "missing type parameter name"
2061+
// go.dev/issue/60812
2062+
if len(list) == 1 {
2063+
msg += " or invalid array length"
2064+
}
20612065
}
20622066
} else {
20632067
msg = "mixed named and unnamed parameters"

src/cmd/compile/internal/syntax/testdata/issue43527.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ package p
77
type (
88
// 0 and 1-element []-lists are syntactically valid
99
_[A, B /* ERROR missing type constraint */ ] int
10-
_[A, /* ERROR type parameters must be named */ interface{}] int
10+
_[A, /* ERROR missing type parameter name */ interface{}] int
1111
_[A, B, C /* ERROR missing type constraint */ ] int
1212
_[A B, C /* ERROR missing type constraint */ ] int
13-
_[A B, /* ERROR type parameters must be named */ interface{}] int
14-
_[A B, /* ERROR type parameters must be named */ interface{}, C D] int
15-
_[A B, /* ERROR type parameters must be named */ interface{}, C, D] int
16-
_[A B, /* ERROR type parameters must be named */ interface{}, C, interface{}] int
17-
_[A B, C interface{}, D, /* ERROR type parameters must be named */ interface{}] int
13+
_[A B, /* ERROR missing type parameter name */ interface{}] int
14+
_[A B, /* ERROR missing type parameter name */ interface{}, C D] int
15+
_[A B, /* ERROR missing type parameter name */ interface{}, C, D] int
16+
_[A B, /* ERROR missing type parameter name */ interface{}, C, interface{}] int
17+
_[A B, C interface{}, D, /* ERROR missing type parameter name */ interface{}] int
1818
)
1919

2020
// function type parameters use the same parsing routine - just have a couple of tests
2121

2222
func _[A, B /* ERROR missing type constraint */ ]() {}
23-
func _[A, /* ERROR type parameters must be named */ interface{}]() {}
23+
func _[A, /* ERROR missing type parameter name */ interface{}]() {}

src/cmd/compile/internal/syntax/testdata/tparams.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,14 @@ type (
4444
t[a ([]t)] struct{}
4545
t[a ([]t)|t] struct{}
4646
)
47+
48+
// go.dev/issue/60812
49+
type (
50+
t [t]struct{}
51+
t [[]t]struct{}
52+
t [[t]t]struct{}
53+
t [/* ERROR missing type parameter name or invalid array length */ t[t]]struct{}
54+
t [t t[t], /* ERROR missing type parameter name */ t[t]]struct{}
55+
t [/* ERROR missing type parameter name */ t[t], t t[t]]struct{}
56+
t [/* ERROR missing type parameter name */ t[t], t[t]]struct{} // report only first error
57+
)

src/cmd/compile/internal/syntax/testdata/typeset.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type (
4949
_[_ [1]t]t
5050
_[_ ~[]t]t
5151
_[_ ~[1]t]t
52-
t [ /* ERROR type parameters must be named */ t[0]]t
52+
t [ /* ERROR missing type parameter name */ t[0]]t
5353
)
5454

5555
// test cases for go.dev/issue/49174
@@ -81,11 +81,11 @@ type (
8181
type (
8282
_[_ t, t /* ERROR missing type constraint */ ] t
8383
_[_ ~t, t /* ERROR missing type constraint */ ] t
84-
_[_ t, /* ERROR type parameters must be named */ ~t] t
85-
_[_ ~t, /* ERROR type parameters must be named */ ~t] t
84+
_[_ t, /* ERROR missing type parameter name */ ~t] t
85+
_[_ ~t, /* ERROR missing type parameter name */ ~t] t
8686

87-
_[_ t|t, /* ERROR type parameters must be named */ t|t] t
88-
_[_ ~t|t, /* ERROR type parameters must be named */ t|t] t
89-
_[_ t|t, /* ERROR type parameters must be named */ ~t|t] t
90-
_[_ ~t|t, /* ERROR type parameters must be named */ ~t|t] t
87+
_[_ t|t, /* ERROR missing type parameter name */ t|t] t
88+
_[_ ~t|t, /* ERROR missing type parameter name */ t|t] t
89+
_[_ t|t, /* ERROR missing type parameter name */ ~t|t] t
90+
_[_ ~t|t, /* ERROR missing type parameter name */ ~t|t] t
9191
)

0 commit comments

Comments
 (0)