Skip to content

Commit d9767f2

Browse files
committed
add TermInfo.isExprGenerator field to identify when TermInfo node is responsible for its expr.
1 parent e3aade1 commit d9767f2

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

src/Lean/Elab/InfoTree/Main.lean

+4-4
Original file line numberDiff line numberDiff line change
@@ -267,20 +267,20 @@ def resolveGlobalNameWithInfos [MonadResolveName m] [MonadEnv m] [MonadError m]
267267

268268
/-- Use this to descend a node on the infotree that is being built.
269269
270-
It saves the current list of trees `t₀` and resets it and then runs `x >>= mkInfo`, producing either an `i : Info` or a hole id.
270+
It saves the current list of trees `t₀` and resets it and then runs `(x >>= mkInfo) <*> getInfoTrees`, producing either an `i : Info` or a hole id.
271271
Running `x >>= mkInfo` will modify the trees state and produce a new list of trees `t₁`.
272272
In the `i : Info` case, `t₁` become the children of a node `node i t₁` that is appended to `t₀`.
273273
-/
274-
def withInfoContext' [MonadFinally m] (x : m α) (mkInfo : α → m (Sum Info MVarId)) : m α := do
274+
def withInfoContext' [MonadFinally m] (x : m α) (mkInfo : α → m ((PersistentArray InfoTree) → Sum Info MVarId)) : m α := do
275275
if (← getInfoState).enabled then
276276
let treesSaved ← getResetInfoTrees
277277
Prod.fst <$> MonadFinally.tryFinally' x fun a? => do
278278
match a? with
279279
| none => modifyInfoTrees fun _ => treesSaved
280280
| some a =>
281-
let info ← mkInfo a
281+
let infoFn ← mkInfo a
282282
modifyInfoTrees fun trees =>
283-
match info with
283+
match infoFn trees with
284284
| Sum.inl info => treesSaved.push <| InfoTree.node info trees
285285
| Sum.inr mvarId => treesSaved.push <| InfoTree.hole mvarId
286286
else

src/Lean/Elab/InfoTree/Types.lean

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ structure TermInfo extends ElabInfo where
3636
lctx : LocalContext -- The local context when the term was elaborated.
3737
expectedType? : Option Expr
3838
expr : Expr
39+
isExprGenerator : Bool := false -- Is this node's `stx` directly responsible for generating `expr`?
3940
isBinder : Bool := false
4041
deriving Inhabited
4142

src/Lean/Elab/Term.lean

+15-4
Original file line numberDiff line numberDiff line change
@@ -915,12 +915,23 @@ def isTacticOrPostponedHole? (e : Expr) : TermElabM (Option MVarId) := do
915915
| _ => return none
916916
| _ => pure none
917917

918-
def mkTermInfo (elaborator : Name) (stx : Syntax) (e : Expr) (expectedType? : Option Expr := none) (lctx? : Option LocalContext := none) (isBinder := false) : TermElabM (Sum Info MVarId) := do
918+
def mkTermInfo (elaborator : Name) (stx : Syntax) (e : Expr) (expectedType? : Option Expr := none) (lctx? : Option LocalContext := none) (isBinder := false) : TermElabM ((PersistentArray InfoTree) → Sum Info MVarId) := do
919919
match (← isTacticOrPostponedHole? e) with
920-
| some mvarId => return Sum.inr mvarId
920+
| some mvarId => return fun _ => Sum.inr mvarId
921921
| none =>
922922
let e := removeSaveInfoAnnotation e
923-
return Sum.inl <| Info.ofTermInfo { elaborator, lctx := lctx?.getD (← getLCtx), expr := e, stx, expectedType?, isBinder }
923+
let lctx ← getLCtx
924+
return fun trees =>
925+
-- NOTE: this can probably be made more efficient, but for now
926+
-- a sufficient check for whether this node is responsible for `e` is to make sure
927+
-- that no term subtree has the same expression
928+
let isExprGenerator :=
929+
trees.all fun tree => match tree with
930+
| .node (Info.ofTermInfo info) _ =>
931+
info.expr != e
932+
| _ => true
933+
934+
Sum.inl <| Info.ofTermInfo { elaborator, lctx := lctx?.getD lctx, expr := e, isExprGenerator, stx, expectedType?, isBinder }
924935

925936
/--
926937
Pushes a new leaf node to the info tree associating the expression `e` to the syntax `stx`.
@@ -949,7 +960,7 @@ def addTermInfo (stx : Syntax) (e : Expr) (expectedType? : Option Expr := none)
949960
def addTermInfo' (stx : Syntax) (e : Expr) (expectedType? : Option Expr := none) (lctx? : Option LocalContext := none) (elaborator := Name.anonymous) (isBinder := false) : TermElabM Unit :=
950961
discard <| addTermInfo stx e expectedType? lctx? elaborator isBinder
951962

952-
def withInfoContext' (stx : Syntax) (x : TermElabM Expr) (mkInfo : Expr → TermElabM (Sum Info MVarId)) : TermElabM Expr := do
963+
def withInfoContext' (stx : Syntax) (x : TermElabM Expr) (mkInfo : Expr → TermElabM ((PersistentArray InfoTree) → Sum Info MVarId)) : TermElabM Expr := do
953964
if (← read).inPattern then
954965
let e ← x
955966
return mkPatternWithRef e stx

src/Lean/Server/FileWorker/RequestHandling.lean

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def locationLinksOfInfo (kind : GoToKind) (ci : Elab.ContextInfo) (i : Elab.Info
123123
| Expr.fvar id .. => return ← ci.runMetaM i.lctx <| locationLinksFromBinder i id
124124
| _ => pure ()
125125

126-
if kind != declaration then
126+
if kind != declaration && ti.isExprGenerator then
127127
-- go-to-definition on a projection application of a typeclass
128128
-- should return all instances generated by TC
129129
expr ← ci.runMetaM i.lctx do instantiateMVars expr

0 commit comments

Comments
 (0)