microsoft / microsoft/TypeScript

Instantiations of constrained generic signatures allow all assignments

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

@weswigham がすでに取り組んでいます。

2023年9月7日 から。

Bug Domain: check: Variance Relationships
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

🔎 Search Terms

variance generic signature measurement

🕗 Version & Regression Information

This has been our behavior forever as far as I can tell - in fact, I've seen this pattern used once or twice to work around other bugs, even though the behavior is in no way safe.

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAYgdgHgCoD4oF4oIKpQgD2AjgBMBnKVACnwC4psBKDNbAbgCgPRIoBhAPZwAxgCcIRAIwYsuAkVIU4AVwC2AIwiiUNekxYNOPaIJHiiAJhk48hYuShlgogJZwA5jroNm6Vpw4AM2URYBchKEDkSQAaSgsdYEl6eGiUOOALFMQkBLjhZP4hMQkIWKhhLKKzUotmAG8OKGaoAHpWvAA3LRBHN3cAG2ghYcCoYAALCDJoAEMyMhd3OFViYAoyCYFlAZI8UVEBUS4W8elMTLY2js3t3f3D0Tj1ZWAoEgFpuAByYCaWzIyJJXdqOLY7PZaR7PV7vT5kH5-f7NAoySqcU6VNGSAKnJLYjEtVEXHEcAC+QA

💻 Code
type Fn<T> = <U extends T>(x: U) => U;

type Concrete1 = <U extends number>(x: U) => U;
type Concrete2 = <U extends string>(x: U) => U;

function f<T1, T2>(t1: Fn<T1>, t2: Fn<T2>, c1: Concrete1, c2: Concrete2) {
    // every single one of these assignments should error

    t1 = t2; // should error, but doesn't
    t2 = t1; // should error, but doesn't

    c1 = c2;
    c2 = c1;

    t1 = c1;
    c1 = t1;
}
🙁 Actual behavior

Missing two errors on the generic relationships.

🙂 Expected behavior

All of the assignments are errors, as the generic relationship should be even stricter than the non-generic one.

Additional information about the issue

The issue lies in our variance analysis - we currently incorrectly measure T as independent, which is quite different from the invariant it should adopt (via U's invariant usages). This discrepancy arises because of some oddities in how we do generic signature relationships.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

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

評価

この issue はまだ評価されていません。

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

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