fix: .. in patterns should not make use of optparams or autoparams (#5933)

In patterns, ellipsis should always fill in each remaining argument as
an implicit argument, even if it is an optparam or autoparam. This
prevents examples such as the one in #4555 from failing:
```lean
match e with
| .internal .. => sorry
| .error .. => sorry
```
The `internal` constructor has an optparam (`| internal (id :
InternalExceptionId) (extra : KVMap := {})`).

We may consider having ellipsis suppress optparams and autoparams in
general. We avoid doing so for now since it's possible to opt-out of
them individually (for example with `.internal (extra := _) ..`) but
it's not possible to opt-in, and it is plausible that `..` with
optparams is useful in contexts such as the `refine` tactic. With
patterns however, it is hard to imagine a use case that offsets the
inconvenience of optparams being eagerly supplied.

Closes #4555
This commit is contained in:
Kyle Miller 2024-11-03 10:40:21 -08:00 committed by GitHub
parent 87d3f1f2c8
commit 1659f3bfe2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 0 deletions

View file

@ -595,6 +595,22 @@ mutual
elabAndAddNewArg argName arg
main
| _ =>
if (← read).ellipsis && (← readThe Term.Context).inPattern then
/-
In patterns, ellipsis should always be an implicit argument, even if it is an optparam or autoparam.
This prevents examples such as the one in #4555 from failing:
```lean
match e with
| .internal .. => sorry
| .error .. => sorry
```
The `internal` has an optparam (`| internal (id : InternalExceptionId) (extra : KVMap := {})`).
We may consider having ellipsis suppress optparams and autoparams in general.
We avoid doing so for now since it's possible to opt-out of them (for example with `.internal (extra := _) ..`)
but it's not possible to opt-in.
-/
return ← addImplicitArg argName
let argType ← getArgExpectedType
match (← read).explicit, argType.getOptParamDefault?, argType.getAutoParamTactic? with
| false, some defVal, _ => addNewArg argName defVal; main

16
tests/lean/run/4555.lean Normal file
View file

@ -0,0 +1,16 @@
import Lean.Exception
/-!
# In pattern, `..` shouldn't make use of opt-params
https://github.com/leanprover/lean4/issues/4555
-/
open Lean
/-!
The `.internal` constructor has an opt-param, which caused this to be a non-exhaustive match.
-/
example (e : Exception) : Nat :=
match e with
| .internal .. => 0
| .error .. => 1