-
Notifications
You must be signed in to change notification settings - Fork 470
Allow coercing elgible variants to string/int/float #6311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0743b86
coerce variants to string/int
zth 4f7da57
coerce elgible variants to float
zth f0a9be9
reuse existing compiler logic for as attribute payloads
zth 02b4281
extract match logic and bake variant matching into the same branch as…
zth 3eb7b49
restructure guard for variant coercion
zth 5cce900
format
zth 2b5dfb0
changelog and small cleanup
zth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
jscomp/build_tests/super_errors/expected/variant_coercion_float.res.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
[1;31mWe've found a bug for you![0m | ||
[36m/.../fixtures/variant_coercion_float.res[0m:[2m5:10-19[0m | ||
|
||
3 [2m│[0m let x = One(true) | ||
4 [2m│[0m | ||
[1;31m5[0m [2m│[0m let y = ([1;31mx :> float[0m) | ||
6 [2m│[0m | ||
|
||
Type x is not a subtype of float |
10 changes: 10 additions & 0 deletions
10
jscomp/build_tests/super_errors/expected/variant_coercion_float_as.res.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
[1;31mWe've found a bug for you![0m | ||
[36m/.../fixtures/variant_coercion_float_as.res[0m:[2m5:10-19[0m | ||
|
||
3 [2m│[0m let x = One | ||
4 [2m│[0m | ||
[1;31m5[0m [2m│[0m let y = ([1;31mx :> float[0m) | ||
6 [2m│[0m | ||
|
||
Type x is not a subtype of float |
10 changes: 10 additions & 0 deletions
10
jscomp/build_tests/super_errors/expected/variant_coercion_int.res.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
[1;31mWe've found a bug for you![0m | ||
[36m/.../fixtures/variant_coercion_int.res[0m:[2m5:10-17[0m | ||
|
||
3 [2m│[0m let x = One(true) | ||
4 [2m│[0m | ||
[1;31m5[0m [2m│[0m let y = ([1;31mx :> int[0m) | ||
6 [2m│[0m | ||
|
||
Type x is not a subtype of int |
10 changes: 10 additions & 0 deletions
10
jscomp/build_tests/super_errors/expected/variant_coercion_int_as.res.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
[1;31mWe've found a bug for you![0m | ||
[36m/.../fixtures/variant_coercion_int_as.res[0m:[2m5:10-17[0m | ||
|
||
3 [2m│[0m let x = One | ||
4 [2m│[0m | ||
[1;31m5[0m [2m│[0m let y = ([1;31mx :> int[0m) | ||
6 [2m│[0m | ||
|
||
Type x is not a subtype of int |
10 changes: 10 additions & 0 deletions
10
jscomp/build_tests/super_errors/expected/variant_coercion_string.res.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
[1;31mWe've found a bug for you![0m | ||
[36m/.../fixtures/variant_coercion_string.res[0m:[2m5:10-20[0m | ||
|
||
3 [2m│[0m let x = One(true) | ||
4 [2m│[0m | ||
[1;31m5[0m [2m│[0m let y = ([1;31mx :> string[0m) | ||
6 [2m│[0m | ||
|
||
Type x is not a subtype of string |
10 changes: 10 additions & 0 deletions
10
jscomp/build_tests/super_errors/expected/variant_coercion_string_as.res.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
[1;31mWe've found a bug for you![0m | ||
[36m/.../fixtures/variant_coercion_string_as.res[0m:[2m5:10-20[0m | ||
|
||
3 [2m│[0m let x = One | ||
4 [2m│[0m | ||
[1;31m5[0m [2m│[0m let y = ([1;31mx :> string[0m) | ||
6 [2m│[0m | ||
|
||
Type x is not a subtype of string |
5 changes: 5 additions & 0 deletions
5
jscomp/build_tests/super_errors/fixtures/variant_coercion_float.res
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type x = | @as(1.1) One(bool) | @as(2.2) Two | ||
|
||
let x = One(true) | ||
|
||
let y = (x :> float) |
5 changes: 5 additions & 0 deletions
5
jscomp/build_tests/super_errors/fixtures/variant_coercion_float_as.res
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type x = | @as(1.1) One | Two | ||
|
||
let x = One | ||
|
||
let y = (x :> float) |
5 changes: 5 additions & 0 deletions
5
jscomp/build_tests/super_errors/fixtures/variant_coercion_int.res
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type x = | @as(1) One(bool) | @as(2) Two | ||
|
||
let x = One(true) | ||
|
||
let y = (x :> int) |
5 changes: 5 additions & 0 deletions
5
jscomp/build_tests/super_errors/fixtures/variant_coercion_int_as.res
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type x = | @as(1) One | Two | ||
|
||
let x = One | ||
|
||
let y = (x :> int) |
5 changes: 5 additions & 0 deletions
5
jscomp/build_tests/super_errors/fixtures/variant_coercion_string.res
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type x = One(bool) | Two | ||
|
||
let x = One(true) | ||
|
||
let y = (x :> string) |
5 changes: 5 additions & 0 deletions
5
jscomp/build_tests/super_errors/fixtures/variant_coercion_string_as.res
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type x = One | @as(2) Two | ||
|
||
let x = One | ||
|
||
let y = (x :> string) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
type variant_representation = String | Int | Float | Other | ||
|
||
let find_as_attribute_payload (attributes : Parsetree.attribute list) = | ||
zth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
attributes | ||
|> List.find_map (fun (({txt = name}, payload) : Parsetree.attribute) -> | ||
match (name, payload) with | ||
| ( "as", | ||
PStr | ||
[ | ||
{ | ||
pstr_desc = | ||
Pstr_eval ({pexp_desc = Pexp_constant constant}, _); | ||
}; | ||
] ) -> | ||
Some constant | ||
| _ -> None) | ||
|
||
let constructors_representations | ||
(constructors : Types.constructor_declaration list) = | ||
constructors | ||
|> List.map (fun (c : Types.constructor_declaration) -> | ||
match (c.cd_args, c.cd_attributes |> find_as_attribute_payload) with | ||
| Cstr_tuple [], (Some (Pconst_string _) | None) -> String | ||
| Cstr_tuple [], Some (Pconst_integer _) -> Int | ||
| Cstr_tuple [], Some (Pconst_float _) -> Float | ||
| _ -> Other) | ||
|
||
(* TODO: Improve error messages? Say why we can't coerce. *) | ||
|
||
let can_coerce_to_string (constructors : Types.constructor_declaration list) = | ||
zth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
constructors |> constructors_representations |> List.for_all (( = ) String) | ||
|
||
let can_coerce_to_int (constructors : Types.constructor_declaration list) = | ||
constructors |> constructors_representations |> List.for_all (( = ) Int) | ||
|
||
let can_coerce_to_float (constructors : Types.constructor_declaration list) = | ||
constructors |> constructors_representations |> List.for_all (( = ) Float) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
type a = One | @as("two") Two | Three | ||
|
||
let a: a = Three | ||
|
||
let b = (a :> string) | ||
|
||
type onlyInts = | @as(1) One1 | @as(2) Two2 | @as(3) Three3 | ||
|
||
let i = One1 | ||
|
||
let d = (i :> int) | ||
|
||
type onlyFloats = | @as(1.1) Onef | @as(2.2) Twof | @as(3.3) Threef | ||
|
||
let i = Onef | ||
|
||
let d = (i :> float) |
Large diffs are not rendered by default.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.