Skip to content

Commit

Permalink
[flow][match] Error on as patterns directly on binding patterns
Browse files Browse the repository at this point in the history
Summary:
Error on `as` patterns directly on binding patterns.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D67502324

fbshipit-source-id: 82cb5634c4ac3bbf88cc8020dfe013982cc19458
  • Loading branch information
gkz authored and facebook-github-bot committed Dec 20, 2024
1 parent fd84ce5 commit 83ff18e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/typing/debug_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,7 @@ let dump_error_message =
| EMatchDuplicateObjectProperty { loc; name } ->
spf "EMatchDuplicateObjectProperty (%s) (%s)" (string_of_aloc loc) name
| EMatchBindingInOrPattern { loc } -> spf "EMatchBindingInOrPattern (%s)" (string_of_aloc loc)
| EMatchInvalidAsPattern { loc } -> spf "EMatchInvalidAsPattern (%s)" (string_of_aloc loc)
| EDevOnlyRefinedLocInfo { refined_loc; refining_locs = _ } ->
spf "EDevOnlyRefinedLocInfo {refined_loc=%s}" (string_of_aloc refined_loc)
| EDevOnlyInvalidatedRefinementInfo { read_loc; invalidation_info = _ } ->
Expand Down
8 changes: 7 additions & 1 deletion src/typing/errors/error_message.ml
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ and 'loc t' =
name: string;
}
| EMatchBindingInOrPattern of { loc: 'loc }
| EMatchInvalidAsPattern of { loc: 'loc }
(* Dev only *)
| EDevOnlyRefinedLocInfo of {
refined_loc: 'loc;
Expand Down Expand Up @@ -1440,6 +1441,7 @@ let rec map_loc_of_error_message (f : 'a -> 'b) : 'a t' -> 'b t' =
| EMatchDuplicateObjectProperty { loc; name } ->
EMatchDuplicateObjectProperty { loc = f loc; name }
| EMatchBindingInOrPattern { loc } -> EMatchBindingInOrPattern { loc = f loc }
| EMatchInvalidAsPattern { loc } -> EMatchInvalidAsPattern { loc = f loc }
| EDevOnlyInvalidatedRefinementInfo { read_loc; invalidation_info } ->
EDevOnlyInvalidatedRefinementInfo
{
Expand Down Expand Up @@ -1743,7 +1745,8 @@ let util_use_op_of_msg nope util = function
| EMatchInvalidUnaryZero _
| EMatchInvalidUnaryPlusBigInt _
| EMatchDuplicateObjectProperty _
| EMatchBindingInOrPattern _ ->
| EMatchBindingInOrPattern _
| EMatchInvalidAsPattern _ ->
nope

(* Not all messages (i.e. those whose locations are based on use_ops) have locations that can be
Expand Down Expand Up @@ -1951,6 +1954,7 @@ let loc_of_msg : 'loc t' -> 'loc option = function
| EMatchInvalidUnaryPlusBigInt { loc } -> Some loc
| EMatchDuplicateObjectProperty { loc; _ } -> Some loc
| EMatchBindingInOrPattern { loc } -> Some loc
| EMatchInvalidAsPattern { loc } -> Some loc
| EDevOnlyRefinedLocInfo { refined_loc; refining_locs = _ } -> Some refined_loc
| EDevOnlyInvalidatedRefinementInfo { read_loc; invalidation_info = _ } -> Some read_loc
| EUnableToSpread _
Expand Down Expand Up @@ -2907,6 +2911,7 @@ let friendly_message_of_msg = function
| EMatchDuplicateObjectProperty { loc = _; name } ->
Normal (MessageMatchDuplicateObjectProperty { name })
| EMatchBindingInOrPattern { loc = _ } -> Normal MessageMatchBindingInOrPattern
| EMatchInvalidAsPattern { loc = _ } -> Normal MessageMatchInvalidAsPattern

let defered_in_speculation = function
| EUntypedTypeImport _
Expand Down Expand Up @@ -3250,3 +3255,4 @@ let error_code_of_message err : error_code option =
| EMatchInvalidUnaryPlusBigInt _ -> Some MatchInvalidPattern
| EMatchDuplicateObjectProperty _ -> Some MatchInvalidPattern
| EMatchBindingInOrPattern _ -> Some MatchInvalidPattern
| EMatchInvalidAsPattern _ -> Some MatchInvalidPattern
2 changes: 2 additions & 0 deletions src/typing/errors/flow_intermediate_error.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4010,6 +4010,8 @@ let to_printable_error :
[text "Duplicate property "; code name; text " in object pattern."]
| MessageMatchBindingInOrPattern ->
[text "New bindings in 'or' patterns are not yet supported."]
| MessageMatchInvalidAsPattern ->
[text "Invalid "; code "as"; text " pattern. Direct use on a binding pattern is not allowed."]
in
let rec convert_error_message { kind; loc; error_code; root; message; misplaced_source_file = _ }
=
Expand Down
1 change: 1 addition & 0 deletions src/typing/errors/flow_intermediate_error_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ type 'loc message =
| MessageMatchInvalidUnaryPlusBigInt
| MessageMatchDuplicateObjectProperty of { name: string }
| MessageMatchBindingInOrPattern
| MessageMatchInvalidAsPattern

type 'loc intermediate_error = {
kind: Flow_errors_utils.error_kind;
Expand Down
3 changes: 3 additions & 0 deletions src/typing/match_pattern.ml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ let rec pattern_ cx ~on_identifier ~on_expression ~on_binding ~in_or_pattern acc
in
OrPattern { OrPattern.patterns; comments }
| AsPattern { AsPattern.pattern = p; target; comments } ->
(match p with
| (_, BindingPattern _) -> Flow_js.add_output cx (Error_message.EMatchInvalidAsPattern { loc })
| _ -> ());
let p = pattern_ cx ~on_identifier ~on_expression ~on_binding ~in_or_pattern acc p in
let target =
match target with
Expand Down
18 changes: 17 additions & 1 deletion tests/match/match.exp
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,22 @@ New bindings in 'or' patterns are not yet supported. [match-invalid-pattern]
^


Error ------------------------------------------------------------------------------------------ pattern-errors.js:109:5

Invalid `as` pattern. Direct use on a binding pattern is not allowed. [match-invalid-pattern]

109| const a as b: 0, // ERROR
^^^^^^^^^^^^


Error ------------------------------------------------------------------------------------------ pattern-errors.js:110:5

Invalid `as` pattern. Direct use on a binding pattern is not allowed. [match-invalid-pattern]

110| const a as const b: 0, // ERROR
^^^^^^^^^^^^^^^^^^


Error -------------------------------------------------------------------------------------------------- patterns.js:9:3

Cannot cast `out` to empty because number [1] is incompatible with empty [2]. [incompatible-cast]
Expand Down Expand Up @@ -777,4 +793,4 @@ References:



Found 62 errors
Found 64 errors
10 changes: 10 additions & 0 deletions tests/match/pattern-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,13 @@
_ | {...const a}: 0, // ERROR
};
}

// As pattern on binding pattern
{
declare const x: [boolean];

const e1 = match (x) {
const a as b: 0, // ERROR
const a as const b: 0, // ERROR
}
}

0 comments on commit 83ff18e

Please sign in to comment.