Skip to content

Commit

Permalink
cue/errors: use cue/token.Pos.Compare
Browse files Browse the repository at this point in the history
Unlike comparePos, it compares by offset rather than line and column,
but that's fine as we don't currently support nor use //line directives.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I2624f99e311237eb278aa389ad9370637d37e7ff
Dispatch-Trailer: {"type":"trybot","CL":1206371,"patchset":2,"ref":"refs/changes/71/1206371/2","targetBranch":"master"}
  • Loading branch information
mvdan authored and cueckoo committed Dec 27, 2024
1 parent bbcdd62 commit b25858b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 21 deletions.
22 changes: 3 additions & 19 deletions cue/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func Positions(err error) []token.Pos {
}
}

slices.SortFunc(a[sortOffset:], comparePos)
slices.SortFunc(a[sortOffset:], token.Pos.Compare)
return slices.Compact(a)
}

Expand Down Expand Up @@ -362,16 +362,6 @@ func (p *list) Add(err Error) {
// Reset resets an List to no errors.
func (p *list) Reset() { *p = (*p)[:0] }

func comparePos(a, b token.Pos) int {
if c := cmp.Compare(a.Filename(), b.Filename()); c != 0 {
return c
}
if c := cmp.Compare(a.Line(), b.Line()); c != 0 {
return c
}
return cmp.Compare(a.Column(), b.Column())
}

func comparePath(a, b []string) int {
for i, x := range a {
if i >= len(b) {
Expand Down Expand Up @@ -414,12 +404,9 @@ func (p list) sanitize() list {
// entry.
func (p list) Sort() {
slices.SortFunc(p, func(a, b Error) int {
if c := comparePos(a.Position(), b.Position()); c != 0 {
if c := a.Position().Compare(b.Position()); c != 0 {
return c
}
// Note that it is not sufficient to simply compare file offsets because
// the offsets do not reflect modified line information (through //line
// comments).
if c := comparePath(a.Path(), b.Path()); c != 0 {
return c
}
Expand Down Expand Up @@ -449,10 +436,7 @@ func approximateEqual(a, b Error) bool {
if aPos == token.NoPos || bPos == token.NoPos {
return a.Error() == b.Error()
}
return aPos.Filename() == bPos.Filename() &&
aPos.Line() == bPos.Line() &&
aPos.Column() == bPos.Column() &&
comparePath(a.Path(), b.Path()) == 0
return aPos.Compare(bPos) == 0 && comparePath(a.Path(), b.Path()) == 0
}

// An List implements the error interface.
Expand Down
6 changes: 4 additions & 2 deletions cue/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,9 +691,11 @@ func TestStdErrorHander(t *testing.T) {
errors.Print(os.Stderr, list, nil)
}

// Note that this is 9 errors when sanitized, and not 8,
// as we currently don't support //line comment directives.
n = len(errors.Errors(errors.Sanitize(list)))
if n != 8 {
t.Errorf("found %d one-per-line errors, expected 8", n)
if n != 9 {
t.Errorf("found %d one-per-line errors, expected 9", n)
errors.Print(os.Stderr, list, nil)
}
}
Expand Down

0 comments on commit b25858b

Please sign in to comment.