`ReadonlySet<T>` and `ReadonlyMap<K, V>` are not mapped to `set` / `map`

オープン 初心者向け
#308 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

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

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript

調査の方向性

tools/src/types.ts の tyFromTypeNode(199行目付近)から始め、issue が示唆するように、文字列ベースの型解析経路があるかどうかを確認してください。ReadonlyArray のマッピング方法を、既存の Set、Map、Record のケースと比較してください。ReadonlySet と ReadonlyMap のパラメーターが Dafny の set 型と map 型にマッピングされ、issue の例が正常に出力され検証されれば完了です。

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

説明

Version: lsc 0.6.10 (npm), Dafny 4.11. Still present on main at 93283ec, tools/src/types.ts, tyFromTypeNode (around line 199).

ReadonlyArray<T> and readonly T[] become seq<T>. Set<T> becomes set<T>, and Map<K, V> and Record<K, V> become map<K, V>. ReadonlySet and ReadonlyMap fall through to a user type, so the usual read-only operations fail:

//@ backend dafny
export function known(names: ReadonlySet<string>, n: string): boolean {
  //@ verify
  //@ ensures \result === names.has(n)
  return names.has(n)
}
ERROR: Dafny emission failed for 'known' in i7.ts: Unsupported Dafny method call: .has() on user
export function copy(s: ReadonlySet<string>): Set<string> {
  //@ verify
  return new Set(s)
}
ERROR: new Set(...) expects an array argument (got user)
export function lookup(m: ReadonlyMap<string, number>, k: string): number {
  //@ verify
  return m.get(k) ?? 0
}
ERROR: nullish reached transform — narrow should have rewritten it: {"kind":"nullish","left":{"kind":"call","fn":{"kind":"field","obj":{"kind":"var","name":"m","ty":{"kind":"user","name":"ReadonlyMap<string, number>"}}, ...

Reading only .size gets past emission. lsc then expands the lib interface into a datatype that does not parse:

type forEach(==)
type has(==)
...
datatype ReadonlySet = ReadonlySet(forEach: forEach, has: has, size: int, entries: entries, keys: keys, values: values, i__@iterator@9: i__)

function size(s: ReadonlySet<string>): int
i7d.dfy(15,123): Error: closeparen expected
i7d.dfy(15,134): Error: invalid TopDecl

ReadonlySet<T> is Set<T> without the mutators, and ReadonlyMap<K, V> is Map<K, V> without them. Verified code is pure, so it already can't call add / set / delete. These are the natural parameter types for it.

Suggested fix: in tyFromTypeNode, and in the string-based type path if there is one, treat ReadonlySet like Set and ReadonlyMap like Map, as ReadonlyArray is already treated like Array:

if ((name === "Set" || name === "ReadonlySet") && args.length === 1) return { kind: "set", ... };
if ((name === "Map" || name === "ReadonlyMap" || name === "Record") && args.length === 2) ...

Workaround: declare the parameter as Set<T> / Map<K, V>. With Set<string>, the known function above verifies, but callers that hold a ReadonlySet then need a cast.

主要言語
TypeScript
スター
113
フォーク
13
平均マージ
16時間 22分
マージ済み PR(30日)
40

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

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

midspiral/LemmaScript のほかの issue

midspiral/LemmaScript の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

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

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