Skip to content

Commit f246599

Browse files
aykevldeadprogram
authored andcommitted
compiler: report error instead of crashing on missing function body
This can happen with generic functions, see: #4486
1 parent ec3f387 commit f246599

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

compiler/symbol.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,22 +231,33 @@ func (c *compilerContext) getFunction(fn *ssa.Function) (llvm.Type, llvm.Value)
231231
}
232232
}
233233

234+
// Build the function if needed.
235+
c.maybeCreateSyntheticFunction(fn, llvmFn)
236+
237+
return fnType, llvmFn
238+
}
239+
240+
// If this is a synthetic function (such as a generic function or a wrapper),
241+
// create it now.
242+
func (c *compilerContext) maybeCreateSyntheticFunction(fn *ssa.Function, llvmFn llvm.Value) {
234243
// Synthetic functions are functions that do not appear in the source code,
235244
// they are artificially constructed. Usually they are wrapper functions
236245
// that are not referenced anywhere except in a SSA call instruction so
237246
// should be created right away.
238247
// The exception is the package initializer, which does appear in the
239248
// *ssa.Package members and so shouldn't be created here.
240249
if fn.Synthetic != "" && fn.Synthetic != "package initializer" && fn.Synthetic != "generic function" && fn.Synthetic != "range-over-func yield" {
250+
if len(fn.Blocks) == 0 {
251+
c.addError(fn.Pos(), "missing function body")
252+
return
253+
}
241254
irbuilder := c.ctx.NewBuilder()
242255
b := newBuilder(c, irbuilder, fn)
243256
b.createFunction()
244257
irbuilder.Dispose()
245258
llvmFn.SetLinkage(llvm.LinkOnceODRLinkage)
246259
llvmFn.SetUnnamedAddr(true)
247260
}
248-
249-
return fnType, llvmFn
250261
}
251262

252263
// getFunctionInfo returns information about a function that is not directly

testdata/errors/compiler.go

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

10+
// Test for https://github.com/tinygo-org/tinygo/issues/4486
11+
type genericType[T any] struct{}
12+
13+
func (genericType[T]) methodWithoutBody()
14+
15+
func callMethodWithoutBody() {
16+
msg := &genericType[int]{}
17+
msg.methodWithoutBody()
18+
}
19+
1020
// ERROR: # command-line-arguments
1121
// ERROR: compiler.go:4:6: can only use //go:wasmimport on declarations
1222
// ERROR: compiler.go:8:5: global variable alignment must be a positive power of two
23+
// ERROR: compiler.go:13:23: missing function body

0 commit comments

Comments
 (0)