chapel-lang / chapel

a Productive Parallel Programming Language
https://chapel-lang.org
Other
1.79k stars 420 forks source link

Confusing error message about default initializing a variable with generic type #19113

Open bradcray opened 2 years ago

bradcray commented 2 years ago

Summary of Problem

On a line of Chapel in which I did not believe I was default initializing anything, the compiler gave me an error message about default initializing a variable with generic type. I'm not certain whether the problem is mine or the compiler's, but if mine, I think the error message needs to be clarified for the end-user.

Capturing the error message here in case others hit cases like this:

day18-bug.chpl:23: In function 'lineToTree':
day18-bug.chpl:28: error: cannot default-initialize a variable with generic type
day18-bug.chpl:28: note: '<temporary>' has generic type 'Node'
day18-bug.chpl:28: note: cannot find initialization point to split-init this variable

Steps to Reproduce

Associated Future Test(s): test/studies/adventOfCode/2021/bradc/futures/day18-bug.chpl #19028

Configuration Information

mppf commented 2 years ago

The problem is that Node is a generic type (it has generic memory management). This is tangentially related to #19120 (in that if you had to write Node(?) in the class for the left field maybe you would be aware of it in the rest of the program).

Since the compiler knows Node is a generic type,

proc lineToTree(line: string, ref pos: int): Node {
  var left, right: Node; // type is generic, so relying on split-init
  left = lineToTree(line, pos); // recursive function call
                                // -> can't infer return type, so use declared return type.
                                // But declared return type is generic.
bradcray commented 2 years ago

I think you're saying "the problem was yours, and it's appropriate that the compiler is generating an error for this program", but it's not clear to me whether you're also saying "and the error message we have doesn't need improving."

Specifically, I was confused about the note about default initialization pointing to the line where I was explicitly either initializing or assigning it rather than the line where the declaration occurred (which is where I think of the default initialization happening—am I wrong about that?). And of course the reference to <temporary> didn't help since I couldn't figure out what that was referring to.

mppf commented 2 years ago

For sure the error message could be better. Besides improving the line numbers, it could mention that it's generic due to the class having generic management.