Skip to content

Commit d19b504

Browse files
committed
de-classify ExtraModUses
1 parent 0493629 commit d19b504

File tree

7 files changed

+16
-38
lines changed

7 files changed

+16
-38
lines changed

src/Lean/Elab/Attributes.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def toAttributeKind (attrKindStx : Syntax) : MacroM AttributeKind := do
4444
def mkAttrKindGlobal : Syntax :=
4545
mkNode ``Lean.Parser.Term.attrKind #[mkNullNode]
4646

47-
def elabAttr [Monad m] [MonadEnv m] [MonadResolveName m] [MonadError m] [MonadMacroAdapter m] [MonadRecDepth m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] [MonadLiftT IO m] (attrInstance : Syntax) : m Attribute := do
47+
def elabAttr (attrInstance : Syntax) : CoreM Attribute := do
4848
/- attrInstance := ppGroup $ leading_parser attrKind >> attrParser -/
4949
let attrKind ← liftMacroM <| toAttributeKind attrInstance[0]
5050
let attr := attrInstance[1]
@@ -60,7 +60,7 @@ def elabAttr [Monad m] [MonadEnv m] [MonadResolveName m] [MonadError m] [MonadMa
6060
So, we expand them before here before we invoke the attributer handlers implemented using `AttrM`. -/
6161
return { kind := attrKind, name := attrName, stx := attr }
6262

63-
def elabAttrs [Monad m] [MonadEnv m] [MonadResolveName m] [MonadError m] [MonadMacroAdapter m] [MonadRecDepth m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] [MonadLog m] [MonadLiftT IO m] (attrInstances : Array Syntax) : m (Array Attribute) := do
63+
def elabAttrs (attrInstances : Array Syntax) : CoreM (Array Attribute) := do
6464
let mut attrs := #[]
6565
for attr in attrInstances do
6666
try
@@ -70,7 +70,7 @@ def elabAttrs [Monad m] [MonadEnv m] [MonadResolveName m] [MonadError m] [MonadM
7070
return attrs
7171

7272
-- leading_parser "@[" >> sepBy1 attrInstance ", " >> "]"
73-
def elabDeclAttrs [Monad m] [MonadEnv m] [MonadResolveName m] [MonadError m] [MonadMacroAdapter m] [MonadRecDepth m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] [MonadLog m] [MonadLiftT IO m] (stx : Syntax) : m (Array Attribute) :=
73+
def elabDeclAttrs (stx : Syntax) : CoreM (Array Attribute) :=
7474
elabAttrs stx[1].getSepArgs
7575

7676
end Lean.Elab

src/Lean/Elab/Command.lean

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,6 @@ def withMacroExpansion (beforeStx afterStx : Syntax) (x : CommandElabM α) : Com
446446
withInfoContext (mkInfo := pure <| .ofMacroExpansionInfo { stx := beforeStx, output := afterStx, lctx := .empty }) do
447447
withReader (fun ctx => { ctx with macroStack := { before := beforeStx, after := afterStx } :: ctx.macroStack }) x
448448

449-
instance : MonadMacroAdapter CommandElabM where
450-
getNextMacroScope := return (← get).nextMacroScope
451-
setNextMacroScope next := modify fun s => { s with nextMacroScope := next }
452-
453449
instance : MonadRecDepth CommandElabM where
454450
withRecDepth d x := withReader (fun ctx => { ctx with currRecDepth := d }) x
455451
getRecDepth := return (← read).currRecDepth

src/Lean/Elab/DeclModifiers.lean

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,8 @@ def expandOptDocComment? [Monad m] [MonadError m] (optDocComment : Syntax) : m (
164164

165165
section Methods
166166

167-
variable [Monad m] [MonadEnv m] [MonadResolveName m] [MonadError m] [MonadFinally m] [MonadMacroAdapter m] [MonadRecDepth m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] [MonadLog m] [MonadInfoTree m] [MonadLiftT IO m]
168-
169167
/-- Elaborate declaration modifiers (i.e., attributes, `partial`, `private`, `protected`, `unsafe`, `meta`, `noncomputable`, doc string)-/
170-
def elabModifiers (stx : TSyntax ``Parser.Command.declModifiers) : m Modifiers := do
168+
def elabModifiers (stx : TSyntax ``Parser.Command.declModifiers) : CoreM Modifiers := do
171169
let docCommentStx := stx.raw[0]
172170
let attrsStx := stx.raw[1]
173171
let visibilityStx := stx.raw[2]
@@ -209,7 +207,7 @@ Ensure the function has not already been declared, and apply the given visibilit
209207
If `private`, return the updated name using our internal encoding for private names.
210208
If `protected`, register `declName` as protected in the environment.
211209
-/
212-
def applyVisibility (modifiers : Modifiers) (declName : Name) : m Name := do
210+
def applyVisibility (modifiers : Modifiers) (declName : Name) : CoreM Name := do
213211
let mut declName := declName
214212
if !modifiers.visibility.isInferredPublic (← getEnv) then
215213
declName := mkPrivateName (← getEnv) declName
@@ -218,7 +216,7 @@ def applyVisibility (modifiers : Modifiers) (declName : Name) : m Name := do
218216
modifyEnv fun env => addProtected env declName
219217
pure declName
220218

221-
def checkIfShadowingStructureField (declName : Name) : m Unit := do
219+
def checkIfShadowingStructureField (declName : Name) : CoreM Unit := do
222220
match declName with
223221
| Name.str pre .. =>
224222
if isStructure (← getEnv) pre then
@@ -228,7 +226,7 @@ def checkIfShadowingStructureField (declName : Name) : m Unit := do
228226
throwError "invalid declaration name `{.ofConstName declName}`, structure `{pre}` has field `{fieldName}`"
229227
| _ => pure ()
230228

231-
def mkDeclName (currNamespace : Name) (modifiers : Modifiers) (shortName : Name) : m (Name × Name) := do
229+
def mkDeclName (currNamespace : Name) (modifiers : Modifiers) (shortName : Name) : CoreM (Name × Name) := do
232230
let mut shortName := shortName
233231
let mut currNamespace := currNamespace
234232
let view := extractMacroScopes shortName

src/Lean/Elab/RecommendedSpelling.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ open Lean.Parser.Command
1919
@[builtin_command_elab «recommended_spelling»] def elabRecommendedSpelling : CommandElab
2020
| `(«recommended_spelling»|$[$docs:docComment]? recommended_spelling $spelling:str fornotation»:str in [$[$decls:ident],*]) => do
2121
let declNames ← liftTermElabM <| decls.mapM (fun decl => realizeGlobalConstNoOverloadWithInfo decl)
22-
declNames.forM (recordExtraModUseFromDecl (isMeta := false))
22+
declNames.forM (recordExtraModUseFromDecl (isMeta := false) ·)
2323
let recommendedSpelling : RecommendedSpelling := {
2424
«notation» := «notation».getString
2525
recommendedSpelling := spelling.getString

src/Lean/Elab/Term/TermElabM.lean

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,10 +1457,6 @@ private def elabUsingElabFns (stx : Syntax) (expectedType? : Option Expr) (catch
14571457
| [] => throwError "elaboration function for `{k}` has not been implemented{indentD stx}"
14581458
| elabFns => elabUsingElabFnsAux s stx expectedType? catchExPostpone elabFns
14591459

1460-
instance : MonadMacroAdapter TermElabM where
1461-
getNextMacroScope := return (← getThe Core.State).nextMacroScope
1462-
setNextMacroScope next := modifyThe Core.State fun s => { s with nextMacroScope := next }
1463-
14641460
private def isExplicit (stx : Syntax) : Bool :=
14651461
match stx with
14661462
| `(@$_) => true

src/Lean/Elab/Util.lean

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,7 @@ def expandMacroImpl? (env : Environment) : Syntax → MacroM (Option (Name × Ex
166166
| ex => return (e.declName, Except.error ex)
167167
return none
168168

169-
class MonadMacroAdapter (m : TypeType) extends MonadQuotation m where
170-
getNextMacroScope : m MacroScope
171-
setNextMacroScope : MacroScope → m Unit
172-
173-
@[always_inline]
174-
instance (m n) [MonadLift m n] [MonadQuotation n] [MonadMacroAdapter m] : MonadMacroAdapter n := {
175-
getNextMacroScope := liftM (MonadMacroAdapter.getNextMacroScope : m _)
176-
setNextMacroScope := fun s => liftM (MonadMacroAdapter.setNextMacroScope s : m _)
177-
}
178-
179-
def liftMacroM [Monad m] [MonadMacroAdapter m] [MonadEnv m] [MonadRecDepth m] [MonadError m] [MonadResolveName m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] [MonadLiftT IO m] (x : MacroM α) : m α := do
169+
def liftMacroM (x : MacroM α) : CoreM α := do
180170
let env ← getEnv
181171
let opts ← getOptions
182172
let currNamespace ← getCurrNamespace
@@ -198,7 +188,7 @@ def liftMacroM [Monad m] [MonadMacroAdapter m] [MonadEnv m] [MonadRecDepth m] [M
198188
quotContext := ← MonadQuotation.getContext
199189
currRecDepth := ← MonadRecDepth.getRecDepth
200190
maxRecDepth := ← MonadRecDepth.getMaxRecDepth
201-
} { macroScope := (← MonadMacroAdapter.getNextMacroScope) } with
191+
} { macroScope := (← get).nextMacroScope } with
202192
| EStateM.Result.error Macro.Exception.unsupportedSyntax _ => throwUnsupportedSyntax
203193
| EStateM.Result.error (Macro.Exception.error ref msg) _ =>
204194
if msg == maxRecDepthErrorMessage then
@@ -209,11 +199,11 @@ def liftMacroM [Monad m] [MonadMacroAdapter m] [MonadEnv m] [MonadRecDepth m] [M
209199
| EStateM.Result.ok a s =>
210200
for n in s.expandedMacroDecls do
211201
recordExtraModUseFromDecl (isMeta := true) n
212-
MonadMacroAdapter.setNextMacroScope s.macroScope
202+
modify ({ · with nextMacroScope := s.macroScope })
213203
s.traceMsgs.reverse.forM fun (clsName, msg) => trace clsName fun _ => msg
214204
return a
215205

216-
@[inline] def adaptMacro {m : TypeType} [Monad m] [MonadMacroAdapter m] [MonadEnv m] [MonadRecDepth m] [MonadError m] [MonadResolveName m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] [MonadLiftT IO m] (x : Macro) (stx : Syntax) : m Syntax :=
206+
@[inline] def adaptMacro (x : Macro) (stx : Syntax) : CoreM Syntax :=
217207
liftMacroM (x stx)
218208

219209
partial def mkUnusedBaseName (baseName : Name) : MacroM Name := do

src/Lean/ExtraModUses.lean

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ public def copyExtraModUses (src dest : Environment) : Environment := Id.run do
4949
env := extraModUses.addEntry env entry
5050
env
5151

52-
variable [Monad m] [MonadEnv m] [MonadTrace m] [MonadOptions m] [MonadRef m] [AddMessageContext m]
53-
54-
def recordExtraModUseCore (mod : Name) (isMeta : Bool) (hint : Name := .anonymous) : m Unit := do
52+
def recordExtraModUseCore (mod : Name) (isMeta : Bool) (hint : Name := .anonymous) : CoreM Unit := do
5553
let entry := { module := mod, isExported := (← getEnv).isExporting, isMeta }
5654
if !(extraModUses.getState (asyncMode := .local) (← getEnv)).contains entry then
5755
trace[extraModUses] "recording {if entry.isExported then "public" else "private"} \
@@ -63,7 +61,7 @@ def recordExtraModUseCore (mod : Name) (isMeta : Bool) (hint : Name := .anonymou
6361
Records an additional import dependency for the current module, using `Environment.isExporting` as
6462
the visibility level.
6563
-/
66-
public def recordExtraModUse (modName : Name) (isMeta : Bool) : m Unit := do
64+
public def recordExtraModUse (modName : Name) (isMeta : Bool) : CoreM Unit := do
6765
if modName != (← getEnv).mainModule then
6866
recordExtraModUseCore modName isMeta
6967

@@ -72,7 +70,7 @@ Records the module of the given declaration as an additional import dependency f
7270
module, using `Environment.isExporting` as the visibility level. If the declaration itself is
7371
already `meta`, the module dependency is recorded as a non-`meta` dependency.
7472
-/
75-
public def recordExtraModUseFromDecl (declName : Name) (isMeta : Bool) : m Unit := do
73+
public def recordExtraModUseFromDecl (declName : Name) (isMeta : Bool) : CoreM Unit := do
7674
let env ← getEnv
7775
if let some mod := env.getModuleIdxFor? declName |>.bind (env.header.modules[·]?) then
7876
-- If the declaration itself is already `meta`, no need to mark the import.
@@ -91,7 +89,7 @@ public def isExtraRevModUse (env : Environment) (modIdx : ModuleIdx) : Bool :=
9189
!(isExtraRevModUseExt.getModuleEntries env modIdx |>.isEmpty)
9290

9391
/-- Records this module to be preserved as an import by `shake`. -/
94-
public def recordExtraRevUseOfCurrentModule : m Unit := do
92+
public def recordExtraRevUseOfCurrentModule : CoreM Unit := do
9593
if isExtraRevModUseExt.getEntries (asyncMode := .local) (← getEnv) |>.isEmpty then
9694
modifyEnv (isExtraRevModUseExt.addEntry · ())
9795

0 commit comments

Comments
 (0)