Skip to content

@[init] declarations are sometimes run in the wrong order #10175

@Rob23oba

Description

@Rob23oba

Prerequisites

Please put an X between the brackets as you perform the following steps:

Description

@[init] declarations are not run in dependency order but by order on declaration names, producing the error cannot evaluate `[init]` declaration 'a' in the same module sometimes when two init declarations depend on each other.

Context

https://leanprover.zulipchat.com/#narrow/channel/270676-lean4/topic/Cannot.20evaluation.20.5Binit.5D.20declaration.20in.20same.20module/near/536828403
and
https://leanprover.zulipchat.com/#narrow/channel/428973-nightly-testing/topic/Mathlib.20status.20updates/near/535934676

Steps to Reproduce

  1. Create two files Foo.lean and Bar.lean
  2. Import Foo in Bar.lean
  3. Put this into Foo.lean:
    def a_init : IO Nat :=
      pure 5
    
    @[init a_init]
    opaque a : Nat
    
    def b_init : IO Nat :=
      pure (a + 7)
    
    @[init b_init]
    opaque b : Nat
  4. Reload Bar.lean
  5. Put this into Foo.lean:
    def b_init : IO Nat :=
      pure 5
    
    @[init b_init]
    opaque b : Nat
    
    def a_init : IO Nat :=
      pure (b + 7)
    
    @[init a_init]
    opaque a : Nat
  6. Reload Bar.lean

Expected behavior: The initializers are run in dependency order and thus both variants work, regardless of naming.

Actual behavior: Only the second one works because the order depends on the names and not on dependencies.

Versions

Lean 4.24.0-nightly-2025-08-29

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P-highWe will work on this issuebugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions