Skip to content

Store the label loc directly in the label, for application for now. #7253

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 24, 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
19 changes: 11 additions & 8 deletions analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
(* Transform away pipe with apply call *)
exprToContextPath
{
pexp_desc =
Pexp_apply {funct = d; args = (Nolabel, lhs) :: args; partial};
pexp_desc = Pexp_apply {funct = d; args = (Nolbl, lhs) :: args; partial};
pexp_loc;
pexp_attributes;
}
Expand All @@ -289,7 +288,7 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
Pexp_apply
{
funct = {pexp_desc = Pexp_ident id; pexp_loc; pexp_attributes};
args = [(Nolabel, lhs)];
args = [(Nolbl, lhs)];
partial;
};
pexp_loc;
Expand All @@ -298,7 +297,11 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
| Pexp_apply {funct = e1; args} -> (
match exprToContextPath e1 with
| None -> None
| Some contexPath -> Some (CPApply (contexPath, args |> List.map fst)))
| Some contexPath ->
Some
(CPApply
(contexPath, args |> List.map fst |> List.map Asttypes.to_arg_label))
)
| Pexp_tuple exprs ->
let exprsAsContextPaths = exprs |> List.filter_map exprToContextPath in
if List.length exprs = List.length exprsAsContextPaths then
Expand Down Expand Up @@ -1434,7 +1437,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
| Some (ctxPath, currentUnlabelledCount) ->
(processingFun :=
match lbl with
| Nolabel -> Some (ctxPath, currentUnlabelledCount + 1)
| Nolbl -> Some (ctxPath, currentUnlabelledCount + 1)
| _ -> Some (ctxPath, currentUnlabelledCount));
if Debug.verbose () then
print_endline "[expr_iter] Completing for argument value";
Expand All @@ -1444,10 +1447,10 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
functionContextPath = ctxPath;
argumentLabel =
(match lbl with
| Nolabel ->
| Nolbl ->
Unlabelled {argumentPosition = currentUnlabelledCount}
| Optional name -> Optional name
| Labelled name -> Labelled name);
| Opt {txt = name} -> Optional name
| Lbl {txt = name} -> Labelled name);
})
in
(match defaultExpOpt with
Expand Down
12 changes: 5 additions & 7 deletions analysis/src/CompletionJsx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -465,20 +465,18 @@ let extractJsxProps ~(compName : Longident.t Location.loc) ~args =
in
let rec processProps ~acc args =
match args with
| (Asttypes.Labelled "children", {Parsetree.pexp_loc}) :: _ ->
| (Asttypes.Lbl {txt = "children"}, {Parsetree.pexp_loc}) :: _ ->
{
compName;
props = List.rev acc;
childrenStart =
(if pexp_loc.loc_ghost then None else Some (Loc.start pexp_loc));
}
| ((Labelled s | Optional s), (eProp : Parsetree.expression)) :: rest -> (
let namedArgLoc =
eProp.pexp_attributes
|> List.find_opt (fun ({Asttypes.txt}, _) -> txt = "res.namedArgLoc")
in
| ((Lbl {txt = s; loc} | Opt {txt = s; loc}), (eProp : Parsetree.expression))
:: rest -> (
let namedArgLoc = if loc = Location.none then None else Some loc in
match namedArgLoc with
| Some ({loc}, _) ->
| Some loc ->
processProps
~acc:
({
Expand Down
6 changes: 3 additions & 3 deletions analysis/src/DumpAst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ and printExprItem expr ~pos ~indentation =
^ addIndentation (indentation + 1)
^ "arg: "
^ (match arg with
| Nolabel -> "Nolabel"
| Labelled name -> "Labelled(" ^ name ^ ")"
| Optional name -> "Optional(" ^ name ^ ")")
| Nolbl -> "Nolabel"
| Lbl {txt = name} -> "Labelled(" ^ name ^ ")"
| Opt {txt = name} -> "Optional(" ^ name ^ ")")
^ ",\n"
^ addIndentation (indentation + 2)
^ "pattern: "
Expand Down
2 changes: 1 addition & 1 deletion analysis/src/SemanticTokens.ml
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ let command ~debug ~emitter ~path =

let posOfGreatherthanAfterProps =
let rec loop = function
| (Asttypes.Labelled "children", {Parsetree.pexp_loc}) :: _ ->
| (Asttypes.Lbl {txt = "children"}, {Parsetree.pexp_loc}) :: _ ->
Loc.start pexp_loc
| _ :: args -> loop args
| [] -> (* should not happen *) (-1, -1)
Expand Down
14 changes: 6 additions & 8 deletions analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -898,28 +898,26 @@ type arg = {label: label; exp: Parsetree.expression}
let extractExpApplyArgs ~args =
let rec processArgs ~acc args =
match args with
| (((Asttypes.Labelled s | Optional s) as label), (e : Parsetree.expression))
| ( ((Asttypes.Lbl {txt = s; loc} | Opt {txt = s; loc}) as label),
(e : Parsetree.expression) )
:: rest -> (
let namedArgLoc =
e.pexp_attributes
|> List.find_opt (fun ({Asttypes.txt}, _) -> txt = "res.namedArgLoc")
in
let namedArgLoc = if loc = Location.none then None else Some loc in
match namedArgLoc with
| Some ({loc}, _) ->
| Some loc ->
let labelled =
{
name = s;
opt =
(match label with
| Optional _ -> true
| Opt _ -> true
| _ -> false);
posStart = Loc.start loc;
posEnd = Loc.end_ loc;
}
in
processArgs ~acc:({label = Some labelled; exp = e} :: acc) rest
| None -> processArgs ~acc rest)
| (Asttypes.Nolabel, (e : Parsetree.expression)) :: rest ->
| (Nolbl, (e : Parsetree.expression)) :: rest ->
if e.pexp_loc.loc_ghost then processArgs ~acc rest
else processArgs ~acc:({label = None; exp = e} :: acc) rest
| [] -> List.rev acc
Expand Down
3 changes: 2 additions & 1 deletion analysis/src/SignatureHelp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ let extractParameters ~signature ~typeStrForParser ~labelPrefixLen =
(* The AST locations does not account for "=?" of optional arguments, so add that to the offset here if needed. *)
let endOffset =
match argumentLabel with
| Asttypes.Optional _ -> endOffset + 2
| Asttypes.Opt _ -> endOffset + 2
| _ -> endOffset
in
extractParams nextFunctionExpr
Expand Down Expand Up @@ -474,6 +474,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
parameters =
parameters
|> List.map (fun (argLabel, start, end_) ->
let argLabel = Asttypes.to_arg_label argLabel in
let paramArgCount = !paramUnlabelledArgCount in
paramUnlabelledArgCount := paramArgCount + 1;
let unlabelledArgCount = ref 0 in
Expand Down
2 changes: 1 addition & 1 deletion analysis/src/TypeUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ module Codegen = struct
let mkFailWithExp () =
Ast_helper.Exp.apply
(Ast_helper.Exp.ident {txt = Lident "failwith"; loc = Location.none})
[(Nolabel, Ast_helper.Exp.constant (Pconst_string ("TODO", None)))]
[(Nolbl, Ast_helper.Exp.constant (Pconst_string ("TODO", None)))]

let mkConstructPat ?payload name =
Ast_helper.Pat.construct
Expand Down
4 changes: 2 additions & 2 deletions analysis/src/Xform.ml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module IfThenElse = struct
Pexp_ident
{txt = Longident.Lident (("==" | "!=") as op)};
};
args = [(Nolabel, arg1); (Nolabel, arg2)];
args = [(Nolbl, arg1); (Nolbl, arg2)];
};
},
e1,
Expand Down Expand Up @@ -300,7 +300,7 @@ module AddTypeAnnotation = struct
match e.pexp_desc with
| Pexp_fun {arg_label; lhs = pat; rhs = e} ->
let isUnlabeledOnlyArg =
argNum = 1 && arg_label = Nolabel
argNum = 1 && arg_label = Nolbl
&&
match e.pexp_desc with
| Pexp_fun _ -> false
Expand Down
39 changes: 16 additions & 23 deletions compiler/frontend/ast_compatible.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ open Parsetree
let default_loc = Location.none

let arrow ?loc ?attrs ~arity a b =
Ast_helper.Typ.arrow ?loc ?attrs ~arity Nolabel a b
Ast_helper.Typ.arrow ?loc ?attrs ~arity Nolbl a b

let apply_simple ?(loc = default_loc) ?(attrs = []) (fn : expression)
(args : expression list) : expression =
Expand All @@ -42,7 +42,7 @@ let apply_simple ?(loc = default_loc) ?(attrs = []) (fn : expression)
Pexp_apply
{
funct = fn;
args = Ext_list.map args (fun x -> (Asttypes.Nolabel, x));
args = Ext_list.map args (fun x -> (Asttypes.Nolbl, x));
partial = false;
};
}
Expand All @@ -51,8 +51,7 @@ let app1 ?(loc = default_loc) ?(attrs = []) fn arg1 : expression =
{
pexp_loc = loc;
pexp_attributes = attrs;
pexp_desc =
Pexp_apply {funct = fn; args = [(Nolabel, arg1)]; partial = false};
pexp_desc = Pexp_apply {funct = fn; args = [(Nolbl, arg1)]; partial = false};
}

let app2 ?(loc = default_loc) ?(attrs = []) fn arg1 arg2 : expression =
Expand All @@ -61,7 +60,7 @@ let app2 ?(loc = default_loc) ?(attrs = []) fn arg1 arg2 : expression =
pexp_attributes = attrs;
pexp_desc =
Pexp_apply
{funct = fn; args = [(Nolabel, arg1); (Nolabel, arg2)]; partial = false};
{funct = fn; args = [(Nolbl, arg1); (Nolbl, arg2)]; partial = false};
}

