Skip to content

do not evaluate for loop iterator expr multiple times #1704

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zeldovich
Copy link
Collaborator

Without this fix, trying to verify a simple program like:

        let m = HashSet::<u32>::new();
        for k in m.iter() {}

would lead to an error:

        error: loop invariant not satisfied
         --> bug.rs:9:9
          |
        9 |         for k in m.iter() {}
          |         ^^^^^^^^^^^^^^^^^^^^
          |         |
          |         at this loop exit
          |         failed this invariant

        verification results:: 1 verified, 1 errors
        error: aborting due to 1 previous error

but if m.iter() was instead an identifier, as in below, verification passed:

        let m = HashSet::<u32>::new();
        let m_iter = m.iter();
        for k in m_iter {}

This commit changes the for loop macro to avoid evaluating the iterator expression, such as m.iter() in the above example, multiple times, which makes the first example above verify just fine.

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

Without this fix, trying to verify a simple program like:

        let m = HashSet::<u32>::new();
        for k in m.iter() {}

would lead to an error:

        error: loop invariant not satisfied
         --> bug.rs:9:9
          |
        9 |         for k in m.iter() {}
          |         ^^^^^^^^^^^^^^^^^^^^
          |         |
          |         at this loop exit
          |         failed this invariant

        verification results:: 1 verified, 1 errors
        error: aborting due to 1 previous error

but if m.iter() was instead an identifier, as in below, verification passed:

        let m = HashSet::<u32>::new();
        let m_iter = m.iter();
        for k in m_iter {}

This commit changes the for loop macro to avoid evaluating the iterator
expression, such as m.iter() in the above example, multiple times, which
makes the first example above verify just fine.
@utaal utaal requested a review from Chris-Hawblitzel May 30, 2025 20:34
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.

1 participant