Visibility of section variables is not verified correctly
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
In some cases, a section variable can be used in a theorem even if its type is private. In some other cases, Lean correctly rejects this, but the error message is confusing.
Steps to Reproduce
Run this:
module
import Std.Data.HashMap
structure X
variable {m : Std.HashMap Nat Nat} {n : X}
/--
error: Unknown constant `Std.HashMap`
Note: A public declaration `Std.HashMap` exists but is imported privately; consider adding `public import Std.Data.HashMap.Basic`.
---
error: Unknown constant `Std.HashMap`
Note: A public declaration `Std.HashMap` exists but is imported privately; consider adding `public import Std.Data.HashMap.Basic`.
-/
#guard_msgs in
public theorem a : m = m := rfl
#guard_msgs in
public theorem b : n = n := rfl
Expected behavior:
Theorems a and b should fail because they are relying on section variables with types that are not publicly visible.
In theorem a, the error message should explain that there is a problem with a section variable.
Actual behavior:
Theorem a fails, but the error message appears at the two m's in m = m. This is confusing: The theorem's signature doesn't explicitly reference the unknown constant Std.HashMap. It would be much better if the error message said something like: "The section variable m's type Std.HashMap Nat Nat is not visible in the public scope for the following reason: Unknown constant Std.HashMap [...]"
Theorem b succeeds even though n is of type X, which is a private constant.
Versions
Lean 4.25.0-nightly-2025-10-12
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
Run the supplied minimal reproducer against the Lean nightly release and compare the behavior and diagnostics for the public theorems a and b. Trace the section-variable visibility checking involved in this example; done means both declarations reject non-public types and theorem a reports the section variable and underlying visibility reason clearly.
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
- 42/100