@@ -176,7 +176,8 @@ private def replaceBinderAnnotation (binder : TSyntax ``Parser.Term.bracketedBin
176
176
let mut binderIds := binderIds
177
177
let mut binderIdsIniSize := binderIds.size
178
178
let mut modifiedVarDecls := false
179
- for varDecl in varDecls do
179
+ -- Go through declarations in reverse to respect shadowing
180
+ for varDecl in varDecls.reverse do
180
181
let (ids, ty?, explicit') ← match varDecl with
181
182
| `(bracketedBinderF|($ids* $[: $ty?]? $(annot?)?)) =>
182
183
if annot?.isSome then
@@ -208,15 +209,15 @@ private def replaceBinderAnnotation (binder : TSyntax ``Parser.Term.bracketedBin
208
209
`(bracketedBinderF| ($id $[: $ty?]?))
209
210
else
210
211
`(bracketedBinderF| {$id $[: $ty?]?})
211
- for id in ids do
212
+ for id in ids.reverse do
212
213
if let some idx := binderIds.findIdx? fun binderId => binderId.raw.isIdent && binderId.raw.getId == id.raw.getId then
213
214
binderIds := binderIds.eraseIdx idx
214
215
modifiedVarDecls := true
215
216
varDeclsNew := varDeclsNew.push (← mkBinder id explicit)
216
217
else
217
218
varDeclsNew := varDeclsNew.push (← mkBinder id explicit')
218
219
if modifiedVarDecls then
219
- modifyScope fun scope => { scope with varDecls := varDeclsNew }
220
+ modifyScope fun scope => { scope with varDecls := varDeclsNew.reverse }
220
221
if binderIds.size != binderIdsIniSize then
221
222
binderIds.mapM fun binderId =>
222
223
if explicit then
@@ -228,15 +229,14 @@ private def replaceBinderAnnotation (binder : TSyntax ``Parser.Term.bracketedBin
228
229
229
230
@[builtin_command_elab «variable»] def elabVariable : CommandElab
230
231
| `(variable $binders*) => do
232
+ let binders ← binders.concatMapM replaceBinderAnnotation
231
233
-- Try to elaborate `binders` for sanity checking
232
234
runTermElabM fun _ => Term.withSynthesize <| Term.withAutoBoundImplicit <|
233
235
Term.elabBinders binders fun _ => pure ()
236
+ -- Remark: if we want to produce error messages when variables shadow existing ones, here is the place to do it.
234
237
for binder in binders do
235
- let binders ← replaceBinderAnnotation binder
236
- -- Remark: if we want to produce error messages when variables shadow existing ones, here is the place to do it.
237
- for binder in binders do
238
- let varUIds ← (← getBracketedBinderIds binder) |>.mapM (withFreshMacroScope ∘ MonadQuotation.addMacroScope)
239
- modifyScope fun scope => { scope with varDecls := scope.varDecls.push binder, varUIds := scope.varUIds ++ varUIds }
238
+ let varUIds ← (← getBracketedBinderIds binder) |>.mapM (withFreshMacroScope ∘ MonadQuotation.addMacroScope)
239
+ modifyScope fun scope => { scope with varDecls := scope.varDecls.push binder, varUIds := scope.varUIds ++ varUIds }
240
240
| _ => throwUnsupportedSyntax
241
241
242
242
open Meta
0 commit comments