let app3 ?(loc = default_loc) ?(attrs = []) fn arg1 arg2 arg3 : expression =
Expand All @@ -72,7 +71,7 @@ let app3 ?(loc = default_loc) ?(attrs = []) fn arg1 arg2 arg3 : expression =
Pexp_apply
{
funct = fn;
args = [(Nolabel, arg1); (Nolabel, arg2); (Nolabel, arg3)];
args = [(Nolbl, arg1); (Nolbl, arg2); (Nolbl, arg3)];
partial = false;
};
}
Expand All @@ -83,15 +82,7 @@ let fun_ ?(loc = default_loc) ?(attrs = []) ?(async = false) ~arity pat exp =
pexp_attributes = attrs;
pexp_desc =
Pexp_fun
{
arg_label = Nolabel;
label_loc = Location.none;
default = None;
lhs = pat;
rhs = exp;
arity;
async;
};
{arg_label = Nolbl; default = None; lhs = pat; rhs = exp; arity; async};
}

let const_exp_string ?(loc = default_loc) ?(attrs = []) ?delimiter (s : string)
Expand All @@ -118,25 +109,27 @@ let apply_labels ?(loc = default_loc) ?(attrs = []) fn
Pexp_apply
{
funct = fn;
args = Ext_list.map args (fun (l, a) -> (Asttypes.Labelled l, a));
args =
Ext_list.map args (fun (l, a) ->
(Asttypes.Lbl {txt = l; loc = Location.none}, a));
partial = false;
};
}

