Skip to content

Commit

Permalink
diagnostics: sort package diagnostics by position
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aykevl authored and deadprogram committed Jul 20, 2024
1 parent 80269b9 commit 824bf24
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
17 changes: 16 additions & 1 deletion diagnostics/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go/types"
"io"
"path/filepath"
"sort"
"strings"

"github.com/tinygo-org/tinygo/builder"
Expand Down Expand Up @@ -59,6 +60,7 @@ func CreateDiagnostics(err error) ProgramDiagnostic {
// Create diagnostics for a single package (though, in practice, it may also be
// used for whole-program diagnostics in some cases).
func createPackageDiagnostic(err error) PackageDiagnostic {
// Extract diagnostics for this package.
var pkgDiag PackageDiagnostic
switch err := err.(type) {
case loader.Errors:
Expand Down Expand Up @@ -89,7 +91,20 @@ func createPackageDiagnostic(err error) PackageDiagnostic {
default:
pkgDiag.Diagnostics = createDiagnostics(err)
}
// TODO: sort

// Sort these diagnostics by file/line/column.
sort.SliceStable(pkgDiag.Diagnostics, func(i, j int) bool {
posI := pkgDiag.Diagnostics[i].Pos
posJ := pkgDiag.Diagnostics[j].Pos
if posI.Filename != posJ.Filename {
return posI.Filename < posJ.Filename
}
if posI.Line != posJ.Line {
return posI.Line < posJ.Line
}
return posI.Column < posJ.Column
})

return pkgDiag
}

Expand Down
2 changes: 1 addition & 1 deletion testdata/errors/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ func main() {
}

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

0 comments on commit 824bf24

Please sign in to comment.