Skip to content

Commit 923afaf

Browse files
committed
feat: non-unfoldable defs note on apply
1 parent e9f5d14 commit 923afaf

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

src/Lean/Meta/Check.lean

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,24 @@ def throwLetTypeMismatchMessage {α} (fvarId : FVarId) : MetaM α := do
194194
throwError "invalid {declKind} declaration, term{indentExpr v}\nhas type{indentExpr vType}\nbut is expected to have type{indentExpr t}"
195195
| _ => unreachable!
196196

197+
/-- Adds note about definitions not unfolded because of the module system, if any. -/
198+
def mkUnfoldAxiomsNote (givenType expectedType : Expr) : MetaM MessageData := do
199+
let env ← getEnv
200+
if env.header.isModule then
201+
let origDiag := (← get).diag
202+
try
203+
let _ ← observing <| withOptions (diagnostics.set · true) <| isDefEq givenType expectedType
204+
let blocked := (← get).diag.unfoldAxiomCounter.toList.filterMap fun (n, count) => do
205+
let count := count - origDiag.unfoldAxiomCounter.findD n 0
206+
guard <| count > 0 && getOriginalConstKind? env n matches some .defn
207+
return m!"{.ofConstName n} ↦ {count}"
208+
if !blocked.isEmpty then
209+
return MessageData.note m!"The following definitions were not unfolded because \
210+
their definition is not exposed:{indentD <| .joinSep blocked Format.line}"
211+
finally
212+
modify ({ · with diag := origDiag })
213+
return .nil
214+
197215
/--
198216
Return error message "has type{givenType}\nbut is expected to have type{expectedType}"
199217
Adds the type’s types unless they are defeq.
@@ -226,19 +244,7 @@ def mkHasTypeButIsExpectedMsg (givenType expectedType : Expr)
226244
let (givenType, expectedType) ← addPPExplicitToExposeDiff givenType expectedType
227245
let trailing := trailing?.map (m!"\n" ++ ·) |>.getD .nil
228246
pure m!"has type{indentExpr givenType}\nbut is expected to have type{indentExpr expectedType}{trailing}")
229-
let env ← getEnv
230-
if env.header.isModule then
231-
let origDiag := (← get).diag
232-
let _ ← observing <| withOptions (diagnostics.set · true) <| isDefEq givenType expectedType
233-
let blocked := (← get).diag.unfoldAxiomCounter.toList.filterMap fun (n, count) => do
234-
let count := count - origDiag.unfoldAxiomCounter.findD n 0
235-
guard <| count > 0 && getOriginalConstKind? env n matches some .defn
236-
return m!"{.ofConstName n} ↦ {count}"
237-
if !blocked.isEmpty then
238-
msg := msg ++ MessageData.note m!"The following definitions were not unfolded because \
239-
their definition is not exposed:{indentD <| .joinSep blocked Format.line}"
240-
modify ({ · with diag := origDiag })
241-
return msg
247+
return msg ++ (← mkUnfoldAxiomsNote givenType expectedType)
242248

243249
def throwAppTypeMismatch (f a : Expr) : MetaM α := do
244250
-- Clarify that `a` is "last" only if it may be confused with some preceding argument; otherwise,

src/Lean/Meta/Tactic/Apply.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private def throwApplyError {α} (mvarId : MVarId)
3434
let (conclusionType, targetType) ← addPPExplicitToExposeDiff conclusionType targetType
3535
let conclusion := if conclusionType?.isNone then "type" else "conclusion"
3636
return m!"could not unify the {conclusion} of {term?.getD "the term"}{indentExpr conclusionType}\n\
37-
with the goal{indentExpr targetType}{note}"
37+
with the goal{indentExpr targetType}{note}{← mkUnfoldAxiomsNote conclusionType targetType}"
3838

3939
def synthAppInstances (tacticName : Name) (mvarId : MVarId) (mvarsNew : Array Expr) (binderInfos : Array BinderInfo)
4040
(synthAssignedInstances : Bool) (allowSynthFailures : Bool) : MetaM Unit := do

tests/pkg/module/Module/Imported.lean

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ Note: The following definitions were not unfolded because their definition is no
2727
#guard_msgs in
2828
example : f = 1 := rfl
2929
30+
/--
31+
error: Tactic `apply` failed: could not unify the conclusion of `@rfl`
32+
?a = ?a
33+
with the goal
34+
f = 1
35+
36+
Note: The full type of `@rfl` is
37+
∀ {α : Sort ?u.115} {a : α}, a = a
38+
39+
Note: The following definitions were not unfolded because their definition is not exposed:
40+
f ↦ 1
41+
42+
⊢ f = 1
43+
-/
44+
#guard_msgs in
45+
example : f = 1 := by apply rfl
46+
3047
/-! Theorems should be exported without their bodies -/
3148
3249
/--

0 commit comments

Comments
 (0)