Dafny: a `string | readonly string[]` value passed where that same union is expected is wrapped again in `NonArrayBranch`; `.includes()` on the narrowed array fails

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

メンテナーはふだん 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:

  1. In coerceToTargetTy, return value unchanged when value.ty is 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.
  2. Give the narrowed binder its array type when lowering method calls, so .includes, .some and 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 を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

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

midspiral/LemmaScript のほかの issue

midspiral/LemmaScript の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

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

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