Skip to content

Commit

Permalink
build: add package ID to compiler and optimization error messages
Browse files Browse the repository at this point in the history
This improves error reporting slightly.
  • Loading branch information
aykevl authored and deadprogram committed Jul 21, 2024
1 parent 3e2230e commit 04d1261
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
4 changes: 2 additions & 2 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
defer mod.Context().Dispose()
defer mod.Dispose()
if errs != nil {
return newMultiError(errs)
return newMultiError(errs, pkg.ImportPath)
}
if err := llvm.VerifyModule(mod, llvm.PrintMessageAction); err != nil {
return errors.New("verification error after compiling package " + pkg.ImportPath)
Expand Down Expand Up @@ -1130,7 +1130,7 @@ func optimizeProgram(mod llvm.Module, config *compileopts.Config, globalValues m
// O0/O1/O2/Os/Oz optimization pipeline).
errs := transform.Optimize(mod, config)
if len(errs) > 0 {
return newMultiError(errs)
return newMultiError(errs, "")
}
if err := llvm.VerifyModule(mod, llvm.PrintMessageAction); err != nil {
return errors.New("verification failure after LLVM optimization passes")
Expand Down
8 changes: 5 additions & 3 deletions builder/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package builder
// MultiError is a list of multiple errors (actually: diagnostics) returned
// during LLVM IR generation.
type MultiError struct {
Errs []error
ImportPath string
Errs []error
}

func (e *MultiError) Error() string {
Expand All @@ -15,14 +16,15 @@ func (e *MultiError) Error() string {
// newMultiError returns a *MultiError if there is more than one error, or
// returns that error directly when there is only one. Passing an empty slice
// will lead to a panic.
func newMultiError(errs []error) error {
// The importPath may be passed if this error is for a single package.
func newMultiError(errs []error, importPath string) error {
switch len(errs) {
case 0:
panic("attempted to create empty MultiError")
case 1:
return errs[0]
default:
return &MultiError{errs}
return &MultiError{importPath, errs}
}
}

Expand Down
23 changes: 12 additions & 11 deletions diagnostics/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,10 @@ func CreateDiagnostics(err error) ProgramDiagnostic {
if err == nil {
return nil
}
switch err := err.(type) {
case *builder.MultiError:
var diags ProgramDiagnostic
for _, err := range err.Errs {
diags = append(diags, createPackageDiagnostic(err))
}
return diags
default:
return ProgramDiagnostic{
createPackageDiagnostic(err),
}
// Right now, the compiler will only show errors for the first pacakge that
// fails to build. This is likely to change in the future.
return ProgramDiagnostic{
createPackageDiagnostic(err),
}
}

Expand All @@ -63,6 +56,14 @@ func createPackageDiagnostic(err error) PackageDiagnostic {
// Extract diagnostics for this package.
var pkgDiag PackageDiagnostic
switch err := err.(type) {
case *builder.MultiError:
if err.ImportPath != "" {
pkgDiag.ImportPath = err.ImportPath
}
for _, err := range err.Errs {
diags := createDiagnostics(err)
pkgDiag.Diagnostics = append(pkgDiag.Diagnostics, diags...)
}
case loader.Errors:
if err.Pkg != nil {
pkgDiag.ImportPath = err.Pkg.ImportPath
Expand Down
3 changes: 1 addition & 2 deletions testdata/errors/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ func foo() {
//go:align 7
var global int

// TODO: include package name in the error message

// ERROR: # command-line-arguments
// ERROR: compiler.go:4:6: can only use //go:wasmimport on declarations
// ERROR: compiler.go:8:5: global variable alignment must be a positive power of two
1 change: 1 addition & 0 deletions testdata/errors/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ func main() {
})
}

// ERROR: # command-line-arguments
// ERROR: optimizer.go:9:15: interrupt ID is not a constant
// ERROR: optimizer.go:13:15: interrupt ID is not a constant

0 comments on commit 04d1261

Please sign in to comment.