@@ -83,26 +83,29 @@ where
8383 let (args, _, _) ← forallMetaBoundedTelescope (← inferType m) 1
8484 return mkAppN m args
8585
86- private def addAndCompileExprForEval (declName : Name) (value : Expr) (allowSorry := false ) : TermElabM Unit := do
86+ private def addAndCompileExprForEval (declName : Name) (value : Expr) (allowSorry := false ) : TermElabM Name := do
8787 -- Use the `elabMutualDef` machinery to be able to support `let rec`.
8888 -- Hack: since we are using the `TermElabM` version, we can insert the `value` as a metavariable via `exprToSyntax`.
8989 -- An alternative design would be to make `elabTermForEval` into a term elaborator and elaborate the command all at once
9090 -- with `unsafe def _eval := term_for_eval% $t`, which we did try, but unwanted error messages
9191 -- such as "failed to infer definition type" can surface.
92- let defView := mkDefViewOfDef { isUnsafe := true , visibility := .public }
92+ let defView := mkDefViewOfDef { isUnsafe := true , visibility := .private }
9393 (← `(Parser.Command.definition|
9494 def $(mkIdent <| `_root_ ++ declName) := $(← Term.exprToSyntax value)))
95+ let declName := mkPrivateName (← getEnv) declName
9596 -- Allow access to both `meta` and non-`meta` declarations as the compilation result does not
9697 -- escape the current module.
9798 withOptions (Compiler.compiler.checkMeta.set · false ) do
9899 Term.elabMutualDef #[] { header := "" } #[defView]
100+ assert! (← getEnv).contains declName
99101 unless allowSorry do
100102 let axioms ← collectAxioms declName
101103 if axioms.contains ``sorryAx then
102104 throwError "\
103105 aborting evaluation since the expression depends on the 'sorry' axiom, \
104106 which can lead to runtime instability and crashes.\n\n \
105107 To attempt to evaluate anyway despite the risks, use the '#eval!' command."
108+ return declName
106109
107110/--
108111Try to make a `@projFn ty inst e` application, even if it takes unfolding the type `ty` of `e` to synthesize the instance `inst`.
@@ -181,13 +184,13 @@ unsafe def elabEvalCoreUnsafe (bang : Bool) (tk term : Syntax) (expectedType? :
181184 | return none
182185 let eType := e.appFn!.appArg!
183186 if ← isDefEq eType (mkConst ``Unit) then
184- addAndCompileExprForEval declName e (allowSorry := bang)
187+ let declName ← addAndCompileExprForEval declName e (allowSorry := bang)
185188 let mf : m Unit ← evalConst (m Unit) declName (checkMeta := !Elab.inServer.get (← getOptions))
186189 return some { eval := do MonadEvalT.monadEval mf; pure "" , printVal := none }
187190 else
188191 let rf ← withLocalDeclD `x eType fun x => do mkLambdaFVars #[x] (← mkT x)
189192 let r ← mkAppM ``Functor.map #[rf, e]
190- addAndCompileExprForEval declName r (allowSorry := bang)
193+ let declName ← addAndCompileExprForEval declName r (allowSorry := bang)
191194 let mf : m t ← evalConst (m t) declName (checkMeta := !Elab.inServer.get (← getOptions))
192195 return some { eval := toMessageData <$> MonadEvalT.monadEval mf, printVal := some eType }
193196 if let some act ← mkMAct? ``CommandElabM CommandElabM e
@@ -212,15 +215,15 @@ unsafe def elabEvalCoreUnsafe (bang : Bool) (tk term : Syntax) (expectedType? :
212215 throwError m!"unable to synthesize `{.ofConstName ``MonadEval}` instance \
213216 to adapt{indentExpr (← inferType e)}\n \
214217 to `{.ofConstName ``IO}` or `{.ofConstName ``CommandElabM}`."
215- addAndCompileExprForEval declName r (allowSorry := bang)
218+ let declName ← addAndCompileExprForEval declName r (allowSorry := bang)
216219 -- `evalConst` may emit IO, but this is collected by `withIsolatedStreams` below.
217220 let r ← toMessageData <$> evalConst t declName (checkMeta := !Elab.inServer.get (← getOptions))
218221 return { eval := pure r, printVal := some (← inferType e) }
219222 let (output, exOrRes) ← IO.FS.withIsolatedStreams (isolateStderr := Core.stderrAsMessages.get (← getOptions)) do
220223 try
221224 -- Generate an action without executing it. We use `withoutModifyingEnv` to ensure
222225 -- we don't pollute the environment with auxiliary declarations.
223- let act : EvalAction ← liftTermElabM do Term.withDeclName declName do withoutModifyingEnv do
226+ let act : EvalAction ← liftTermElabM do Term.withDeclName (mkPrivateName (← getEnv) declName) do withoutModifyingEnv do
224227 withSaveInfoContext do -- save the environment post-elaboration (for matchers, let rec, etc.)
225228 let e ← elabTermForEval term expectedType?
226229 -- If there is an elaboration error, don't evaluate!
0 commit comments