lean4-htt/tests/lean/struct1.lean
Kyle Miller 3acd77a154
fix: make elabTermEnsuringType respect errToSorry when there is a type mismatch (#3633)
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`.
2024-03-09 15:30:47 +00:00

56 lines
986 B
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

--
structure A (α : Type) :=
(x : α)
structure B (α : Type) :=
(x : α)
structure S : Nat := -- error expected Type
(x : Nat)
structure S extends Nat → Nat := -- error expected structure
(x : Nat)
set_option structureDiamondWarning true in
structure S' extends A Nat, A Bool := -- error field toA already declared
(x : Nat)
structure S extends A Nat, B Bool := -- error field `x` from `B` has already been declared
(x : Nat)
structure S1 :=
(_x : Nat)
structure S2 :=
(x _y : Nat)
structure S :=
(x : Nat)
(x : Nat) -- error
structure S extends A Nat :=
(x : Nat) -- error
structure S' extends A Nat :=
(x := true) -- error type mismatch
structure S extends A Nat :=
(x : Bool := true) -- error omit type
structure S'' :=
(x : Nat := true) -- error type mismatch
private structure S :=
private mk :: (x : Nat)
private structure S :=
protected mk :: (x : Nat)
private structure S :=
protected (x : Nat)
private structure S :=
mk2 :: (x : Nat)
#check S
#check S.mk2