Branch: refs/heads/main
Revision: 65aef75
Author: dlongnecke-cray
Log Message:
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).
Adjusts the vim
highlighter to include manage
as a keyword.
Rebuilds the old compiler parser.
Future work:
- Move new code added to
preFold
intocullOverReferences
- Formalize inference rules when storage kind of resource is omitted
- Clean up error for managers in
lateConstCheck
- 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
compiler/next
(bothuAST
and parser)
Reviewed by @e-kayrakli and @mppf. Thanks!
Testing:
- [x]
ALL
onlinux64
whenCOMM=none
- [x]
ALL
onlinux64
whenCOMM=gasnet
Signed-off-by: David Longnecker dlongnecke-cray@users.noreply.github.com
Modified Files:
A test/statements/manage/ContextManagerSyntax.chpl
A test/statements/manage/ContextManagerSyntax.good
A test/statements/manage/EXECOPTS
A test/statements/manage/InferredConstRefResourceMutation.chpl
A test/statements/manage/InferredConstRefResourceMutation.good
A test/statements/manage/InferredConstRefResourceMutation.noexec
A test/statements/manage/InferredRefResourceMutation.chpl
A test/statements/manage/InferredRefResourceMutation.good
A test/statements/manage/InferredResourceStorage.chpl
A test/statements/manage/InferredResourceStorage.good
A test/statements/manage/OverloadedReturnIntent.chpl
A test/statements/manage/OverloadedReturnIntent.good
A test/statements/manage/SpinlockGuard.chpl
A test/statements/manage/SpinlockGuard.notest
A test/statements/manage/TrackingRecord.chpl
A test/statements/manage/TrackingRecord.notest
A test/statements/manage/UseSpinlockGuard.chpl
A test/statements/manage/UseSpinlockGuard.good
A test/statements/manage/UseSpinlockGuardBadTaskIntent.chpl
A test/statements/manage/UseSpinlockGuardBadTaskIntent.good
A test/statements/manage/WarnUnstable.chpl
A test/statements/manage/WarnUnstable.compopts
A test/statements/manage/WarnUnstable.good
M compiler/AST/build.cpp
M compiler/include/bison-chapel.h
M compiler/include/build.h
M compiler/include/flags_list.h
M compiler/include/flex-chapel.h
M compiler/parser/bison-chapel.cpp
M compiler/parser/chapel.lex
M compiler/parser/chapel.ypp
M compiler/parser/flex-chapel.cpp
M compiler/resolution/cullOverReferences.cpp
M compiler/resolution/lateConstCheck.cpp
M compiler/resolution/preFold.cpp
M compiler/resolution/resolveFunction.cpp
M highlight/vim/syntax/chpl.vim
Compare: https://github.com/chapel-lang/chapel/compare/dedb811fe263...65aef75b7226