Sitelet https://github.com/starkware-libs/cairo/pull/10147
Skip to content

feat(semantic): Warn when an extern type/function is declared outside the corelib. - #10147

Merged
orizi merged 1 commit into
mainfrom
orizi/06-22-feat_semantic_warn_when_an_extern_type_function_is_declared_outside_the_corelib
Jul 5, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/06-22-feat_semantic_warn_when_an_extern_type_function_is_declared_outside_the_corelib

Conversation

@orizi

@orizi orizi commented Jun 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Introduces a new extern_outside_corelib lint (warning E2201) that fires when extern fn or extern type items are declared outside the core library. The warning message reads: "Extern types and functions should only be declared in the core library." It can be suppressed with #[allow(extern_outside_corelib)].

The lint is registered as a known allow-able lint in declared_allows, and the check is implemented in report_extern_item_outside_corelib, which is called from both extern_function.rs and extern_type.rs during semantic analysis. The constant EXTERN_OUTSIDE_CORELIB is added to cairo-lang-syntax's attribute consts.

All existing test fixtures that declare extern fn or extern type outside the corelib have been updated with #[allow(extern_outside_corelib)] to suppress the new warning, and line numbers in expected diagnostics have been adjusted accordingly. New test cases in diagnostic_test_data/allow verify that the warning is emitted without the attribute and suppressed with it.


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?

Declaring extern fn and extern type outside the core library is an advanced, low-level operation that bypasses Cairo's type system guarantees and directly names Sierra libfuncs. Allowing this silently in user code makes it easy to accidentally introduce unsound or non-portable declarations. The lint surfaces this as a warning so developers are aware of the risk, while still allowing it when explicitly opted in.


What was the behavior or documentation before?

extern fn and extern type could be declared anywhere without any diagnostic.


What is the behavior or documentation after?

Declaring extern fn or extern type outside the core library emits warning E2201. The warning is silenced by annotating the item with #[allow(extern_outside_corelib)].


Related issue or discussion (if any)


Additional context

The error code E2201 is assigned to this diagnostic. The severity is Warning, consistent with other lint-style diagnostics such as unused_variables and unused_imports.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@orizi
orizi marked this pull request as ready for review June 22, 2026 11:19
@cursor

cursor Bot commented Jun 22, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Lint-only semantic change with broad test fixture updates; no runtime or corelib behavior change.

Overview
Adds warning E2201 when extern fn or extern type is declared outside the core crate. Semantic analysis calls report_extern_item_outside_corelib from extern function and extern type declaration paths; the core crate and items with #[allow(extern_outside_corelib)] are exempt. The lint name is registered in declared_allows via new constant EXTERN_OUTSIDE_CORELIB.

Compiler test fixtures that intentionally use extern items in tests are annotated with #[allow(extern_outside_corelib)], with expected diagnostic line numbers adjusted where module code grew. New allow-lint tests cover the warning and suppression behavior.

Reviewed by Cursor Bugbot for commit 7b1ecc8. Bugbot is set up for automated code reviews on this repo. Configure here.

@TomerStarkware TomerStarkware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

:lgtm:

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

@orizi
orizi added this pull request to the merge queue Jul 3, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 3, 2026
@orizi
orizi force-pushed the orizi/06-22-feat_semantic_warn_when_an_extern_type_function_is_declared_outside_the_corelib branch from f8af227 to 01c3432 Compare July 3, 2026 16:01

@eytan-starkware eytan-starkware left a comment

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.

@eytan-starkware reviewed 78 files and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on orizi).


crates/cairo-lang-semantic/src/diagnostic.rs line 808 at r2 (raw file):

            }
            SemanticDiagnosticKind::ExternItemOutsideCorelib => {
                "Extern types and functions should only be declared in the core library.".into()

Maybe guidance: "Extern... expected only in .... To allow use #[....] "

@eytan-starkware eytan-starkware left a comment

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.

@eytan-starkware reviewed 78 files and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on orizi).


crates/cairo-lang-semantic/src/diagnostic.rs line 808 at r2 (raw file):

            }
            SemanticDiagnosticKind::ExternItemOutsideCorelib => {
                "Extern types and functions should only be declared in the core library.".into()

Maybe guidance: "Extern... expected only in .... To allow use #[....] "

… the corelib.

Adds the `extern_outside_corelib` warning (E2201), emitted on any `extern type`
or `extern fn` declared outside the core crate. Silenceable per-item with
`#[allow(extern_outside_corelib)]`; internal tests that declare externs are
annotated accordingly.
@orizi
orizi force-pushed the orizi/06-22-feat_semantic_warn_when_an_extern_type_function_is_declared_outside_the_corelib branch from 01c3432 to 7b1ecc8 Compare July 5, 2026 12:23

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@orizi made 1 comment.
Reviewable status: 76 of 78 files reviewed, 1 unresolved discussion (waiting on eytan-starkware and TomerStarkware).


crates/cairo-lang-semantic/src/diagnostic.rs line 808 at r2 (raw file):

Previously, eytan-starkware wrote…

Maybe guidance: "Extern... expected only in .... To allow use #[....] "

Done.

@eytan-starkware eytan-starkware left a comment

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.

:lgtm:

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

@orizi
orizi added this pull request to the merge queue Jul 5, 2026
Merged via the queue into main with commit cb0fcd7 Jul 5, 2026
55 checks passed
@orizi
orizi deleted the orizi/06-22-feat_semantic_warn_when_an_extern_type_function_is_declared_outside_the_corelib branch July 8, 2026 09:59
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