Skip to content

fix: generate ctorIdx without an unsafe implementation - #16324

Closed
vincentqb wants to merge 1 commit into
leanprover:masterfrom
vincentqb:fix/ctoridx-tag-in-compiler
Closed

vincentqb wants to merge 1 commit into
leanprover:masterfrom
vincentqb:fix/ctoridx-tag-in-compiler

Conversation

@vincentqb

Copy link
Copy Markdown
Contributor

This PR stops adding an unsafe declaration T.ctorIdx._impl and an @[implemented_by] attribute to every inductive type T with more than one constructor. #15407 added them so that T.ctorIdx reads the constructor tag; the code generator now emits that read itself, so the compiled code stays the same.

toLCNF compiles a casesOn whose alternatives each return the index of their constructor as a raw literal, which is the body of T.ctorIdx, to getObjTagNat, the way #16253 substitutes withPtrEqUnsafe for withPtrEq. The conditions are those of #15407: the type has more than one constructor and is not a runtime builtin type such as Nat. The index is then the tag that every compiled cases on the type already switches on, so the rewrite assumes nothing new. Recognizing the body rather than the name T.ctorIdx keeps a user definition of T.ctorIdx, possible with set_option genCtorIdx false, compiled as written.

Closes #16323

Generative AI assistance: Claude assisted with the investigation, implementation and tests.

This PR stops adding an `unsafe` declaration `T.ctorIdx._impl` and an `@[implemented_by]` attribute to every inductive type `T` with more than one constructor. leanprover#15407 added them so that `T.ctorIdx` reads the constructor tag; the code generator now emits that read itself, so the compiled code stays the same.

`toLCNF` compiles a `casesOn` whose alternatives each return the index of their constructor as a raw literal, which is the body of `T.ctorIdx`, to `getObjTagNat`, the way leanprover#16253 substitutes `withPtrEqUnsafe` for `withPtrEq`. The conditions are those of leanprover#15407: the type has more than one constructor and is not a runtime builtin type such as `Nat`. The index is then the tag that every compiled `cases` on the type already switches on, so the rewrite assumes nothing new. Recognizing the body rather than the name `T.ctorIdx` keeps a user definition of `T.ctorIdx`, possible with `set_option genCtorIdx false`, compiled as written.

Closes leanprover#16323
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Thank you for your interest in contributing to Lean!

This repository currently only accepts pull requests from regular contributors, primarily from the Lean FRO, so this PR has been closed automatically.

If you have found a bug or want to propose a change, please open an issue describing it instead; see also our contribution guidelines. A maintainer may reopen this PR if appropriate.

@github-actions github-actions Bot closed this Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant