Merge pull request #18227 from dlongnecke-cray/context-manager-syntax
Initial implementation of manage statement
This PR introduces the manage statement to Chapel. The manage
statement provides a convenient way to perform setup and teardown
actions automatically at the beginning and end of a block.
use SpinlockGuard; // Here 'spinlock' is a lock which guards a resource. It can be used // in manage statements as a convenient way to return an exclusive // reference to a resource. See: 'test/statements/manage' var sum = new spinlock(int); forall i in 1..5 with (ref sum) do manage sum as count do count += 1; assert(sum.nonLockingRead() == 15);
This PR introduces support for several minor variations of the manage
statement. The first, most simple form, requires users to specify
the storage kind of the managed resource (should it be present):
// Mutation performed on local copy. manage myManager() as var myResource do myResource.mutate();
The storage kind of the managed resource can also be omitted. In this
case, the compiler chooses the appropriate return intent overload to
use, preferring ref overloads of
enterThis() when possible. The
exact ordering is to be decided.
// Prefer ref overloads when possible. Whether ref or const ref is // selected depends on how the resource is used. manage myManager() as myResource do myResource.mutate();
Multiple managers can be nested on a single line.
var man1 = new MyManager(); var man2 = new MyManager(); manage man1 as foo, man2 as bar do writeln(foo + bar);
Support for error handling is also included, although the design is
not final and is subject to change (see #18190).
vim highlighter to include
manage as a keyword.
Rebuilds the old compiler parser.
- Move new code added to
- Formalize inference rules when storage kind of resource is omitted
- Clean up error for managers in
- Lift manage statements out of try/catch block after parsing when
it is safe to do so
- Formalize how context managers should interact with error handling
- Add manage statement to
Signed-off-by: David Longnecker firstname.lastname@example.org
Modified Files: A test/statements/manage/ContextManagerSyntax.chpl