`while case` copies the payload out of a temporary subject instead of moving it

オープン
#7,614 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
領域
compilers

調査の方向性

Start by reproducing both snippets with the Mojo compiler, then compare how while case lowers an owned temporary subject against if case and match, focusing on where the payload binding chooses copy versus move. The related issue #7418 matters because moving here may expose a double-destroy path, so read that thread before changing ownership behavior. Done means the NoCopy repro compiles and prints 1 then 0, and the Counted repro prints no copy lines.

索引モデルが issue の本文から書いたものです。

説明

bug_feature_triaged Team: Mojo Compiler

When a while case subject is a temporary, such as a call that returns an Optional, a var binding copies the payload instead of moving it. Two things follow:

  • Non-copyable payload: while case rejects it. So you can't use while case to pull owned values from a source until it returns None.
  • Copyable payload: it's copied once per iteration, and the copy isn't needed.

if case and match move the payload out of a temporary subject.

Minimal repro
struct NoCopy(Movable):
    var id: Int

    def __init__(out self, id: Int):
        self.id = id


def pop(mut n: Int) -> Optional[NoCopy]:
    if n == 0:
        return None
    n -= 1
    return NoCopy(n)


def consume(var x: NoCopy):
    print(x.id)


def main():
    var n = 2
    while case .Some(var x) = pop(n):
        consume(x^)

Expected: it compiles and prints 1, then 0. The loop owns the temporary for one iteration, so var x can take the payload. if case .Some(var x) = pop(n): consume(x^) compiles and runs.

Actual:

repro.mojo:21:26: error: value of type 'NoCopy' cannot be implicitly copied, it does not conform to 'ImplicitlyCopyable'
    while case .Some(var x) = pop(n):
                         ^
repro.mojo:22:9: error: statement indentation must match the rest of the block; adjust to align
        consume(x^)
        ^
repro.mojo:22:17: error: use of unknown declaration 'x'
        consume(x^)
                ^

Writing the move explicitly, as = pop(n)^, gives the same error.

The second and third errors are follow-on errors. After the binding fails, the compiler no longer reads the indented line as the loop body.

A copyable payload is copied on each iteration

Here the payload type is ImplicitlyCopyable and its copy constructor prints:

struct Counted(ImplicitlyCopyable):
    var id: Int

    def __init__(out self, id: Int):
        self.id = id

    def __init__(out self, *, copy: Self):
        print("copy", copy.id)
        self.id = copy.id


def pop(mut n: Int) -> Optional[Counted]:
    if n == 0:
        return None
    n -= 1
    return Counted(n)


def consume(var x: Counted):
    print("consume", x.id)


def main():
    var n = 2
    while case .Some(var x) = pop(n):
        consume(x^)

Expected: consume 1, then consume 0, with no copies.

Actual:

copy 1
consume 1
copy 0
consume 0
Workaround

Hold the Optional in a variable and take the payload out of it:

while True:
    var next = pop(n)
    __guard case .Some(_) = next else:
        break
    consume(next.take())
Related

(https://github.com/modular/modular/issues/7418): if case and match move the payload out of an owned subject, but then destroy it a second time. If while case moved the way if case does, it would hit that double destroy. Fix #7418 first, or with this one.

主要言語
Mojo
スター
30.2k
フォーク
3.2k
平均マージ
5日 18時間
マージ済み PR(30日)
27

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

modular/modular のほかの issue

modular/modular の issue をすべて見る

似ている issue

Compilers の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。