leanprover / leanprover/lean4

Unhelpful error when inferred type of parameter depends on succeeding parameter

Open
#10,054 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P-medium
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:

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.