Skip to content

Commit 2501602

Browse files
niaowaykevl
authored andcommitted
ir: add descriptive error messages to SimpleDCE pass
This commit modifies the SimpleDCE pass to emit errors similar to those emitted by gc when the main function is missing.
1 parent 5cc130b commit 2501602

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

compiler/compiler.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ func Compile(pkgName string, machine llvm.TargetMachine, config *compileopts.Con
237237
c.ir = ir.NewProgram(lprogram, pkgName)
238238

239239
// Run a simple dead code elimination pass.
240-
c.ir.SimpleDCE()
240+
err = c.ir.SimpleDCE()
241+
if err != nil {
242+
return c.mod, nil, []error{err}
243+
}
241244

242245
// Initialize debug information.
243246
if c.Debug() {

ir/passes.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ir
22

33
import (
4+
"errors"
45
"go/types"
56

67
"golang.org/x/tools/go/ssa"
@@ -58,15 +59,22 @@ func signature(sig *types.Signature) string {
5859

5960
// Simple pass that removes dead code. This pass makes later analysis passes
6061
// more useful.
61-
func (p *Program) SimpleDCE() {
62+
func (p *Program) SimpleDCE() error {
6263
// Unmark all functions.
6364
for _, f := range p.Functions {
6465
f.flag = false
6566
}
6667

6768
// Initial set of live functions. Include main.main, *.init and runtime.*
6869
// functions.
69-
main := p.mainPkg.Members["main"].(*ssa.Function)
70+
main, ok := p.mainPkg.Members["main"].(*ssa.Function)
71+
if !ok {
72+
if p.mainPkg.Members["main"] == nil {
73+
return errors.New("function main is undeclared in the main package")
74+
} else {
75+
return errors.New("cannot declare main - must be func")
76+
}
77+
}
7078
runtimePkg := p.Program.ImportedPackage("runtime")
7179
mathPkg := p.Program.ImportedPackage("math")
7280
taskPkg := p.Program.ImportedPackage("internal/task")
@@ -136,4 +144,6 @@ func (p *Program) SimpleDCE() {
136144
}
137145
}
138146
p.Functions = livefunctions
147+
148+
return nil
139149
}

0 commit comments

Comments
 (0)