fix: ensure lean_initialize is called when Lean is only privately imported - #14505
Merged
Merged
Conversation
Member
Author
|
!bench |
1 similar comment
Member
Author
|
!bench |
|
Benchmark results for 16f8546 against 3259610 are in. (These commits have already been benchmarked in a previous command.) No significant results found. @Kha
Small changes (2✅, 1🟥)
|
1 similar comment
|
Benchmark results for 16f8546 against 3259610 are in. (These commits have already been benchmarked in a previous command.) No significant results found. @Kha
Small changes (2✅, 1🟥)
|
Collaborator
|
Reference manual CI status:
|
|
Mathlib CI status (docs):
|
Member
Author
|
!bench mathlib |
|
Benchmark results for leanprover-community/mathlib4-nightly-testing@8b920c2 against leanprover-community/mathlib4-nightly-testing@eaeb601 are in. No significant results found. @Kha
No significant changes detected. |
|
The adaptation PR for this PR is leanprover/downstream-lean4#9. |
Kha
force-pushed
the
lean-initialize-module-test
branch
from
July 23, 2026 12:37
16f8546 to
09e325e
Compare
tydeu
reviewed
Jul 24, 2026
… imported When a `module` privately imports (parts of) `Lean` and that module is used by an executable whose root module does not visibly import `Lean`, the emitted `main` called only `lean_initialize_runtime_module`, so the C++ components of the Lean package (kernel, library, ...) were never initialized and the executable crashed at startup. Localize the initialization decision into each module: the generated initializer of every module whose environment shows runtime use of the `Lean` package now calls `lean_initialize` itself (except for modules of the core packages, which `lean_initialize` initializes), and the emitted `main` always calls just `lean_initialize_runtime_module`. Make `lean_initialize` and `lean_initialize_runtime_module` idempotent so repeated calls from several module initializers are no-ops. The LLVM emitter keeps the previous `main`-level check as it has no module system support. Also add a regression test building and running such an executable. Co-Authored-By: Claude feat: initialize the Lean runtime in every generated module initializer Every generated module initializer now calls `lean_initialize_runtime_module` (or, for modules using the `Lean` package, the full `lean_initialize`) before anything else, so external users of a Lean library no longer have to remember calling it before the library's initializer. Both functions are idempotent, so the emitted `main` and existing embedders that already call them are unaffected. Co-Authored-By: Claude doc: drop manual runtime initialization from the reverse FFI example Module initializers now initialize the Lean runtime themselves, so the example no longer needs to call `lean_initialize_runtime_module` first; a comment notes the call is still required on older toolchains. This also makes the example test verify the new self-initialization behavior. Co-Authored-By: Claude
Kha
force-pushed
the
lean-initialize-module-test
branch
from
July 26, 2026 13:50
09e325e to
7983e94
Compare
Kha
enabled auto-merge
July 26, 2026 13:50
…d `main` The module initializer that `main` calls initializes the runtime itself, so `main` no longer needs to call `lean_initialize_runtime_module` beforehand. Co-Authored-By: Claude
Kha
force-pushed
the
lean-initialize-module-test
branch
from
July 26, 2026 13:51
7983e94 to
63a979f
Compare
robsimmons
pushed a commit
that referenced
this pull request
Jul 29, 2026
… imported (#14505) This PR fixes a compiler issue where private imports of the `Lean` library could lead to segfaults by ensuring the necessary call to `lean_initialize` happens in each module's initializer when necessary. As a follow-up clean up, the call to `lean_initialize_runtime_module` is made implicit as well, meaning users of Lean as an FFI library do not need to call these functions themselves anymore.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a compiler issue where private imports of the
Leanlibrary could lead to segfaults by ensuring the necessary call tolean_initializehappens in each module's initializer when necessary. As a follow-up clean up, the call tolean_initialize_runtime_moduleis made implicit as well, meaning users of Lean as an FFI library do not need to call these functions themselves anymore.