A generic class instantiated with two types reads the second instance as the first, natively (regressed in 0.4.16)

Đang mở
#4,864 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
58/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
node.js, typescript
Lĩnh vực
compilers

Hướng nghiên cứu

Start by reproducing the failure from the issue’s gm6.js example with allowJs, checkJs, and strict enabled; compare native output with the Node.js and scriptc 0.4.12 results. The issue points to packages/light/native/probes/p-g4-generic-composition.js as another reproducer. Trace how the compiler handles the two Lazy specializations and confirm the fix by getting both examples to print 42 ab without diagnostics.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

A generic class instantiated with a number and then with a string miscompiles the second instance natively, with no diagnostic. It builds 100% static and throws at run time. It regressed between 0.4.12 and 0.4.16 and is still in 0.5.0.

Reproduction

gm6.js, built with a tsconfig that has allowJs, checkJs and strict (so the JSDoc types reach the compiler):

/** @template T */
class Lazy {
  /** @param {() => T} fn */
  constructor(fn) {
    this._fn = fn;
    /** @type {T} */
    this._value = fn();
  }
  /** @param {T} value */
  _assign(value) {
    this._value = value;
  }
  /** @returns {T} */
  get value() {
    this._assign(this._fn());
    return this._value;
  }
}
const n = new Lazy(() => 42);
const s = new Lazy(() => "ab");
console.log(n.value, s.value);
Build Output Exit
Node.js v25.3.0 42 ab 0
scriptc 0.4.12 no diagnostics 42 ab 0
scriptc 0.4.16 no diagnostics Uncaught TypeError: expected number at $, got string 1
scriptc 0.5.0 no diagnostics Uncaught TypeError: expected number at $, got string 1

Without checkJs all three releases print 42 ab. The two instantiations of Lazy seem to share one specialization, so the string instance is read as a number.

Environment

Linux x86_64, glibc 2.39, scriptc's linux-x64-gnu packages from npm.

Found through light's probe packages/light/native/probes/p-g4-generic-composition.js, which fails the same way.

Ngôn ngữ chính
TypeScript
Star
6.7k
Fork
229
Merge trung bình
14 giờ 29 phút
Pull request đã merge (30 ngày)
787

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của vercel-labs/scriptc

Tất cả issue của vercel-labs/scriptc

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.