mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile/internal/syntax: better error message for missing type constraint
For #43527. Change-Id: I8c706e68572286d5675383eb2dfd75b5618b646b Reviewed-on: https://go-review.googlesource.com/c/go/+/348730 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
e1c3f2158f
commit
73483df406
4 changed files with 44 additions and 11 deletions
|
|
@ -1908,8 +1908,9 @@ func (p *parser) paramList(name *Name, close token, requireNames bool) (list []*
|
|||
defer p.trace("paramList")()
|
||||
}
|
||||
|
||||
var named int // number of parameters that have an explicit name and type/bound
|
||||
p.list(_Comma, close, func() bool {
|
||||
var named int // number of parameters that have an explicit name and type
|
||||
var typed int // number of parameters that have an explicit type
|
||||
end := p.list(_Comma, close, func() bool {
|
||||
par := p.paramDeclOrNil(name)
|
||||
name = nil // 1st name was consumed if present
|
||||
if par != nil {
|
||||
|
|
@ -1919,6 +1920,9 @@ func (p *parser) paramList(name *Name, close token, requireNames bool) (list []*
|
|||
if par.Name != nil && par.Type != nil {
|
||||
named++
|
||||
}
|
||||
if par.Type != nil {
|
||||
typed++
|
||||
}
|
||||
list = append(list, par)
|
||||
}
|
||||
return false
|
||||
|
|
@ -1939,10 +1943,11 @@ func (p *parser) paramList(name *Name, close token, requireNames bool) (list []*
|
|||
}
|
||||
} else if named != len(list) {
|
||||
// some named => all must have names and types
|
||||
var pos Pos // left-most error position (or unknown)
|
||||
var typ Expr
|
||||
var pos Pos // left-most error position (or unknown)
|
||||
var typ Expr // current type (from right to left)
|
||||
for i := len(list) - 1; i >= 0; i-- {
|
||||
if par := list[i]; par.Type != nil {
|
||||
par := list[i]
|
||||
if par.Type != nil {
|
||||
typ = par.Type
|
||||
if par.Name == nil {
|
||||
pos = typ.Pos()
|
||||
|
|
@ -1961,7 +1966,12 @@ func (p *parser) paramList(name *Name, close token, requireNames bool) (list []*
|
|||
if pos.IsKnown() {
|
||||
var msg string
|
||||
if requireNames {
|
||||
msg = "type parameters must be named"
|
||||
if named == typed {
|
||||
pos = end // position error at closing ]
|
||||
msg = "missing type constraint"
|
||||
} else {
|
||||
msg = "type parameters must be named"
|
||||
}
|
||||
} else {
|
||||
msg = "mixed named and unnamed parameters"
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue