Files
sdk/tests/language/anonymous_methods/block/error_test.dart
T
Erik Ernst 3263555dc5 Start implementation of anonymous block methods
This CL introduces support for anonymous block methods (that is,
`e.{ print(this); }` as opposed to `e.=> print(this);`). It introduces
the notion of a `ReturnContext` which is used to change the semantics
of a return statement when it returns from an anonymous method (where
it works like a `break` that terminates the execution of the block
which is the body of the enclosing anonymous method), but keeps the
semantics of return statements returning from a function (including
function literals) still have the same semantics as today.

Change-Id: I404459361fbb7c2e495e46d1bd29924063f3aac4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/503800
SLSA-Policy-Verified: SLSA Policy Verification Service <devtools-gerritcodereview-exitgate@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Erik Ernst <eernst@google.com>
2026-05-27 06:04:46 -07:00

76 lines
2.6 KiB
Dart

// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// SharedOptions=--enable-experiment=anonymous-methods
// This test confirms that compile-time errors specific to the
// 'anonymous-methods' feature are emitted as expected.
void main() {
// Zero parameters.
1.() {};
// ^^
// [analyzer] SYNTACTIC_ERROR.ANONYMOUS_METHOD_WRONG_PARAMETER_LIST
// [cfe] An anonymous method with a parameter list must have exactly one required, positional parameter.
// Multiple parameters.
1.(a, b) {};
// ^^^^^^
// [analyzer] SYNTACTIC_ERROR.ANONYMOUS_METHOD_WRONG_PARAMETER_LIST
// [cfe] An anonymous method with a parameter list must have exactly one required, positional parameter.
// An optional positional parameter.
1.([a]) {};
// ^^^^^
// [analyzer] SYNTACTIC_ERROR.ANONYMOUS_METHOD_WRONG_PARAMETER_LIST
// [cfe] An anonymous method with a parameter list must have exactly one required, positional parameter.
// An optional named parameter.
1.({a}) {};
// ^^^^^
// [analyzer] SYNTACTIC_ERROR.ANONYMOUS_METHOD_WRONG_PARAMETER_LIST
// [cfe] An anonymous method with a parameter list must have exactly one required, positional parameter.
// A required named parameter.
1.({required a}) {};
// ^^^^^^^^^^^^^^
// [analyzer] SYNTACTIC_ERROR.ANONYMOUS_METHOD_WRONG_PARAMETER_LIST
// [cfe] An anonymous method with a parameter list must have exactly one required, positional parameter.
// A parameter type mismatch.
"".(int i) {};
// ^^^
// [analyzer] COMPILE_TIME_ERROR.ANONYMOUS_METHOD_WRONG_PARAMETER_TYPE
// [cfe] The receiver type 'String' must be assignable to the formal parameter type 'int' in an anonymous method.
// Using a void value as receiver.
StringBuffer('').{ return print('0'); }.toString();
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.USE_OF_VOID_RESULT
//^
// [cfe] This expression has type 'void' and can't be used.
StringBuffer('').{ return print('0'); }.{ print('1'); };
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.USE_OF_VOID_RESULT
//^
// [cfe] This expression has type 'void' and can't be used.
// Break outside of loop/switch.
1.{
break;
// ^^^^^
// [analyzer] SYNTACTIC_ERROR.BREAK_OUTSIDE_OF_LOOP
// [cfe] A break statement can't be used outside of a loop or switch statement.
};
// Continue outside of loop.
1.{
continue;
// ^^^^^^^^
// [analyzer] SYNTACTIC_ERROR.CONTINUE_OUTSIDE_OF_LOOP
// [cfe] A continue statement can't be used outside of a loop or switch statement.
};
}