diff options
author | Ayke van Laethem <[email protected]> | 2024-12-14 12:36:17 +0100 |
---|---|---|
committer | Ron Evans <[email protected]> | 2024-12-14 13:34:58 +0100 |
commit | f2465992533adcf9f98dfea359aba9171a80902f (patch) | |
tree | d8783db045b535b0af533270f043e2a225ce6a14 /compiler/symbol.go | |
parent | ec3f387f21ed97b504eaa4e7cee6bed8cf13bdbb (diff) | |
download | tinygo-f2465992533adcf9f98dfea359aba9171a80902f.tar.gz tinygo-f2465992533adcf9f98dfea359aba9171a80902f.zip |
compiler: report error instead of crashing on missing function body
This can happen with generic functions, see:
https://github.com/tinygo-org/tinygo/issues/4486
Diffstat (limited to 'compiler/symbol.go')
-rw-r--r-- | compiler/symbol.go | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/compiler/symbol.go b/compiler/symbol.go index 93c27803e..ff7ef0550 100644 --- a/compiler/symbol.go +++ b/compiler/symbol.go @@ -231,6 +231,15 @@ func (c *compilerContext) getFunction(fn *ssa.Function) (llvm.Type, llvm.Value) } } + // Build the function if needed. + c.maybeCreateSyntheticFunction(fn, llvmFn) + + return fnType, llvmFn +} + +// If this is a synthetic function (such as a generic function or a wrapper), +// create it now. +func (c *compilerContext) maybeCreateSyntheticFunction(fn *ssa.Function, llvmFn llvm.Value) { // Synthetic functions are functions that do not appear in the source code, // they are artificially constructed. Usually they are wrapper functions // that are not referenced anywhere except in a SSA call instruction so @@ -238,6 +247,10 @@ func (c *compilerContext) getFunction(fn *ssa.Function) (llvm.Type, llvm.Value) // The exception is the package initializer, which does appear in the // *ssa.Package members and so shouldn't be created here. if fn.Synthetic != "" && fn.Synthetic != "package initializer" && fn.Synthetic != "generic function" && fn.Synthetic != "range-over-func yield" { + if len(fn.Blocks) == 0 { + c.addError(fn.Pos(), "missing function body") + return + } irbuilder := c.ctx.NewBuilder() b := newBuilder(c, irbuilder, fn) b.createFunction() @@ -245,8 +258,6 @@ func (c *compilerContext) getFunction(fn *ssa.Function) (llvm.Type, llvm.Value) llvmFn.SetLinkage(llvm.LinkOnceODRLinkage) llvmFn.SetUnnamedAddr(true) } - - return fnType, llvmFn } // getFunctionInfo returns information about a function that is not directly |