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

Aperta
#16,490 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Ambito
compilers

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Lean
Stelle
10.3k
Fork
1.1k
Merge medio
19h 8m
PR unite (30g)
421

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Compilers

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.