Avoid construction of incorrect syntax trees in presence of errors.
For #19663.
Change-Id: I43025a3cf0fe02cae9a57e8bb9489b5f628c3fd7
Reviewed-on: https://go-review.googlesource.com/38604
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
case string:
f.Note = u
default:
- yyerror("field annotation must be string")
+ yyerror("field tag must be a string")
case nil:
- // noop
+ // no-op
}
if n.Left != nil && n.Left.Op == ONAME {
func importfile(f *Val, indent []byte) *Pkg {
path_, ok := f.U.(string)
if !ok {
- yyerror("import statement not a string")
+ yyerror("import path must be a string")
return nil
}
}
// compute follow set
- // TODO(gri) the args are constants - do as constant expressions?
+ // (not speed critical, advance is only called in error situations)
var followset uint64 = 1 << _EOF // never skip over EOF
for _, tok := range followlist {
followset |= 1 << tok
case _Func:
p.next()
- f.DeclList = append(f.DeclList, p.funcDecl())
+ f.DeclList = appendDecl(f.DeclList, p.funcDecl())
default:
if p.tok == _Lbrace && len(f.DeclList) > 0 && emptyFuncDecl(f.DeclList[len(f.DeclList)-1]) {
if p.got(_Lparen) {
g := new(Group)
for p.tok != _EOF && p.tok != _Rparen {
- list = append(list, f(g))
+ list = appendDecl(list, f(g))
if !p.osemi(_Rparen) {
break
}
return list
}
- return append(list, f(nil))
+ return appendDecl(list, f(nil))
+}
+
+func appendDecl(list []Decl, d Decl) []Decl {
+ if d != nil {
+ return append(list, d)
+ }
+ return list
}
func (p *parser) importDecl(group *Group) Decl {
d.LocalPkgName = n
p.next()
}
- if p.tok == _Literal && p.kind == StringLit {
- d.Path = p.oliteral()
- } else {
- p.syntax_error("missing import path; require quoted string")
+ d.Path = p.oliteral()
+ if d.Path == nil {
+ p.syntax_error("missing import path")
p.advance(_Semi, _Rparen)
+ return nil
}
d.Group = group
if d.Type == nil {
p.syntax_error("in type declaration")
p.advance(_Semi, _Rparen)
+ return nil
}
d.Group = group
d.Pragma = p.pragma
f := new(FuncDecl)
f.pos = p.pos()
- badRecv := false
if p.tok == _Lparen {
rcvr := p.paramList()
switch len(rcvr) {
case 0:
p.error("method has no receiver")
- badRecv = true
- case 1:
- f.Recv = rcvr[0]
default:
p.error("method has multiple receivers")
- badRecv = true
+ fallthrough
+ case 1:
+ f.Recv = rcvr[0]
}
}
// p.error("can only use //go:noescape with external func implementations")
// }
- if badRecv {
- return nil // TODO(gri) better solution
- }
return f
}
type PragmaHandler func(pos src.Pos, text string) Pragma
// Parse parses a single Go source file from src and returns the corresponding
-// syntax tree. If there are errors, Parse will return the first error found.
-// The base argument is only used for position information.
+// syntax tree. If there are errors, Parse will return the first error found,
+// and a possibly partially constructed syntax tree, or nil if no correct package
+// clause was found. The base argument is only used for position information.
//
// If errh != nil, it is called with each error encountered, and Parse will
// process as much source as possible. If errh is nil, Parse will terminate
func (a b, c d) x() // ERROR "multiple receiver"
+type b int
+
var x // error on line 24, not 30
-// ERROR "missing import path"
+// ERROR "import path must be a string"
//import "greek/αβ"
// Import paths must be strings.
-import 42 // ERROR "missing import path; require quoted string"
-import 'a' // ERROR "missing import path; require quoted string"
-import 3.14 // ERROR "missing import path; require quoted string"
-import 0.25i // ERROR "missing import path; require quoted string"
+import 42 // ERROR "import path must be a string"
+import 'a' // ERROR "import path must be a string"
+import 3.14 // ERROR "import path must be a string"
+import 0.25i // ERROR "import path must be a string"