refactor: add ImportingFlag.lean

This commit is contained in:
Leonardo de Moura 2021-08-03 14:29:36 -07:00
parent 65aafc070c
commit 526cbfbcd0
4 changed files with 30 additions and 13 deletions

View file

@ -47,7 +47,8 @@ builtin_initialize attributeMapRef : IO.Ref (PersistentHashMap Name AttributeImp
def registerBuiltinAttribute (attr : AttributeImpl) : IO Unit := do
let m ← attributeMapRef.get
if m.contains attr.name then throw (IO.userError ("invalid builtin attribute declaration, '" ++ toString attr.name ++ "' has already been used"))
unless (← IO.initializing) || (← importing) do throw (IO.userError "failed to register attribute, attributes can only be registered during initialization")
unless (← initializing) do
throw (IO.userError "failed to register attribute, attributes can only be registered during initialization")
attributeMapRef.modify fun m => m.insert attr.name attr
/-

View file

@ -3,6 +3,7 @@ Copyright (c) 2018 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich and Leonardo de Moura
-/
import Lean.ImportingFlag
import Lean.Data.KVMap
namespace Lean
@ -33,6 +34,8 @@ private constant optionDeclsRef : IO.Ref OptionDecls
@[export lean_register_option]
def registerOption (name : Name) (decl : OptionDecl) : IO Unit := do
unless (← initializing) do
throw (IO.userError "failed to register option, options can only be registered during initialization")
let decls ← optionDeclsRef.get
if decls.contains name then
throw $ IO.userError s!"invalid option declaration '{name}', option already exists"

View file

@ -4,6 +4,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Std.Data.HashMap
import Lean.ImportingFlag
import Lean.Data.SMap
import Lean.Declaration
import Lean.LocalContext
@ -12,13 +13,6 @@ import Lean.Util.FindExpr
import Lean.Util.Profile
namespace Lean
builtin_initialize importingRef : IO.Ref Bool ← IO.mkRef false
/- True while modules are being imported. We use this flag to test check whether environment extensions are registered only
during initialization (builtin ones), and importing (user defined ones). -/
def importing : IO Bool :=
importingRef.get
/- Opaque environment extension state. -/
constant EnvExtensionStateSpec : PointedType.{0}
def EnvExtensionState : Type := EnvExtensionStateSpec.type
@ -224,7 +218,7 @@ unsafe def getState {σ} [Inhabited σ] (ext : Ext σ) (env : Environment) : σ
panic! invalidExtMsg
unsafe def registerExt {σ} (mkInitial : IO σ) : IO (Ext σ) := do
unless (← IO.initializing) || (← importing) do
unless (← initializing) do
throw (IO.userError "failed to register environment, extensions can only be registered during initialization")
let exts ← envExtensionsRef.get
let idx := exts.size
@ -613,8 +607,7 @@ structure ImportState where
@[export lean_import_modules]
partial def importModules (imports : List Import) (opts : Options) (trustLevel : UInt32 := 0) : IO Environment := profileitIO "import" opts do
try
importingRef.set true
withImporting do
let (_, s) ← importMods imports |>.run {}
let mut numConsts := 0
for mod in s.moduleData do
@ -648,8 +641,6 @@ partial def importModules (imports : List Import) (opts : Options) (trustLevel :
let env ← setImportedEntries env s.moduleData
let env ← finalizePersistentExtensions env s.moduleData opts
pure env
finally
importingRef.set false
where
importMods : List Import → StateRefT ImportState IO Unit
| [] => pure ()

View file

@ -0,0 +1,22 @@
/-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
namespace Lean
builtin_initialize importingRef : IO.Ref Bool ← IO.mkRef false
/- We say Lean is "initializing" when it is executing `builtin_initialize` declarations and importing modules.
Recall that Lean excutes `initialize` declarations while importing modules. -/
def initializing : IO Bool :=
IO.initializing <||> importingRef.get
def withImporting (x : IO α) : IO α :=
try
importingRef.set true
x
finally
importingRef.set false
end Lean