Skip to content

Commit d9a35f1

Browse files
committed
feat: improve infer binder type failure message and range
1 parent ef603cf commit d9a35f1

15 files changed

+99
-48
lines changed

src/Lean/Elab/Binders.lean

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,13 @@ private def toBinderViews (stx : Syntax) : TermElabM (Array BinderView) := do
175175
else
176176
throwUnsupportedSyntax
177177

178-
private def registerFailedToInferBinderTypeInfo (type : Expr) (ref : Syntax) : TermElabM Unit := do
179-
registerCustomErrorIfMVar type ref "failed to infer binder type"
180-
registerLevelMVarErrorExprInfo type ref m!"failed to infer universe levels in binder type"
178+
private def registerFailedToInferBinderTypeInfo (type : Expr) (view : BinderView) : TermElabM Unit := do
179+
let msg := if view.id.getId.hasMacroScopes then
180+
m!"binder type"
181+
else
182+
m!"type of binder '{view.id.getId}'"
183+
registerCustomErrorIfMVar type view.ref m!"Failed to infer {msg}"
184+
registerLevelMVarErrorExprInfo type view.ref m!"Failed to infer universe levels in {msg}"
181185

182186
def addLocalVarInfo (stx : Syntax) (fvar : Expr) : TermElabM Unit :=
183187
addTermInfo' (isBinder := true) stx fvar
@@ -209,7 +213,7 @@ private partial def elabBinderViews (binderViews : Array BinderView) (fvars : Ar
209213
let binderView := binderViews[i]
210214
ensureAtomicBinderName binderView
211215
let type ← elabType binderView.type
212-
registerFailedToInferBinderTypeInfo type binderView.type
216+
registerFailedToInferBinderTypeInfo type binderView
213217
if binderView.bi.isInstImplicit && checkBinderAnnotations.get (← getOptions) then
214218
unless (← isClass? type).isSome do
215219
throwErrorAt binderView.type "invalid binder annotation, type is not a class instance{indentExpr type}\nuse the command `set_option checkBinderAnnotations false` to disable the check"
@@ -420,7 +424,7 @@ private partial def elabFunBinderViews (binderViews : Array BinderView) (i : Nat
420424
ensureAtomicBinderName binderView
421425
withRef binderView.type <| withLCtx s.lctx s.localInsts do
422426
let type ← elabType binderView.type
423-
registerFailedToInferBinderTypeInfo type binderView.type
427+
registerFailedToInferBinderTypeInfo type binderView
424428
let fvarId ← mkFreshFVarId
425429
let fvar := mkFVar fvarId
426430
let s := { s with fvars := s.fvars.push fvar }

src/Lean/Elab/MutualDef.lean

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,18 @@ private def check (prevHeaders : Array DefViewElabHeader) (newHeader : DefViewEl
8989
else
9090
pure ()
9191

92-
private def registerFailedToInferDefTypeInfo (type : Expr) (ref : Syntax) : TermElabM Unit :=
93-
registerCustomErrorIfMVar type ref "failed to infer definition type"
92+
private def registerFailedToInferDefTypeInfo (type : Expr) (view : DefView) :
93+
TermElabM Unit :=
94+
let ref := match view.kind with
95+
| .example => view.ref[0]
96+
| .instance => view.ref[1]
97+
| _ => view.declId
98+
let msg := match view.kind with
99+
| .example => m!"example"
100+
| .instance => m!"instance"
101+
| .theorem => m!"theorem '{view.declId}'"
102+
| _ => m!"definition '{view.declId}'"
103+
registerCustomErrorIfMVar type ref m!"Failed to infer type of {msg}"
94104

95105
/--
96106
Return `some [b, c]` if the given `views` are representing a declaration of the form
@@ -188,13 +198,13 @@ private def elabHeaders (views : Array DefView) (expandedDeclIds : Array ExpandD
188198
let mut type ← match view.type? with
189199
| some typeStx =>
190200
let type ← elabType typeStx
191-
registerFailedToInferDefTypeInfo type typeStx
201+
registerFailedToInferDefTypeInfo type view
192202
pure type
193203
| none =>
194204
let hole := mkHole refForElabFunType
195205
let type ← elabType hole
196206
trace[Elab.definition] ">> type: {type}\n{type.mvarId!}"
197-
registerFailedToInferDefTypeInfo type refForElabFunType
207+
registerFailedToInferDefTypeInfo type view
198208
pure type
199209
Term.synthesizeSyntheticMVarsNoPostponing
200210
if view.isInstance then

tests/lean/331.lean.expected.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
331.lean:6:13-6:14: error: failed to infer binder type
1+
331.lean:6:9-6:10: error: Failed to infer type of binder 'x'
22
when the resulting type of a declaration is explicitly provided, all holes (e.g., `_`) in the header are resolved before the declaration body is processed
3-
331.lean:7:13-7:14: error: failed to infer binder type
3+
331.lean:7:9-7:10: error: Failed to infer type of binder 'x'
44
when the resulting type of a declaration is explicitly provided, all holes (e.g., `_`) in the header are resolved before the declaration body is processed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
autoImplicitCtorParamIssue.lean:1:9-1:10: error: failed to infer binder type
2-
autoImplicitCtorParamIssue.lean:4:9-4:10: error: failed to infer binder type
3-
autoImplicitCtorParamIssue.lean:7:7-7:8: error: failed to infer binder type
1+
autoImplicitCtorParamIssue.lean:1:9-1:10: error: Failed to infer type of binder 'x'
2+
autoImplicitCtorParamIssue.lean:4:9-4:10: error: Failed to infer type of binder 'x'
3+
autoImplicitCtorParamIssue.lean:7:7-7:8: error: Failed to infer type of binder 'x'

tests/lean/holeErrors.lean.expected.out

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ holeErrors.lean:3:14-3:20: error: don't know how to synthesize implicit argument
22
@id ?m
33
context:
44
⊢ Sort u
5-
holeErrors.lean:3:11-3:20: error: failed to infer definition type
6-
holeErrors.lean:5:9-5:10: error: failed to infer definition type
5+
holeErrors.lean:3:4-3:10: error: Failed to infer type of definition 'f1.{u}'
6+
holeErrors.lean:5:4-5:6: error: Failed to infer type of definition 'f2'
77
when the resulting type of a declaration is explicitly provided, all holes (e.g., `_`) in the header are resolved before the declaration body is processed
88
holeErrors.lean:8:9-8:15: error: don't know how to synthesize implicit argument 'α'
99
@id ?m
1010
context:
1111
⊢ Sort u
1212
holeErrors.lean:8:4-8:5: error: failed to infer 'let' declaration type
13-
holeErrors.lean:7:11-9:1: error: failed to infer definition type
14-
holeErrors.lean:11:11-11:15: error: failed to infer definition type
15-
holeErrors.lean:11:8-11:9: error: failed to infer binder type
16-
holeErrors.lean:13:15-13:19: error: failed to infer definition type
17-
holeErrors.lean:13:12-13:13: error: failed to infer binder type
18-
holeErrors.lean:16:4-16:5: error: failed to infer binder type
19-
holeErrors.lean:15:7-16:10: error: failed to infer definition type
13+
holeErrors.lean:7:4-7:10: error: Failed to infer type of definition 'f3.{u}'
14+
holeErrors.lean:11:4-11:6: error: Failed to infer type of definition 'f4'
15+
holeErrors.lean:11:8-11:9: error: Failed to infer type of binder 'x'
16+
holeErrors.lean:13:4-13:6: error: Failed to infer type of definition 'f5'
17+
holeErrors.lean:13:8-13:9: error: Failed to infer type of binder 'x'
18+
holeErrors.lean:16:4-16:5: error: Failed to infer type of binder 'x'
19+
holeErrors.lean:15:4-15:6: error: Failed to infer type of definition 'f6'
2020
holeErrors.lean:19:13-19:19: error: don't know how to synthesize implicit argument 'α'
2121
@id ?m
2222
context:

tests/lean/holes.lean.expected.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ holes.lean:10:9-10:12: error: don't know how to synthesize implicit argument 'β
2020
context:
2121
x : Nat
2222
⊢ Type
23-
holes.lean:13:10-13:11: error: failed to infer binder type
24-
holes.lean:15:16-15:17: error: failed to infer binder type
23+
holes.lean:13:10-13:11: error: Failed to infer type of binder 'β'
24+
holes.lean:15:12-15:13: error: Failed to infer type of binder 'β'
2525
holes.lean:19:0-19:3: error: don't know how to synthesize implicit argument 'β'
2626
@f Nat (?m a) a
2727
context:
2828
a : Nat
2929
f : {α : Type} → {β : ?m a} → α → α := fun {α} {β} a => a
3030
⊢ ?m a
31-
holes.lean:18:9-18:10: error: failed to infer binder type
32-
holes.lean:21:25-22:4: error: failed to infer definition type
31+
holes.lean:18:9-18:10: error: Failed to infer type of binder 'β'
32+
holes.lean:21:12-21:14: error: Failed to infer type of definition 'f7'
3333
holes.lean:25:8-25:11: error: failed to infer 'let rec' declaration type
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
implicitTypePos.lean:1:8-1:9: error: failed to infer binder type
2-
implicitTypePos.lean:1:6-1:7: error: failed to infer binder type
3-
implicitTypePos.lean:3:9-3:10: error: failed to infer binder type
4-
implicitTypePos.lean:5:9-5:10: error: failed to infer binder type
5-
implicitTypePos.lean:7:10-7:11: error: failed to infer binder type
1+
implicitTypePos.lean:1:8-1:9: error: Failed to infer type of binder 'y'
2+
implicitTypePos.lean:1:6-1:7: error: Failed to infer type of binder 'x'
3+
implicitTypePos.lean:3:9-3:10: error: Failed to infer type of binder 'y'
4+
implicitTypePos.lean:5:9-5:10: error: Failed to infer type of binder 'β'
5+
implicitTypePos.lean:7:10-7:11: error: Failed to infer type of binder 'β'

tests/lean/interactive/2058.lean.expected.out

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
{"source": "Lean 4",
1515
"severity": 1,
1616
"range":
17-
{"start": {"line": 21, "character": 26},
18-
"end": {"line": 21, "character": 31}},
19-
"message": "failed to infer universe levels in binder type\n PUnit.{_}",
17+
{"start": {"line": 21, "character": 22},
18+
"end": {"line": 21, "character": 23}},
19+
"message":
20+
"Failed to infer universe levels in type of binder 'x'\n PUnit.{_}",
2021
"fullRange":
21-
{"start": {"line": 21, "character": 26},
22-
"end": {"line": 21, "character": 31}}},
22+
{"start": {"line": 21, "character": 22},
23+
"end": {"line": 21, "character": 23}}},
2324
{"source": "Lean 4",
2425
"severity": 1,
2526
"range":
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
multiConstantError.lean:1:11-1:12: error: failed to infer binder type
1+
multiConstantError.lean:1:11-1:12: error: Failed to infer type of binder 'c'
22
recall that you cannot declare multiple constants in a single declaration. The identifier(s) `b`, `c` are being interpreted as parameters `(b : _)`, `(c : _)`
3-
multiConstantError.lean:1:9-1:10: error: failed to infer binder type
3+
multiConstantError.lean:1:9-1:10: error: Failed to infer type of binder 'b'
44
recall that you cannot declare multiple constants in a single declaration. The identifier(s) `b`, `c` are being interpreted as parameters `(b : _)`, `(c : _)`
5-
multiConstantError.lean:3:9-3:10: error: failed to infer binder type
5+
multiConstantError.lean:3:9-3:10: error: Failed to infer type of binder 'α'
66
recall that you cannot declare multiple constants in a single declaration. The identifier(s) `α`, `β` are being interpreted as parameters `(α : _)`, `(β : _)`

tests/lean/run/2058.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Test: Works for `fun` binders.
6464
-/
6565

6666
/--
67-
error: failed to infer universe levels in binder type
67+
error: Failed to infer universe levels in type of binder 'x'
6868
PUnit.{_}
6969
-/
7070
#guard_msgs in

0 commit comments

Comments
 (0)