Skip to content
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

Detect binding_op punning during parsing #2474

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/Sugar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,7 @@ module Let_binding = struct
; lb_args
; lb_typ
; lb_exp
; lb_pun=
( match (lb_pat.ast.ppat_desc, lb_exp.ast.pexp_desc) with
| Ppat_var {txt= v; _}, Pexp_ident {txt= Lident e; _} ->
String.equal v e
| _ -> false )
; lb_pun= bo.pbop_is_pun
; lb_attrs= []
; lb_loc= bo.pbop_loc } )
end
3 changes: 2 additions & 1 deletion vendor/parser-extended/ast_helper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,12 @@ module Exp = struct
pc_rhs = rhs;
}

let binding_op op pat exp loc =
let binding_op op pat exp pun loc =
{
pbop_op = op;
pbop_pat = pat;
pbop_exp = exp;
pbop_is_pun = pun;
pbop_loc = loc;
}
end
Expand Down
4 changes: 2 additions & 2 deletions vendor/parser-extended/ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -605,13 +605,13 @@ module E = struct
| Pexp_infix (op, e1, e2) ->
infix ~loc ~attrs (map_loc sub op) (sub.expr sub e1) (sub.expr sub e2)

let map_binding_op sub {pbop_op; pbop_pat; pbop_exp; pbop_loc} =
let map_binding_op sub {pbop_op; pbop_pat; pbop_exp; pbop_is_pun; pbop_loc} =
let open Exp in
let op = map_loc sub pbop_op in
let pat = sub.pat sub pbop_pat in
let exp = sub.expr sub pbop_exp in
let loc = sub.location sub pbop_loc in
binding_op op pat exp loc
binding_op op pat exp pbop_is_pun loc

end

Expand Down
24 changes: 12 additions & 12 deletions vendor/parser-extended/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -2231,10 +2231,10 @@ expr:
| let_bindings(ext) IN seq_expr
{ expr_of_let_bindings ~loc:$sloc $1 $3 }
| pbop_op = mkrhs(LETOP) bindings = letop_bindings IN body = seq_expr
{ let (pbop_pat, pbop_exp, rev_ands) = bindings in
{ let (pbop_pat, pbop_exp, pbop_is_pun, rev_ands) = bindings in
let ands = List.rev rev_ands in
let pbop_loc = make_loc $sloc in
let let_ = {pbop_op; pbop_pat; pbop_exp; pbop_loc} in
let let_ = {pbop_op; pbop_pat; pbop_exp; pbop_is_pun; pbop_loc} in
mkexp ~loc:$sloc (Pexp_letop{ let_; ands; body}) }
| expr COLONCOLON e = expr
{ match e.pexp_desc, e.pexp_attributes with
Expand Down Expand Up @@ -2548,26 +2548,26 @@ and_let_binding:
;
letop_binding_body:
pat = let_ident exp = strict_binding
{ (pat, exp) }
{ (pat, exp, false) }
| val_ident
(* Let-punning *)
{ (mkpatvar ~loc:$loc $1, mkexpvar ~loc:$loc $1) }
{ (mkpatvar ~loc:$loc $1, mkexpvar ~loc:$loc $1, true) }
| pat = simple_pattern COLON typ = core_type EQUAL exp = seq_expr
{ let loc = ($startpos(pat), $endpos(typ)) in
(ghpat ~loc (Ppat_constraint(pat, typ)), exp) }
(ghpat ~loc (Ppat_constraint(pat, typ)), exp, false) }
| pat = pattern_no_exn EQUAL exp = seq_expr
{ (pat, exp) }
{ (pat, exp, false) }
;
letop_bindings:
body = letop_binding_body
{ let let_pat, let_exp = body in
let_pat, let_exp, [] }
{ let let_pat, let_exp, let_is_pun = body in
let_pat, let_exp, let_is_pun, [] }
| bindings = letop_bindings pbop_op = mkrhs(ANDOP) body = letop_binding_body
{ let let_pat, let_exp, rev_ands = bindings in
let pbop_pat, pbop_exp = body in
{ let let_pat, let_exp, let_is_pun, rev_ands = bindings in
let pbop_pat, pbop_exp, pbop_is_pun = body in
let pbop_loc = make_loc $sloc in
let and_ = {pbop_op; pbop_pat; pbop_exp; pbop_loc} in
let_pat, let_exp, and_ :: rev_ands }
let and_ = {pbop_op; pbop_pat; pbop_exp; pbop_is_pun; pbop_loc} in
let_pat, let_exp, let_is_pun, and_ :: rev_ands }
;
fun_binding:
strict_binding
Expand Down
1 change: 1 addition & 0 deletions vendor/parser-extended/parsetree.mli
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ and binding_op =
pbop_op : string loc;
pbop_pat : pattern;
pbop_exp : expression;
pbop_is_pun: bool;
pbop_loc : Location.t;
}

Expand Down
Loading