fix: Use context environment in sorry capture

This commit is contained in:
Leni Aniva 2025-01-28 17:40:49 -08:00
parent 274e29199d
commit c77f14d383
Signed by: aniva
GPG Key ID: 4D9B1C8D10EA4C50
2 changed files with 28 additions and 25 deletions

View File

@ -148,34 +148,39 @@ structure AnnotatedGoalState where
Since we cannot directly merge `MetavarContext`s, we have to get creative. This Since we cannot directly merge `MetavarContext`s, we have to get creative. This
function duplicates frozen mvars in term and tactic info nodes, and add them to function duplicates frozen mvars in term and tactic info nodes, and add them to
the current `MetavarContext`. the current `MetavarContext`.
WARNING: Behaviour is unstable when there are multiple `sorry`s. Consider using
the draft tactic instead.
-/ -/
@[export pantograph_frontend_sorrys_to_goal_state_m] @[export pantograph_frontend_sorrys_to_goal_state_m]
def sorrysToGoalState (sorrys : List InfoWithContext) : MetaM AnnotatedGoalState := do def sorrysToGoalState (sorrys : List InfoWithContext) : MetaM AnnotatedGoalState := do
let env := sorrys.head? >>= (·.context?) |>.map (·.env) |>.getD (← getEnv)
assert! !sorrys.isEmpty assert! !sorrys.isEmpty
let goalsM := sorrys.mapM λ i => do withEnv env do
match i.info with let goalsM := sorrys.mapM λ i => do
| .ofTermInfo termInfo => do match i.info with
let mvarId ← MetaTranslate.translateMVarFromTermInfo termInfo i.context? | .ofTermInfo termInfo => do
if (← mvarId.getType).hasSorry then let mvarId ← MetaTranslate.translateMVarFromTermInfo termInfo i.context?
throwError s!"Coupling is not allowed in drafting"
return [(mvarId, stxByteRange termInfo.stx)]
| .ofTacticInfo tacticInfo => do
let mvarIds ← MetaTranslate.translateMVarFromTacticInfoBefore tacticInfo i.context?
for mvarId in mvarIds do
if (← mvarId.getType).hasSorry then if (← mvarId.getType).hasSorry then
throwError s!"Coupling is not allowed in drafting" throwError s!"Coupling is not allowed in drafting"
let range := stxByteRange tacticInfo.stx return [(mvarId, stxByteRange termInfo.stx)]
return mvarIds.map (·, range) | .ofTacticInfo tacticInfo => do
| _ => panic! "Invalid info" let mvarIds ← MetaTranslate.translateMVarFromTacticInfoBefore tacticInfo i.context?
let annotatedGoals := List.flatten (← goalsM.run {} |>.run' {}) for mvarId in mvarIds do
let goals := annotatedGoals.map Prod.fst if (← mvarId.getType).hasSorry then
let srcBoundaries := annotatedGoals.map Prod.snd throwError s!"Coupling is not allowed in drafting"
let root := match goals with let range := stxByteRange tacticInfo.stx
| [] => panic! "No MVars generated" return mvarIds.map (·, range)
| [g] => g | _ => panic! "Invalid info"
| _ => { name := .anonymous } let annotatedGoals := List.flatten (← goalsM.run {} |>.run' {})
let state ← GoalState.createFromMVars goals root let goals := annotatedGoals.map Prod.fst
return { state, srcBoundaries } let srcBoundaries := annotatedGoals.map Prod.snd
let root := match goals with
| [] => panic! "No MVars generated"
| [g] => g
| _ => { name := .anonymous }
let state ← GoalState.createFromMVars goals root
return { state, srcBoundaries }
@[export pantograph_frontend_collect_new_defined_constants_m] @[export pantograph_frontend_collect_new_defined_constants_m]

View File

@ -44,8 +44,6 @@ structure CompilationUnit where
messages : Array String messages : Array String
newConstants : List Name newConstants : List Name
def frontend_process_inner (args: Protocol.FrontendProcess): MainM (CR Protocol.FrontendProcessResult) := do def frontend_process_inner (args: Protocol.FrontendProcess): MainM (CR Protocol.FrontendProcessResult) := do
let options := (← get).options let options := (← get).options
let (fileName, file) ← match args.fileName?, args.file? with let (fileName, file) ← match args.fileName?, args.file? with
@ -78,7 +76,7 @@ def frontend_process_inner (args: Protocol.FrontendProcess): MainM (CR Protocol.
else else
pure [] pure []
return { return {
env := step.after, env := step.before,
boundary, boundary,
invocations, invocations,
sorrys, sorrys,