Skip to content

Commit 87caf36

Browse files
kmillluisacicolini
authored andcommitted
fix: make sure parent structure projections have 'go to definition' information (leanprover#6487)
This PR adds source position information for `structure` parent projections, supporting "go to definition". Closes leanprover#3063.
1 parent 187ccf6 commit 87caf36

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/Lean/Elab/Structure.lean

+9-5
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,9 @@ private def addProjections (r : ElabHeaderResult) (fieldInfos : Array StructFiel
691691
let env ← getEnv
692692
let env ← ofExceptKernelException (mkProjections env r.view.declName projNames.toList r.view.isClass)
693693
setEnv env
694+
for fieldInfo in fieldInfos do
695+
if fieldInfo.isSubobject then
696+
addDeclarationRangesFromSyntax fieldInfo.declName r.view.ref fieldInfo.ref
694697

695698
private def registerStructure (structName : Name) (infos : Array StructFieldInfo) : TermElabM Unit := do
696699
let fields ← infos.filterMapM fun info => do
@@ -775,14 +778,14 @@ private def setSourceInstImplicit (type : Expr) : Expr :=
775778
/--
776779
Creates a projection function to a non-subobject parent.
777780
-/
778-
private partial def mkCoercionToCopiedParent (levelParams : List Name) (params : Array Expr) (view : StructView) (source : Expr) (parentStructName : Name) (parentType : Expr) : MetaM StructureParentInfo := do
781+
private partial def mkCoercionToCopiedParent (levelParams : List Name) (params : Array Expr) (view : StructView) (source : Expr) (parent : StructParentInfo) (parentType : Expr) : MetaM StructureParentInfo := do
779782
let isProp ← Meta.isProp parentType
780783
let env ← getEnv
781784
let structName := view.declName
782785
let sourceFieldNames := getStructureFieldsFlattened env structName
783-
let binfo := if view.isClass && isClass env parentStructName then BinderInfo.instImplicit else BinderInfo.default
786+
let binfo := if view.isClass && isClass env parent.structName then BinderInfo.instImplicit else BinderInfo.default
784787
let mut declType ← instantiateMVars (← mkForallFVars params (← mkForallFVars #[source] parentType))
785-
if view.isClass && isClass env parentStructName then
788+
if view.isClass && isClass env parent.structName then
786789
declType := setSourceInstImplicit declType
787790
declType := declType.inferImplicit params.size true
788791
let rec copyFields (parentType : Expr) : MetaM Expr := do
@@ -823,7 +826,8 @@ private partial def mkCoercionToCopiedParent (levelParams : List Name) (params :
823826
-- (Instances will get instance reducibility in `Lean.Elab.Command.addParentInstances`.)
824827
if !binfo.isInstImplicit && !(← Meta.isProp parentType) then
825828
setReducibleAttribute declName
826-
return { structName := parentStructName, subobject := false, projFn := declName }
829+
addDeclarationRangesFromSyntax declName view.ref parent.ref
830+
return { structName := parent.structName, subobject := false, projFn := declName }
827831

828832
private def mkRemainingProjections (levelParams : List Name) (params : Array Expr) (view : StructView)
829833
(parents : Array StructParentInfo) (fieldInfos : Array StructFieldInfo) : TermElabM (Array StructureParentInfo) := do
@@ -844,7 +848,7 @@ private def mkRemainingProjections (levelParams : List Name) (params : Array Exp
844848
pure { structName := parent.structName, subobject := true, projFn := info.declName }
845849
else
846850
let parent_type := (← instantiateMVars parent.type).replace fun e => parentFVarToConst[e]?
847-
mkCoercionToCopiedParent levelParams params view source parent.structName parent_type)
851+
mkCoercionToCopiedParent levelParams params view source parent parent_type)
848852
parentInfos := parentInfos.push parentInfo
849853
if let some fvar := parent.fvar? then
850854
parentFVarToConst := parentFVarToConst.insert fvar <|

0 commit comments

Comments
 (0)