Kernel: nested-inductive auxiliary name `_nested_1` can be referenced by user declarations when the nested type's name is anonymous/macro-scoped

Open
#16,490 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
compilers

Research direction

Start with src/kernel/inductive.cpp, especially elim_nested_inductive_fn::replace_if_nested and check_no_nested_aux, which the issue identifies as the name creation and validation paths. Reproduce the case with the supplied Lean file, then trace how anonymous and macro-scoped names are minted and checked. Done means addDeclCore rejects the reproduced declaration with an unknown constant error.

Written by the indexing model from the issue text.

Description

Prerequisites
  • 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
  • When reporting a perceived soundness bug: Does it affect lake check --paranoid?
    (Not believed to be a soundness bug: no inconsistency was derived.)
Description

The kernel accepts an inductive T whose constructor T.bad : _nested_1 → T refers to a
constant that does not exist, and stores T.bad with a different type than declared.

When T is nested (T.mk : J T → T), the kernel replaces J T by an auxiliary type it
names _nested + J and declares that name in a temporary environment. User declarations
are kept from naming these auxiliaries by check_no_nested_aux, which rejects any constant
whose name has the prefix _nested. But if J's name is anonymous (as here) or
macro-scoped (e.g. [email protected]._hyg.1, which macro-generated inductives have), the minted name is
_nested_1 or [email protected]._hyg.1. Neither has _nested as a prefix, so the user can
name it, it resolves to the auxiliary during checking, and the restore pass then rewrites it
to J T.

Reachable through addDecl or a crafted .olean (the elaborator does not create an
anonymous inductive). No inconsistency was derived; the environment ends up containing a
constructor whose declared type mentioned a constant that is declared nowhere.

Steps to Reproduce

Save as NestedAuxUndeclaredConstant.lean and run lean NestedAuxUndeclaredConstant.lean:

import Lean
open Lean Elab Command

-- inductive <anonymous> (α : Type) : Type | mk : α → <anonymous> α
def declJ : Declaration := .inductDecl [] 1 [{
  name := .anonymous, type := .forallE `α (.sort 1) (.sort 1) .default,
  ctors := [{ name := `AnonMk,
              type := .forallE `α (.sort 1) (.forallE `a (.bvar 0) (.app (.const .anonymous []) (.bvar 1)) .default) .implicit }] }] false

-- inductive T : Type | mk : <anonymous> T → T | bad : _nested_1 → T
def declT : Declaration := .inductDecl [] 0 [{
  name := `T, type := .sort 1,
  ctors := [{ name := `T.mk,  type := .forallE `x (.app (.const .anonymous []) (.const `T [])) (.const `T []) .default },
            { name := `T.bad, type := .forallE `x (.const `_nested_1 []) (.const `T []) .default }] }] false

#eval show CommandElabM Unit from do
  let .ok env := (← getEnv).addDeclCore 0 512 declJ none | throwError "J rejected"
  match env.addDeclCore 0 512 declT none with
  | .error e => logInfo m!"T rejected: {e.toMessageData {}}"
  | .ok env =>
    logInfo m!"T ACCEPTED, although its constructor T.bad mentions the undeclared constant _nested_1"
    logInfo m!"_nested_1 declared afterwards? {(env.find? `_nested_1).isSome}"
    logInfo m!"T.bad stored as: {(env.find? `T.bad).get!.type}"

Expected output if the kernel were correct: T rejected: unknown constant '_nested_1'. Actual output (Lean 4.35.0, and 4.37.0-nightly-2026-11-14, commit 62e0cf0):

Actual behavior:

T ACCEPTED, although its constructor T.bad mentions the undeclared constant _nested_1
_nested_1 declared afterwards? false
T.bad stored as: [anonymous] T → T
Versions

Lean 4.35.0, 4.36.0 (arm64-apple-darwin, macOS / Darwin 25.6.0), and 4.37.0-nightly-2026-11-14 (commit 62e0cf0).

Additional Information

Where it is in the kernel (leanprover/lean4 master, commit e048ef7, 2026-11-16):

src/kernel/inductive.cpp

  • Line 1093, elim_nested_inductive_fn::replace_if_nested:

    name auxJ_name = mk_unique_name(*g_nested + J_name);
    

    mk_unique_name (line 989) appends an index with append_after, which for an anonymous
    J_name produces the atomic name _nested_1, and for a macro-scoped name puts the index
    before the macro scopes. Neither result is under the prefix _nested.

  • Lines 1210-1219, check_no_nested_aux: tests is_prefix_of(*g_nested, const_name(e)),
    which is false for these names, so the user's reference is not rejected.

Possible fix: after minting auxJ_name, check is_prefix_of(*g_nested, auxJ_name) and, if it
fails, mint the name under a fixed prefix such as _nested._aux instead.

Dominant language
Lean
Stars
10.3k
Forks
1.1k
Avg merge
19h 8m
Merged PRs (30d)
421

Getting set up

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.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Compilers issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.