Skip to content

Commit

Permalink
fix: make sure parent structure projections have 'go to definition'…
Browse files Browse the repository at this point in the history
… information (#6487)

This PR adds source position information for `structure` parent
projections, supporting "go to definition". Closes #3063.
  • Loading branch information
kmill authored Dec 31, 2024
1 parent 3427630 commit 6d44715
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/Lean/Elab/Structure.lean
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,9 @@ private def addProjections (r : ElabHeaderResult) (fieldInfos : Array StructFiel
let env ← getEnv
let env ← ofExceptKernelException (mkProjections env r.view.declName projNames.toList r.view.isClass)
setEnv env
for fieldInfo in fieldInfos do
if fieldInfo.isSubobject then
addDeclarationRangesFromSyntax fieldInfo.declName r.view.ref fieldInfo.ref

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

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

0 comments on commit 6d44715

Please sign in to comment.