Skip to content

Commit

Permalink
fix: check is valid structure projection when pretty printing
Browse files Browse the repository at this point in the history
For structure projections, the pretty printer assumed that the expression was type correct. Now it checks that the object being projected is of the correct type. Such terms appear in type mismatch errors.

Also, fixes and improves `#print` for structures. The types of projections now use MessageData (so are now hoverable), and the type of `self` is now the correct type.

Closes leanprover#4670
  • Loading branch information
kmill committed Aug 11, 2024
1 parent 4236d8a commit b5a11e1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Lean/Elab/Print.lean
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,19 @@ private def printStructure (id : Name) (levelParams : List Name) (numParams : Na
logInfo m
where
doFields := liftTermElabM do
forallTelescope (← getConstInfo id).type fun params type =>
withLocalDeclD `self type fun self => do
forallTelescope (← getConstInfo id).type fun params _ =>
withLocalDeclD `self (mkAppN (Expr.const id (levelParams.map .param)) params) fun self => do
let params := params.push self
let mut m : Format := ""
let mut m : MessageData := ""
for field in fields do
match getProjFnForField? (← getEnv) id field with
| some proj =>
let field : Format := if isPrivateName proj then "private " ++ toString field else toString field
let cinfo ← getConstInfo proj
let ftype ← instantiateForall cinfo.type params
m := m ++ Format.line ++ field ++ " : " ++ (← ppExpr ftype) -- Why ppExpr here?
m := m ++ Format.line ++ field ++ " : " ++ ftype
| none => panic! "missing structure field info"
return m
addMessageContext m

private def printIdCore (id : Name) : CommandElabM Unit := do
let env ← getEnv
Expand Down
2 changes: 2 additions & 0 deletions src/Lean/PrettyPrinter/Delaborator/FieldNotation.lean
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ def fieldNotationCandidate? (f : Expr) (args : Array Expr) (useGeneralizedFieldN
-- Handle structure projections
try
let (field, numParams, _) ← projInfo c
unless numParams + 1 ≤ args.size do return none
unless (← whnf <| ← inferType args[numParams]!).isAppOf c.getPrefix do return none
return (field, numParams)
catch _ => pure ()
-- Handle generalized field notation
Expand Down

0 comments on commit b5a11e1

Please sign in to comment.