Files
Erik Ernst e05ce41c0d Add anonymous => method related implementation in the CFE
This CL adds an implementation of anonymous `=>` methods in the CFE
which is sufficiently complete to handle the existing test cases in
language/anonymous_methods/expression.

Coverage is handled by adding magic comments to ignore the fact that new
code is not covered by existing testcases. This will be settled in a
separate CL.

To keep the failures visible, the failures in configurations
dart2js-hostasserts-linux-d8-try, dart2js-linux-chrome-try,
dart2js-minified-linux-d8-try, and ddc-linux-chrome-try have not been
approved. It seems likely to me that those are bugs in dart2js and in
DDC.

Change-Id: Ia70fedd4de6166d6a3bf8a108f49728b6e16c9f6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/494440
Commit-Queue: Erik Ernst <eernst@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2026-04-21 02:09:41 -07:00

60 lines
2.2 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.() => 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) => 1;
// ^^^^^^
// [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]) => 1;
// ^^^^^
// [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}) => 1;
// ^^^^^
// [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}) => 1;
// ^^^^^^^^^^^^^^
// [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) => 1;
// ^^^
// [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('').=> print('0')).toString();
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.USE_OF_VOID_RESULT
// ^
// [cfe] This expression has type 'void' and can't be used.
(StringBuffer('').=> print('0')).=> print('1');
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.USE_OF_VOID_RESULT
// ^
// [cfe] This expression has type 'void' and can't be used.
}