diff --git a/src/Lean/Elab/Binders.lean b/src/Lean/Elab/Binders.lean index 2f260e0d10..cd0b75cba2 100644 --- a/src/Lean/Elab/Binders.lean +++ b/src/Lean/Elab/Binders.lean @@ -178,9 +178,13 @@ private def toBinderViews (stx : Syntax) : TermElabM (Array BinderView) := do else throwUnsupportedSyntax -private def registerFailedToInferBinderTypeInfo (type : Expr) (ref : Syntax) : TermElabM Unit := do - registerCustomErrorIfMVar type ref "failed to infer binder type" - registerLevelMVarErrorExprInfo type ref m!"failed to infer universe levels in binder type" +private def registerFailedToInferBinderTypeInfo (type : Expr) (view : BinderView) : TermElabM Unit := do + let msg := if view.id.getId.hasMacroScopes then + m!"binder type" + else + m!"type of binder `{view.id.getId}`" + registerCustomErrorIfMVar type view.ref m!"Failed to infer {msg}" + registerLevelMVarErrorExprInfo type view.ref m!"Failed to infer universe levels in {msg}" def addLocalVarInfo (stx : Syntax) (fvar : Expr) : TermElabM Unit := addTermInfo' (isBinder := true) stx fvar @@ -212,7 +216,7 @@ private partial def elabBinderViews (binderViews : Array BinderView) (fvars : Ar let binderView := binderViews[i] ensureAtomicBinderName binderView let type ← elabType binderView.type - registerFailedToInferBinderTypeInfo type binderView.type + registerFailedToInferBinderTypeInfo type binderView if binderView.bi.isInstImplicit && checkBinderAnnotations.get (← getOptions) then unless (← isClass? type).isSome do throwErrorAt binderView.type (m!"invalid binder annotation, type is not a class instance{indentExpr type}" ++ .note "Use the command `set_option checkBinderAnnotations false` to disable the check") @@ -423,7 +427,7 @@ private partial def elabFunBinderViews (binderViews : Array BinderView) (i : Nat ensureAtomicBinderName binderView withRef binderView.type <| withLCtx s.lctx s.localInsts do let type ← elabType binderView.type - registerFailedToInferBinderTypeInfo type binderView.type + registerFailedToInferBinderTypeInfo type binderView let fvarId ← mkFreshFVarId let fvar := mkFVar fvarId let s := { s with fvars := s.fvars.push fvar } diff --git a/src/Lean/Elab/MutualDef.lean b/src/Lean/Elab/MutualDef.lean index e5c13fe20b..5f3bb98db8 100644 --- a/src/Lean/Elab/MutualDef.lean +++ b/src/Lean/Elab/MutualDef.lean @@ -124,15 +124,21 @@ private def check (prevHeaders : Array DefViewElabHeader) (newHeader : DefViewEl else pure () -private def registerFailedToInferDefTypeInfo (type : Expr) (ref : Syntax) (view : DefView) : TermElabM Unit := - let msg := if view.kind.isExample then - m!"failed to infer type of example" - else if view.kind matches .instance then - -- TODO: instances are sometime named. We should probably include the name if available. - m!"failed to infer type of instance" - else - m!"failed to infer type of `{view.declId}`" - registerCustomErrorIfMVar type ref msg +private def registerFailedToInferDefTypeInfo (type : Expr) (view : DefView) : + TermElabM Unit := + let ref := view.type?.getD <| match view.kind with + -- Use def name (or keyword if anonymous) as fallback location + | .example => view.ref[0] + | .instance => view.ref[1] + | _ => view.declId + let msg := match view.kind with + | .example => m!"example" + | .instance => if view.declId.getHeadInfo matches .original .. then + m!"instance `{view.declId}`" + else m!"instance" + | .theorem => m!"theorem `{view.declId}`" + | _ => m!"definition `{view.declId}`" + registerCustomErrorIfMVar type ref m!"Failed to infer type of {msg}" /-- Return `some [b, c]` if the given `views` are representing a declaration of the form @@ -233,13 +239,13 @@ private def elabHeaders (views : Array DefView) (expandedDeclIds : Array ExpandD let mut type ← match view.type? with | some typeStx => let type ← elabType typeStx - registerFailedToInferDefTypeInfo type typeStx view + registerFailedToInferDefTypeInfo type view pure type | none => let hole := mkHole refForElabFunType let type ← elabType hole trace[Elab.definition] ">> type: {type}\n{type.mvarId!}" - registerFailedToInferDefTypeInfo type refForElabFunType view + registerFailedToInferDefTypeInfo type view pure type Term.synthesizeSyntheticMVarsNoPostponing if view.isInstance then diff --git a/tests/lean/331.lean.expected.out b/tests/lean/331.lean.expected.out index 7a420631d5..953ec1be0a 100644 --- a/tests/lean/331.lean.expected.out +++ b/tests/lean/331.lean.expected.out @@ -1,6 +1,6 @@ -331.lean:6:13-6:14: error: failed to infer binder type +331.lean:6:9-6:10: error: Failed to infer type of binder `x` Note: 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 -331.lean:7:13-7:14: error: failed to infer binder type +331.lean:7:9-7:10: error: Failed to infer type of binder `x` Note: 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 diff --git a/tests/lean/autoImplicitCtorParamIssue.lean.expected.out b/tests/lean/autoImplicitCtorParamIssue.lean.expected.out index b5da0ccdd2..ccc35cd50a 100644 --- a/tests/lean/autoImplicitCtorParamIssue.lean.expected.out +++ b/tests/lean/autoImplicitCtorParamIssue.lean.expected.out @@ -1,3 +1,3 @@ -autoImplicitCtorParamIssue.lean:1:9-1:10: error: failed to infer binder type -autoImplicitCtorParamIssue.lean:4:9-4:10: error: failed to infer binder type -autoImplicitCtorParamIssue.lean:7:7-7:8: error: failed to infer binder type +autoImplicitCtorParamIssue.lean:1:9-1:10: error: Failed to infer type of binder `x` +autoImplicitCtorParamIssue.lean:4:9-4:10: error: Failed to infer type of binder `x` +autoImplicitCtorParamIssue.lean:7:7-7:8: error: Failed to infer type of binder `x` diff --git a/tests/lean/holeErrors.lean.expected.out b/tests/lean/holeErrors.lean.expected.out index a6a5bfb5e5..076f73d547 100644 --- a/tests/lean/holeErrors.lean.expected.out +++ b/tests/lean/holeErrors.lean.expected.out @@ -2,8 +2,8 @@ holeErrors.lean:3:14-3:20: error: don't know how to synthesize implicit argument @id ?m context: ⊢ Sort u -holeErrors.lean:3:11-3:20: error: failed to infer type of `f1.{u}` -holeErrors.lean:5:9-5:10: error: failed to infer type of `f2` +holeErrors.lean:3:4-3:10: error: Failed to infer type of definition `f1.{u}` +holeErrors.lean:5:9-5:10: error: Failed to infer type of definition `f2` Note: 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 holeErrors.lean:8:9-8:15: error: don't know how to synthesize implicit argument 'α' @@ -11,13 +11,13 @@ holeErrors.lean:8:9-8:15: error: don't know how to synthesize implicit argument context: ⊢ Sort u holeErrors.lean:8:4-8:5: error: failed to infer 'let' declaration type -holeErrors.lean:7:11-9:1: error: failed to infer type of `f3.{u}` -holeErrors.lean:11:11-11:15: error: failed to infer type of `f4` -holeErrors.lean:11:8-11:9: error: failed to infer binder type -holeErrors.lean:13:15-13:19: error: failed to infer type of `f5` -holeErrors.lean:13:12-13:13: error: failed to infer binder type -holeErrors.lean:16:4-16:5: error: failed to infer binder type -holeErrors.lean:15:7-16:10: error: failed to infer type of `f6` +holeErrors.lean:7:4-7:10: error: Failed to infer type of definition `f3.{u}` +holeErrors.lean:11:4-11:6: error: Failed to infer type of definition `f4` +holeErrors.lean:11:8-11:9: error: Failed to infer type of binder `x` +holeErrors.lean:13:4-13:6: error: Failed to infer type of definition `f5` +holeErrors.lean:13:8-13:9: error: Failed to infer type of binder `x` +holeErrors.lean:16:4-16:5: error: Failed to infer type of binder `x` +holeErrors.lean:15:4-15:6: error: Failed to infer type of definition `f6` holeErrors.lean:19:13-19:19: error: don't know how to synthesize implicit argument 'α' @id ?m context: diff --git a/tests/lean/holes.lean.expected.out b/tests/lean/holes.lean.expected.out index c151f9808a..1a5ebfdffe 100644 --- a/tests/lean/holes.lean.expected.out +++ b/tests/lean/holes.lean.expected.out @@ -20,14 +20,14 @@ holes.lean:10:9-10:12: error: don't know how to synthesize implicit argument 'β context: x : Nat ⊢ Type -holes.lean:13:10-13:11: error: failed to infer binder type -holes.lean:15:16-15:17: error: failed to infer binder type +holes.lean:13:10-13:11: error: Failed to infer type of binder `β` +holes.lean:15:12-15:13: error: Failed to infer type of binder `β` holes.lean:19:0-19:3: error: don't know how to synthesize implicit argument 'β' @f Nat (?m a) a context: a : Nat f : {α : Type} → {β : ?m a} → α → α := ⋯ ⊢ ?m a -holes.lean:18:9-18:10: error: failed to infer binder type -holes.lean:21:25-22:4: error: failed to infer type of `f7` +holes.lean:18:9-18:10: error: Failed to infer type of binder `β` +holes.lean:21:12-21:14: error: Failed to infer type of definition `f7` holes.lean:25:8-25:11: error: failed to infer 'let rec' declaration type diff --git a/tests/lean/implicitTypePos.lean.expected.out b/tests/lean/implicitTypePos.lean.expected.out index 67464413a8..ba2a3c3c6d 100644 --- a/tests/lean/implicitTypePos.lean.expected.out +++ b/tests/lean/implicitTypePos.lean.expected.out @@ -1,5 +1,5 @@ -implicitTypePos.lean:1:8-1:9: error: failed to infer binder type -implicitTypePos.lean:1:6-1:7: error: failed to infer binder type -implicitTypePos.lean:3:9-3:10: error: failed to infer binder type -implicitTypePos.lean:5:9-5:10: error: failed to infer binder type -implicitTypePos.lean:7:10-7:11: error: failed to infer binder type +implicitTypePos.lean:1:8-1:9: error: Failed to infer type of binder `y` +implicitTypePos.lean:1:6-1:7: error: Failed to infer type of binder `x` +implicitTypePos.lean:3:9-3:10: error: Failed to infer type of binder `y` +implicitTypePos.lean:5:9-5:10: error: Failed to infer type of binder `β` +implicitTypePos.lean:7:10-7:11: error: Failed to infer type of binder `β` diff --git a/tests/lean/interactive/2058.lean.expected.out b/tests/lean/interactive/2058.lean.expected.out index a790588354..c0068b8b33 100644 --- a/tests/lean/interactive/2058.lean.expected.out +++ b/tests/lean/interactive/2058.lean.expected.out @@ -14,12 +14,13 @@ {"source": "Lean 4", "severity": 1, "range": - {"start": {"line": 21, "character": 26}, - "end": {"line": 21, "character": 31}}, - "message": "failed to infer universe levels in binder type\n PUnit.{_}", + {"start": {"line": 21, "character": 22}, + "end": {"line": 21, "character": 23}}, + "message": + "Failed to infer universe levels in type of binder `x`\n PUnit.{_}", "fullRange": - {"start": {"line": 21, "character": 26}, - "end": {"line": 21, "character": 31}}}, + {"start": {"line": 21, "character": 22}, + "end": {"line": 21, "character": 23}}}, {"source": "Lean 4", "severity": 1, "range": diff --git a/tests/lean/multiConstantError.lean.expected.out b/tests/lean/multiConstantError.lean.expected.out index ad5bc59b29..f5fa211466 100644 --- a/tests/lean/multiConstantError.lean.expected.out +++ b/tests/lean/multiConstantError.lean.expected.out @@ -1,9 +1,9 @@ -multiConstantError.lean:1:11-1:12: error: failed to infer binder type +multiConstantError.lean:1:11-1:12: error: Failed to infer type of binder `c` Note: Recall that you cannot declare multiple constants in a single declaration. The identifier(s) `b`, `c` are being interpreted as parameters `(b : _)`, `(c : _)`. -multiConstantError.lean:1:9-1:10: error: failed to infer binder type +multiConstantError.lean:1:9-1:10: error: Failed to infer type of binder `b` Note: Recall that you cannot declare multiple constants in a single declaration. The identifier(s) `b`, `c` are being interpreted as parameters `(b : _)`, `(c : _)`. -multiConstantError.lean:3:9-3:10: error: failed to infer binder type +multiConstantError.lean:3:9-3:10: error: Failed to infer type of binder `α` Note: Recall that you cannot declare multiple constants in a single declaration. The identifier(s) `α`, `β` are being interpreted as parameters `(α : _)`, `(β : _)`. diff --git a/tests/lean/run/2058.lean b/tests/lean/run/2058.lean index dc645cf234..42a9be964e 100644 --- a/tests/lean/run/2058.lean +++ b/tests/lean/run/2058.lean @@ -64,7 +64,7 @@ Test: Works for `fun` binders. -/ /-- -error: failed to infer universe levels in binder type +error: Failed to infer universe levels in type of binder `x` PUnit.{_} -/ #guard_msgs in diff --git a/tests/lean/run/2226.lean b/tests/lean/run/2226.lean index 71c5f311db..01afdccf46 100644 --- a/tests/lean/run/2226.lean +++ b/tests/lean/run/2226.lean @@ -6,7 +6,7 @@ A : Nat #guard_msgs in variable (A : Nat) (B : by skip) -/-- error: failed to infer type of `foo` -/ +/-- error: Failed to infer type of definition `foo` -/ #guard_msgs in def foo := A = B diff --git a/tests/lean/run/5475.lean b/tests/lean/run/5475.lean index f25557564e..2e112542f1 100644 --- a/tests/lean/run/5475.lean +++ b/tests/lean/run/5475.lean @@ -16,7 +16,7 @@ error: don't know how to synthesize implicit argument 'α' context: ⊢ Type --- -error: failed to infer type of example +error: Failed to infer type of example -/ #guard_msgs in example := diff --git a/tests/lean/run/forInColErr.lean b/tests/lean/run/forInColErr.lean index f75ecf3673..51143304b4 100644 --- a/tests/lean/run/forInColErr.lean +++ b/tests/lean/run/forInColErr.lean @@ -21,7 +21,7 @@ error: don't know how to synthesize implicit argument 'ρ' context: ⊢ Type _ --- -error: failed to infer binder type +error: Failed to infer type of binder `x` --- error: don't know how to synthesize implicit argument 'α' @List.nil ?_ @@ -63,9 +63,9 @@ error: don't know how to synthesize implicit argument 'ρ' context: ⊢ Type _ --- -error: failed to infer binder type +error: Failed to infer type of binder `h` --- -error: failed to infer binder type +error: Failed to infer type of binder `x` --- error: don't know how to synthesize implicit argument 'α' @List.nil ?_ diff --git a/tests/lean/run/inferTypeFailure.lean b/tests/lean/run/inferTypeFailure.lean new file mode 100644 index 0000000000..0a9c1c2029 --- /dev/null +++ b/tests/lean/run/inferTypeFailure.lean @@ -0,0 +1,38 @@ +/-! Check messages of binder type inference failures. -/ + +/-- +error: Failed to infer type of binder `y` +--- +error: Failed to infer type of binder `x` +--- +error: Failed to infer type of definition `l1` +-/ +#guard_msgs in +def l1 := fun x y => x + +/-- +error: Failed to infer binder type +--- +error: Failed to infer type of definition `l2` +-/ +#guard_msgs in +def l2 := fun _ => 0 + +/-- +error: Failed to infer type of theorem `t` + +Note: All holes (e.g., `_`) in the header of a theorem are resolved before the proof is processed; information from the proof cannot be used to infer what these values should be +--- +error: type of theorem 't' is not a proposition + ?m.65 +-/ +#guard_msgs in +theorem t : _ := _ + +/-- +error: Failed to infer type of example + +Note: 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 +-/ +#guard_msgs in +example : _ := _ diff --git a/tests/lean/run/structBinderUpdates.lean b/tests/lean/run/structBinderUpdates.lean index 088f3e072d..9b90269707 100644 --- a/tests/lean/run/structBinderUpdates.lean +++ b/tests/lean/run/structBinderUpdates.lean @@ -118,7 +118,7 @@ end Ex4 Here, `(α β)` is not an override since `β` is not an existing parameter, so `α` is treated as a binder. -/ namespace Ex5 -/-- error: failed to infer binder type -/ +/-- error: Failed to infer type of binder `α` -/ #guard_msgs in class C (α : Type) where f (α β) : β @@ -128,7 +128,7 @@ end Ex5 Here, `(α β)` is not an override since `β` is a field. -/ namespace Ex6 -/-- error: failed to infer binder type -/ +/-- error: Failed to infer type of binder `α` -/ #guard_msgs in class C (α : Type) where β : Type diff --git a/tests/lean/run/type_as_hole.lean b/tests/lean/run/type_as_hole.lean index f6bac39900..a3c71dc0f7 100644 --- a/tests/lean/run/type_as_hole.lean +++ b/tests/lean/run/type_as_hole.lean @@ -1,17 +1,19 @@ +set_option pp.mvars false + /-- -error: failed to infer type of `foo` +error: Failed to infer type of theorem `foo` Note: All holes (e.g., `_`) in the header of a theorem are resolved before the proof is processed; information from the proof cannot be used to infer what these values should be --- error: type of theorem 'foo' is not a proposition - ?m.2 + ?_ -/ #guard_msgs (error) in theorem foo : _ := sorry /-- -error: failed to infer type of example +error: Failed to infer type of example Note: 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 -/ @@ -20,7 +22,7 @@ example : _ := sorry /-- -error: failed to infer type of `boo` +error: Failed to infer type of definition `boo` Note: 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 -/ @@ -29,10 +31,19 @@ def boo : _ := sorry /-- -error: failed to infer type of instance +error: Failed to infer type of instance `boo` Note: 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 -/ #guard_msgs (error) in instance boo : _ := sorry + +/-- +error: Failed to infer type of instance + +Note: 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 +-/ +#guard_msgs (error) in +instance : _ := + sorry