`while case` copies the payload out of a temporary subject instead of moving it
メンテナーはふだん 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 の本文から書いたものです。
説明
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 caserejects it. So you can't usewhile caseto pull owned values from a source until it returnsNone. - 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
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
modular/modular のほかの issue
-
accepted bug bug_feature_triaged good first issue mojo Team: Mojo Libraries
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 2 日以内に返信
-
accepted bug bug_feature_triaged good first issue mojo Team: Mojo Libraries
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 2 日以内に返信
-
bug_feature_triaged Team: Mojo Libraries
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 2 日以内に返信
-
bug_feature_triaged Team: Mojo Libraries
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 2 日以内に返信
-
bug_feature_triaged
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 2 日以内に返信
modular/modular の issue をすべて見る
似ている issue
-
new issue
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
llvm/llvm-project#245835 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
triton-lang/triton#12945 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
JulianKemmerer/PipelineC#409 ·
-
`trawl build --help` doesn't say what `--keep-c` or `-o` do対応中かも @siddesh-nexus が今日担当しました。 オープンgood first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
メンテナーはふだん 1 日以内に返信