Skip to content

fix: re-privatize constant name prefix in realizeConst to avoid diamond import collisions#12964

Merged
nomeata merged 3 commits intomasterfrom
joachim/issue12825
Mar 18, 2026
Merged

fix: re-privatize constant name prefix in realizeConst to avoid diamond import collisions#12964
nomeata merged 3 commits intomasterfrom
joachim/issue12825

Conversation

@nomeata
Copy link
Copy Markdown
Collaborator

@nomeata nomeata commented Mar 18, 2026

This PR fixes an issue where realizeConst would generate auxiliary declarations
(like _sparseCasesOn) using the original defining module's private name prefix
rather than the realizing module's prefix. When two modules independently realized
the same imported constant, they produced identically-named auxiliary declarations,
causing "environment already contains" errors on diamond import.

The fix re-privatizes the constant name under the current module before passing it
to withDeclNameForAuxNaming, ensuring each realizing module generates distinctly
named auxiliary declarations.

Fixes #12825

Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com

nomeata and others added 3 commits March 18, 2026 10:26
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…mond import collisions

When `realizeConst` generates auxiliary declarations (like `_sparseCasesOn`) for
a private constant from an imported module, the name prefix inherited the original
module's private prefix. Since the name was already private, `mkUniqueName` would
not re-privatize it under the current module, causing two importing modules to
generate identically-named auxiliary declarations. This led to "environment already
contains" errors when both modules were imported together (diamond import pattern).

The fix re-privatizes the constant name under the current module before passing it
to `withDeclNameForAuxNaming`, so each realizing module generates distinctly-named
auxiliary declarations.

Fixes #12825

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@nomeata nomeata added the changelog-language Language features and metaprograms label Mar 18, 2026
@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Mar 18, 2026
@leanprover-bot
Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-03-17 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-03-18 11:59:12)

mathlib-nightly-testing bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 18, 2026
@github-actions github-actions bot added the mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN label Mar 18, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 18, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Mar 18, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@nomeata nomeata marked this pull request as ready for review March 18, 2026 13:54
@nomeata nomeata added this pull request to the merge queue Mar 18, 2026
Merged via the queue into master with commit b2aec78 Mar 18, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

builds-mathlib CI has verified that Mathlib builds against this PR changelog-language Language features and metaprograms mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Import failed, environment already contains sparseCasesOn_2

2 participants