mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-12 06:53:40 +00:00
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.
This commit is contained in:
@@ -1680,6 +1680,10 @@ func (b *builder) createBuiltin(argTypes []types.Type, argValues []llvm.Value, c
|
|||||||
result = b.CreateSelect(cmp, result, arg, "")
|
result = b.CreateSelect(cmp, result, arg, "")
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
|
case "panic":
|
||||||
|
// This is rare, but happens in "defer panic()".
|
||||||
|
b.createRuntimeInvoke("_panic", argValues, "")
|
||||||
|
return llvm.Value{}, nil
|
||||||
case "print", "println":
|
case "print", "println":
|
||||||
for i, value := range argValues {
|
for i, value := range argValues {
|
||||||
if i >= 1 && callName == "println" {
|
if i >= 1 && callName == "println" {
|
||||||
|
|||||||
Vendored
+15
@@ -19,6 +19,9 @@ func main() {
|
|||||||
|
|
||||||
println("\n# panic replace")
|
println("\n# panic replace")
|
||||||
panicReplace()
|
panicReplace()
|
||||||
|
|
||||||
|
println("\n# defer panic")
|
||||||
|
deferPanic()
|
||||||
}
|
}
|
||||||
|
|
||||||
func recoverSimple() {
|
func recoverSimple() {
|
||||||
@@ -89,6 +92,18 @@ func panicReplace() {
|
|||||||
panic("panic 1")
|
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{}) {
|
func printitf(msg string, itf interface{}) {
|
||||||
switch itf := itf.(type) {
|
switch itf := itf.(type) {
|
||||||
case string:
|
case string:
|
||||||
|
|||||||
Vendored
+4
@@ -23,3 +23,7 @@ recovered: panic
|
|||||||
panic 1
|
panic 1
|
||||||
panic 2
|
panic 2
|
||||||
recovered: panic 2
|
recovered: panic 2
|
||||||
|
|
||||||
|
# defer panic
|
||||||
|
defer panic
|
||||||
|
recovered from deferred call: deferred panic
|
||||||
|
|||||||
Reference in New Issue
Block a user