Unhelpful error when inferred type of parameter depends on succeeding parameter
Nobody has claimed this yet.
- Dominant language
- Lean
- Stars
- 9.2k
- Forks
- 990
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 175
Description
Prerequisites
Please put an X between the brackets as you perform the following steps:
- Check that your issue is not already filed:
https://github.com/leanprover/lean4/issues - Reduce the issue to a minimal, self-contained, reproducible test case.
Avoid dependencies to Mathlib or Batteries. - Test your test case against the latest nightly release, for example on
https://live.lean-lang.org/#project=lean-nightly
(You can also use the settings there to switch to “Lean nightly”)
Description
When the expected type of a parameter depends on another parameter that comes after it, this understandably leads to a type error. But the error message is his very misleading, especially when using auto-implicits.
Steps to Reproduce
Run this:
def f (n : Nat) (xs : Vector Nat n) := xs = xs
/--
error: Application type mismatch: The argument
xs
has type
?m.1
but is expected to have type
Vector Nat n
in the application
f n xs
-/
#guard_msgs in
example (n : Nat) (h : f n xs) : Unit := ()
Expected behavior:
Lean detects that the type of xs depends on n and clearly explains to the user that xs must be declared after n. If xs is an auto-implicit, Lean tells the user to declare xs by hand because auto-implicits are always placed in front of all other parameters.
Deluxe version: In addition to improved error messages, Lean automatically places auto-implicits after their type dependencies instead of always at front position.
Actual behavior:
Lean produces an error message that is not helpful. The error message does not explain why Vector Nat n cannot be assigned to the metavariable ?m.1, and it is impossible to infer from the error message that n is not in the environment of the metavariable.
Moreover, no inlay hints for auto-implicits are shown in the presence of this error, making it even harder to figure out that the error has to do with the placement of the auto-implicits.
Versions
Lean 4.24.0-nightly-2025-08-21
Target: x86_64-unknown-linux-gnu
Impact
Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the minimal reproducer against the specified Lean nightly release or live. Trace how the dependent parameter and auto-implicit are processed, then verify that the diagnostic explains the dependency and placement problem and that inlay hints remain available in this error case.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100