Skip to content

Commit 3b084a5

Browse files
committed
1 parent 4043e70 commit 3b084a5

File tree

8 files changed

+40
-23
lines changed

8 files changed

+40
-23
lines changed

lib/flags.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ let in_ml_toplevel = ref false
5353

5454
let raw_print = ref false
5555

56-
let in_synterp_phase = ref false
56+
let in_synterp_phase = ref None
5757

5858
(* Translate *)
5959
let beautify = ref false

lib/flags.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ val in_debugger : bool ref
4545
val in_ml_toplevel : bool ref
4646

4747
(* Used to check stages are used correctly. *)
48-
val in_synterp_phase : bool ref
48+
val in_synterp_phase : bool option ref
4949

5050
(* Set Printing All flag. For some reason it is a global flag *)
5151
val raw_print : bool ref

library/global.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let is_curmod_library () =
3535
Safe_typing.is_curmod_library !global_env
3636

3737
let assert_not_synterp () =
38-
if !Flags.in_synterp_phase then
38+
if !Flags.in_synterp_phase = Some true then
3939
CErrors.anomaly (
4040
Pp.strbrk"The global environment cannot be accessed during the syntactic interpretation phase.")
4141

parsing/procq.ml

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ let empty_full_state =
5555
custom_entries = EntryDataMap.empty;
5656
}
5757

58+
let assert_synterp () =
59+
if !Flags.in_synterp_phase = Some false then
60+
CErrors.anomaly Pp.(str "The grammar cannot be modified during the interp phase.")
61+
5862
(** Not marshallable! *)
5963
let state = ref empty_full_state
6064

@@ -78,7 +82,9 @@ let modify_state_unsync f state =
7882
let current_state = if is_base then base_state else f state.current_state in
7983
{ state with base_state; current_state }
8084

81-
let modify_state_unsync f () = state := modify_state_unsync f !state
85+
let modify_state_unsync f () =
86+
assert_synterp ();
87+
state := modify_state_unsync f !state
8288

8389
let modify_keyword_state f =
8490
modify_state_unsync (fun {estate;kwstate} -> {estate; kwstate = f kwstate})
@@ -95,6 +101,7 @@ let make_entry_unsync make remake state =
95101
{ state with base_state; current_state }, e
96102

97103
let make_entry_unsync make remake () =
104+
assert_synterp();
98105
let statev, e = make_entry_unsync make remake !state in
99106
state := statev;
100107
e
@@ -134,7 +141,9 @@ let grammar_extend_sync user_state entry rules state =
134141
current_sync_extensions = GramExt entry :: state.current_sync_extensions;
135142
}
136143

137-
let grammar_extend_sync st e r () = state := grammar_extend_sync st e r !state
144+
let grammar_extend_sync st e r () =
145+
assert_synterp();
146+
state := grammar_extend_sync st e r !state
138147

139148
let extend_entry_sync (type a) (tag : a EntryCommand.tag) (name : string) state : _ * a Entry.t =
140149
let current_estate, e = Entry.make name state.current_state.estate in
@@ -158,6 +167,7 @@ let extend_entry_sync (type a) (tag : a EntryCommand.tag) (name : string) state
158167
state, e
159168

160169
let extend_entry_command tag name =
170+
assert_synterp();
161171
let statev, e = extend_entry_sync tag name !state in
162172
state := statev;
163173
e
@@ -467,19 +477,21 @@ let replay_sync_extension = function
467477
| GramExt (Dyn (tag,g)) -> extend_grammar_command tag g
468478
| EntryExt (tag,name) -> ignore (extend_entry_command tag name : _ Entry.t)
469479

470-
let unfreeze = function
471-
| {frozen_sync;} as frozen ->
472-
let to_remove, to_add, _common = factorize_grams (!state).current_sync_extensions frozen_sync in
473-
if CList.is_empty to_remove then begin
474-
List.iter replay_sync_extension (List.rev to_add);
475-
unfreeze_only_keywords frozen
476-
end
477-
else begin
478-
state := reset_to_base !state;
479-
List.iter replay_sync_extension (List.rev frozen_sync);
480-
(* put back the keyword state, needed to support ssr hacks *)
481-
unfreeze_only_keywords frozen
482-
end
480+
let unfreeze ({frozen_sync;} as frozen) =
481+
(* allow unfreezing synterp state even during interp phase *)
482+
Flags.with_modified_ref Flags.in_synterp_phase (fun _ -> None) (fun () ->
483+
let to_remove, to_add, _common = factorize_grams (!state).current_sync_extensions frozen_sync in
484+
if CList.is_empty to_remove then begin
485+
List.iter replay_sync_extension (List.rev to_add);
486+
unfreeze_only_keywords frozen
487+
end
488+
else begin
489+
state := reset_to_base !state;
490+
List.iter replay_sync_extension (List.rev frozen_sync);
491+
(* put back the keyword state, needed to support ssr hacks *)
492+
unfreeze_only_keywords frozen
493+
end)
494+
()
483495

484496
let freeze_state state = {
485497
frozen_sync = state.current_sync_extensions;

stm/stm.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ end = struct (* {{{ *)
402402
403403
In case you are hitting the race enable stm_debug.
404404
*)
405-
if !stm_debug then Flags.in_synterp_phase := false;
405+
Flags.in_synterp_phase := Some false;
406406

407407
let fname =
408408
"stm_" ^ Str.global_replace (Str.regexp " ") "_" (Spawned.process_id ()) in

vernac/synterp.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,4 +501,4 @@ and synterp_control ~intern CAst.{ loc; v = cmd } =
501501
CAst.make ?loc { expr; control; attrs = cmd.attrs }
502502

503503
let synterp_control ~intern cmd =
504-
Flags.with_option Flags.in_synterp_phase (synterp_control ~intern) cmd
504+
Flags.with_modified_ref Flags.in_synterp_phase (fun _ -> Some true) (synterp_control ~intern) cmd

vernac/vernacentries.ml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,8 +1698,13 @@ let vernac_require_interp needed modrefl export qidl =
16981698
export
16991699

17001700
let vernac_require ~intern from export qidl =
1701-
let needed, modrefl = Synterp.synterp_require ~intern from export qidl in
1702-
vernac_require_interp needed modrefl export qidl
1701+
let needed, modrefl = Flags.with_modified_ref Flags.in_synterp_phase (fun _ -> Some true) (fun () ->
1702+
Synterp.synterp_require ~intern from export qidl)
1703+
()
1704+
in
1705+
Flags.with_modified_ref Flags.in_synterp_phase (fun _ -> Some false) (fun () ->
1706+
vernac_require_interp needed modrefl export qidl)
1707+
()
17031708

17041709
(* Coercions and canonical structures *)
17051710

vernac/vernacinterp.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ let interp_control_gen ~loc ~st ~unfreeze_transient control f =
3737
~with_local_state:(with_interp_state ~unfreeze_transient st)
3838
control
3939
~noop
40-
f
40+
(Flags.with_modified_ref Flags.in_synterp_phase (fun _ -> Some false) f)
4141
in
4242
if VernacControl.after_last_phase ~loc control
4343
then noop

0 commit comments

Comments
 (0)