Skip to content

Commit 8840a5f

Browse files
authored
Allow coercing elgible variants to string/int/float (#6311)
* coerce variants to string/int * coerce elgible variants to float * reuse existing compiler logic for as attribute payloads * extract match logic and bake variant matching into the same branch as record matching * restructure guard for variant coercion * format * changelog and small cleanup
1 parent de9b806 commit 8840a5f

18 files changed

+205
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- Untagged variants: consider regexp as an object type. https://github.com/rescript-lang/rescript-compiler/pull/6296
1717
- Semantic-based optimization of code generated for untagged variants https://github.com/rescript-lang/rescript-compiler/issues/6108
1818
- Record type spreads: Allow using type variables in type spreads. Both uninstantiated and instantiated ones https://github.com/rescript-lang/rescript-compiler/pull/6309
19+
- Variants: Allow coercing variants to string/int/float when applicable https://github.com/rescript-lang/rescript-compiler/pull/6311
1920

2021
#### :bug: Bug Fix
2122
- Fix issue where dynamic import of module in the expression https://github.com/rescript-lang/rescript-compiler/pull/6310
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/variant_coercion_float.res:5:10-19
4+
5+
3 │ let x = One(true)
6+
4 │
7+
5 │ let y = (x :> float)
8+
6 │
9+
10+
Type x is not a subtype of float
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/variant_coercion_float_as.res:5:10-19
4+
5+
3 │ let x = One
6+
4 │
7+
5 │ let y = (x :> float)
8+
6 │
9+
10+
Type x is not a subtype of float
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/variant_coercion_int.res:5:10-17
4+
5+
3 │ let x = One(true)
6+
4 │
7+
5 │ let y = (x :> int)
8+
6 │
9+
10+
Type x is not a subtype of int
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/variant_coercion_int_as.res:5:10-17
4+
5+
3 │ let x = One
6+
4 │
7+
5 │ let y = (x :> int)
8+
6 │
9+
10+
Type x is not a subtype of int
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/variant_coercion_string.res:5:10-20
4+
5+
3 │ let x = One(true)
6+
4 │
7+
5 │ let y = (x :> string)
8+
6 │
9+
10+
Type x is not a subtype of string
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/variant_coercion_string_as.res:5:10-20
4+
5+
3 │ let x = One
6+
4 │
7+
5 │ let y = (x :> string)
8+
6 │
9+
10+
Type x is not a subtype of string
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type x = | @as(1.1) One(bool) | @as(2.2) Two
2+
3+
let x = One(true)
4+
5+
let y = (x :> float)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type x = | @as(1.1) One | Two
2+
3+
let x = One
4+
5+
let y = (x :> float)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type x = | @as(1) One(bool) | @as(2) Two
2+
3+
let x = One(true)
4+
5+
let y = (x :> int)

0 commit comments

Comments
 (0)