Skip to content

Commit 74aa974

Browse files
authored
Some small patch-ups around matching on extensions (#140)
These might fix a few bugs, but they were just infelicities I noticed in the course of other work.
1 parent 07127fe commit 74aa974

File tree

9 files changed

+128
-26
lines changed

9 files changed

+128
-26
lines changed

.depend

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ parsing/extensions.cmx : \
439439
parsing/extensions.cmi : \
440440
parsing/parsetree.cmi \
441441
parsing/location.cmi \
442+
parsing/extensions_parsing.cmi \
442443
parsing/asttypes.cmi
443444
parsing/extensions_parsing.cmo : \
444445
parsing/parsetree.cmi \
@@ -457,8 +458,7 @@ parsing/extensions_parsing.cmx : \
457458
parsing/extensions_parsing.cmi : \
458459
parsing/parsetree.cmi \
459460
parsing/location.cmi \
460-
utils/language_extension.cmi \
461-
parsing/asttypes.cmi
461+
utils/language_extension.cmi
462462
parsing/lexer.cmo : \
463463
utils/warnings.cmi \
464464
parsing/parser.cmi \

parsing/depend.ml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ let add_pattern bv pat =
205205
!pattern_bv
206206

207207
let rec add_expr bv exp =
208+
match Extensions.Expression.of_ast exp with
209+
| Some eexp -> add_expr_extension bv eexp
210+
| None ->
208211
match exp.pexp_desc with
209212
Pexp_ident l -> add bv l
210213
| Pexp_constant _ -> ()
@@ -277,6 +280,47 @@ let rec add_expr bv exp =
277280
| Pexp_extension e -> handle_extension e
278281
| Pexp_unreachable -> ()
279282

283+
and add_expr_extension bv : Extensions.Expression.t -> _ = function
284+
| Eexp_comprehension cexp -> add_comprehension_expr bv cexp
285+
| Eexp_immutable_array iaexp -> add_immutable_array_expr bv iaexp
286+
287+
and add_comprehension_expr bv : Extensions.Comprehensions.expression -> _ =
288+
function
289+
| Cexp_list_comprehension comp -> add_comprehension bv comp
290+
| Cexp_array_comprehension (_, comp) -> add_comprehension bv comp
291+
292+
and add_comprehension bv
293+
({ body; clauses } : Extensions.Comprehensions.comprehension) =
294+
let bv = List.fold_left add_comprehension_clause bv clauses in
295+
add_expr bv body
296+
297+
and add_comprehension_clause bv : Extensions.Comprehensions.clause -> _ =
298+
function
299+
(* fold_left here is a little suspicious, because the different
300+
clauses should be interpreted in parallel. But this treatment
301+
echoes the treatment in [Pexp_let] (in [add_bindings]). *)
302+
| For cbs -> List.fold_left add_comprehension_clause_binding bv cbs
303+
| When expr -> add_expr bv expr; bv
304+
305+
and add_comprehension_clause_binding bv
306+
({ pattern; iterator; attributes = _ } :
307+
Extensions.Comprehensions.clause_binding) =
308+
let bv = add_pattern bv pattern in
309+
add_comprehension_iterator bv iterator;
310+
bv
311+
312+
and add_comprehension_iterator bv : Extensions.Comprehensions.iterator -> _ =
313+
function
314+
| Range { start; stop; direction = _ } ->
315+
add_expr bv start;
316+
add_expr bv stop
317+
| In expr ->
318+
add_expr bv expr
319+
320+
and add_immutable_array_expr bv : Extensions.Immutable_arrays.expression -> _ =
321+
function
322+
| Iaexp_immutable_array exprs -> List.iter (add_expr bv) exprs
323+
280324
and add_cases bv cases =
281325
List.iter (add_case bv) cases
282326

parsing/extensions.ml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ open Extensions_parsing
1010
1111
When we spot a comprehension for an immutable array, we need to make sure
1212
that both [comprehensions] and [immutable_arrays] are enabled. But our
13-
general mechanism for checking for enabled extensions (in
14-
Extensions_parsing.Translate(...).of_ast) won't work well here: it triggers
15-
when converting from e.g. [[%extensions.comprehensions.array] ...] to the
16-
comprehensions-specific AST. But if we spot a
17-
[[%extensions.comprehensions.immutable]], there is no expression to
18-
translate. So we just check for the immutable arrays extension when
19-
processing a comprehension expression for an immutable array.
13+
general mechanism for checking for enabled extensions (in [of_ast]) won't
14+
work well here: it triggers when converting from
15+
e.g. [[%extensions.comprehensions.array] ...] to the comprehensions-specific
16+
AST. But if we spot a [[%extensions.comprehensions.immutable]], there is no
17+
expression to translate. So we just check for the immutable arrays extension
18+
when processing a comprehension expression for an immutable array.
2019
2120
Note [Wrapping with make_extension]
2221
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -205,9 +204,9 @@ module Comprehensions = struct
205204

206205
let expand_comprehension_extension_expr expr =
207206
match Expression.match_extension expr with
208-
| Some (comprehensions :: name, expr)
207+
| Some (comprehensions :: names, expr)
209208
when String.equal comprehensions extension_string ->
210-
name, expr
209+
names, expr
211210
| Some (name, _) ->
212211
Desugaring_error.raise expr (Non_comprehension_extension_point name)
213212
| None ->

parsing/extensions.mli

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
For details on the rationale behind this approach (and for some of the gory
1818
details), see [Extensions_parsing]. *)
1919

