[Chapel Merge] Fix an internal error with generic nilable class

Branch: refs/heads/main
Revision: f067fce
Author: vasslitvinov
Link: Unavailable
Log Message:

Merge pull request #19709 from vasslitvinov/fix-cast-nil-to-generic

Fix an internal error with generic nilable class

This PR:

  • resolves the crash for the code in the OP of #14148
  • cleans up the error reported upon a variant of it
  • files a .future for the code in the first comment of #14148
  • resolves #11456
  • repurposes the futures for #11456 to request a better error message

DETAILS

Given

proc p(): MyClass {
  if ... then  return nil;
  else         return new MyClass();
}

The compiler inserts a cast nil: MyClass for the first return
because it does not yet know that the returned type is the owned flavor
of MyClass. This used to crash the compiler. Now this causes a user-friendly
error message.

The same error message is produced if the order of the returns is switched.

We could probably teach the compiler to infer the ownership of the return
type and use that to cast nil to. However, that may be a significant effort.
For now, punt and have the user adjust the code.

The expressions nil:owned and nil:shared used to cause obscure error
messages, now they cause the compiler to state clearly that it is illegal.
We could potentially introduced the "owned nil" and "shared nil" types
so that these casts succeed. I am leaving that for another day.

r: @dlongnecke-cray

Modified Files:
A test/classes/nilability/errors/cast-nil-to-generic-1.chpl

A test/classes/nilability/errors/cast-nil-to-generic-1.good
A test/classes/nilability/errors/cast-nil-to-generic-2.chpl
A test/classes/nilability/errors/cast-nil-to-generic-2.good
A test/classes/nilability/errors/cast-nil-to-generic-3.chpl
A test/classes/nilability/errors/cast-nil-to-generic-3.good
A test/classes/nilability/errors/cast-nil-to-generic-4.chpl
A test/classes/nilability/errors/cast-nil-to-generic-4.good
A test/classes/nilability/errors/cast-nil-to-generic-5.chpl
A test/classes/nilability/errors/cast-nil-to-generic-5.good
A test/classes/nilability/errors/cast-nil-to-generic-6.bad
A test/classes/nilability/errors/cast-nil-to-generic-6.chpl
A test/classes/nilability/errors/cast-nil-to-generic-6.future
A test/classes/nilability/errors/cast-nil-to-generic-6.good
M compiler/AST/primitive.cpp
M modules/internal/OwnedObject.chpl
M modules/internal/SharedObject.chpl
M test/classes/delete-free/coercions-upon-return.bad
M test/classes/delete-free/coercions-upon-return.future
M test/classes/delete-free/coercions-upon-return.good
M test/classes/delete-free/coercions-upon-yield.bad
M test/classes/delete-free/coercions-upon-yield.future
M test/classes/delete-free/coercions-upon-yield.good
M test/studies/adventOfCode/2021/bradc/futures/day18-retNil2.bad

Compare: https://github.com/chapel-lang/chapel/compare/b12c1c0807b4...f067fce4a9c3