Floris van Doorn [reported on Zulip](https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/have.20tactic.20error.20recovery/near/425283053) that it is confusing that the `have : T := e` tactic completely fails if the body `e` is not of type `T`. This is in contrast to `have : T := by exact e`, which does not completely fail when `e` is not of type `T`. This ends up being caused by `elabTermEnsuringType` throwing an error when it fails to insert a coercion. Now, it detects this case, and it checks the `errToSorry` flag to decide whether to throw the error or to log the error and insert a `sorry`. This is justified by `elabTermEnsuringType` being a frontend to `elabTerm`, which inserts `sorry` on error. An alternative would be to make `ensureType` respect `errToSorry`, but there exists code that expects being able to catch when `ensureType` fails. Making such code manipulate `errToSorry` seems error prone, and this function is not a main entry point to the term elaborator, unlike `elabTermEnsuringType`.
24 lines
388 B
Text
24 lines
388 B
Text
/-!
|
|
# Tests for the `have` tactic.
|
|
-/
|
|
|
|
/-!
|
|
If the body of a `have` fails to elaborate, the tactic completes with a `sorry` for the proof.
|
|
-/
|
|
/--
|
|
error: type mismatch
|
|
False.elim
|
|
has type
|
|
False → ?m.6 : Sort ?u.5
|
|
but is expected to have type
|
|
True : Prop
|
|
---
|
|
info: h : True
|
|
⊢ True
|
|
-/
|
|
#guard_msgs in
|
|
example : True := by
|
|
have h : True :=
|
|
False.elim
|
|
trace_state
|
|
assumption
|