Skip to content

Commit 516e650

Browse files
authored
Remove the unreachable Longident.Lapply constructor (rescript-lang#8469)
* Remove the unreachable Longident.Lapply constructor Longident.Lapply is the parsetree form of OCaml's applicative-functor path syntax (F(X).t), where a functor is applied inline inside an identifier path. ReScript's grammar has no such production: functor application is only a module expression (module M = F(X)), which the type checker resolves to Path.Papply, never a Longident.Lapply. The parser never builds Lapply, so every match on it was dead (assert false / fatal_error / raise Not_found / defensive printer fallbacks). Remove the constructor and all its handling across the type checker, printers, depend, and analysis. The sole construction site was Ctype.lid_of_path (Path.Papply -> Longident.Lapply); since no Papply flows into that type-path conversion, replace it with assert false, matching the existing Papply -> assert false invariants. Removing the Env.lookup_module Lapply arms also left functor_components.fcomp_arg write-only, so drop that field too. Add a super_errors fixture exercising functor application plus a type-path error, confirming the functor machinery and error printing are unchanged. * Update changelog * Format functor fixture and refresh super_errors snapshot * lid_of_path: render Papply paths gracefully instead of asserting Applicative functors are on by default, so Path.Papply paths are produced internally (typemod path_of_module, Mtype.strengthen) even though ReScript source cannot reference such a path directly. Replace the assert false in Ctype.lid_of_path's Papply arm (introduced when removing Longident.Lapply) with a graceful Lident rendering via Path.name, so any diagnostic that ever reaches it degrades to a readable name (e.g. F(Arg)) instead of aborting the compiler. Add an ounit test that calls Ctype.lid_of_path on a synthetic Papply path (unreachable from source, so exercised directly) to lock in the graceful behavior.
1 parent b8a6089 commit 516e650

23 files changed

Lines changed: 79 additions & 113 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
- Analysis refactor: remove global state `Shared_types.state`. https://github.com/rescript-lang/rescript/pull/8465
6868
- Refactor analysis CLI helpers to use source input. https://github.com/rescript-lang/rescript/pull/8466
6969
- Include syntax, gentype, analysis, tools, and reanalyze tests in coverage reports. https://github.com/rescript-lang/rescript/pull/8467
70+
- Remove the unreachable `Longident.Lapply` constructor (OCaml's applicative-functor path syntax `F(X).t`, which ReScript's grammar cannot produce). https://github.com/rescript-lang/rescript/pull/8469
7071

7172
# 13.0.0-alpha.4
7273

‎analysis/src/completion_front_end.ml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,6 @@ let completion_with_parser1 ~debug ~offset ~pos_cursor ~kind_file
13221322
}
13231323
in
13241324
set_result (Cpath context_path)
1325-
| Lapply _ -> ()
13261325
else if Loc.end_ e.pexp_loc = pos_before_cursor then
13271326
match expr_to_context_path ~in_jsx_context:!in_jsx_context e with
13281327
| Some context_path ->

‎analysis/src/process_extra.ml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,9 @@ let handle_constructor txt =
8181
match txt with
8282
| Longident.Lident name -> name
8383
| Ldot (_left, name) -> name
84-
| Lapply (_, _) -> assert false
8584

8685
let rec lid_is_complex (lid : Longident.t) =
8786
match lid with
88-
| Lapply _ -> true
8987
| Ldot (lid, _) -> lid_is_complex lid
9088
| _ -> false
9189

‎analysis/src/semantic_tokens.ml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ let emit_longident ?(backwards = false) ?(jsx = false)
127127
match lid with
128128
| Longident.Lident txt -> txt :: acc
129129
| Ldot (lid, txt) -> flatten (txt :: acc) lid
130-
| _ -> acc
131130
in
132131
let rec loop pos segments =
133132
match segments with

‎analysis/src/utils.ml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ let flatten_long_ident ?(jsx = false) ?(cut_at_offset = None) lid =
7777
else if jsx && txt = "createElement" then (path, offset)
7878
else if txt = "_" then (extend_path "" path, offset + 1)
7979
else (extend_path txt path, offset + 1 + String.length txt)
80-
| Lapply _ -> ([], 0)
8180
in
8281
let path, _ = loop lid in
8382
List.rev path
@@ -169,7 +168,6 @@ let rec get_unqualified_name txt =
169168
match txt with
170169
| Longident.Lident field_name -> field_name
171170
| Ldot (t, _) -> get_unqualified_name t
172-
| _ -> ""
173171

