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

オープン
#16,490 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
領域
compilers

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Lean
スター
10.3k
フォーク
1.1k
平均マージ
19時間 8分
マージ済み PR(30日)
421

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

leanprover/lean4 のほかの issue

leanprover/lean4 の issue をすべて見る

似ている issue

Compilers の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。