Skip to content

Implement panic expression #7073

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

Merged
merged 31 commits into from
Apr 30, 2025
Merged

Implement panic expression #7073

merged 31 commits into from
Apr 30, 2025

Conversation

ironcev
Copy link
Member

@ironcev ironcev commented Apr 8, 2025

Description

This PR introduces the panic expression to the language, as defined in the ABI Errors RFC.

The panic expression can be used without arguments, or accept an argument that implements the std::marker::Error trait. The Error trait is implemented by the compiler for the unit (), string slices, and #[error_type] enums.

Using the panic expression without arguments gives the symetry with the return expression and acts in the same way as having unit as an argument.

panic;
panic ();
panic "This is some error.";
panic Errors::SomeError(42);

Panicking without an argument or with unit as argument is discouraged to use. In the upcoming PR that finalizes the ABI errors feature, we will emit a warning if the panic is used without arguments or with unit as argument.

panic expression is available in all program kinds. In predicates it currently compiles only to revert. Once __dbg intrinsic is implemented, we can consider compiling to it in predicates. In the upcoming PR, the error_codes entry in the ABI JSON will be available for all program kinds.

The dead code analysis for the panic expression is implemented in the straightforward way, following the current approach of connecting reverts to the exit node. This will be revisted in a separate PR, together with the open TODOs in the DCA implementation of Return. Essentially, we want reverting/panicking to connect to program exit and implicit returns to the exit node.

Additionally, the PR:

Partially addresses #6765.

Breaking Change

panic is a new reserved keyword. Once the error_type feature becomes integrated, compiling any existing code containing a "panic" as an identifier will result in an "Identifiers cannot be a reserved keyword." error.

In this PR, panic keyword is hidden behind the error_type feature flag. This prevents existing code from breaking, unless opted-in.

Note that, although being behind a feature flag, panic cannot be used in conditional compilation, means in combination with the #[cfg(experimental_error_type = true/false)]. The reason is that cfg evaluation happens after parsing, and we can either parse panic as a keyword or identifier during the parsing, because we cannot distinguish ambiguous cases like panic; or just panic.

This limitation means, that introducing panic to std can be done only once the error_type feature is fully integrated. In practice, this is not a serious limitation, because introducing panic in std would anyhow, due to effort and design decisions, happen after the feature is integrated.

We will provide a migration for this breaking change that will migrate the existing "panic" identifiers to "r#panic".

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@ironcev ironcev self-assigned this Apr 8, 2025
@ironcev ironcev temporarily deployed to fuel-sway-bot April 8, 2025 10:37 — with GitHub Actions Inactive
@ironcev ironcev added compiler General compiler. Should eventually become more specific as the issue is triaged compiler: ir IRgen and sway-ir including optimization passes language feature Core language features visible to end users breaking May cause existing user code to break. Requires a minor or major release. compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler: parser Everything to do with the parser compiler: ui Mostly compiler messages labels Apr 8, 2025
Copy link

codspeed-hq bot commented Apr 8, 2025

CodSpeed Performance Report

Merging #7073 will not alter performance

Comparing ironcev/panic-expression (cd86555) with master (d258921)

Summary

✅ 22 untouched benchmarks

@ironcev ironcev temporarily deployed to fuel-sway-bot April 8, 2025 11:15 — with GitHub Actions Inactive
@ironcev ironcev temporarily deployed to fuel-sway-bot April 8, 2025 11:34 — with GitHub Actions Inactive
@ironcev ironcev marked this pull request as ready for review April 8, 2025 11:53
@ironcev ironcev requested review from a team as code owners April 8, 2025 11:53
@ironcev ironcev enabled auto-merge (squash) April 8, 2025 11:53
@ironcev ironcev temporarily deployed to fuel-sway-bot April 8, 2025 12:28 — with GitHub Actions Inactive
sdankel
sdankel previously approved these changes Apr 8, 2025
Copy link
Member

@sdankel sdankel left a comment

Choose a reason for hiding this comment

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

The tooling changes look good to me 👍

@ironcev ironcev marked this pull request as ready for review April 29, 2025 12:39
@ironcev ironcev requested review from JoshuaBatty and tritao April 29, 2025 12:40
tritao
tritao previously approved these changes Apr 29, 2025
xunilrj
xunilrj previously approved these changes Apr 29, 2025
@ironcev ironcev removed the blocked label Apr 29, 2025
calldelegation
calldelegation previously approved these changes Apr 29, 2025
kayagokalp
kayagokalp previously approved these changes Apr 29, 2025
Copy link
Member

@kayagokalp kayagokalp left a comment

Choose a reason for hiding this comment

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

We seem to have a tiny conflict happy to re-approve once it is fixed as well :)

bitzoic
bitzoic previously approved these changes Apr 30, 2025
@ironcev ironcev merged commit 5f64b96 into master Apr 30, 2025
45 checks passed
@ironcev ironcev deleted the ironcev/panic-expression branch April 30, 2025 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking May cause existing user code to break. Requires a minor or major release. compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler: ir IRgen and sway-ir including optimization passes compiler: parser Everything to do with the parser compiler: ui Mostly compiler messages compiler General compiler. Should eventually become more specific as the issue is triaged language feature Core language features visible to end users
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Confusing error message when calling __log with invalid number of arguments
8 participants