174172
let indent n text =
175173
let spaces = String.make n ' ' in

‎compiler/frontend/ast_util.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ let record_as_js_object loc (self : Bs_ast_mapper.mapper)
3636
( {Asttypes.loc; txt = x} :: labels,
3737
(x, self.expr self e) :: args,
3838
i + 1 )
39-
| Ldot _ | Lapply _ -> Location.raise_errorf ~loc "invalid js label ")
39+
| Ldot _ -> Location.raise_errorf ~loc "invalid js label ")
4040
in
4141
Ast_external_mk.local_external_obj loc
4242
~pval_prim:(Ast_external_process.pval_prim_of_labels labels)

‎compiler/ml/ctype.ml‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,7 +2113,6 @@ let rec concat_longident lid1 =
21132113
function
21142114
| Lident s -> Ldot (lid1, s)
21152115
| Ldot (lid2, s) -> Ldot (concat_longident lid1 lid2, s)
2116-
| Lapply (lid2, lid) -> Lapply (concat_longident lid1 lid2, lid)
21172116

21182117
let nondep_instance env level id ty =
21192118
let ty = !nondep_type' env id ty in
@@ -3353,8 +3352,12 @@ let memq_warn t visited =
33533352
let rec lid_of_path ?(hash = "") = function
33543353
| Path.Pident id -> Longident.Lident (hash ^ Ident.name id)
33553354
| Path.Pdot (p1, s, _) -> Longident.Ldot (lid_of_path p1, hash ^ s)
3356-
| Path.Papply (p1, p2) ->
3357-
Longident.Lapply (lid_of_path ~hash p1, lid_of_path p2)
3355+
(* Applicative-functor application paths (Path.Papply) are produced
3356+
internally (applicative functors are on by default) even though ReScript's
3357+
surface syntax cannot reference such a path directly. Render the
3358+
application by name (e.g. "F(Arg)") rather than aborting, so any diagnostic
3359+
that reaches here degrades gracefully instead of crashing the compiler. *)
3360+
| Path.Papply _ as p -> Longident.Lident (hash ^ Path.name p)
33583361

33593362
let find_cltype_for_path env p =
33603363
let cl_path = Env.lookup_type (lid_of_path ~hash:"#" p) env in

‎compiler/ml/depend.ml‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ let rec lookup_map lid m =
5555
match lid with
5656
| Lident s -> String_map.find s m
5757
| Ldot (l, s) -> String_map.find s (get_map (lookup_map l m))
58-
| Lapply _ -> raise Not_found
5958

6059
(* Collect free module identifiers in the a.s.t. *)
6160

@@ -73,9 +72,6 @@ let rec add_path bv ?(p = []) = function
7372
prerr_endline "";*)
7473
add_names free
7574
| Ldot (l, s) -> add_path bv ~p:(s :: p) l
76-
| Lapply (l1, l2) ->
77-
add_path bv l1;
78-
add_path bv l2
7975

8076
let open_module bv lid =
8177
match lookup_map lid bv with