let label_arrow ?(loc = default_loc) ?(attrs = []) ~arity s arg ret : core_type
=
let label_arrow ?(loc = default_loc) ?(attrs = []) ~arity txt arg ret :
core_type =
{
ptyp_desc =
Ptyp_arrow {lbl = Labelled s; lbl_loc = Location.none; arg; ret; arity};
Ptyp_arrow {lbl = Asttypes.Lbl {txt; loc = default_loc}; arg; ret; arity};
ptyp_loc = loc;
ptyp_attributes = attrs;
}

let opt_arrow ?(loc = default_loc) ?(attrs = []) ~arity s arg ret : core_type =
let opt_arrow ?(loc = default_loc) ?(attrs = []) ~arity txt arg ret : core_type
=
{
ptyp_desc =
Ptyp_arrow
{lbl = Asttypes.Optional s; lbl_loc = Location.none; arg; ret; arity};
Ptyp_arrow {lbl = Asttypes.Opt {txt; loc = default_loc}; arg; ret; arity};
ptyp_loc = loc;
ptyp_attributes = attrs;
}
Expand Down Expand Up @@ -167,4 +160,4 @@ type object_field = Parsetree.object_field

let object_field l attrs ty = Parsetree.Otag (l, attrs, ty)

type args = (Asttypes.arg_label * Parsetree.expression) list
type args = (Asttypes.arg_label_loc * Parsetree.expression) list
2 changes: 1 addition & 1 deletion compiler/frontend/ast_compatible.mli
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,4 @@ type object_field = Parsetree.object_field
val object_field :
Asttypes.label Asttypes.loc -> attributes -> core_type -> object_field

