aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2024-10-31 09:18:32 +0100
committerAyke <[email protected]>2024-11-01 09:11:38 +0100
commit449eefdb19c58df9303ee5b9fe62fe18876eb2f9 (patch)
tree81eda99e6092b6508d5dab27c26774b1ea77b596
parent058f62ac08ace927f01b005e7b0a21b1a41590a7 (diff)
downloadtinygo-449eefdb19c58df9303ee5b9fe62fe18876eb2f9.tar.gz
tinygo-449eefdb19c58df9303ee5b9fe62fe18876eb2f9.zip
compiler: allow deferred panic
This is rare, but apparently some programs do this: defer panic("...") This is emitted in the IR as a builtin function.
-rw-r--r--compiler/compiler.go4
-rw-r--r--testdata/recover.go15
-rw-r--r--testdata/recover.txt4
3 files changed, 23 insertions, 0 deletions
diff --git a/compiler/compiler.go b/compiler/compiler.go
index 752e4a5c6..981993fe7 100644
--- a/compiler/compiler.go
+++ b/compiler/compiler.go
@@ -1680,6 +1680,10 @@ func (b *builder) createBuiltin(argTypes []types.Type, argValues []llvm.Value, c
result = b.CreateSelect(cmp, result, arg, "")
}
return result, nil
+ case "panic":
+ // This is rare, but happens in "defer panic()".
+ b.createRuntimeInvoke("_panic", argValues, "")
+ return llvm.Value{}, nil
case "print", "println":
for i, value := range argValues {
if i >= 1 && callName == "println" {
diff --git a/testdata/recover.go b/testdata/recover.go
index ced90cfae..c7c02c94a 100644
--- a/testdata/recover.go
+++ b/testdata/recover.go
@@ -19,6 +19,9 @@ func main() {
println("\n# panic replace")
panicReplace()
+
+ println("\n# defer panic")
+ deferPanic()
}
func recoverSimple() {
@@ -89,6 +92,18 @@ func panicReplace() {
panic("panic 1")
}
+func deferPanic() {
+ defer func() {
+ printitf("recovered from deferred call:", recover())
+ }()
+
+ // This recover should not do anything.
+ defer recover()
+
+ defer panic("deferred panic")
+ println("defer panic")
+}
+
func printitf(msg string, itf interface{}) {
switch itf := itf.(type) {
case string:
diff --git a/testdata/recover.txt b/testdata/recover.txt
index d27649855..357505881 100644
--- a/testdata/recover.txt
+++ b/testdata/recover.txt
@@ -23,3 +23,7 @@ recovered: panic
panic 1
panic 2
recovered: panic 2
+
+# defer panic
+defer panic
+recovered from deferred call: deferred panic