‎compiler/ml/env.ml‎

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,6 @@ and structure_components = {
432432

433433
and functor_components = {
434434
fcomp_param: Ident.t; (* Formal parameter *)
435-
fcomp_arg: module_type option; (* Argument signature *)
436435
fcomp_res: module_type; (* Result signature *)
437436
fcomp_cache: (Path.t, module_components) Hashtbl.t; (* For memoization *)
438437
fcomp_subst_cache: (Path.t, module_type) Hashtbl.t;
@@ -1013,20 +1012,6 @@ let rec lookup_module_descr_aux ?loc lid env =
10131012
let descr, pos = Tbl.find_str s c.comp_components in
10141013
(Pdot (p, s, pos), descr)
10151014
| Functor_comps _ -> raise Not_found)
1016-
| Lapply (l1, l2) -> (
1017-
let p1, desc1 = lookup_module_descr ?loc l1 env in
1018-
let p2 = lookup_module ~load:true ?loc l2 env in
1019-
let {md_type = mty2} = find_module p2 env in
1020-
match get_components desc1 with
1021-
| Functor_comps f ->
1022-
let loc =
1023-
match loc with
1024-
| Some l -> l
1025-
| None -> Location.none
1026-
in
1027-
Misc.may (!check_modtype_inclusion ~loc env mty2 p2) f.fcomp_arg;
1028-
(Papply (p1, p2), !components_of_functor_appl' f env p1 p2)
1029-
| Structure_comps _ -> raise Not_found)
10301015

10311016
and lookup_module_descr ?loc lid env =
10321017
let ((p, comps) as res) = lookup_module_descr_aux ?loc lid env in
@@ -1073,21 +1058,6 @@ and lookup_module ~load ?loc lid env : Path.t =
10731058
report_deprecated ?loc p comps.deprecated;
10741059
p
10751060
| Functor_comps _ -> raise Not_found)
1076-
| Lapply (l1, l2) -> (
1077-
let p1, desc1 = lookup_module_descr ?loc l1 env in
1078-
let p2 = lookup_module ~load:true ?loc l2 env in
1079-
let {md_type = mty2} = find_module p2 env in
1080-
let p = Papply (p1, p2) in
1081-
match get_components desc1 with
1082-
| Functor_comps f ->
1083-
let loc =
1084-
match loc with
1085-
| Some l -> l
1086-
| None -> Location.none
1087-
in
1088-
Misc.may (!check_modtype_inclusion ~loc env mty2 p2) f.fcomp_arg;
1089-
p
1090-
| Structure_comps _ -> raise Not_found)
10911061

10921062
let lookup proj1 proj2 ?loc lid env =
10931063
match lid with
@@ -1099,7 +1069,6 @@ let lookup proj1 proj2 ?loc lid env =
10991069
let data, pos = Tbl.find_str s (proj2 c) in
11001070
(Pdot (p, s, pos), data)
11011071
| Functor_comps _ -> raise Not_found)
1102-
| Lapply _ -> raise Not_found
11031072

11041073
let lookup_all_simple proj1 proj2 shadow ?loc lid env =
11051074
match lid with
@@ -1119,7 +1088,6 @@ let lookup_all_simple proj1 proj2 shadow ?loc lid env =
11191088
let comps = try Tbl.find_str s (proj2 c) with Not_found -> [] in
11201089
List.map (fun data -> (data, fun () -> ())) comps
11211090
| Functor_comps _ -> raise Not_found)
1122-
| Lapply _ -> raise Not_found
11231091

11241092
let has_local_constraints env = not (Path_map.is_empty env.local_constraints)
11251093

@@ -1588,14 +1556,13 @@ and components_of_module_maker (env, sub, path, mty) =
15881556
| Sig_class_type () -> assert false)
15891557
sg pl;
15901558
Some (Structure_comps c)
1591-
| Mty_functor (param, ty_arg, ty_res) ->
1559+
| Mty_functor (param, _ty_arg, ty_res) ->
15921560
Some
15931561
(Functor_comps
15941562
{
15951563
fcomp_param = param;
1596-
(* fcomp_arg and fcomp_res must be prefixed eagerly, because
1597-
they are interpreted in the outer environment *)
1598-
fcomp_arg = may_map (Subst.modtype sub) ty_arg;
1564+
(* fcomp_res must be prefixed eagerly, because it is interpreted
1565+
in the outer environment *)
15991566
fcomp_res = Subst.modtype sub ty_res;
16001567
fcomp_cache = Hashtbl.create 17;
16011568
fcomp_subst_cache = Hashtbl.create 17;

‎compiler/ml/longident.ml‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
(* *)
1414
(**************************************************************************)
1515

16-
type t = Lident of string | Ldot of t * string | Lapply of t * t
16+
type t = Lident of string | Ldot of t * string
1717
let rec cmp : t -> t -> int =
1818
fun a b ->
1919
if a == b then 0
@@ -26,24 +26,16 @@ let rec cmp : t -> t -> int =
2626
match cmp a c with
2727
| 0 -> compare b d
2828
| n -> n)
29-
| Ldot _, _ -> -1
30-
| _, Ldot _ -> 1
31-
| Lapply (a, b), Lapply (c, d) -> (
32-
match cmp a c with
33-
| 0 -> cmp b d
34-
| n -> n)
3529

3630
let rec flat accu = function
3731
| Lident s -> s :: accu
3832
| Ldot (lid, s) -> flat (s :: accu) lid
39-
| Lapply (_, _) -> Misc.fatal_error "Longident.flat"
4033

4134
let flatten lid = flat [] lid
4235

4336
let last = function
4437
| Lident s -> s
4538
| Ldot (_, s) -> s
46-
| Lapply (_, _) -> Misc.fatal_error "Longident.last"
4739

4840
let rec split_at_dots s pos =
4941
try

0 commit comments

Comments
 (0)