Skip to content

Commit 03ea9eb

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[wildcard-variables] Basic language tests for non-binding locals.
Making language tests in small batches. These are very basic ones to test that you can have multiple local declarations named `_` in the same namespace without a collision error. Bug: #55652 Change-Id: I4d00e9072ba745b363d68db72505c599953c41ad Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366022 Reviewed-by: Lasse Nielsen <[email protected]> Commit-Queue: Kallen Tu <[email protected]> Reviewed-by: Erik Ernst <[email protected]>
1 parent ba8ac0f commit 03ea9eb

6 files changed

+258
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Tests multiple catch clause wildcard variable declarations with rethrow.
6+
7+
// SharedOptions=--enable-experiment=wildcard-variables
8+
9+
import 'package:expect/expect.dart';
10+
11+
void main() {
12+
var error = StateError("State bad!");
13+
var stack = StackTrace.fromString("My stack trace");
14+
var caught = false;
15+
16+
// Multiple wildcard catch clause variables.
17+
try {
18+
try {
19+
Error.throwWithStackTrace(error, stack);
20+
} on StateError catch (_, _) {
21+
caught = true;
22+
rethrow;
23+
}
24+
} on StateError catch (e, s) {
25+
Expect.isTrue(caught);
26+
Expect.identical(error, e);
27+
Expect.equals(stack.toString(), s.toString());
28+
Expect.equals(stack.toString(), e.stackTrace.toString());
29+
}
30+
31+
// Single wildcard catch clause variable.
32+
try {
33+
try {
34+
Error.throwWithStackTrace(error, stack);
35+
} on StateError catch (_) {
36+
caught = true;
37+
rethrow;
38+
}
39+
} on StateError catch (e, s) {
40+
Expect.isTrue(caught);
41+
Expect.identical(error, e);
42+
Expect.equals(stack.toString(), s.toString());
43+
Expect.equals(stack.toString(), e.stackTrace.toString());
44+
}
45+
46+
try {
47+
try {
48+
Error.throwWithStackTrace(error, stack);
49+
} on StateError catch (_, s) {
50+
Expect.equals(stack.toString(), s.toString());
51+
caught = true;
52+
rethrow;
53+
}
54+
} on StateError catch (e, s) {
55+
Expect.isTrue(caught);
56+
Expect.identical(error, e);
57+
Expect.equals(stack.toString(), s.toString());
58+
Expect.equals(stack.toString(), e.stackTrace.toString());
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Tests multiple catch clause wildcard variable declarations.
6+
7+
// SharedOptions=--enable-experiment=wildcard-variables
8+
9+
void main() {
10+
try {
11+
throw '!';
12+
} on Exception catch (_, _) {
13+
rethrow; // Should not hit this catch.
14+
} catch (_, _) {
15+
print('catch');
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Tests for-loop wildcard variable declarations.
6+
7+
// SharedOptions=--enable-experiment=wildcard-variables
8+
9+
void main() async {
10+
// Multiple for-loop wildcard declarations.
11+
for (int _ = 0, _ = 2;;) {
12+
break;
13+
}
14+
15+
var list = [];
16+
for (var _ in list) {}
17+
18+
var stream = Stream.empty();
19+
await for (var _ in stream) {}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Tests multiple wildcard function parameters.
6+
7+
// SharedOptions=--enable-experiment=wildcard-variables
8+
9+
import 'package:expect/expect.dart';
10+
11+
class Constructor {
12+
final _;
13+
14+
// TODO(kallentu): Update this once the behaviour of super._ is finalized.
15+
// https://github.com/dart-lang/language/issues/3792
16+
Constructor.multiple(_, this._, void _()) {}
17+
Constructor.parameter(_, _, this._) {}
18+
Constructor.functionType(this._, void _(), void _()) {}
19+
}
20+
21+
bool topLevelFunction(int _, int _) => true;
22+
bool topLevelFunction2(_, _) => true;
23+
bool topLevelFunction_functionType(void _(), void _()) => true;
24+
25+
class InstanceMethod {
26+
bool instanceMethod(int _, int _) => true;
27+
bool instanceMethod2(_, _) => true;
28+
bool instanceMethod_functionType(void _(), void _()) => true;
29+
30+
// User-defined operators
31+
int operator +(int _) => 42;
32+
void operator []=(int _, _) {}
33+
34+
// Inherited `Object` methods
35+
dynamic noSuchMethod(Invocation _) => true;
36+
bool operator ==(Object _) => true;
37+
}
38+
39+
abstract class AbstractMethod {
40+
bool abstractMethod(int _, int _);
41+
bool abstractMethod2(_, _);
42+
bool abstractMethod_functionType(void _(), void _());
43+
}
44+
45+
class AbstractMethodSubclass extends AbstractMethod {
46+
bool abstractMethod(int _, int _) => true;
47+
bool abstractMethod2(_, _) => true;
48+
bool abstractMethod_functionType(void _(), void _()) => true;
49+
}
50+
51+
class Setter {
52+
int _x;
53+
Setter(this._x);
54+
55+
int get x => _x;
56+
set x(int _) => _x = 2;
57+
}
58+
59+
class StaticMethod {
60+
static int staticMethod(int _, int _) => 2;
61+
static int staticMethod2(_, _) => 2;
62+
static int staticMethod_functionType(void _(), void _()) => 2;
63+
static int staticMethod_functionTypeNested(
64+
void _(_, _), void _(int _, int _)) =>
65+
2;
66+
static int staticMethod_functionTypeNew(
67+
void Function(int _, int _) _, void Function(int _, int _) _) =>
68+
2;
69+
static int staticMethod_functionTypeGeneric(
70+
void Function<_, _>(int _, int _) _, void _<_>(_, _)) =>
71+
2;
72+
}
73+
74+
void main() {
75+
// Function expression
76+
var list = [true];
77+
list.where(
78+
(_, [_]) => true,
79+
);
80+
81+
// Abstract methods
82+
var abstractMethod = AbstractMethodSubclass();
83+
abstractMethod.abstractMethod(1, 2);
84+
abstractMethod.abstractMethod2(1, 2);
85+
abstractMethod.abstractMethod_functionType(() {}, () {});
86+
87+
// Static methods
88+
StaticMethod.staticMethod(1, 2);
89+
StaticMethod.staticMethod2(1, 2);
90+
StaticMethod.staticMethod_functionType(() {}, () {});
91+
StaticMethod.staticMethod_functionTypeNested((e, x) {}, (e, x) {});
92+
StaticMethod.staticMethod_functionTypeNew((e, x) {}, (e, x) {});
93+
StaticMethod.staticMethod_functionTypeNested((_, _) {}, (_, _) {});
94+
StaticMethod.staticMethod_functionTypeNew((_, _) {}, (_, _) {});
95+
96+
// Top level functions
97+
topLevelFunction(1, 2);
98+
topLevelFunction2(1, 2);
99+
topLevelFunction_functionType(() {}, () {});
100+
101+
// Instance methods
102+
var instanceMethod = InstanceMethod();
103+
instanceMethod.instanceMethod(1, 2);
104+
instanceMethod.instanceMethod2(1, 2);
105+
instanceMethod.instanceMethod_functionType(() {}, () {});
106+
Expect.equals(42, instanceMethod + 2);
107+
instanceMethod[1] = 2;
108+
Expect.isTrue(instanceMethod == 2);
109+
Expect.isTrue((instanceMethod as dynamic).noMethod());
110+
111+
// Constructor
112+
Constructor.multiple(1, 2, () {});
113+
Constructor.parameter(1, 2, 3);
114+
Constructor.functionType(() {}, () {}, () {});
115+
116+
// Setter
117+
var setter = Setter(1);
118+
setter.x = 2;
119+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Tests multiple local wildcard variable declarations.
6+
7+
// SharedOptions=--enable-experiment=wildcard-variables
8+
9+
void main() {
10+
var _ = 1;
11+
int _ = 2;
12+
final _ = 3;
13+
14+
var i = 2, _ = 2;
15+
i = i + 1;
16+
17+
int _;
18+
final _;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Tests multiple wildcard generic type parameters.
6+
7+
// SharedOptions=--enable-experiment=wildcard-variables
8+
9+
// Class type parameters
10+
class T<_, _> {}
11+
12+
typedef ForgetfulMap<_, _> = Map<Object?, Object?>;
13+
typedef F<_, X, _> = X Function<_ extends X>(X _, X _);
14+
15+
// Function type parameters
16+
void genericFunction<_, _>() {}
17+
void genericFunction2<_ extends Iterable<int>, _ extends num>() {}
18+
void genericFunction3<_ extends void Function<_>(_, _), _>() {}
19+
20+
void main() {
21+
void genericCallback(bool Function<T, E>() func) {}
22+
genericCallback(<_, _>() => true);
23+
}

0 commit comments

Comments
 (0)