Skip to content
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

fix: Allow macros to change types on each iteration of a comptime loop #6105

Merged
merged 3 commits into from
Sep 19, 2024

Conversation

jfecher
Copy link
Contributor

@jfecher jfecher commented Sep 19, 2024

Description

Problem*

Resolves #6086 (comment)

Summary*

Allows macros to have differing result types on different loop iterations again after it was banned by the unification in #6086.

This is a difficult feature to use since users will still have to work around the type system only type checking loops once. The recommended technique found for this PR is to delay type checks by quoting the relevant function calls that need to be re-typechecked so that when they are unquoted by the interpreter they are forced to be typechecked every loop iteration.

The downside of this is that it requires some knowledge of when to do this. If we go back to the naive approach of comptime_change_type_each_loop_iteration before the quoting:

fn main() {
    comptime {
        for i in 9..11 {
            // Lengths are different on each iteration: "foo9", "foo10"
            let name = f"foo{i}".as_ctstring().as_quoted_str!();
            let hash = from_signature(name);
            assert(hash > 3);
        }
    }
}

fn from_signature<let N: u32>(_signature: str<N>) -> u32 {
    N
}

We get a confusing error now that the unification is gone: "Non-integer array length _" pointing to the body of from_signature. The fix for this is to quote & unquote the from_signature call:

let hash = unquote!(quote { from_signature($name) });

But there's nothing telling users this. Ideally we could warn users when a type changes like this and perhaps have them opt-in to it with a "I know what I'm doing" of some kind.

Additional Context

Documentation*

Check one:

  • No documentation needed.
  • Documentation included in this PR.
  • [For Experimental Features] Documentation to be submitted in a separate PR.

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@jfecher jfecher requested a review from a team September 19, 2024 16:52
Copy link
Contributor

github-actions bot commented Sep 19, 2024

Changes to Brillig bytecode sizes

Generated at commit: 0b58c43dedb0aeb1ff3b8e9daf3c79f3de00affb, compared to commit: 4170c55019bd27fd51be8a46637514dfe86de53c

There are no changes in circuit sizes

@jfecher
Copy link
Contributor Author

jfecher commented Sep 19, 2024

As another consequence of this, the macro_result_type test added in the previous PR has also been moved to compile_failure

Copy link
Collaborator

@asterite asterite left a comment

Choose a reason for hiding this comment

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

Looks great! I also tried on the code I have in Aztec-Packages and it works fine.

@jfecher jfecher added this pull request to the merge queue Sep 19, 2024
Merged via the queue into master with commit 0864e7c Sep 19, 2024
46 checks passed
@jfecher jfecher deleted the jf/change-types-each-iteration branch September 19, 2024 17:44
AztecBot added a commit to AztecProtocol/aztec-packages that referenced this pull request Sep 19, 2024
…comptime loop (noir-lang/noir#6105)

chore: Schnorr signature verification in Noir (noir-lang/noir#5437)
feat: Implement solver for mov_registers_to_registers (noir-lang/noir#6089)
AztecBot added a commit to AztecProtocol/aztec-packages that referenced this pull request Sep 19, 2024
noir-lang/noir#6105)

chore: Schnorr signature verification in Noir (noir-lang/noir#5437)
feat: Implement solver for mov_registers_to_registers (noir-lang/noir#6089)
sirasistant added a commit to AztecProtocol/aztec-packages that referenced this pull request Sep 19, 2024
Automated pull of development from the
[noir](https://github.com/noir-lang/noir) programming language, a
dependency of Aztec.
BEGIN_COMMIT_OVERRIDE
fix: Allow macros to change types on each iteration of a comptime loop
(noir-lang/noir#6105)
chore: Schnorr signature verification in Noir
(noir-lang/noir#5437)
feat: Implement solver for mov_registers_to_registers
(noir-lang/noir#6089)
END_COMMIT_OVERRIDE

---------

Co-authored-by: sirasistant <sirasistant@gmail.com>
Co-authored-by: TomAFrench <tom@tomfren.ch>
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.

2 participants