Skip to content

Commit 824bf24

Browse files
aykevldeadprogram
authored andcommitted
diagnostics: sort package diagnostics by position
Previously, the error messages could be shown out of order. With this patch, the errors are sorted before being shown to the user. This makes it easier to read the error messages, and matches what the `go` toolchain does.
1 parent 80269b9 commit 824bf24

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

diagnostics/diagnostics.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"go/types"
1111
"io"
1212
"path/filepath"
13+
"sort"
1314
"strings"
1415

1516
"github.com/tinygo-org/tinygo/builder"
@@ -59,6 +60,7 @@ func CreateDiagnostics(err error) ProgramDiagnostic {
5960
// Create diagnostics for a single package (though, in practice, it may also be
6061
// used for whole-program diagnostics in some cases).
6162
func createPackageDiagnostic(err error) PackageDiagnostic {
63+
// Extract diagnostics for this package.
6264
var pkgDiag PackageDiagnostic
6365
switch err := err.(type) {
6466
case loader.Errors:
@@ -89,7 +91,20 @@ func createPackageDiagnostic(err error) PackageDiagnostic {
8991
default:
9092
pkgDiag.Diagnostics = createDiagnostics(err)
9193
}
92-
// TODO: sort
94+
95+
// Sort these diagnostics by file/line/column.
96+
sort.SliceStable(pkgDiag.Diagnostics, func(i, j int) bool {
97+
posI := pkgDiag.Diagnostics[i].Pos
98+
posJ := pkgDiag.Diagnostics[j].Pos
99+
if posI.Filename != posJ.Filename {
100+
return posI.Filename < posJ.Filename
101+
}
102+
if posI.Line != posJ.Line {
103+
return posI.Line < posJ.Line
104+
}
105+
return posI.Column < posJ.Column
106+
})
107+
93108
return pkgDiag
94109
}
95110

testdata/errors/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ func main() {
77
}
88

99
// ERROR: # command-line-arguments
10+
// ERROR: types.go:4:6: a declared and not used
1011
// ERROR: types.go:5:6: cannot use "foobar" (untyped string constant) as int value in assignment
1112
// ERROR: types.go:6:2: undefined: nonexisting
12-
// ERROR: types.go:4:6: a declared and not used

0 commit comments

Comments
 (0)