Dafny: `<T extends B>` is replaced by `B`, so a call with any other record type is ill-typed (and an inline `B` is emitted as TypeScript text)

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

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

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

評価

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

調査の方向性

Start in tools/src/extract.ts, in extractFunctionInner around line 2122, and trace how bounded type parameters are added to _typeParamMap; compare the inline object bound path with the synthetic naming described in issue #276. Reproduce both examples with Dafny 4.11 and inspect the generated Dafny and its type errors. Done means wider record arguments retain the generic return type, inline bounds no longer emit TypeScript syntax, or unsupported cases receive a clear diagnostic.

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

説明

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

When a bound is a record, extractFunctionInner substitutes T with the bound's text (_typeParamMap.set(tp.getName(), constraint.getText())). This lets field reads through T typecheck. But it loses the reason for writing a bounded generic, which is that callers pass a type wider than the bound. It breaks in two ways.

1. A caller with a wider record doesn't resolve.

//@ backend dafny
export interface HasParams { readonly params: readonly string[] }
export interface Fn { readonly name: string; readonly params: readonly string[] }

export function firstOrSelf<T extends HasParams>(fn: T): T {
  //@ verify
  return fn
}
export function paramCount<T extends HasParams>(fn: T): number {
  //@ verify
  return fn.params.length
}
export function useIt(f: Fn): number {
  //@ verify
  //@ ensures \result === f.params.length
  return paramCount(f)
}
datatype HasParams = HasParams(params: seq<string>)
datatype Fn = Fn(name: string, params: seq<string>)

function firstOrSelf(fn: HasParams): HasParams { fn }
function paramCount(fn: HasParams): int { |fn.params| }
function useIt(f: Fn): int { paramCount(f) }
i4b.dfy(19,13): Error: incorrect argument type for function parameter 'fn' (expected HasParams, found Fn)

firstOrSelf also returns HasParams. A caller can't get its Fn back, which a T-returning signature promises.

2. An inline object bound is emitted as TypeScript text.

//@ backend dafny
export function paramCount<T extends { params: string[] }>(fn: T): number {
  //@ verify
  //@ ensures \result === fn.params.length
  return fn.params.length
}
type { params: string[] }(==)

function paramCount(fn: { params: string[] }): int
i4.dfy(3,5): Error: invalid Ident

This is the same mechanism as #276 (an inline object type on a parameter), reached through the bound.

In TypeScript, paramCount(f) with f: Fn is fine. T is inferred as Fn, and firstOrSelf(f) has type Fn.

Suggested direction: keep T as a Dafny type parameter. Model the fields the body reads through the bound as accessor functions passed in, or as a trait. Another option is to monomorphise per call-site type: emit paramCount_Fn(fn: Fn) for each record type paramCount is called with. Either way, substituting the bound only works when every caller passes exactly the bound type, so lsc could at least reject a call whose argument type differs from it, with a clear message instead of a Dafny type error. An inline bound needs the synthetic naming that interface fields get (see #276).

Workaround: drop the generic and pass what the function reads, e.g. paramCount(params: readonly string[]), called as paramCount(f.params).

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

環境構築

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

はじめの一歩

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

midspiral/LemmaScript のほかの issue

midspiral/LemmaScript の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

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

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