Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiler: allow deferred panic #4571

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
15 changes: 15 additions & 0 deletions testdata/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func main() {

println("\n# panic replace")
panicReplace()

println("\n# defer panic")
deferPanic()
}

func recoverSimple() {
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions testdata/recover.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ recovered: panic
panic 1
panic 2
recovered: panic 2

# defer panic
defer panic
recovered from deferred call: deferred panic
Loading