Skip to content

Commit 94e3a30

Browse files
Jay Conrodgriesemer
Jay Conrod
authored andcommitted
cmd/cover: don't try to attach directives to synthetic decls
Fixed an error that occurred in atomic mode. cover adds a global variable declaration that forces sync/atomic to be used. fixDirectives was confused by this declaration since it has an invalid position. These declarations are now skipped. Fixes #22309 Change-Id: I84f5fec13ef847fca35ad49f7704fb93b60503e0 Reviewed-on: https://go-review.googlesource.com/71351 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 5f740d6 commit 94e3a30

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/cmd/cover/cover.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,29 +258,48 @@ func (f *File) Visit(node ast.Node) ast.Visitor {
258258
func (f *File) fixDirectives() []*ast.Comment {
259259
// Scan comments in the file and collect directives. Detach all comments.
260260
var directives []*ast.Comment
261+
prev := token.NoPos // smaller than any valid token.Pos
261262
for _, cg := range f.astFile.Comments {
262263
for _, c := range cg.List {
263264
// Skip directives that will be included by initialComments, i.e., those
264265
// before the package declaration but not in the file doc comment group.
265266
if f.isDirective(c) && (c.Pos() >= f.astFile.Package || cg == f.astFile.Doc) {
267+
// Assume (but verify) that comments are sorted by position.
268+
pos := c.Pos()
269+
if !pos.IsValid() {
270+
log.Fatalf("compiler directive has no position: %q", c.Text)
271+
} else if pos < prev {
272+
log.Fatalf("compiler directives are out of order. %s was before %s.",
273+
f.fset.Position(prev), f.fset.Position(pos))
274+
}
275+
prev = pos
276+
266277
directives = append(directives, c)
267278
}
268279
}
269280
cg.List = nil
270281
}
271282
f.astFile.Comments = nil // force printer to use node comments
283+
if len(directives) == 0 {
284+
// Common case: no directives to attach.
285+
return nil
286+
}
272287

273288
// Iterate over top-level declarations and attach preceding directives.
274289
di := 0
275-
var prevPos token.Pos
290+
prev = token.NoPos
276291
for _, decl := range f.astFile.Decls {
277-
// Assume (but verify) that comments are sorted by position.
292+
// Assume (but verify) that declarations are sorted by position.
278293
pos := decl.Pos()
279-
if pos < prevPos {
280-
log.Fatalf("comments are out of order. %s was before %s.",
281-
f.fset.Position(prevPos), f.fset.Position(pos))
294+
if !pos.IsValid() {
295+
// Synthetic decl. Don't add directives.
296+
continue
297+
}
298+
if pos < prev {
299+
log.Fatalf("declarations are out of order. %s was before %s.",
300+
f.fset.Position(prev), f.fset.Position(pos))
282301
}
283-
prevPos = pos
302+
prev = pos
284303

285304
var doc **ast.CommentGroup
286305
switch d := decl.(type) {

src/cmd/cover/cover_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ func TestDirectives(t *testing.T) {
121121
}
122122
sourceDirectives := findDirectives(source)
123123

124-
// go tool cover -mode=set ./testdata/directives.go
125-
cmd := exec.Command(testenv.GoToolPath(t), "tool", "cover", "-mode=set", testDirectives)
124+
// go tool cover -mode=atomic ./testdata/directives.go
125+
cmd := exec.Command(testenv.GoToolPath(t), "tool", "cover", "-mode=atomic", testDirectives)
126126
cmd.Stderr = os.Stderr
127127
output, err := cmd.Output()
128128
if err != nil {

0 commit comments

Comments
 (0)