aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2016-06-10 14:18:29 -0700
committerMatthew Dempsky <mdempsky@google.com>2016-08-16 14:32:09 -0700
commit83153e7f372a5822a771e132885633cd24d3df12 (patch)
tree040f739564a630c2c34bc624e43c2ef29c5486d0
parent7a8201b92470c729e4e30fd22d16dca600ec9b7e (diff)
downloadgo-83153e7f372a5822a771e132885633cd24d3df12.tar.gz
go-83153e7f372a5822a771e132885633cd24d3df12.zip
cmd/compile/internal/syntax: emit errors for unexpected top-level tokens
Fixes cmd/go's TestIssue7108. Change-Id: I8436b0e3e5a9b36649d46a9c2c741d820115a5d1
-rw-r--r--src/cmd/compile/internal/syntax/parser.go17
1 files changed, 11 insertions, 6 deletions
diff --git a/src/cmd/compile/internal/syntax/parser.go b/src/cmd/compile/internal/syntax/parser.go
index 7093a7a606..3666a49082 100644
--- a/src/cmd/compile/internal/syntax/parser.go
+++ b/src/cmd/compile/internal/syntax/parser.go
@@ -230,12 +230,12 @@ func (p *parser) file() *File {
f.DeclList = append(f.DeclList, p.funcDecl())
default:
- // if p.tok == _Lbrace && l != nil && l.End.N.Op == ODC_Func && l.End.N.Nbody == nil {
- // // opening { of function declaration on next line
- // p.syntax_error("unexpected semicolon or newline before {")
- // } else {
- // p.syntax_error("non-declaration statement outside function body")
- // }
+ if p.tok == _Lbrace && len(f.DeclList) > 0 && emptyFuncDecl(f.DeclList[len(f.DeclList)-1]) {
+ // opening { of function declaration on next line
+ p.syntax_error("unexpected semicolon or newline before {")
+ } else {
+ p.syntax_error("non-declaration statement outside function body")
+ }
p.advance(_Const, _Type, _Var, _Func)
continue
}
@@ -253,6 +253,11 @@ func (p *parser) file() *File {
return f
}
+func emptyFuncDecl(dcl Decl) bool {
+ f, ok := dcl.(*FuncDecl)
+ return ok && f.Body == nil
+}
+
// ----------------------------------------------------------------------------
// Declarations