Skip to content

fix: reject declarations naming the kernel's _nested auxiliary types - #14616

Merged
leodemoura merged 1 commit into
masterfrom
_nested_abuse
Jul 31, 2026
Merged

leodemoura merged 1 commit into
masterfrom
_nested_abuse

Conversation

@leodemoura

Copy link
Copy Markdown
Member

This PR fixes a kernel bug: an inductive declaration could reference one of the auxiliary types the kernel generates when eliminating nested inductives, and end up with a stored constructor type that is ill typed. Such a declaration can only be produced with metaprogramming.

The auxiliary types live under _nested and exist only in the temporary environment used for the elimination. A constructor naming one is checked there, but restore_nested then rewrites the name back to the nested type, which can live in a different universe. Combined with proof irrelevance, the resulting ill-typed constructor makes two distinct functions look like proofs of the same proposition, and equiv_manager closes them transitively, giving False with no axiom dependencies.

The kernel now rejects the reserved prefix, alongside the existing checks for metavariables and free variables. Both Expr.const names and Expr.proj structure names are covered: the kernel rewrites nested occurrences in the constructor to the auxiliary type, so a projection can reach that type without the declaration naming it as a constant.

This PR fixes a kernel bug: an inductive declaration could name one of the auxiliary types
the kernel generates when eliminating nested inductives, and end up with a stored constructor
type that is ill typed. Such a declaration can only be produced with metaprogramming.

The auxiliary types live under `_nested` and exist only in the temporary environment
used for the elimination. A constructor naming one is checked there, but `restore_nested`
then rewrites the name back to the nested type, which can live in a different universe.
Combined with proof irrelevance, the resulting ill-typed constructor makes two distinct
functions look like proofs of the same proposition, and `equiv_manager` closes them transitively,
giving `False` with no axiom dependencies.

`environment::add_inductive` now rejects the reserved prefix, alongside the existing checks
for metavariables and free variables. Both `Expr.const` names and `Expr.proj` structure names
are covered: the kernel rewrites nested occurrences in the constructor to the auxiliary type,
so a projection can reach that type without the declaration naming it as a constant.
@leodemoura
leodemoura enabled auto-merge July 31, 2026 12:15
@leodemoura
leodemoura added this pull request to the merge queue Jul 31, 2026
@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Jul 31, 2026
@mathlib-lean-pr-testing

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 17dbc815cf3055e73a9e37d854b8427dc1e9c46c --onto 5b8d06c1764fa367a1e27b3e89f64667eac425af. You can force Mathlib CI using the force-mathlib-ci label. (2026-07-31 12:59:19)

@leanprover-bot

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 17dbc815cf3055e73a9e37d854b8427dc1e9c46c --onto a39eab69e1eee9ad38f4efe507907b1026a77808. You can force reference manual CI using the force-manual-ci label. (2026-07-31 12:59:21)

Merged via the queue into master with commit 35f6968 Jul 31, 2026
25 checks passed
leodemoura added a commit that referenced this pull request Jul 31, 2026
…ration

This PR makes the kernel re-check what it writes into the environment after eliminating a nested inductive. `restore_nested` rewrites the auxiliary types back into nested applications, and the resulting constructor types, recursor types, and recursor computation rules were added without being type checked.

The other checks in `add_inductive` are expected to make this redundant, and no declaration in the standard library or the test suite fails it. It is defense in depth against a mistake in the restoration reaching the environment: with the `_nested` prefix check from #14616 disabled, this check on its own rejects the reproducer for that issue, with the ill-typed application that was its root cause.

The inductive types themselves are added unchanged rather than restored, so they need no check. The cost is not measurable: on a file declaring 300 nested inductives, 5778/5532/5708/5731 ms with the check versus 5555/5598/5761/5780 ms without.

