Commit af558acc authored by Matthew Dempsky's avatar Matthew Dempsky

cmd/compile: fix contrived line number errors

If a general comment contains multiple newline characters, we can't
simply unread one and then re-lex it via the general whitespace lexing
phase, because then we'll reset lineno to the line before the "*/"
marker, rather than keeping it where we found the "/*" marker.

Also, for processing imports, call importfile before advancing the
lexer with p.next(), so that lineno reflects the line where we found
the import path, and not the token afterwards.

Fixes #14520.

Change-Id: I785a2d83d632280113d4b757de0d57c88ba2caf4
Reviewed-on: https://go-review.googlesource.com/19934Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent a131a66e
......@@ -1000,31 +1000,31 @@ l0:
case '/':
c1 = l.getr()
if c1 == '*' {
nl := false
for {
c = l.getr()
if c == '\n' {
nl = true
}
for c == '*' {
for {
if c == '*' {
c = l.getr()
if c == '/' {
if nl {
l.ungetr('\n')
}
goto l0
}
if c == '\n' {
nl = true
break
}
continue
}
if c == EOF {
Yyerror("eof in comment")
errorexit()
}
c = l.getr()
}
// A comment containing newlines acts like a newline.
if lexlineno > lineno && nlsemi {
if Debug['x'] != 0 {
fmt.Printf("lex: implicit semi\n")
}
l.tok = ';'
return
}
goto l0
}
if c1 == '/' {
......
......@@ -333,20 +333,22 @@ func (p *parser) importdcl() {
}
line := int32(parserline())
path := p.val
p.next()
importfile(&path, p.indent)
if importpkg == nil {
// We need to clear importpkg before calling p.next(),
// otherwise it will affect lexlineno.
// TODO(mdempsky): Fix this clumsy API.
importfile(&p.val, p.indent)
ipkg := importpkg
importpkg = nil
p.next()
if ipkg == nil {
if nerrors == 0 {
Fatalf("phase error in import")
}
return
}
ipkg := importpkg
importpkg = nil
ipkg.Direct = true
if my == nil {
......
// errorcheck
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package f
import /* // ERROR "import path" */ `
bogus`
func f(x int /* // ERROR "unexpected semicolon"
*/)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment