move-language / move

Apache License 2.0
2.24k stars 676 forks source link

[Bug] unexpected error with continue #1068

Closed ALPAC-4 closed 10 months ago

ALPAC-4 commented 11 months ago

🐛 Bug

continue in while cause unexpected error. Here is the example codes

    fun a() {
        let index: u64 = 0;
        let sum = 0;
        while (index < 10) {

            if (index % 2 == 0) {
                sum = sum + index
            } else {
                index = index + 1;
                continue
            };

            index = index + 1;
        };
        std::debug::print(&sum);
    }

This causes the following error at index: In a loop, this typically means it was moved in the first iteration, and is not available by the second iteration.

I added copy to fix this, but _index is not updated.

    fun a() {

        let _index: u64 = 0;
        let sum = 0;
        while (_index < 10) {
            std::debug::print(&_index); // _index is not increasing when it get 1
            if (_index % 2 == 0) {
                sum = sum + _index
            } else {
                _index = copy _index + 1;
                continue
            };

            _index = _index + 1;
        };
        std::debug::print(&sum);
    }

Most weird part is when I add &index; before continue, it works as expected.

    fun a() {
        let index: u64 = 0;
        let sum = 0;
        while (index < 10) {

            if (index % 2 == 0) {
                sum = sum + index
            } else {
                index = index + 1;
                &index;
                continue
            };

            index = index + 1;
        };
        std::debug::print(&sum); // print 20 here
    }

I think that continue makes some error. Without continue, all the codes above works.

ksolana commented 11 months ago

Any plans to commit this change in this repo?

ksolana commented 11 months ago

This can be closed now.