`(() :> string)` compiles, and other coercions and variant spreads treat `bool` and `unit` as variants
Maintainer thường phản hồi trong vòng 1 ngà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
- 35/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- javascript, ocaml
- Lĩnh vực
- compilers
Hướng nghiên cứu
Start with Variant_coercion.can_try_coerce_variant_to_primitive and type_is_variant in compiler/ml/variant_coercion.ml, then trace subtype_rec in Ctype and constructor spreading in compiler/ml/variant_type_spread.ml. Reproduce the coercion and spread cases from the issue, then run the compiler tests that cover variant coercions and spreads. Done means bool and unit values retain correct runtime behavior and invalid coercions are rejected; pull request #9218 is already open.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
unit can be coerced to string, a closed polymorphic variant whose tags are "true", "false" or "()" can be coerced to bool or unit, and bool or unit can be spread into a variant. The coercions compile to nothing, so undefined gets the type string, and a string gets the type bool or unit. The compiler also suggests these coercions: let s: string = u() for u: unit => unit is reported with "These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (u() :> string)", and let b: bool = p for p: [#"true" | #"false"] with (p :> bool).
let s: string = (() :> string)
let n = s->String.length
let n = undefined.length;
let s;
Loading the module throws TypeError: Cannot read properties of undefined (reading 'length'). The subtype check accepts unit for string wherever it compares the two: (f :> string => string) for f: string => unit, (g :> unit => int) for g: string => int, (p :> promise<string>) for p: promise<unit>, and a unit in a tuple, a record or object field or a constructor payload.
let toBool = (p: [#"true" | #"false"]): bool => (p :> bool)
let answer = toBool(#"false") ? "yes" : "no"
let isFalse = toBool(#"false") == false
function toBool(p) {
return p;
}
let isFalse = "false" === false;
let answer = "yes";
toBool(#"false") returns the string "false", which is truthy and is not false, so answer is "yes" and isFalse is false. In the same way, let u: unit = (p :> unit) for p: [#"()"] makes u the string "()", and u == () is false.
A variant that spreads bool builds true and false, but its switches and coercions take them for the strings "true" and "false":
type t = | ...bool | Other
let x: t = true
let s = (x :> string)
let r = switch x { | true => "yes" | false => "no" | Other => "other" }
let r;
switch (true) {
case "false" :
r = "no";
break;
case "true" :
r = "yes";
break;
case "Other" :
r = "other";
break;
}
let x = true;
let s = true;
r is undefined, and s is the boolean true typed as a string. On master, a variant that spreads unit builds undefined for (), and its switch compares undefined === "()"; 13.0.0-alpha.6 and earlier build the string "()" there.
Some coercions of unit that are rejected give reasons that do not apply to it. (() :> int) says "The constructor () of variant unit has no @as payload, which makes it a string at runtime", ("a" :> unit) says "Fix this by adding the @unboxed attribute to the variant unit", and on master (() :> unknown) says that the variants have different @unboxed configurations.
bool and unit are declared in compiler/ml/predef.ml as variants with the constructors false, true and () and no @as, but their values are false, true and undefined. Variant_coercion.can_try_coerce_variant_to_primitive in compiler/ml/variant_coercion.ml leaves out bool but not unit, so Ctype.subtype_rec checks unit with variant_has_same_runtime_representation_as_target, which takes a constructor without @as to be its name as a string, and so accepts string. Variant_coercion.type_is_variant, which guards the coercion from a polymorphic variant to a variant, leaves out neither type, and can_coerce_polyvariant_to_variant then matches #"true" to the constructor true by name. Variant_type_spread.map_constructors in compiler/ml/variant_type_spread.ml copies the constructors of bool and unit like those of any variant; Translcore.transl_exp builds true and false by name as booleans, and on master Lambda.const_constructor builds () as undefined, while the spread variant matches and coerces them as untagged constructors, which are strings.
Reproduced with 12.0.0-alpha.6, 12.3.1, 13.0.0-alpha.6 and master (c10223e80). 11.1.4 and 12.0.0-alpha.1 also accept (() :> string) and the spreads, but reject the polymorphic variant coercions, which became possible with #6981. The suggested coercion and the @as and @unboxed reasons appear in 12.3.1, 13.0.0-alpha.6 and master, not in 12.0.0-alpha.6.
Related: #6311, #6316, #6980, #6981, #7505.
- Ngôn ngữ chính
- OCaml
- Star
- 7.5k
- Fork
- 484
- Merge trung bình
- 16 giờ 57 phút
- Pull request đã merge (30 ngày)
- 258
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của rescript-lang/rescript
-
Exception analysis does not know the Temporal `fromStringOrThrow` functionsCó thể đã có người làm @cknitt đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
rescript-lang/rescript#9182 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
await in a functor body compiles to invalid JavaScriptCó thể đã có người làm @mununki đã nhận hôm nay. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
rescript-lang/rescript#9219 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
A polymorphic variant argument of an external is evaluated twice, or inside a generated functionCó thể đã có người làm @mununki đã nhận hôm nay. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 20/100
rescript-lang/rescript#9215 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
rescript-lang/rescript#9212 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
rescript-lang/rescript#9197 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của rescript-lang/rescript
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
bug stdlib
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
0.kind: enhancement 9.needs: package (update)
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
S3: friction & debt ws:tooling
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
MedakaLang/medaka#4865 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
subsystem: saw-core type: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
GaloisInc/saw-script#3516 ·
Maintainer thường phản hồi trong vòng 1 ngày