chapel-lang / chapel

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

Compiler Segmentation Fault when returning `nil` from function with return type of generic lifetime nilable class #14063

Open LouisJenkinsCS opened 5 years ago

LouisJenkinsCS commented 5 years ago
proc p() : object? {
    return nil;
}

writeln(p());

Output

segfault.chpl:5: internal error: seg fault [util/misc.cpp:672]
Note: This source location is a guess.
mppf commented 5 years ago

The compiler is getting tripped up because object does not specify management strategy (unmanaged, borrowed, etc) but rather means generic management.

Here is a workaround:

proc p() : borrowed object? {
    return nil;
}

writeln(p());

The error message should be improved for this case, but it will be an error.

jabraham17 commented 3 months ago

The updated reproducer for 2.0 is as follows

proc p() : RootClass? {
    return nil;
}

writeln(p());

On main today this gives the following error

error: could not coerce a value of type 'nil' to type 'RootClass?'

This was fixed by #19709 and is tested by test/classes/nilability/errors/cast-nil-to-generic-1.chpl. So while this is better, it is still not the most descriptive error message. https://github.com/chapel-lang/chapel/issues/14148 has some discussion on improving the error

mppf commented 3 months ago

Issue #25468 shows a related case where it's more plausible that the compiler has enough information to resolve it:

class C {}

proc f(): C? {
  var x = true;
  if x then return nil; // can't coerce nil to anymanaged C?

  return new C();
}
f();