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

fix(semantic): narrow return-type mismatch span to the value-producing expression - #9931

Merged
orizi merged 1 commit into
mainfrom
orizi/05-14-fix_semantic_narrow_return-type_mismatch_span_to_the_value-producing_expression
May 19, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/05-14-fix_semantic_narrow_return-type_mismatch_span_to_the_value-producing_expression

Conversation

@orizi

@orizi orizi commented May 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The WrongReturnType diagnostic now points to the actual value-producing expression instead of the return type annotation in the function signature.

A new helper function value_producer_stable_ptr is introduced in compute.rs that drills through blocks (to their tail), match expressions (to the first non-never arm), and if expressions (to the first non-never branch) to locate the concrete sub-expression responsible for producing the returned value. This pointer is used as the diagnostic span instead of the return type clause in the signature.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)

Why is this change needed?

Previously, when a function returned a value of the wrong type, the error diagnostic highlighted the return type annotation in the function signature (e.g., -> u8). This was often unhelpful because the annotation itself is correct — the problem is the expression that actually produces the value. Pointing at the return type annotation forced developers to manually trace which expression was at fault.


What was the behavior or documentation before?

The E2042: Unexpected return type diagnostic pointed to the return type clause in the function signature, for example:

error[E2042]: Unexpected return type. Expected: "()", found: "core::integer::u8".
 --> lib.cairo:1:13
fn foo() -> () {
            ^^

What is the behavior or documentation after?

The diagnostic now points directly to the offending value-producing expression:

error[E2042]: Unexpected return type. Expected: "()", found: "core::integer::u8".
 --> lib.cairo:2:5
    4_u8
    ^^^^

For compound expressions like blocks, matches, and ifs, the diagnostic drills into the relevant sub-expression rather than spanning the entire function body.


Related issue or discussion (if any)

Fixes #6486


Additional context

For empty function bodies (no tail expression), the diagnostic falls back to pointing at the closing } of the block, which correctly indicates that a value is missing.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

@orizi
orizi marked this pull request as ready for review May 14, 2026 14:45
@cursor

cursor Bot commented May 14, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Low risk: changes only diagnostic span selection for E2042 and updates golden test expectations; no runtime or lowering semantics are modified.

Overview
E2042 (WrongReturnType) now reports its span on the value-producing sub-expression (block tail, first non-never match arm, or first non-never if branch) via a new helper value_producer_stable_ptr, instead of pointing at the return type clause in the signature.

Updates a large set of semantic, lowering, and StarkNet plugin diagnostic golden tests to match the new, more precise error locations.

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

@orizi
orizi force-pushed the orizi/05-14-fix_semantic_point_const-decl_literal_errors_at_the_value_expression branch from 7ba1a92 to d47df27 Compare May 14, 2026 15:59
@orizi
orizi force-pushed the orizi/05-14-fix_semantic_narrow_return-type_mismatch_span_to_the_value-producing_expression branch from 93b5567 to bab48b5 Compare May 14, 2026 16:00

@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 18 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 force-pushed the orizi/05-14-fix_semantic_narrow_return-type_mismatch_span_to_the_value-producing_expression branch from bab48b5 to 53c78ee Compare May 17, 2026 09:29
@orizi
orizi force-pushed the orizi/05-14-fix_semantic_point_const-decl_literal_errors_at_the_value_expression branch from d47df27 to 5c3381f Compare May 17, 2026 13:34
@orizi
orizi force-pushed the orizi/05-14-fix_semantic_narrow_return-type_mismatch_span_to_the_value-producing_expression branch from 53c78ee to 5970494 Compare May 17, 2026 13:34
@orizi
orizi force-pushed the orizi/05-14-fix_semantic_point_const-decl_literal_errors_at_the_value_expression branch from 5c3381f to b9b4318 Compare May 17, 2026 13:43
@orizi
orizi force-pushed the orizi/05-14-fix_semantic_narrow_return-type_mismatch_span_to_the_value-producing_expression branch from 5970494 to 50c1c11 Compare May 17, 2026 13:43
@orizi
orizi force-pushed the orizi/05-14-fix_semantic_point_const-decl_literal_errors_at_the_value_expression branch from b9b4318 to edb43d9 Compare May 17, 2026 13:53
@orizi
orizi force-pushed the orizi/05-14-fix_semantic_narrow_return-type_mismatch_span_to_the_value-producing_expression branch from 50c1c11 to 5de80a4 Compare May 17, 2026 13:53
@orizi
orizi force-pushed the orizi/05-14-fix_semantic_point_const-decl_literal_errors_at_the_value_expression branch from edb43d9 to d9d755d Compare May 17, 2026 14:21
@orizi
orizi force-pushed the orizi/05-14-fix_semantic_narrow_return-type_mismatch_span_to_the_value-producing_expression branch from 5de80a4 to e14ed81 Compare May 17, 2026 14:21

@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 3 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 force-pushed the orizi/05-14-fix_semantic_narrow_return-type_mismatch_span_to_the_value-producing_expression branch from e14ed81 to f943def Compare May 19, 2026 11:59
@orizi
orizi force-pushed the orizi/05-14-fix_semantic_point_const-decl_literal_errors_at_the_value_expression branch from d9d755d to 12ffa14 Compare May 19, 2026 11:59
…g expression

  Replace `compute_root_expr`'s return-type conform stable_ptr (the whole
  function block, or the static return-type clause) with a recursive
  `value_producer_stable_ptr` that drills through block tails, match arms,
  and if branches to the actual value-contributing sub-expression.

  For `fn foo() { let x = 5; x }`, the error now points at `x` instead of
  the whole function block. For `fn foo() { match b { true => 5, … } }`,
  it points at `5` — the first non-`never` arm's value-producer.
@orizi
orizi changed the base branch from orizi/05-14-fix_semantic_point_const-decl_literal_errors_at_the_value_expression to graphite-base/9931 May 19, 2026 12:29
@orizi
orizi force-pushed the graphite-base/9931 branch from 12ffa14 to ad32e5c Compare May 19, 2026 12:30
@orizi
orizi force-pushed the orizi/05-14-fix_semantic_narrow_return-type_mismatch_span_to_the_value-producing_expression branch from f943def to 87ddad5 Compare May 19, 2026 12:30
@orizi
orizi changed the base branch from graphite-base/9931 to main May 19, 2026 12:30
@orizi
orizi enabled auto-merge May 19, 2026 12:30
@orizi
orizi added this pull request to the merge queue May 19, 2026
Merged via the queue into main with commit 10b5a1f May 19, 2026
105 checks passed
@orizi
orizi deleted the orizi/05-14-fix_semantic_narrow_return-type_mismatch_span_to_the_value-producing_expression branch May 26, 2026 08:52
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.

bug: Incorrect diagnostic placement when returning wrong type

3 participants