@@ -259,13 +259,12 @@ func (tv *tokenVisitor) comment(c *ast.Comment, importByName map[string]*types.P
259
259
260
260
// token emits a token of the specified extent and semantics.
261
261
func (tv * tokenVisitor ) token (start token.Pos , length int , typ semtok.TokenType , modifiers []string ) {
262
- if length <= 0 {
263
- return // vscode doesn't like 0-length Tokens
264
- }
265
262
if ! start .IsValid () {
266
- // This is not worth reporting. TODO(pjw): does it still happen?
267
263
return
268
264
}
265
+ if length <= 0 {
266
+ return // vscode doesn't like 0-length Tokens
267
+ }
269
268
end := start + token .Pos (length )
270
269
if start >= tv .end || end <= tv .start {
271
270
return
@@ -849,19 +848,21 @@ func (tv *tokenVisitor) multiline(start, end token.Pos, tok semtok.TokenType) {
849
848
}
850
849
851
850
// findKeyword returns the position of a keyword by searching within
852
- // the specified range, for when its cannot be exactly known from the AST.
851
+ // the specified range, for when it cannot be exactly known from the AST.
852
+ // It returns NoPos if the keyword was not present in the source due to parse error.
853
853
func (tv * tokenVisitor ) findKeyword (keyword string , start , end token.Pos ) token.Pos {
854
854
// TODO(adonovan): use safetoken.Offset.
855
855
offset := int (start ) - tv .pgf .Tok .Base ()
856
856
last := int (end ) - tv .pgf .Tok .Base ()
857
857
buf := tv .pgf .Src
858
858
idx := bytes .Index (buf [offset :last ], []byte (keyword ))
859
- if idx != - 1 {
860
- return start + token .Pos (idx )
859
+ if idx < 0 {
860
+ // Ill-formed code may form syntax trees without their usual tokens.
861
+ // For example, "type _ <-<-chan int" parses as <-chan (chan int),
862
+ // with two nested ChanTypes but only one chan keyword.
863
+ return token .NoPos
861
864
}
862
- //(in unparsable programs: type _ <-<-chan int)
863
- tv .errorf ("not found:%s %v" , keyword , safetoken .StartPosition (tv .fset , start ))
864
- return token .NoPos
865
+ return start + token .Pos (idx )
865
866
}
866
867
867
868
func (tv * tokenVisitor ) importSpec (spec * ast.ImportSpec ) {
0 commit comments