diff --git a/compiler/compiler.go b/compiler/compiler.go index 14b45a43..a40c5b48 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -237,7 +237,10 @@ func Compile(pkgName string, machine llvm.TargetMachine, config *compileopts.Con c.ir = ir.NewProgram(lprogram, pkgName) // Run a simple dead code elimination pass. - c.ir.SimpleDCE() + err = c.ir.SimpleDCE() + if err != nil { + return c.mod, nil, []error{err} + } // Initialize debug information. if c.Debug() { diff --git a/ir/passes.go b/ir/passes.go index 55390cad..ea5ecccb 100644 --- a/ir/passes.go +++ b/ir/passes.go @@ -1,6 +1,7 @@ package ir import ( + "errors" "go/types" "golang.org/x/tools/go/ssa" @@ -58,7 +59,7 @@ func signature(sig *types.Signature) string { // Simple pass that removes dead code. This pass makes later analysis passes // more useful. -func (p *Program) SimpleDCE() { +func (p *Program) SimpleDCE() error { // Unmark all functions. for _, f := range p.Functions { f.flag = false @@ -66,7 +67,14 @@ func (p *Program) SimpleDCE() { // Initial set of live functions. Include main.main, *.init and runtime.* // functions. - main := p.mainPkg.Members["main"].(*ssa.Function) + main, ok := p.mainPkg.Members["main"].(*ssa.Function) + if !ok { + if p.mainPkg.Members["main"] == nil { + return errors.New("function main is undeclared in the main package") + } else { + return errors.New("cannot declare main - must be func") + } + } runtimePkg := p.Program.ImportedPackage("runtime") mathPkg := p.Program.ImportedPackage("math") taskPkg := p.Program.ImportedPackage("internal/task") @@ -136,4 +144,6 @@ func (p *Program) SimpleDCE() { } } p.Functions = livefunctions + + return nil }