type args = (Asttypes.arg_label * Parsetree.expression) list
type args = (Asttypes.arg_label_loc * Parsetree.expression) list
14 changes: 3 additions & 11 deletions compiler/frontend/ast_core_type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ let get_curry_arity (ty : t) =
let is_arity_one ty = get_curry_arity ty = 1

type param_type = {
label: Asttypes.arg_label;
label: Asttypes.arg_label_loc;
ty: Parsetree.core_type;
attr: Parsetree.attributes;
loc: loc;
Expand All @@ -142,15 +142,7 @@ let mk_fn_type (new_arg_types_ty : param_type list) (result : t) : t =
Ext_list.fold_right new_arg_types_ty result
(fun {label; ty; attr; loc} acc ->
{
ptyp_desc =
Ptyp_arrow
{
lbl = label;
lbl_loc = Location.none;
arg = ty;
ret = acc;
arity = None;
};
ptyp_desc = Ptyp_arrow {lbl = label; arg = ty; ret = acc; arity = None};
ptyp_loc = loc;
ptyp_attributes = attr;
})
Expand Down Expand Up @@ -179,5 +171,5 @@ let list_of_arrow (ty : t) : t * param_type list =
let add_last_obj (ty : t) (obj : t) =
let result, params = list_of_arrow ty in
mk_fn_type
(params @ [{label = Nolabel; ty = obj; attr = []; loc = obj.ptyp_loc}])
(params @ [{label = Nolbl; ty = obj; attr = []; loc = obj.ptyp_loc}])
result
2 changes: 1 addition & 1 deletion compiler/frontend/ast_core_type.mli
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ val get_uncurry_arity : t -> int option
*)

type param_type = {
label: Asttypes.arg_label;
label: Asttypes.arg_label_loc;
ty: t;
attr: Parsetree.attributes;
loc: Location.t;
Expand Down
2 changes: 1 addition & 1 deletion compiler/frontend/ast_core_type_class_type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ let typ_mapper (self : Bs_ast_mapper.mapper) (ty : Parsetree.core_type) =
| Meth_callback attr, attrs -> (attrs, attr +> ty)
in
Ast_compatible.object_field name attrs
(Ast_typ_uncurry.to_uncurry_type loc self Nolabel core_type
(Ast_typ_uncurry.to_uncurry_type loc self Nolbl core_type
(Ast_literal.type_unit ~loc ()))
in
let not_getter_setter ty =
Expand Down
7 changes: 3 additions & 4 deletions compiler/frontend/ast_exp_apply.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) : exp =
| Pexp_apply {funct = fn1; args; partial} ->
Bs_ast_invariant.warn_discarded_unused_attributes fn1.pexp_attributes;
{
pexp_desc =
Pexp_apply {funct = fn1; args = (Nolabel, a) :: args; partial};
pexp_desc = Pexp_apply {funct = fn1; args = (Nolbl, a) :: args; partial};
pexp_loc = e.pexp_loc;
pexp_attributes = e.pexp_attributes @ f.pexp_attributes;
}
Expand All @@ -116,7 +115,7 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) : exp =
Pexp_apply
{
funct = fn;
args = (Nolabel, bounded_obj_arg) :: args;
args = (Nolbl, bounded_obj_arg) :: args;
partial = false;
};
pexp_attributes = [];
Expand Down Expand Up @@ -170,7 +169,7 @@ let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) : exp =
let arg = self.expr self arg in
let fn = Exp.send ~loc obj {txt = name ^ Literals.setter_suffix; loc} in
Exp.constraint_ ~loc
(Exp.apply ~loc fn [(Nolabel, arg)])
(Exp.apply ~loc fn [(Nolbl, arg)])
(Ast_literal.type_unit ~loc ())
in
match obj.pexp_desc with
Expand Down
2 changes: 1 addition & 1 deletion compiler/frontend/ast_exp_extension.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ let handle_extension e (self : Bs_ast_mapper.mapper)
Exp.apply ~loc
(Exp.ident ~loc {txt = Longident.parse "Js.Exn.raiseError"; loc})
[
( Nolabel,
( Nolbl,
Exp.constant ~loc
(Pconst_string
( (pretext
Expand Down
Loading