Skip to content

Const expr array length #28687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2025
Merged

Const expr array length #28687

merged 1 commit into from
Jun 18, 2025

Conversation

mohammadfawaz
Copy link
Collaborator

@mohammadfawaz mohammadfawaz commented Jun 12, 2025

Closes #28676

This PR allows array lengths to be arbitrary expressions. Of course, we still want those expression to evaluate to a literal at compile time. We error out otherwise. Due to monomorphization and loop unrolling, type checking now has to be done multiple times which is why I had to add it, along with symbol table creation, to const_propagation_and_unrolling.rs. This seems to work fine except for a few small issues that I had to work around such as making sure we're not emitting duplicate warnings.

The default length type is u32. That is, if the length provided is an unsuffixed literal, we will assume it's a u32.

Because types can now have expressions in them (this will also be the case when we add const generic structs), I had to add a TypeVisitor and TypeReconstructor and address types where appropriate. Most of the changes happen in early passes since later passes can assume that types have been resolved (so no need to visit their expressions at that point).

The interpreter now treats const generics as regular inputs so that calling generic inline now works fine. For that, I also had to move ProcessingScript until after ConstPropagationAndUnrolling. Otherwise, we were sometimes error out in the build stage of leo test because we were removing scripts before monomorphizing.

Unsuffixied literal now get converted to type literals in const propagation when possible.

@mohammadfawaz mohammadfawaz force-pushed the mohammadfawaz/28676_2 branch from 0becbf4 to a8b255e Compare June 12, 2025 18:40
@mohammadfawaz mohammadfawaz self-assigned this Jun 13, 2025
@mohammadfawaz mohammadfawaz force-pushed the mohammadfawaz/28676_2 branch 12 times, most recently from 5d5f10b to 00292fd Compare June 17, 2025 21:25
@mohammadfawaz mohammadfawaz marked this pull request as ready for review June 17, 2025 21:25
@mohammadfawaz mohammadfawaz changed the title [WIP] Const expr array length Const expr array length Jun 17, 2025
@mohammadfawaz mohammadfawaz force-pushed the mohammadfawaz/28676_2 branch from 00292fd to 512b577 Compare June 17, 2025 23:02
Copy link
Collaborator

@mikebenfield mikebenfield left a comment

Choose a reason for hiding this comment

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

Overall looks great.

@mohammadfawaz mohammadfawaz force-pushed the mohammadfawaz/28676_2 branch 2 times, most recently from bdc619b to fe3982e Compare June 18, 2025 17:15
Copy link
Collaborator

@mikebenfield mikebenfield left a comment

Choose a reason for hiding this comment

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

LGTM!

@mohammadfawaz mohammadfawaz force-pushed the mohammadfawaz/28676_2 branch from fe3982e to 7357e93 Compare June 18, 2025 18:00
@mohammadfawaz mohammadfawaz force-pushed the mohammadfawaz/28676_2 branch from 7357e93 to 58fbe9f Compare June 18, 2025 18:02
@mohammadfawaz mohammadfawaz merged commit ca095ca into mainnet Jun 18, 2025
8 checks passed
@mohammadfawaz mohammadfawaz deleted the mohammadfawaz/28676_2 branch June 18, 2025 18:50
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.

[Feature] Parameterized array using const generics
2 participants