Co-Authored-By: Claude Fable 5 <[email protected]>
nomeata added a commit to leanprover/lean-kernel-arena that referenced this pull request Jul 31, 2026
Scaled-down regression test for the kernel bug fixed in
leanprover/lean4#14616. A declaration that names one of the kernel's
_nested auxiliary types must be rejected by a correct kernel (the fix
adds a reserved-prefix check in add_inductive, covering both Expr.const
names and Expr.proj structure names).

This is deliberately NOT a proof of False. The bug's axiom-free False
relies on transient in-kernel state (the equiv_manager defeq cache) that
is not reconstructed on a fresh replay of an export, so it cannot be
captured as an export-based arena test: the same buggy official kernel
that accepts the exploit in-memory rejects the exported form. Instead,
this mirrors the upstream test tests/elab/kernelNestedAuxName.lean.

tests/nested-aux-name.{lean,yaml} export a well-typed inductive KNAux
whose constructor field names the ordinary Prop-valued def
_nested.KNHost_1. Because KNAux is well-typed, an unguarded kernel
accepts it; a guarded kernel rejects it for the reserved _nested prefix.

Verified: official (v4.32.1, guard unreleased) accepts the exported
KNAux -- reported incorrect, as expected; flips to correct once the fix
ships. Uses metaprogramming (skipKernelTC), so a standalone test.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01N8V39PLAKbpoG7ctgCFhbp

Co-authored-by: Claude Fable 5 <[email protected]>
Garmelon pushed a commit that referenced this pull request Aug 3, 2026
#14616)

This PR fixes a kernel bug: an inductive declaration could reference one
of the auxiliary types the kernel generates when eliminating nested
inductives, and end up with a stored constructor type that is ill typed.
Such a declaration can only be produced with metaprogramming.

The auxiliary types live under `_nested` and exist only in the temporary
environment used for the elimination. A constructor naming one is
checked there, but `restore_nested` then rewrites the name back to the
nested type, which can live in a different universe. Combined with proof
irrelevance, the resulting ill-typed constructor makes two distinct
functions look like proofs of the same proposition, and `equiv_manager`
closes them transitively, giving `False` with no axiom dependencies.

The kernel now rejects the reserved prefix, alongside the existing
checks for metavariables and free variables. Both `Expr.const` names and
`Expr.proj` structure names are covered: the kernel rewrites nested
occurrences in the constructor to the auxiliary type, so a projection
can reach that type without the declaration naming it as a constant.
keithadler added a commit to keithadler/tenet that referenced this pull request Sep 27, 2026
Divergence 3 and the negative corpus said Lean reserves the whole _nested
namespace and Tenet does the same. It does not. leanprover/lean4#14616 adds
check_no_nested_aux to add_inductive, which rejects an inductive whose type
or constructor types mention a _nested constant or proj structure name, and
nothing more. reserved-namespace.ndjson, which declares axiom
_nested.squatter, comes back ok=1 failed=0 through tools/leancheck on Lean
4.34.1. mk_unique_name skips names already taken, so a squatter does not
disturb the elimination either.

Tenet already makes that check, in Inductive.CheckNoNestedAux. Reserving
every name under the prefix is on top of it, so Tenet rejects a file Lean
accepts. The entry now says so and keeps the reservation as a deliberate
strictness divergence, with its cost stated. The overview no longer counts
it among the entries where Tenet is closer to the type theory, since the
theory has nothing to say about names. The manifest and the comments on
NestedPrefix and its test, which repeated the claim, are corrected too.
Kernel behaviour is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
kim-em added a commit to kim-em/lean4lean that referenced this pull request Oct 12, 2026
`environment::add_inductive` runs `check_no_nested_aux` on the inductive
types as well as on the constructor types (leanprover/lean4#14616); do
the same in `checkInductiveSources` and retire the divergence entry (the
kernel does not otherwise reserve the `_nested` namespace). The proofs
discard the new check's result. `Tests/KernelHardening.lean` checks that
both kernels reject an inductive type naming `_nested` for the reserved
prefix.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-other toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants