Open LouisJenkinsCS opened 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.
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
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();
Output