]> Cypherpunks repositories - gostls13.git/commitdiff
go/parser: report error for var/const decls with missing init exprs
authorRobert Griesemer <gri@golang.org>
Fri, 23 Jan 2015 05:54:26 +0000 (21:54 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 23 Jan 2015 17:01:28 +0000 (17:01 +0000)
Fixes #9639.

Change-Id: I311045d3df26b29b9380c159ef4727e85650d13b
Reviewed-on: https://go-review.googlesource.com/3211
Reviewed-by: Alan Donovan <adonovan@google.com>
src/go/parser/parser.go
src/go/parser/short_test.go

index c9dbd06ad24114497ae44f06b6e4d3b4d9276169..0409122c81787dcd1d8df2c95255f7e1f99b0bf0 100644 (file)
@@ -2228,6 +2228,7 @@ func (p *parser) parseValueSpec(doc *ast.CommentGroup, keyword token.Token, iota
                defer un(trace(p, keyword.String()+"Spec"))
        }
 
+       pos := p.pos
        idents := p.parseIdentList()
        typ := p.tryType()
        var values []ast.Expr
@@ -2238,6 +2239,17 @@ func (p *parser) parseValueSpec(doc *ast.CommentGroup, keyword token.Token, iota
        }
        p.expectSemi() // call before accessing p.linecomment
 
+       switch keyword {
+       case token.VAR:
+               if typ == nil && values == nil {
+                       p.error(pos, "missing variable type or initialization")
+               }
+       case token.CONST:
+               if values == nil && (iota == 0 || typ != nil) {
+                       p.error(pos, "missing constant value")
+               }
+       }
+
        // Go spec: The scope of a constant or variable identifier declared inside
        // a function begins at the end of the ConstSpec or VarSpec and ends at
        // the end of the innermost containing block.
index 7d12170c0e43ab3b0623130c86109bad6b78c5fd..14a14d5a5928b1b02d7c57b91ec2959655a60b3e 100644 (file)
@@ -43,6 +43,7 @@ var valids = []string{
        `package p; func _() { map[int]int{}[0]++; map[int]int{}[0] += 1 }`,
        `package p; func _(x interface{f()}) { interface{f()}(x).f() }`,
        `package p; func _(x chan int) { chan int(x) <- 0 }`,
+       `package p; const (x = 0; y; z)`, // issue 9639
 }
 
 func TestValid(t *testing.T) {
@@ -97,7 +98,11 @@ var invalids = []string{
        `package p; func f() { go f /* ERROR HERE "function must be invoked" */ }`,
        `package p; func f() { defer func() {} /* ERROR HERE "function must be invoked" */ }`,
        `package p; func f() { go func() { func() { f(x func /* ERROR "expected '\)'" */ (){}) } } }`,
-       `package p; func f() (a b string /* ERROR "expected '\)'" */ , ok bool) // issue 8656`,
+       `package p; func f() (a b string /* ERROR "expected '\)'" */ , ok bool)`,         // issue 8656
+       `package p; var x /* ERROR "missing variable type or initialization" */ , y, z;`, // issue 9639
+       `package p; const x /* ERROR "missing constant value" */ ;`,                      // issue 9639
+       `package p; const x /* ERROR "missing constant value" */ int;`,                   // issue 9639
+       `package p; const (x = 0; y; z /* ERROR "missing constant value" */ int);`,       // issue 9639
 }
 
 func TestInvalid(t *testing.T) {