New Issue: [Bug]: domain may be used before it is initialized via (unused) record's fields

27453, "bradcray", "[Bug]: domain may be used before it is initialized via (unused) record's fields", "2025-06-27T00:28:57Z"

Summary of Problem

Description:

This is a smaller reproducer of an unfamiliar-to-me bug that was reported on Discourse this past week. We generate a "[variable] may be used before it is initialized" error (in this case for the global variable state_domain) through a particular series of declarations.

Most puzzling is that the issue seems to be caused by a record type that is not even used in this minimal version of the program (grid below):

const state_domain: domain(1) = {1..5};  // removing the `: domain(1)` makes this work

var myWall = new wall();
writeln(myWall);

record wall {
  var states_count = state_domain;  // replacing `state_domain` by `{1..5}` makes this work
}

// commenting out this record type makes it work
record grid {
  var cells_tot: [1..1000] wall;
}

As noted in the comments above, replacing the reference to state_domain in wall makes things work, though this is not surprising since it removes the only reference to the global variable whose initialization order is in question. Slightly more suspicious is that removing the : domain(1) type constraint on state_domain makes things work.

From what I've been able to determine so far, I think it is the declarations in Chapel's internal init_elts() routine that are causing the problem. I don't quite understand what's happening but am guessing that the compiler is creating some sort of initialization routine for grid (early in the compilation), and in doing so attempts to declare a variable of type wall, resulting in the attempt to evaluate state_domain before it has been resolved itself. I'm not sure how the domain(1) type constraint plays a role yet, though.

Steps to Reproduce

Source Code:

Compile command:
chpl testit.chpl

Associated Future Test(s):

Configuration Information

  • Output of chpl --version: chpl version 2.5.0