20+
(*********************************************)
21+
(* Individual extensions *)
22+
2023
(** The ASTs for list and array comprehensions *)
2124
module Comprehensions : sig
2225
type iterator =
@@ -94,9 +97,14 @@ module type AST = sig
9497
if it's not a language extension term, return [None]; if it's a disabled
9598
language extension term, raise an error.
9699
97-
AN IMPORTANT NOTE: We indent calls to this function *very* strangely: we
98-
*do not change the indentation level* when we match on its result!
99-
E.g. from [type_expect_] in [typecore.ml]:
100+
AN IMPORTANT NOTE: The design of this function is careful to make merge
101+
conflicts with upstream less likely: we want no edits at all -- not even
102+
indentation -- to surrounding code. This is why we return a [t option],
103+
not some structure that could include the [ast_desc] if there is no
104+
extension.
105+
106+
Indentation: we *do not change the indentation level* when we match on
107+
this function's result! E.g. from [type_expect_] in [typecore.ml]:
100108
101109
{[
102110
match Extensions.Expression.of_ast sexp with
@@ -123,9 +131,7 @@ module type AST = sig
123131
Note that we match on the result of this function, forward to
124132
[type_expect_extension] if we get something, and otherwise do the real
125133
match on [sexp.pexp_desc] *without going up an indentation level*. This
126-
is important to reduce the number of merge conflicts with upstream by
127-
avoiding changing the body of every single important function in the type
128-
checker to add pointless indentation. *)
134+
is important to reduce the number of merge conflicts. *)
129135
val of_ast : ast -> t option
130136
end
131137

parsing/parsetree.mli

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ and object_field_desc =
211211
and pattern =
212212
{
213213
ppat_desc: pattern_desc;
214+
(** (Jane Street specific; delete when upstreaming.)
215+
Consider using [Extensions.Pattern.of_ast] before matching on
216+
this field directly, as the former will detect extension nodes
217+
correctly. Extensions are encoded as
218+
[Ppat_tuple [Ppat_extension _; _]]; if your pattern match avoids
219+
matching that pattern, it is OK to skip [of_ast]. *)
220+
214221
ppat_loc: Location.t;
215222
ppat_loc_stack: location_stack;
216223
ppat_attributes: attributes; (** [... [\@id1] [\@id2]] *)
@@ -277,6 +284,13 @@ and pattern_desc =
277284
and expression =
278285
{
279286
pexp_desc: expression_desc;
287+
(** (Jane Street specific; delete when upstreaming.)
288+
Consider using [Extensions.Expression.of_ast] before matching on
289+
this field directly, as the former will detect extension nodes
290+
correctly. Extensions are encoded as
291+
[Pexp_apply(Pexp_extension _, _)]; if your pattern match avoids
292+
matching that pattern, it is OK to skip [of_ast]. *)
293+
280294
pexp_loc: Location.t;
281295
pexp_loc_stack: location_stack;
282296
pexp_attributes: attributes; (** [... [\@id1] [\@id2]] *)

parsing/pprintast.ml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -461,16 +461,18 @@ and pattern_or ctxt f x =
461461
pp f "@[<hov0>%a@]" (list ~sep:"@ | " (pattern1 ctxt)) orpats
462462

463463
and pattern1 ctxt (f:Format.formatter) (x:pattern) : unit =
464-
let rec pattern_list_helper f = function
464+
let rec pattern_list_helper f p = match p with
465465
| {ppat_desc =
466466
Ppat_construct
467467
({ txt = Lident("::") ;_},
468-
Some ([], {ppat_desc = Ppat_tuple([pat1; pat2]);_}));
469-
ppat_attributes = []}
470-
471-
->
468+
Some ([], inner_pat));
469+
ppat_attributes = []} ->
470+
begin match Extensions.Pattern.of_ast inner_pat, inner_pat.ppat_desc with
471+
| None, Ppat_tuple([pat1; pat2]) ->
472472
pp f "%a::%a" (simple_pattern ctxt) pat1 pattern_list_helper pat2 (*RA*)
473-
| p -> pattern1 ctxt f p
473+
| _ -> pattern1 ctxt f p
474+
end
475+
| _ -> pattern1 ctxt f p
474476
in
475477
if x.ppat_attributes <> [] then pattern ctxt f x
476478
else match x.ppat_desc with

