Dafny: a `string | readonly string[]` value passed where that same union is expected is wrapped again in `NonArrayBranch`; `.includes()` on the narrowed array fails
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 56/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- typescript
調査の方向性
Start in tools/src/resolve.ts at coerceToTargetTy, as the issue identifies that function as the source of the repeated union wrapping. Trace how narrowed array types are represented and how method calls are resolved in both function bodies and spec expressions. Done means passing an already-union-typed value preserves its type and narrowed-array calls such as .includes() work; the payload names no test file, so locate or add coverage for both failures.
索引モデルが issue の本文から書いたものです。
説明
Version: lsc 0.6.10 (npm), Dafny 4.11. Still present on main at 93283ec, tools/src/resolve.ts, coerceToTargetTy (around line 200).
A JWT aud claim is one string or a list. lsc synthesizes ArrayOf_string_Or_string = ArrayBranch(arr: seq<string>) | NonArrayBranch(val: string) for it, and a function that takes the union and narrows it with Array.isArray verifies. Passing a value that already has the union type does not.
//@ backend dafny
export interface Claims { readonly aud: string | readonly string[] }
export function audiences(aud: string | readonly string[]): string[] {
//@ verify
if (Array.isArray(aud)) return [...aud]
return [`${aud}`]
}
export function audienceCount(c: Claims): number {
//@ verify
return audiences(c.aud).length
}
datatype ArrayOf_string_Or_string = ArrayBranch(arr: seq<string>) | NonArrayBranch(val: string)
datatype Claims = Claims(aud: ArrayOf_string_Or_string)
function audienceCount(c: Claims): int
{
|audiences(NonArrayBranch(c.aud))|
}
i5.dfy(19,30): Error: incorrect argument type for datatype constructor parameter 'val' (expected string, found ArrayOf_string_Or_string)
coerceToTargetTy wraps any value whose type is not array in NonArrayBranch when the target is a synth array-union. It doesn't check whether the value already has the target type. The same happens with const a = c.aud (var a := NonArrayBranch(c.aud)). It also happens inside if (Array.isArray(a)) for the narrowed array, which is emitted as audiences(NonArrayBranch(i_a_arr)).
Second gap: .includes() on the narrowed array. Element access and .length work after Array.isArray, but a method call is resolved against the union:
//@ backend dafny
export function has(aud: string | readonly string[], want: string): boolean {
//@ verify
//@ ensures Array.isArray(aud) ==> \result === aud.includes(want)
if (Array.isArray(aud)) { const xs: readonly string[] = aud; return xs.includes(want) }
return aud === want
}
ERROR: Dafny emission failed for 'has' in i5b.ts: Unsupported Dafny method call: .includes() on user
The body above is fine, because it copies into a typed local first. The ensures fails, and if (Array.isArray(aud)) return aud.includes(want) fails the same way in a body. typeof aud === "string" is not an alternative (see #266).
In TypeScript, all of this typechecks and narrows as expected.
Suggested fix:
- In
coerceToTargetTy, returnvalueunchanged whenvalue.tyis already the synth union (value.ty.kind === "user" && value.ty.name === targetTy.name). Use the narrowed binder's type (seq<T>/U) when the value is a narrowed path. - Give the narrowed binder its array type when lowering method calls, so
.includes,.someand the others resolve, in spec expressions too.
Workaround: keep the union out of verified signatures. Normalise aud to readonly string[] in unverified code, and verify functions over the array. The local-copy trick above covers .includes() in bodies, but not in contracts.
- 主要言語
- TypeScript
- スター
- 113
- フォーク
- 13
- 平均マージ
- 16時間 22分
- マージ済み PR(30日)
- 40
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
midspiral/LemmaScript のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
midspiral/LemmaScript#308 ·
メンテナーはふだん 1 日以内に返信
-
Dafny: `toUpperCase`/`toLowerCase` are modeled as ASCII-only, so false contracts about non-ASCII text verify対応中かも @quangng2000 が 1 日前に担当しました。 オープン
midspiral/LemmaScript#312 · コメント 1 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 1〜2日 初心者へのやさしさ 56/100
midspiral/LemmaScript#311 ·
メンテナーはふだん 1 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 48/100
midspiral/LemmaScript#310 ·
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 67/100
midspiral/LemmaScript#309 ·
メンテナーはふだん 1 日以内に返信
midspiral/LemmaScript の issue をすべて見る
似ている issue
-
area: batch
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
inabagumi/shinju-date#7294 ·
メンテナーはふだん 1 日以内に返信
-
documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
streaming-video-technology-alliance/common-media-library#700 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
vercel-labs/ui#809 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
KrisCris/village-in-the-shade-guide#75 ·
メンテナーはふだん 1 日以内に返信
-
bot:ai-assisted component:platform-js status:untriaged
難易度 1/5 1時間未満 初心者へのやさしさ 74/100
midnightntwrk/platform-js#38 ·