Skip to content

Commit

Permalink
dart-lang#2138. Add more tests for functions with a short body
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Oct 30, 2024
1 parent abef8c3 commit 68c7a3e
Show file tree
Hide file tree
Showing 16 changed files with 574 additions and 13 deletions.
1 change: 1 addition & 0 deletions Language/Functions/element_type_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/// `T`, and `S` is of the form `Iterable<U>`, it is a compile-time error to
/// yield an expression whose static type isn't assignable to `U`.
/// @author [email protected]
/// @issue 53054
import "dart:async";

Expand Down
1 change: 1 addition & 0 deletions Language/Functions/element_type_A01_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/// `T`, and `S` is of the form `Iterable<U>`, it is a compile-time error to
/// yield an expression whose static type isn't assignable to `U`.
/// @author [email protected]
/// @issue 53054
import "dart:async";

Expand Down
1 change: 1 addition & 0 deletions Language/Functions/element_type_A02_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/// `T`, and `S` is of the form `Stream<U>`, it is a compile-time error to
/// yield an expression whose static type isn't assignable to `U`.
/// @author [email protected]
/// @issue 53054
import "dart:async";

Expand Down
1 change: 1 addition & 0 deletions Language/Functions/element_type_A02_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/// `T`, and `S` is of the form `Stream<U>`, it is a compile-time error to
/// yield an expression whose static type isn't assignable to `U`.
/// @author [email protected]
/// @issue 53054
import "dart:async";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
// 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.

/// @assertion A function body of the form => e is equivalent to a body of
/// the form {return e;} or the form async => e which is equivalent to a body
/// of the form async {return e;}.
///
/// @description Checks that the function body of the form => e is equivalent
/// to a body of the form {return e;}.
/// @assertion A function body is either:
/// ...
/// - of the form `=> e` or the form `async => e`, which both return the value
/// of the expression `e` as if by a `return e`.
///
/// @description Checks that the function body of the form `=> e` is equivalent
/// to a body of the form `{return e;}`.
/// @author msyabro
import "../../Utils/expect.dart";

