Skip to content

ssa,test: lower builtin panic in defer paths#1839

Merged
xushiwei merged 1 commit intoxgo-dev:mainfrom
zhouguangyuan0718:main-fix-defer-panic
May 2, 2026
Merged

ssa,test: lower builtin panic in defer paths#1839
xushiwei merged 1 commit intoxgo-dev:mainfrom
zhouguangyuan0718:main-fix-defer-panic

Conversation

@zhouguangyuan0718
Copy link
Copy Markdown
Contributor

Summary

  • lower builtin panic calls through Builder.Panic during SSA lowering
  • add cl/_testdata/deferpanic coverage for panic raised from a deferred call
  • verify the deferred recover() path prints the expected output

Testing

  • not run

Copy link
Copy Markdown
Contributor

@fennoai fennoai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Clean, well-scoped change that correctly routes the panic builtin through Builder.Panic when it appears as a deferred builtin call (e.g., defer panic("msg")). Without this fix, such calls would fall through to panic("todo: " + fn), crashing the compiler.

The test uses both CHECK directives for IR verification and expect.txt for runtime output verification, which is good.

Two minor nits below.

Comment thread ssa/expr.go
Comment on lines 1475 to +1476
panic("invalid argument for unsafe.Offsetof: must be a selector expression")
case "panic":
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This accesses args[0] without a length guard. Other void-returning builtins in this switch (delete, close, clear) use guards like if len(args) == 1. Since the Go type checker guarantees panic always has exactly one argument, this is safe — and other fixed-arity builtins (complex, real, imag) also skip the guard. Still, adding if len(args) == 1 { would be defensive and self-documenting about the expected arity.

Not a bug, just a consistency observation.

Comment thread ssa/expr.go
case "panic":
b.Panic(args[0])
return
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This blank line was not present before and is inconsistent with the original formatting (the closing } of the switch was immediately followed by panic("todo: " + fn) with no blank line).

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements support for the panic built-in function within the SSA builder and includes a new test case to verify its behavior. The feedback suggests adding an argument count validation for the panic built-in to ensure robustness and consistency with other built-in function implementations in the SSA builder.

Comment thread ssa/expr.go
}
panic("invalid argument for unsafe.Offsetof: must be a selector expression")
case "panic":
b.Panic(args[0])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For robustness, it's good practice to validate the number of arguments for built-in functions. The panic function expects exactly one argument. While the frontend should enforce this, adding a check here would make the SSA builder more resilient to potential issues in earlier compilation stages. Other built-ins like len, cap, and close in this file already have similar checks.

		if len(args) != 1 {
			panic("ssa: builtin panic expects 1 argument")
		}
		b.Panic(args[0])

Handle builtin panic calls during SSA lowering and add a defer/recover regression case for panic raised from a deferred call.

Signed-off-by: ZhouGuangyuan <zhouguangyuan.xian@gmail.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.50%. Comparing base (1196550) to head (7b18956).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1839   +/-   ##
=======================================
  Coverage   88.50%   88.50%           
=======================================
  Files          50       50           
  Lines       14426    14429    +3     
=======================================
+ Hits        12768    12771    +3     
  Misses       1438     1438           
  Partials      220      220           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@xushiwei xushiwei merged commit af8faf2 into xgo-dev:main May 2, 2026
58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants