Skip to content

AST: use inline record for Pexp_apply. #7238

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 4 commits into from
Jan 11, 2025
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
21 changes: 14 additions & 7 deletions analysis/reanalyze/src/Arnold.ml
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ module FindFunctionsCalled = struct
let super = Tast_mapper.default in
let expr (self : Tast_mapper.mapper) (e : Typedtree.expression) =
(match e.exp_desc with
| Texp_apply ({exp_desc = Texp_ident (callee, _, _)}, _args) ->
| Texp_apply {funct = {exp_desc = Texp_ident (callee, _, _)}} ->
let functionName = Path.name callee in
callees := !callees |> StringSet.add functionName
| _ -> ());
Expand Down Expand Up @@ -577,7 +577,10 @@ module ExtendFunctionTable = struct
} ->
Some (path, loc)
| Some
{exp_desc = Texp_apply ({exp_desc = Texp_ident (path, {loc}, _)}, args)}
{
exp_desc =
Texp_apply {funct = {exp_desc = Texp_ident (path, {loc}, _)}; args};
}
when kindOpt <> None ->
let checkArg ((argLabel : Asttypes.arg_label), _argOpt) =
match (argLabel, kindOpt) with
Expand Down Expand Up @@ -617,7 +620,7 @@ module ExtendFunctionTable = struct
calls a progress function"
functionName printPos id_pos;
})))
| Texp_apply ({exp_desc = Texp_ident (callee, _, _)}, args)
| Texp_apply {funct = {exp_desc = Texp_ident (callee, _, _)}; args}
when callee |> FunctionTable.isInFunctionInTable ~functionTable ->
let functionName = Path.name callee in
args
Expand Down Expand Up @@ -665,7 +668,8 @@ module CheckExpressionWellFormed = struct
| Texp_ident (path, {loc}, _) ->
checkIdent ~path ~loc;
e
| Texp_apply ({exp_desc = Texp_ident (functionPath, _, _)}, args) ->
| Texp_apply {funct = {exp_desc = Texp_ident (functionPath, _, _)}; args}
->
let functionName = Path.name functionPath in
args
|> List.iter (fun ((argLabel : Asttypes.arg_label), argOpt) ->
Expand Down Expand Up @@ -739,8 +743,10 @@ module Compile = struct
match expr.exp_desc with
| Texp_ident _ -> Command.nothing
| Texp_apply
(({exp_desc = Texp_ident (calleeToRename, l, vd)} as expr), argsToExtend)
-> (
{
funct = {exp_desc = Texp_ident (calleeToRename, l, vd)} as expr;
args = argsToExtend;
} -> (
let callee, args =
match
Hashtbl.find_opt ctx.innerRecursiveFunctions
Expand Down Expand Up @@ -844,7 +850,8 @@ module Compile = struct
and create a function call with the appropriate arguments *)
assert false
| None -> expr |> expression ~ctx |> evalArgs ~args ~ctx)
| Texp_apply (expr, args) -> expr |> expression ~ctx |> evalArgs ~args ~ctx
| Texp_apply {funct = expr; args} ->
expr |> expression ~ctx |> evalArgs ~args ~ctx
| Texp_let
( Recursive,
[{vb_pat = {pat_desc = Tpat_var (id, _); pat_loc}; vb_expr}],
Expand Down
22 changes: 14 additions & 8 deletions analysis/reanalyze/src/DeadValue.ml
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,16 @@ let rec collectExpr super self (e : Typedtree.expression) =
ValueReferences.add locTo.loc_start Location.none.loc_start)
else addValueReference ~addFileReference:true ~locFrom ~locTo
| Texp_apply
( {
exp_desc =
Texp_ident
(path, _, {Types.val_loc = {loc_ghost = false; _} as locTo});
exp_type;
},
args ) ->
{
funct =
{
exp_desc =
Texp_ident
(path, _, {Types.val_loc = {loc_ghost = false; _} as locTo});
exp_type;
};
args;
} ->
args
|> processOptionalArgs ~expType:exp_type
~locFrom:(locFrom : Location.t)
Expand Down Expand Up @@ -163,7 +166,10 @@ let rec collectExpr super self (e : Typedtree.expression) =
{
exp_desc =
Texp_apply
({exp_desc = Texp_ident (idArg2, _, _)}, args);
{
funct = {exp_desc = Texp_ident (idArg2, _, _)};
args;
};
};
};
};
Expand Down
14 changes: 9 additions & 5 deletions analysis/reanalyze/src/Exception.ml
Original file line number Diff line number Diff line change
Expand Up @@ -294,24 +294,28 @@ let traverseAst () =
}
:: !currentEvents
| Texp_apply
( {exp_desc = Texp_ident (atat, _, _)},
[(_lbl1, Some {exp_desc = Texp_ident (callee, _, _)}); arg] )
{
funct = {exp_desc = Texp_ident (atat, _, _)};
args = [(_lbl1, Some {exp_desc = Texp_ident (callee, _, _)}); arg];
}
when (* raise @@ Exn(...) *)
atat |> Path.name = "Pervasives.@@" && callee |> Path.name |> isRaise
->
let exceptions = [arg] |> raiseArgs in
currentEvents := {Event.exceptions; loc; kind = Raises} :: !currentEvents;
arg |> snd |> iterExprOpt self
| Texp_apply
( {exp_desc = Texp_ident (atat, _, _)},
[arg; (_lbl1, Some {exp_desc = Texp_ident (callee, _, _)})] )
{
funct = {exp_desc = Texp_ident (atat, _, _)};
args = [arg; (_lbl1, Some {exp_desc = Texp_ident (callee, _, _)})];
}
when (* Exn(...) |> raise *)
atat |> Path.name = "Pervasives.|>" && callee |> Path.name |> isRaise
->
let exceptions = [arg] |> raiseArgs in
currentEvents := {Event.exceptions; loc; kind = Raises} :: !currentEvents;
arg |> snd |> iterExprOpt self
| Texp_apply (({exp_desc = Texp_ident (callee, _, _)} as e), args) ->
| Texp_apply {funct = {exp_desc = Texp_ident (callee, _, _)} as e; args} ->
let calleeName = Path.name callee in
if calleeName |> isRaise then
let exceptions = args |> raiseArgs in
Expand Down
2 changes: 1 addition & 1 deletion analysis/reanalyze/src/SideEffects.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let rec exprNoSideEffects (expr : Typedtree.expression) =
| Texp_ident _ | Texp_constant _ -> true
| Texp_construct (_, _, el) -> el |> List.for_all exprNoSideEffects
| Texp_function _ -> true
| Texp_apply ({exp_desc = Texp_ident (path, _, _)}, args)
| Texp_apply {funct = {exp_desc = Texp_ident (path, _, _)}; args}
when path |> pathIsWhitelistedForSideEffects ->
args |> List.for_all (fun (_, eo) -> eo |> exprOptNoSideEffects)
| Texp_apply _ -> false
Expand Down
98 changes: 65 additions & 33 deletions analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -255,33 +255,43 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
| None -> None
| Some contexPath -> Some (CPObj (contexPath, txt)))
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}},
[
(_, lhs);
(_, {pexp_desc = Pexp_apply (d, args); pexp_loc; pexp_attributes});
] ) ->
{
funct =
{
pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")};
pexp_loc;
pexp_attributes;
};
args = [(_, lhs); (_, {pexp_desc = Pexp_apply {funct = d; args}})];
} ->
(* Transform away pipe with apply call *)
exprToContextPath
{
pexp_desc = Pexp_apply (d, (Nolabel, lhs) :: args);
pexp_desc = Pexp_apply {funct = d; args = (Nolabel, lhs) :: args};
pexp_loc;
pexp_attributes;
}
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}},
[(_, lhs); (_, {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes})]
) ->
{
funct = {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}};
args =
[
(_, lhs); (_, {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes});
];
} ->
(* Transform away pipe with identifier *)
exprToContextPath
{
pexp_desc =
Pexp_apply
( {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes},
[(Nolabel, lhs)] );
{
funct = {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes};
args = [(Nolabel, lhs)];
};
pexp_loc;
pexp_attributes;
}
| Pexp_apply (e1, args) -> (
| Pexp_apply {funct = e1; args} -> (
match exprToContextPath e1 with
| None -> None
| Some contexPath -> Some (CPApply (contexPath, args |> List.map fst)))
Expand Down Expand Up @@ -313,14 +323,18 @@ let completePipeChain (exp : Parsetree.expression) =
(* When the left side of the pipe we're completing is a function application.
Example: someArray->Js.Array2.map(v => v + 2)-> *)
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}},
[_; (_, {pexp_desc = Pexp_apply (d, _)})] ) ->
{
funct = {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}};
args = [_; (_, {pexp_desc = Pexp_apply {funct = d}})];
} ->
exprToContextPath exp |> Option.map (fun ctxPath -> (ctxPath, d.pexp_loc))
(* When the left side of the pipe we're completing is an identifier application.
Example: someArray->filterAllTheGoodStuff-> *)
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}},
[_; (_, {pexp_desc = Pexp_ident _; pexp_loc})] ) ->
{
funct = {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}};
args = [_; (_, {pexp_desc = Pexp_ident _; pexp_loc})];
} ->
exprToContextPath exp |> Option.map (fun ctxPath -> (ctxPath, pexp_loc))
| _ -> None

