defer un(trace(p, keyword.String()+"Spec"))
}
- pos := p.pos
idents := p.parseIdentList()
- typ := p.tryIdentOrType()
+ var typ ast.Expr
var values []ast.Expr
- // always permit optional initialization for more tolerant parsing
- if p.tok == token.ASSIGN {
- p.next()
- values = p.parseList(true)
- }
- p.expectSemi() // call before accessing p.linecomment
-
switch keyword {
+ case token.CONST:
+ // always permit optional type and initialization for more tolerant parsing
+ if p.tok != token.EOF && p.tok != token.SEMICOLON && p.tok != token.RPAREN {
+ typ = p.tryIdentOrType()
+ if p.tok == token.ASSIGN {
+ p.next()
+ values = p.parseList(true)
+ }
+ }
case token.VAR:
- if typ == nil && values == nil {
- p.error(pos, "missing variable type or initialization")
+ if p.tok != token.ASSIGN {
+ typ = p.parseType()
}
- case token.CONST:
- if values == nil && (iota == 0 || typ != nil) {
- p.error(pos, "missing constant value")
+ if p.tok == token.ASSIGN {
+ p.next()
+ values = p.parseList(true)
}
+ default:
+ panic("unreachable")
}
+ p.expectSemi() // call before accessing p.linecomment
spec := &ast.ValueSpec{
Doc: doc,
`package p; func f() (a b string /* ERROR "missing ','" */ , ok bool)`,
// issue 9639
- `package p; var x /* ERROR "missing variable type or initialization" */ , y, z;`,
- `package p; const x /* ERROR "missing constant value" */ ;`,
- `package p; const x /* ERROR "missing constant value" */ int;`,
- `package p; const (x = 0; y; z /* ERROR "missing constant value" */ int);`,
+ `package p; var x, y, z; /* ERROR "expected type" */`,
// issue 12437
`package p; var _ = struct { x int, /* ERROR "expected ';', found ','" */ }{};`,
}
// Identifier and expression arity must match.
-// The first error message is produced by the parser.
-// In a real-world scenario, the type-checker would not be run
-// in this case and the 2nd error message would not appear.
-const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
+const _ /* ERROR "missing init expr for _" */
const _ = 1, 2 /* ERROR "extra init expr 2" */
-const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
+const _ /* ERROR "missing init expr for _" */ int
const _ int = 1, 2 /* ERROR "extra init expr 2" */
const (
- _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
+ _ /* ERROR "missing init expr for _" */
_ = 1, 2 /* ERROR "extra init expr 2" */
- _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
+ _ /* ERROR "missing init expr for _" */ int
_ int = 1, 2 /* ERROR "extra init expr 2" */
)
)
func _() {
- const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
+ const _ /* ERROR "missing init expr for _" */
const _ = 1, 2 /* ERROR "extra init expr 2" */
- const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
+ const _ /* ERROR "missing init expr for _" */ int
const _ int = 1, 2 /* ERROR "extra init expr 2" */
const (
- _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
+ _ /* ERROR "missing init expr for _" */
_ = 1, 2 /* ERROR "extra init expr 2" */
- _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
+ _ /* ERROR "missing init expr for _" */ int
_ int = 1, 2 /* ERROR "extra init expr 2" */
)
var _ int
var _, _ int
-// The first error message is produced by the parser.
-// In a real-world scenario, the type-checker would not be run
-// in this case and the 2nd error message would not appear.
-var _ /* ERROR "missing variable type" */ /* ERROR "missing type or init expr" */
-var _ /* ERROR "missing variable type" */ /* ERROR "missing type or init expr" */, _
-var _ /* ERROR "missing variable type" */ /* ERROR "missing type or init expr" */, _, _
+var _; /* ERROR "expected type" */
+var _, _; /* ERROR "expected type" */
+var _, _, _; /* ERROR "expected type" */
// The initializer must be an expression.
var _ = int /* ERROR "not an expression" */