-
Notifications
You must be signed in to change notification settings - Fork 469
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 all 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
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
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,61 @@ | ||
let find_as_attribute_payload (attributes : Parsetree.attribute list) = | ||
attributes | ||
|> List.find_map (fun (attr : Parsetree.attribute) -> | ||
match attr with | ||
| {txt = "as"}, payload -> Some payload | ||
| _ -> None) | ||
|
||
(* TODO: Improve error messages? Say why we can't coerce. *) | ||
|
||
let check_constructors (constructors : Types.constructor_declaration list) check | ||
= | ||
List.for_all | ||
(fun (c : Types.constructor_declaration) -> | ||
check c.cd_args (find_as_attribute_payload c.cd_attributes)) | ||
constructors | ||
|
||
let can_coerce_to_string (constructors : Types.constructor_declaration list) = | ||
check_constructors constructors (fun args payload -> | ||
match (args, payload) with | ||
| Cstr_tuple [], None -> true | ||
| Cstr_tuple [], Some payload | ||
when Ast_payload.is_single_string payload |> Option.is_some -> | ||
true | ||
| _ -> false) | ||
|
||
let can_coerce_to_int (constructors : Types.constructor_declaration list) = | ||
check_constructors constructors (fun args payload -> | ||
match (args, payload) with | ||
| Cstr_tuple [], Some payload | ||
when Ast_payload.is_single_int payload |> Option.is_some -> | ||
true | ||
| _ -> false) | ||
|
||
let can_coerce_to_float (constructors : Types.constructor_declaration list) = | ||
check_constructors constructors (fun args payload -> | ||
match (args, payload) with | ||
| Cstr_tuple [], Some payload | ||
when Ast_payload.is_single_float payload |> Option.is_some -> | ||
true | ||
| _ -> false) | ||
|
||
let can_coerce_path (path : Path.t) = | ||
Path.same path Predef.path_string | ||
|| Path.same path Predef.path_int | ||
|| Path.same path Predef.path_float | ||
|
||
let can_coerce_variant ~(path : Path.t) | ||
(constructors : Types.constructor_declaration list) = | ||
if Path.same path Predef.path_string && can_coerce_to_string constructors then | ||
true | ||
else if Path.same path Predef.path_int && can_coerce_to_int constructors then | ||
true | ||
else if Path.same path Predef.path_float && can_coerce_to_float constructors | ||
then true | ||
else false | ||
|
||
let is_variant_typedecl | ||
((_, _, typedecl) : Path.t * Path.t * Types.type_declaration) = | ||
match typedecl with | ||
| {type_kind = Type_variant constructors} -> Some constructors | ||
| _ -> None |
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 ii = Onef | ||
|
||
let dd = (ii :> 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.