[BUG] A value whose type names an interior origin is checked as if it were a reference

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

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

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

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
領域
compilers

調査の方向性

The payload names no source files. Start by searching the lifetime checker for the diagnostics use of invalidated interior reference and use of a never-initialized interior reference, and find where a value whose type mentions origin_of(args[0]) is treated as forming a reference into args. Reproduce with the first program in the issue. Done when both sample programs compile and a pointer held across a real mutation of args is still rejected.

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

説明

bug_feature_triaged Team: Mojo Compiler
Bug description

A value can name a container's interior origin in its type without holding any reference into the container. The common case is an empty list of element pointers that is filled in afterwards. The lifetime checker treats creating such a value as forming a reference, so ordinary code that builds a table of element pointers is rejected even though the container is never mutated.

This matters because spelling the element type with the element's origin (origin_of(args[0])) is exactly what callers are asked to do as standard library types move to interior origins. The argv array that the standard library builds for posix_spawnp has this shape, and it starts failing as soon as its pointers carry an interior origin.

Actual behavior

Appending a pointer to an element:

def main():
    var args: List[String] = ["a", "b"]
    var ptrs = List[Pointer[String, ImmOrigin(origin_of(args[0]))]]()
    ptrs.append(Pointer(to=args[0]))
    print(len(ptrs))
error: use of invalidated interior reference 'args["element"]'
    ptrs.append(Pointer(to=args[0]))
               ^
note: origin was defined here, after the reference was formed
    ptrs.append(Pointer(to=args[0]))
                               ^

Using the value before anything indexes args:

def main():
    var args: List[String] = ["a", "b"]
    var ptrs = List[Pointer[String, ImmOrigin(origin_of(args[0]))]]()
    print(len(ptrs))
error: use of a never-initialized interior reference 'args["element"]'
    print(len(ptrs))
             ^

The usual loop form, for i in range(len(args)): ptrs.append(Pointer(to=args[i])), fails the same way as the first program.

Expected behavior

Both programs compile. Creating a value whose type names args["element"], without deriving anything from args, should not count as forming a reference. A pointer held across a real mutation of args must still be rejected.

Workaround

Index the container once before creating the value. This compiles and prints 2, but it depends on statement order in a way nobody would guess:

def main():
    var args: List[String] = ["a", "b"]
    _ = Pointer(to=args[0])  # establish the origin first
    var ptrs = List[Pointer[String, ImmOrigin(origin_of(args[0]))]]()
    for i in range(len(args)):
        ptrs.append(Pointer(to=args[i]))
    print(len(ptrs))

The other workaround, spelling the whole-container origin origin_of(args) and casting each pointer to it, gives up invalidation tracking for those pointers.

System information

Reproduces on main built from source (Linux x86_64).

主要言語
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 を短くまとめたダイジェスト。