Skip to content

Conversation

@ironcev
Copy link
Member

@ironcev ironcev commented Jan 5, 2026

Description

This PR fixes #7520 by forbidding const evaluation of expressions that contain variables from outer scope. Forbidding outer variables also partially tackles #7521 but also exposes an issue of SROA panicking on out of bounds array access. We will treat this issue fully in a separate PR.

This PR is a prerequisite for #7489 because optimizing initialization of aggregates requires valid const evaluation for certain cases.

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 Jan 5, 2026
@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 labels Jan 5, 2026
@ironcev
Copy link
Member Author

ironcev commented Jan 5, 2026

👍

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 5, 2026

CodSpeed Performance Report

Merging #7522 will not alter performance

Comparing ironcev/outer-vars-in-const-eval (1284e88) with master (73533a4)

Summary

✅ 25 untouched

@ironcev ironcev marked this pull request as ready for review January 5, 2026 18:43
@ironcev ironcev requested a review from a team as a code owner January 5, 2026 18:43
@cursor
Copy link

cursor bot commented Jan 5, 2026

PR Summary

Prevents invalid constant evaluation that captured outer-scope variables and strengthens negative test coverage for array bounds.

  • Add contains_outer_vars AST walker and codeblock_contains_outer_vars to detect outer-scope variable usage in const expressions
  • Gate compile_constant_expression_to_constant and constant handling in code blocks with the outer-var check; skip evaluation when detected
  • Import ProjectionKind and HashSet to support traversal and indexing checks
  • Tests: add should_fail/const_eval_outer_vars with many outer-var cases; add array_oob_const_index and array_oob_reassignment failing tests; keep array_oob_variable_index with disabled snapshot; remove older array_oob and array_oob_global_const_index tests

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

@ironcev ironcev enabled auto-merge (squash) January 5, 2026 18:43
@ironcev ironcev requested review from a team as code owners January 5, 2026 21:59
@xunilrj
Copy link
Contributor

xunilrj commented Jan 6, 2026

Any reason to call this "outer variables"? I was thinking we were forbidding variable declarations "outside" of the const expression, even other const expressions.

Why not something like "forbid non-const variables" or something to that effect...

@ironcev
Copy link
Member Author

ironcev commented Jan 7, 2026

Any reason to call this "outer variables"? I was thinking we were forbidding variable declarations "outside" of the const expression, even other const expressions.

Why not something like "forbid non-const variables" or something to that effect...

I am not particularly happy with the term "outer" either, but that's the best I figured out so far 😄 Suggestions are welcome!

The reason for "outer" was that they are coming from a lexical scope(s) outer to the const evaluated expression itself. In other words, they are not declared inside of the expression being const evaluated.

Also, we strictly distinguish between variables and constants, so I don't see that the "outer variable" would imply constants as well.

What would be "non-const variables"? Like, immutable variables? Note that we ban both mutable and immutable variables because we don't know even if immutable are changed between the definition and usage in const. Having "immutable" e.g. b256 changed in asm blocks is a very common pattern. Also, expectedly Rust also bans it.

@ironcev ironcev requested a review from xunilrj January 7, 2026 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler: ir IRgen and sway-ir including optimization passes compiler General compiler. Should eventually become more specific as the issue is triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Const eval does not forbid using variables from outer scope

2 participants