Skip to content

fix: emit diagnostic for unsupported extern fn in const evaluation instead of panicking#9811

Open
orizi wants to merge 1 commit intoorizi/03-31-fix_non_type_numeric_literal_suffixfrom
orizi/03-31-fix_unsupported_extern_fn_in_const
Open

fix: emit diagnostic for unsupported extern fn in const evaluation instead of panicking#9811
orizi wants to merge 1 commit intoorizi/03-31-fix_non_type_numeric_literal_suffixfrom
orizi/03-31-fix_unsupported_extern_fn_in_const

Conversation

@orizi
Copy link
Copy Markdown
Collaborator

@orizi orizi commented Mar 31, 2026

Summary

Replace unreachable panic with proper error reporting when user-defined extern const functions are used in constant expressions.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

The code previously used unreachable!() when encountering user-defined extern const functions in constant expressions, which would cause the compiler to panic. This is incorrect behavior as user-defined extern functions should be handled gracefully with a proper diagnostic error rather than crashing the compiler.


What was the behavior or documentation before?

When a user-defined extern const function was used in a constant expression, the compiler would panic with an "Unexpected extern function in constant lowering" message.


What is the behavior or documentation after?

The compiler now properly reports error E2127 "This expression is not supported as constant" when encountering user-defined extern const functions in constant expressions, providing a clear diagnostic message to the user instead of crashing.


Related issue or discussion (if any)

Fixes #9799


Additional context

The fix includes a test case that verifies the proper error reporting behavior when extern const fn f() -> felt252 nopanic; is called within a constant declaration.


Note

Low Risk
Low risk: replaces an unreachable!() panic with a diagnostic and adds a regression test, affecting only const-evaluation error handling.

Overview
Fixes constant evaluation to gracefully reject user-defined extern const fn calls by returning E2127 (unsupported constant) instead of hitting an unreachable!() panic.

Adds a regression case in expr/test_data/constant asserting the new diagnostic when const X = f(); calls a user-defined extern const fn.

Written by Cursor Bugbot for commit 6ef9148. This will update automatically on new commits. Configure here.

@reviewable-StarkWare
Copy link
Copy Markdown

This change is Reviewable

Copy link
Copy Markdown
Contributor

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

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

:lgtm:

@eytan-starkware reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

Copy link
Copy Markdown
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

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

:lgtm:

@TomerStarkware made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on orizi).

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.

4 participants