Skip to content

Commit e980a3d

Browse files
committed
go/parser: document that parse functions need valid token.FileSet
+ panic with explicit error if no file set it provided (Not providing a file set is invalid use of the API; panic is the appropriate action rather than returning an error.) Fixes #16018. Change-Id: I207f5b2a2e318d65826bdd9522fce46d614c24ee Reviewed-on: https://go-review.googlesource.com/24010 Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent fee02d2 commit e980a3d

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/go/parser/interface.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const (
7373
//
7474
// The mode parameter controls the amount of source text parsed and other
7575
// optional parser functionality. Position information is recorded in the
76-
// file set fset.
76+
// file set fset, which must not be nil.
7777
//
7878
// If the source couldn't be read, the returned AST is nil and the error
7979
// indicates the specific failure. If the source was read but syntax
@@ -82,6 +82,10 @@ const (
8282
// are returned via a scanner.ErrorList which is sorted by file position.
8383
//
8484
func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode) (f *ast.File, err error) {
85+
if fset == nil {
86+
panic("parser.ParseFile: no token.FileSet provided (fset == nil)")
87+
}
88+
8589
// get source
8690
text, err := readSource(filename, src)
8791
if err != nil {
@@ -125,7 +129,8 @@ func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode)
125129
//
126130
// If filter != nil, only the files with os.FileInfo entries passing through
127131
// the filter (and ending in ".go") are considered. The mode bits are passed
128-
// to ParseFile unchanged. Position information is recorded in fset.
132+
// to ParseFile unchanged. Position information is recorded in fset, which
133+
// must not be nil.
129134
//
130135
// If the directory couldn't be read, a nil map and the respective error are
131136
// returned. If a parse error occurred, a non-nil but incomplete map and the
@@ -169,9 +174,14 @@ func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, m
169174

170175
// ParseExprFrom is a convenience function for parsing an expression.
171176
// The arguments have the same meaning as for Parse, but the source must
172-
// be a valid Go (type or value) expression.
177+
// be a valid Go (type or value) expression. Specifically, fset must not
178+
// be nil.
173179
//
174180
func ParseExprFrom(fset *token.FileSet, filename string, src interface{}, mode Mode) (ast.Expr, error) {
181+
if fset == nil {
182+
panic("parser.ParseExprFrom: no token.FileSet provided (fset == nil)")
183+
}
184+
175185
// get source
176186
text, err := readSource(filename, src)
177187
if err != nil {

0 commit comments

Comments
 (0)