diff --git a/src/ecEnv.ml b/src/ecEnv.ml index af52385a0..17c5e6f1b 100644 --- a/src/ecEnv.ml +++ b/src/ecEnv.ml @@ -389,9 +389,21 @@ let empty gstate = env_crbds = empty_crbindings; env_thenvs = Mp.empty; } +(* -------------------------------------------------------------------- *) +(* The normalisation cache is a [ref] in a persistent [env]. Its + entries are about module and procedure paths, and are only valid + for the bindings of the environment that computed them. Every + operation that binds a component (a theory or a module, which may + bring new such paths) or enters a new scope gives the new + environment its own copy of the cache: what is cached there must + not reach the environment it was derived from, which may be used + again (failed command, undo, reset, end of section). *) +let fork_norm (env : env) = + { env with env_norm = ref !(env.env_norm) } + (* -------------------------------------------------------------------- *) let copy (env : env) = - { env with env_gstate = EcGState.copy env.env_gstate } + { (fork_norm env) with env_gstate = EcGState.copy env.env_gstate } (* -------------------------------------------------------------------- *) type lookup_error = [ @@ -1283,7 +1295,7 @@ module MC = struct raise (DuplicatedBinding (EcPath.basename path)); bind_submcs - { env with env_comps = Mip.add (IPPath path) mc env.env_comps } + { (fork_norm env) with env_comps = Mip.add (IPPath path) mc env.env_comps } path submcs and bind_submcs env path submcs = @@ -1292,7 +1304,7 @@ module MC = struct and bind_mc x mc env = let path = EcPath.pqname (root env) x in - { env with + { (fork_norm env) with env_current = _up_mc true env.env_current (IPPath path); env_comps = Mip.change @@ -1353,6 +1365,11 @@ let enter mode (name : symbol) (env : env) = if Mip.find_opt (IPPath path) env.env_comps <> None then raise (DuplicatedBinding name); + (* The new scope gets its own normalisation cache: what is cached in + it, e.g. while typing the body of a module, may be about paths + that are not bound in [env], and must not reach [env]. *) + let env = fork_norm env in + match mode, env.env_scope.ec_scope with | `Theory, `Theory -> let env = MC.bind_mc name (empty_mc None) env in @@ -3663,7 +3680,7 @@ module Theory = struct let merged = List.fold_left mc_merge (empty_mc None) (List.map mc_of paths) in let apath = IPPath (EcPath.pqname (root env) name) in - { env with + { (fork_norm env) with env_current = MC._up_mc true env.env_current apath; env_comps = Mip.change @@ -3797,6 +3814,7 @@ module Theory = struct (env : env) = let { cth_items = items; cth_mode = mode; } = cth.ctheory in + let env = fork_norm env in let env = MC.bind_theory cth.name cth.ctheory env in let env = { env with @@ -4022,7 +4040,7 @@ module Theory = struct let comps = env.env_comps in let comps = Mip.add (IPPath rootnm) topmc comps in - let env = { env with env_current = current; env_comps = comps; } in + let env = { (fork_norm env) with env_current = current; env_comps = comps; } in match cth.cth_mode with | `Abstract -> diff --git a/tests/norm-cache-reset.ec b/tests/norm-cache-reset.ec new file mode 100644 index 000000000..a3838e313 --- /dev/null +++ b/tests/norm-cache-reset.ec @@ -0,0 +1,25 @@ +(* Regression for #1246: the environment that `pragma reset` (or an + undo) goes back to must not have learnt anything from the commands + that came after it. Here, closing the theory `T` used to share the + normalisation cache with the root environment. *) +require import AllCore. + +theory T. + module N1 = { proc f() : bool = { return true; } }. + module M = { proc q = N1.f }. +end T. + +lemma foo : hoare [T.M.q : true ==> res]. +proof. by proc. qed. + +pragma reset. + +require import AllCore. + +theory T. + module N1 = { proc f() : bool = { return true; } }. + module M = { proc q() : bool = { return false; } }. +end T. + +lemma M_q : hoare [T.M.q : true ==> !res]. +proof. by proc. qed. diff --git a/tests/norm-cache-scope.ec b/tests/norm-cache-scope.ec new file mode 100644 index 000000000..4f35ee874 --- /dev/null +++ b/tests/norm-cache-scope.ec @@ -0,0 +1,52 @@ +(* Regression for #1246: what the normalisation cache learns while + typing a module must not survive in an environment where that module + is not bound, e.g. after a failed command or at the end of a section. + Otherwise, a module defined later at the same path is normalised as + the old one. *) +require import AllCore. + +module N1 = { proc f() : bool = { return true; } }. + +module type V0 = { proc zz() : bool }. + +(* A definition that fails after its body has been typed. *) +fail module (M1 : V0) = { + proc q = N1.f + proc p() : bool = { var r; r <@ q(); return r; } +}. + +module M1 = { + proc q() : bool = { return false; } +}. + +lemma M1_q : hoare [M1.q : true ==> !res] by proc. + +(* A local module of a section. *) +section. +local module M2 = { + proc q = N1.f + proc p() : bool = { var r; r <@ q(); return r; } +}. +end section. + +module M2 = { + proc q() : bool = { return false; } +}. + +lemma M2_q : hoare [M2.q : true ==> !res] by proc. + +(* The same for the variables of a module. *) +section. +local module M3 = { + proc q() : unit = { } + proc p() : unit = { q(); } +}. +end section. + +module N3 = { var x : int }. +module M3 = N3. + +module W = { proc w() = { M3.x <- 1; N3.x <- 2; } }. + +lemma M3_x : hoare [W.w : true ==> M3.x = 2]. +proof. by proc; auto. qed.