Skip to content

Commit

Permalink
Fixes coq#10739: anomaly with Extraction within a module.
Browse files Browse the repository at this point in the history
  • Loading branch information
herbelin committed Mar 5, 2023
1 parent 25cc482 commit 1e7d564
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 22 deletions.
10 changes: 10 additions & 0 deletions kernel/safe_typing.ml
Expand Up @@ -1399,6 +1399,16 @@ let close_section senv =
in
List.fold_right fold entries senv

let flatten_env senv =
let label = function MPdot (_,l) -> l | _ -> assert false in
let rec close senv =
match senv.modvariant with
| STRUCT _ -> close (snd (end_module (label senv.modpath) None senv))
| SIG _ -> close (snd (end_modtype (label senv.modpath) senv))
| LIBRARY | NONE -> senv in
let senv = close senv in
(senv.modpath, senv.revstruct)

(** {6 Safe typing } *)

type judgment = {
Expand Down
2 changes: 2 additions & 0 deletions kernel/safe_typing.mli
Expand Up @@ -37,6 +37,8 @@ val sections_of_safe_env : safe_environment -> section_data Section.t option

val structure_body_of_safe_env : safe_environment -> Declarations.structure_body

val flatten_env : safe_environment -> ModPath.t * Declarations.structure_body

(** The safe_environment state monad *)

type safe_transformer0 = safe_environment -> safe_environment
Expand Down
7 changes: 4 additions & 3 deletions plugins/extraction/extract_env.ml
Expand Up @@ -27,11 +27,12 @@ open Common
(***************************************)

let toplevel_env () =
List.rev (Safe_typing.structure_body_of_safe_env (Global.safe_env ()))
let mp, struc = Safe_typing.flatten_env (Global.safe_env ()) in
mp, List.rev struc

let environment_until dir_opt =
let rec parse = function
| [] when Option.is_empty dir_opt -> [Lib.current_mp (), toplevel_env ()]
| [] when Option.is_empty dir_opt -> [toplevel_env ()]
| [] -> []
| d :: l ->
let meb =
Expand Down Expand Up @@ -565,7 +566,7 @@ let reset () =
Visit.reset (); reset_tables (); reset_renaming_tables Everything

let init ?(compute=false) ?(inner=false) modular library =
if not inner then (check_inside_section (); check_inside_module ());
if not inner then check_inside_section ();
set_keywords (descr ()).keywords;
set_modular modular;
set_library library;
Expand Down
21 changes: 3 additions & 18 deletions plugins/extraction/table.ml
Expand Up @@ -87,15 +87,13 @@ let common_prefix_from_list mp0 mpl =
| mp :: l -> if MPset.mem mp prefixes then Some mp else f l
in f mpl

let rec parse_labels2 ll mp1 = function
| mp when ModPath.equal mp1 mp -> mp,ll
| MPdot (mp,l) -> parse_labels2 (l::ll) mp1 mp
let rec parse_labels2 ll = function
| MPdot (mp,l) -> parse_labels2 (l::ll) mp
| mp -> mp,ll

let labels_of_ref r =
let mp_top = Lib.current_mp () in
let mp,l = repr_of_r r in
parse_labels2 [l] mp_top mp
parse_labels2 [l] mp


(*S The main tables: constants, inductives, records, ... *)
Expand Down Expand Up @@ -365,19 +363,6 @@ let error_axiom_scheme ?loc r i =
safe_pr_global r ++ spc () ++ str "needs " ++ int i ++
str " type variable(s).")

let warn_extraction_inside_module =
CWarnings.create ~name:"extraction-inside-module" ~category:"extraction"
(fun () -> strbrk "Extraction inside an opened module is experimental." ++ spc () ++
strbrk "In case of problem, close it first.")


let check_inside_module () =
if Lib.is_modtype () then
err (str "You can't do that within a Module Type." ++ fnl () ++
str "Close it and try again.")
else if Lib.is_module () then
warn_extraction_inside_module ()

let check_inside_section () =
if Global.sections_are_opened () then
err (str "You can't do that within a section." ++ fnl () ++
Expand Down
1 change: 0 additions & 1 deletion plugins/extraction/table.mli
Expand Up @@ -35,7 +35,6 @@ val error_unknown_module : ?loc:Loc.t -> qualid -> 'a
val error_scheme : unit -> 'a
val error_not_visible : GlobRef.t -> 'a
val error_MPfile_as_mod : ModPath.t -> bool -> 'a
val check_inside_module : unit -> unit
val check_inside_section : unit -> unit
val check_loaded_modfile : ModPath.t -> unit
val msg_of_implicit : kill_reason -> string
Expand Down
21 changes: 21 additions & 0 deletions test-suite/bugs/bug_10739.v
@@ -0,0 +1,21 @@
Require Extraction.

Definition f := 0.
Module M.
Recursive Extraction f.
Extraction f.

Definition g := f.
Recursive Extraction g.
Extraction g.

Module Type S. End S.
Module N : S.
Definition h := g.
Recursive Extraction h.
Extraction h.
End N.

Recursive Extraction nat.
Extraction nat.
End M.

0 comments on commit 1e7d564

Please sign in to comment.