Skip to content

Commit 04d1261

Browse files
aykevldeadprogram
authored andcommitted
build: add package ID to compiler and optimization error messages
This improves error reporting slightly.
1 parent 3e2230e commit 04d1261

File tree

5 files changed

+21
-18
lines changed

5 files changed

+21
-18
lines changed

builder/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
375375
defer mod.Context().Dispose()
376376
defer mod.Dispose()
377377
if errs != nil {
378-
return newMultiError(errs)
378+
return newMultiError(errs, pkg.ImportPath)
379379
}
380380
if err := llvm.VerifyModule(mod, llvm.PrintMessageAction); err != nil {
381381
return errors.New("verification error after compiling package " + pkg.ImportPath)
@@ -1130,7 +1130,7 @@ func optimizeProgram(mod llvm.Module, config *compileopts.Config, globalValues m
11301130
// O0/O1/O2/Os/Oz optimization pipeline).
11311131
errs := transform.Optimize(mod, config)
11321132
if len(errs) > 0 {
1133-
return newMultiError(errs)
1133+
return newMultiError(errs, "")
11341134
}
11351135
if err := llvm.VerifyModule(mod, llvm.PrintMessageAction); err != nil {
11361136
return errors.New("verification failure after LLVM optimization passes")

builder/error.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package builder
33
// MultiError is a list of multiple errors (actually: diagnostics) returned
44
// during LLVM IR generation.
55
type MultiError struct {
6-
Errs []error
6+
ImportPath string
7+
Errs []error
78
}
89

910
func (e *MultiError) Error() string {
@@ -15,14 +16,15 @@ func (e *MultiError) Error() string {
1516
// newMultiError returns a *MultiError if there is more than one error, or
1617
// returns that error directly when there is only one. Passing an empty slice
1718
// will lead to a panic.
18-
func newMultiError(errs []error) error {
19+
// The importPath may be passed if this error is for a single package.
20+
func newMultiError(errs []error, importPath string) error {
1921
switch len(errs) {
2022
case 0:
2123
panic("attempted to create empty MultiError")
2224
case 1:
2325
return errs[0]
2426
default:
25-
return &MultiError{errs}
27+
return &MultiError{importPath, errs}
2628
}
2729
}
2830

diagnostics/diagnostics.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,10 @@ func CreateDiagnostics(err error) ProgramDiagnostic {
4343
if err == nil {
4444
return nil
4545
}
46-
switch err := err.(type) {
47-
case *builder.MultiError:
48-
var diags ProgramDiagnostic
49-
for _, err := range err.Errs {
50-
diags = append(diags, createPackageDiagnostic(err))
51-
}
52-
return diags
53-
default:
54-
return ProgramDiagnostic{
55-
createPackageDiagnostic(err),
56-
}
46+
// Right now, the compiler will only show errors for the first pacakge that
47+
// fails to build. This is likely to change in the future.
48+
return ProgramDiagnostic{
49+
createPackageDiagnostic(err),
5750
}
5851
}
5952

@@ -63,6 +56,14 @@ func createPackageDiagnostic(err error) PackageDiagnostic {
6356
// Extract diagnostics for this package.
6457
var pkgDiag PackageDiagnostic
6558
switch err := err.(type) {
59+
case *builder.MultiError:
60+
if err.ImportPath != "" {
61+
pkgDiag.ImportPath = err.ImportPath
62+
}
63+
for _, err := range err.Errs {
64+
diags := createDiagnostics(err)
65+
pkgDiag.Diagnostics = append(pkgDiag.Diagnostics, diags...)
66+
}
6667
case loader.Errors:
6768
if err.Pkg != nil {
6869
pkgDiag.ImportPath = err.Pkg.ImportPath

testdata/errors/compiler.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ func foo() {
77
//go:align 7
88
var global int
99

10-
// TODO: include package name in the error message
11-
10+
// ERROR: # command-line-arguments
1211
// ERROR: compiler.go:4:6: can only use //go:wasmimport on declarations
1312
// ERROR: compiler.go:8:5: global variable alignment must be a positive power of two

testdata/errors/optimizer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ func main() {
1414
})
1515
}
1616

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

0 commit comments

Comments
 (0)