Expand Down Expand Up @@ -1094,11 +1108,15 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
scope := oldScope);
resetCurrentCtxPath oldCtxPath
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u"); loc = opLoc}},
[
(_, lhs);
(_, {pexp_desc = Pexp_extension _; pexp_loc = {loc_ghost = true}});
] )
{
funct =
{pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u"); loc = opLoc}};
args =
[
(_, lhs);
(_, {pexp_desc = Pexp_extension _; pexp_loc = {loc_ghost = true}});
];
}
when opLoc |> Loc.hasPos ~pos:posBeforeCursor ->
(* Case foo-> when the parser adds a ghost expression to the rhs
so the apply expression does not include the cursor *)
Expand Down Expand Up @@ -1228,7 +1246,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
exprLoc = e.pexp_loc;
}))
| None -> ())
| Pexp_apply ({pexp_desc = Pexp_ident compName}, args)
| Pexp_apply {funct = {pexp_desc = Pexp_ident compName}; args}
when Res_parsetree_viewer.is_jsx_expression expr ->
inJsxContext := true;
let jsxProps = CompletionJsx.extractJsxProps ~compName ~args in
Expand Down Expand Up @@ -1269,23 +1287,34 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
}))
else iterateJsxProps ~iterator jsxProps
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}},
[
(_, lhs);
(_, {pexp_desc = Pexp_ident {txt = Longident.Lident id; loc}});
] )
{
funct = {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}};
args =
[
(_, lhs);
(_, {pexp_desc = Pexp_ident {txt = Longident.Lident id; loc}});
];
}
when loc |> Loc.hasPos ~pos:posBeforeCursor ->
if Debug.verbose () then print_endline "[expr_iter] Case foo->id";
setPipeResult ~lhs ~id |> ignore
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u"); loc = opLoc}},
[(_, lhs); _] )
{
funct =
{
pexp_desc =
Pexp_ident {txt = Lident ("|." | "|.u"); loc = opLoc};
};
args = [(_, lhs); _];
}
when Loc.end_ opLoc = posCursor ->
if Debug.verbose () then print_endline "[expr_iter] Case foo->";
setPipeResult ~lhs ~id:"" |> ignore
| Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}},
[_; (_, {pexp_desc = Pexp_apply (funExpr, args)})] )
{
funct = {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}};
args = [_; (_, {pexp_desc = Pexp_apply {funct = funExpr; args}})];
}
when (* Normally named arg completion fires when the cursor is right after the expression.
E.g in foo(~<---there
But it should not fire in foo(~a)<---there *)
Expand Down Expand Up @@ -1318,10 +1347,13 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
resetCurrentCtxPath oldCtxPath)
| Some argCompletable -> setResult argCompletable)
| Pexp_apply
({pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}}, [_; _]) ->
{
funct = {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}};
args = [_; _];
} ->
(* Ignore any other pipe. *)
()
| Pexp_apply (funExpr, args)
| Pexp_apply {funct = funExpr; args}
when not
(Loc.end_ expr.pexp_loc = posCursor
&& charBeforeCursor = Some ')') -> (
Expand Down
2 changes: 1 addition & 1 deletion analysis/src/DumpAst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ and printExprItem expr ~pos ~indentation =
|> String.concat "\n")
| Pexp_ident {txt} ->
"Pexp_ident:" ^ (Utils.flattenLongIdent txt |> SharedTypes.ident)
| Pexp_apply (expr, args) ->
| Pexp_apply {funct = expr; args} ->
let printLabel labelled ~pos =
match labelled with
| None -> "<unlabelled>"
Expand Down
15 changes: 9 additions & 6 deletions analysis/src/SemanticTokens.ml
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ let command ~debug ~emitter ~path =
~posEnd:(Some (Loc.end_ loc))
~lid ~debug;
Ast_iterator.default_iterator.expr iterator e
| Pexp_apply ({pexp_desc = Pexp_ident lident; pexp_loc}, args)
| Pexp_apply {funct = {pexp_desc = Pexp_ident lident; pexp_loc}; args}
when Res_parsetree_viewer.is_jsx_expression e ->
(*
Angled brackets:
Expand Down Expand Up @@ -299,11 +299,14 @@ let command ~debug ~emitter ~path =

args |> List.iter (fun (_lbl, arg) -> iterator.expr iterator arg)
| Pexp_apply
( {
pexp_desc =
Pexp_ident {txt = Longident.Lident (("<" | ">") as op); loc};
},
[_; _] ) ->
{
funct =
{
pexp_desc =
Pexp_ident {txt = Longident.Lident (("<" | ">") as op); loc};
};
args = [_; _];
} ->
if debug then
Printf.printf "Binary operator %s %s\n" op (Loc.toString loc);
emitter |> emitFromLoc ~loc ~type_:Operator;
Expand Down
27 changes: 16 additions & 11 deletions analysis/src/SignatureHelp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -364,16 +364,20 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
| {
pexp_desc =
Pexp_apply
( {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}},
[
_;
( _,
{
pexp_desc =
Pexp_apply (({pexp_desc = Pexp_ident _} as exp), args);
pexp_loc;
} );
] );
{
funct = {pexp_desc = Pexp_ident {txt = Lident ("|." | "|.u")}};
args =
[
_;
( _,
{
pexp_desc =
Pexp_apply
{funct = {pexp_desc = Pexp_ident _} as exp; args};
pexp_loc;
} );
];
};
}
when locHasCursor pexp_loc ->
let argAtCursor, extractedArgs =
Expand All @@ -383,7 +387,8 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
(exp.pexp_loc, `FunctionCall (argAtCursor, exp, extractedArgs))
(* Look for applying idents, like someIdent(...) *)
| {
pexp_desc = Pexp_apply (({pexp_desc = Pexp_ident _} as exp), args);
pexp_desc =
Pexp_apply {funct = {pexp_desc = Pexp_ident _} as exp; args};
pexp_loc;
}
when locHasCursor pexp_loc ->
Expand Down
Loading
Loading