Skip to content

go/parser: scanner errors do not cause a bailout #70726

Open

Description

@mateusz834

While investigating #70725, i noticed that, errors from the scanner are passed directly to p.errors:

p.file = file
eh := func(pos token.Position, msg string) { p.errors.Add(pos, msg) }
p.scanner.Init(p.file, src, eh, scanner.ScanComments)

Per the docs i would assume that the parsing should stop after 10 error (including scanner errors):

AllErrors = SpuriousErrors // report all errors (not just the first 10 on different lines)

It should probably call the p.errors instead:

go/src/go/parser/parser.go

Lines 260 to 281 in 8c3e391

func (p *parser) error(pos token.Pos, msg string) {
if p.trace {
defer un(trace(p, "error: "+msg))
}
epos := p.file.Position(pos)
// If AllErrors is not set, discard errors reported on the same line
// as the last recorded error and stop parsing if there are more than
// 10 errors.
if p.mode&AllErrors == 0 {
n := len(p.errors)
if n > 0 && p.errors[n-1].Pos.Line == epos.Line {
return // discard - likely a spurious error
}
if n > 10 {
panic(bailout{})
}
}
p.errors.Add(epos, msg)
}

CC @griesemer @adonovan

Activity

  1. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Dec 8, 2024
  2. changed the title [-]go/parser: scanner errors do not cause a bailout, when AllErrors is not set[/-] [+]go/parser: scanner errors do not cause a bailout[/+] on Dec 9, 2024
  3. gopherbot commented on Dec 9, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/634538 mentions this issue: go/parser: report scanner error by p.errors

  4. gabyhelp commented on Dec 9, 2024

    @gabyhelp

    Related Code Changes

    (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

  5. added this to the Backlog milestone on Dec 10, 2024
  6. griesemer commented on Dec 11, 2024

    @griesemer
    Contributor

    Note that initializing the scanner with Scanner.Init may lead to an error because Init reads the first character. This would cause the parser's error handler to be called before the parser is fully set up (hence the current structure). The scanner initialization may have to happen just before calling Next.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions