Every inductive type gets an `unsafe` declaration `T.ctorIdx._impl`

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

Maintainers usually reply within 1 day

Nobody has claimed this yet.

  • #16324 by @vincentqb — closed without merging

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
39/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Domain
compilers

Research direction

Read the changes associated with #15407 and #16253, then inspect how inductive declarations generate T.ctorIdx and T.ctorIdx._impl. The issue reports that #16324 is a closed, unmerged pull request, so check its changes and status first. Done means safe-only files can use inductive types with multiple constructors without receiving the unwanted unsafe implementation or implemented_by attribute.

Written by the indexing model from the issue text.

Description

Since v4.36.0-rc1, declaring an inductive type T with more than one constructor also adds unsafe def T.ctorIdx._impl and marks T.ctorIdx with @[implemented_by T.ctorIdx._impl]. Files with only safe code therefore contain unsafe code, and tools that refuse unsafe or @[implemented_by] in the code they check refuse every such type. The values are correct, and v4.35.0 added neither.

inductive T | a | b (n : Nat)
#print T.ctorIdx._impl
-- v4.35.0: Unknown constant `T.ctorIdx._impl`
-- v4.36.0-rc2, nightly-2026-11-06: unsafe protected def T.ctorIdx._impl : T → Nat := fun x => getObjTagNat x

Related:

  • #9951 generates T.ctorIdx for every inductive type.
  • #15407 added T.ctorIdx._impl so that T.ctorIdx reads the constructor tag.
  • #16253 made withPtrEq safe by letting the compiler substitute its unsafe implementation, which is how T.ctorIdx can read the tag without _impl.
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.