New Issue: Why did this code cause us to discover PCGRandom's writeThis needed a use of the IO module?

16846, “lydia-duncan”, “Why did this code cause us to discover PCGRandom’s writeThis needed a use of the IO module?”, “2020-12-10T17:06:58Z”

Summary of Problem

The resolution of an unrelated function in another module exposed a resolution error with PCGRandom’s writeThis method. Why was PCGRandom’s writeThis even looked at in this scope? Why was its body resolved to the point where we saw a resolution error, when no instances of the type were supposed to be written? Does it have something to do with first class functions, since replacing the argument with a string avoids the problem?

Steps to Reproduce

Comment out the use IO; statements in the Random module, then compile the following:

Source Code:

module Foo {
  use Random;
  use Random.PCGRandom only PCGRandomStream;

  proc generateToken(len: int=32) : string {
    var alphanum = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
                    "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
                    "a", "b", "c", "d", "e", "f", "g", "h", "i", "j","k", "l", "m",
                    "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
                    ];
    var upper_bound = alphanum.size;
    var indices : [0..upper_bound-1] int;
    for i in 0..upper_bound-1 do
      indices[i] = i;

    var ret : [0..len-1] string;
    var r = new owned PCGRandomStream(int);
    var rindices = try! r.choice(indices, len);

    for i in 1..len-1 do
      ret[i] = alphanum[rindices[i]];
    return ''.join(ret);
  }
}

module Two {
  use IO;
  proc otherGenFunc(x) throws {
    writeln("%s".format(x));
  }
}
module Three {
  use Two;

  proc callGenFunc() {
    try! otherGenFunc(otherFunc); // Note missing parentheses here
  }

  proc otherFunc() {
    writeln("eh");
  }
}
module User {
  use Foo, Three;
  proc main() {
    callGenFunc();
    var token = generateToken(32);
    writeln(token);
  }
}

Compile command:
chpl foo.chpl

Associated Test(s):
test/library/standard/Random/checkWriteThis.chpl #16838

Configuration Information

  • Output of chpl --version: 1.24.0 pre-release