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

fix(starknet): clearer error for a non-ref self constructor - #10223

Merged
orizi merged 1 commit into
mainfrom
orizi/07-20-fix_starknet_clearer_error_for_a_non-_ref_self_constructor
Jul 20, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/07-20-fix_starknet_clearer_error_for_a_non-_ref_self_constructor

Conversation

@orizi

@orizi orizi commented Jul 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds a dedicated ConstructorNotExternal ABI error for the case where a contract constructor is declared with self: @ContractState (view/snapshot) instead of ref self: ContractState (external). Previously, this case fell through to a generic UnexpectedType error, which gave no useful diagnostic information. The new error message explicitly states: "A contract constructor must take 'ref self', not '@self'." and correctly points to the constructor's source location in the diagnostic output.


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?

When a constructor was written with self: @ContractState instead of ref self: ContractState, the compiler emitted a generic UnexpectedType error with no indication of what was actually wrong or how to fix it. This made it difficult for developers to diagnose the issue.


What was the behavior or documentation before?

A constructor declared with self: @ContractState would fail ABI generation with the unhelpful error UnexpectedType, and the source location was not attached to the error.


What is the behavior or documentation after?

ABI generation now fails with the specific error:

A contract constructor must take `ref self`, not `@self`.

The diagnostic correctly points to the constructor definition in the source file.


Related issue or discussion (if any)

N/A


Additional context

A new test case (Test constructor taking '@self' (view) instead of 'ref self') has been added to abi_failures to cover this scenario.

A contract constructor that takes `@self` (view) instead of `ref self` was
rejected with the generic `Got unexpected type.` and no source location, and
`self.ctor` was set before the mutability check — so an invalid constructor was
still registered as the contract's constructor.

Add a dedicated `ConstructorNotExternal` error ("A contract constructor must
take `ref self`, not `@self`.") with the constructor's location, and set
`self.ctor` only after the check passes.
@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jul 20, 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 July 20, 2026 10:41
@cursor

cursor Bot commented Jul 20, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Diagnostic-only change in Starknet ABI validation with no runtime or contract execution behavior changes.

Overview
When ABI generation sees a #[constructor] with self: @ContractState instead of ref self, it now reports ConstructorNotExternal with the message that the constructor must take ref self, not @self, instead of the generic UnexpectedType error.

The check runs in add_constructor right after signature/mutability is computed, and the new error is wired into ABIError::location so the diagnostic points at the constructor. An abi_failures test covers this case.

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

@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, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@orizi
orizi added this pull request to the merge queue Jul 20, 2026
Merged via the queue into main with commit b949865 Jul 20, 2026
55 checks passed
@orizi
orizi deleted the orizi/07-20-fix_starknet_clearer_error_for_a_non-_ref_self_constructor branch July 21, 2026 14:22
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.

3 participants