main() {
int x = 2;

foo1() => 1;
foo2() {return 1;}
bar1(int val) => val * 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// 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.

/// @assertion A function body of the form => e is equivalent to a body of
/// the form {return e;} or the form async => e which is equivalent to a body
/// of the form async {return e;}.
/// @assertion A function body is either:
/// ...
/// - of the form `=> e` or the form `async => e`, which both return the value
/// of the expression `e` as if by a `return e`.
///
/// @description Checks that the function body of the form async => e is
/// equivalent to a body of the form async {return e;}.
/// @description Checks that the function body of the form `async => e` is
/// equivalent to a body of the form `async {return e;}`.
/// @author [email protected]
import 'dart:async';
Expand Down
56 changes: 56 additions & 0 deletions Language/Functions/function_body_short_syntax_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2024, 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.

/// @assertion A function body is either:
/// ...
/// - of the form `=> e` or the form `async => e`, which both return the value
/// of the expression `e` as if by a `return e`. The other modifiers do not
/// apply here, because they apply only to generators, discussed below.
/// Generators are not allowed to explicitly return anything, objects are
/// added to the generated stream or iterable using `yield` or `yield*`.
///
/// @description Checks that it is a compile-time error if a function with a
/// short syntax contains `async*` modifier.
/// @author [email protected]
import 'dart:async';

Stream<int> f1() async* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Stream<int> f2() async* => [1];
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Stream<int> f3() async* => Stream.fromIterable([1]);
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Stream<void> f4() async* => 1 as dynamic;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Stream<void> f5() async* => null;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Stream<void> f6() async* => print(""); // print("") returns void
// ^^
// [analyzer] unspecified
// [cfe] unspecified
FutureOr<Stream<int>> f7() async* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified

main() {
print(f1);
print(f2);
print(f3);
print(f4);
print(f5);
print(f6);
print(f7);
}
56 changes: 56 additions & 0 deletions Language/Functions/function_body_short_syntax_A02_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2024, 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.

/// @assertion A function body is either:
/// ...
/// - of the form `=> e` or the form `async => e`, which both return the value
/// of the expression `e` as if by a `return e`. The other modifiers do not
/// apply here, because they apply only to generators, discussed below.
/// Generators are not allowed to explicitly return anything, objects are
/// added to the generated stream or iterable using `yield` or `yield*`.
///
/// @description Checks that it is a compile-time error if a function with a
/// short syntax contains `sync*` modifier.
/// @author [email protected]
import 'dart:async';

Iterable<int> f1() sync* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Iterable<int> f2() sync* => [1];
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Iterable<int> f3() sync* => Stream.fromIterable([1]);
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Iterable<void> f4() sync* => 1 as dynamic;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Iterable<void> f5() sync* => null;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Iterable<void> f6() sync* => print(""); // print("") returns void
// ^^
// [analyzer] unspecified
// [cfe] unspecified
FutureOr<Iterable<int>> f7() sync* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified

main() {
print(f1);
print(f2);
print(f3);
print(f4);
print(f5);
print(f6);
print(f7);
}
83 changes: 83 additions & 0 deletions Language/Functions/function_body_short_syntax_A02_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2024, 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.

/// @assertion A function body is either:
/// ...
/// - of the form `=> e` or the form `async => e`, which both return the value
/// of the expression `e` as if by a `return e`. The other modifiers do not
/// apply here, because they apply only to generators, discussed below.
/// Generators are not allowed to explicitly return anything, objects are
/// added to the generated stream or iterable using `yield` or `yield*`.
///
/// @description Checks that it is a compile-time error if a method with a
/// short syntax contains `async*` modifier.
/// @author [email protected]
import 'dart:async';

class C {
static Stream<int> staticMethod() async* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Stream<int> instanceMethod() async* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
}

mixin M {
static Stream<int> staticMethod() async* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Stream<int> instanceMethod() async* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
}

enum E {
e0;
static Stream<int> staticMethod() async* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Stream<int> instanceMethod() async* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
}

class A {}

extension Ext on A {
static Stream<int> staticMethod() async* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Stream<int> instanceMethod() async* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
}

extension type ET(int _) {
static Stream<int> staticMethod() async* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Stream<int> instanceMethod() async* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
print(C);
print(M);
print(E);
print(A);
print(ET);
}
83 changes: 83 additions & 0 deletions Language/Functions/function_body_short_syntax_A02_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2024, 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.

/// @assertion A function body is either:
/// ...
/// - of the form `=> e` or the form `async => e`, which both return the value
/// of the expression `e` as if by a `return e`. The other modifiers do not
/// apply here, because they apply only to generators, discussed below.
/// Generators are not allowed to explicitly return anything, objects are
/// added to the generated stream or iterable using `yield` or `yield*`.
///
/// @description Checks that it is a compile-time error if a method with a
/// short syntax contains `sync*` modifier.
/// @author [email protected]
import 'dart:async';

class C {
static Iterable<int> staticMethod() sync* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Iterable<int> instanceMethod() sync* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
}

mixin M {
static Iterable<int> staticMethod() sync* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Iterable<int> instanceMethod() sync* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
}

enum E {
e0;
static Iterable<int> staticMethod() sync* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Iterable<int> instanceMethod() sync* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
}

class A {}

extension Ext on A {
static Iterable<int> staticMethod() sync* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Iterable<int> instanceMethod() sync* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
}

extension type ET(int _) {
static Iterable<int> staticMethod() sync* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
Iterable<int> instanceMethod() sync* => 1;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
print(C);
print(M);
print(E);
print(A);
print(ET);
}
29 changes: 29 additions & 0 deletions Language/Functions/function_body_short_syntax_A03_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2024, 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.

/// @assertion A function body is either:
/// ...
/// - of the form `=> e` or the form `async => e`, which both return the value
/// of the expression `e` as if by a `return e`. ... Let `T` be the declared
/// return type of the function that has this body. It is a compile-time error
/// if one of the following conditions hold:
/// – The function is synchronous, `T` is not `void`, and it would have been a
/// compile-time error to declare the function with the body `{ return e; }`
/// rather than `=> e`.
///
/// @description Checks that it is not an error to declare a synchronous
/// function with a short syntax and the return type `void`.
/// @author [email protected]
void f1() => 1;
void f2() => null;
void f3<T>(T t) => t;
void f4() => print("");

main() {
print(f1);
print(f2);
print(f3);
print(f4);
}
Loading

0 comments on commit 68c7a3e

Please sign in to comment.