tools/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ INCLUDES = $(addprefix -I $(ROOTDIR)/,utils parsing typing bytecomp \
3939
middle_end middle_end/closure middle_end/flambda \
4040
middle_end/flambda/base_types driver toplevel \
4141
file_formats lambda)
42-
COMPFLAGS = -absname -w +a-4-9-41-42-44-45-48-70 -strict-sequence \
42+
COMPFLAGS = -absname -w +a-4-9-40-41-42-44-45-48-70 -strict-sequence \
4343
-warn-error +A -principal -safe-string -strict-formats -bin-annot $(INCLUDES)
4444
LINKFLAGS = $(INCLUDES)
4545
VPATH := $(filter-out -I,$(INCLUDES))

tools/ocamlprof.ml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ and rewrite_exp iflag sexp =
175175
else rw_exp false sexp
176176

177177
and rw_exp iflag sexp =
178+
match Extensions.Expression.of_ast sexp with
179+
| Some eexp -> rewrite_exp_extension iflag eexp
180+
| None ->
178181
match sexp.pexp_desc with
179182
Pexp_ident _lid -> ()
180183
| Pexp_constant _cst -> ()
@@ -306,6 +309,40 @@ and rw_exp iflag sexp =
306309
| Pexp_extension _ -> ()
307310
| Pexp_unreachable -> ()
308311

312+
and rewrite_exp_extension iflag : Extensions.Expression.t -> _ = function
313+
| Eexp_comprehension cexp -> rewrite_comprehension_exp iflag cexp
314+
| Eexp_immutable_array iaexp -> rewrite_immutable_array_exp iflag iaexp
315+
316+
and rewrite_comprehension_exp iflag :
317+
Extensions.Comprehensions.expression -> _ = function
318+
| Cexp_list_comprehension comp -> rewrite_comprehension iflag comp
319+
| Cexp_array_comprehension (_, comp) -> rewrite_comprehension iflag comp
320+
321+
and rewrite_comprehension iflag
322+
({ body; clauses } : Extensions.Comprehensions.comprehension) =
323+
List.iter (rewrite_comprehension_clause iflag) clauses;
324+
rewrite_exp iflag body
325+
326+
and rewrite_comprehension_clause iflag : Extensions.Comprehensions.clause -> _ =
327+
function
328+
| For cbs -> List.iter (rewrite_clause_binding iflag) cbs
329+
| When expr -> rewrite_exp iflag expr
330+
331+
and rewrite_clause_binding iflag
332+
({ pattern = _; iterator; attributes = _ } :
333+
Extensions.Comprehensions.clause_binding) =
334+
match iterator with
335+
| Range { start; stop; direction = _ } ->
336+
rewrite_exp iflag start;
337+
rewrite_exp iflag stop
338+
| In expr -> rewrite_exp iflag expr
339+
340+
and rewrite_immutable_array_exp iflag :
341+
Extensions.Immutable_arrays.expression -> _ =
342+
function
343+
| Iaexp_immutable_array exprs ->
344+
rewrite_exp_list iflag exprs
345+
309346
and rewrite_ifbody iflag ghost sifbody =
310347
if !instr_if && not ghost then
311348
insert_profile rw_exp sifbody

typing/typecore.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4062,8 +4062,8 @@ and type_expect_
40624062
rt, funct
40634063
in
40644064
let type_sfunct_args sfunct extra_args =
4065-
match sfunct.pexp_desc with
4066-
| Pexp_apply (sfunct, args) ->
4065+
match Extensions.Expression.of_ast sfunct, sfunct.pexp_desc with
4066+
| None, Pexp_apply (sfunct, args) ->
40674067
type_sfunct sfunct, args @ extra_args
40684068
| _ ->
40694069
type_sfunct sfunct, extra_args

0 commit comments

Comments
 (0)