Skip to content

Using the iterator inside the for loop causes UB #16011

Closed
@bluss

Description

@bluss

Experimenting with using the iterator inside the loop (awsome feature), found weird bug

fn main() {
    let mut it = range(0u, 5);
    for i in it {
        println!("{}", i)
        if i == -23 {
            it.next();
        }
    }
}

Using rustc 0.12.0-pre (66a0b52 2014-07-25 20:36:10 +0000)

Actual Output:

1
1
1
1
1

Expected Output:

1
2
3
4
5

Bug needs optimization level 2 or higher -- so it is probably undefined behavior-related.

The actual condition doesn't matter, I just picked one that would never be true.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions