Skip to content

Commit

Permalink
[wildcard-variables] Basic language tests for non-binding locals.
Browse files Browse the repository at this point in the history
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 <lrn@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
  • Loading branch information
kallentu authored and Commit Queue committed May 17, 2024
1 parent ba8ac0f commit 03ea9eb
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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.

// Tests multiple catch clause wildcard variable declarations with rethrow.

// SharedOptions=--enable-experiment=wildcard-variables

import 'package:expect/expect.dart';

void main() {
var error = StateError("State bad!");
var stack = StackTrace.fromString("My stack trace");
var caught = false;

// Multiple wildcard catch clause variables.
try {
try {
Error.throwWithStackTrace(error, stack);
} on StateError catch (_, _) {
caught = true;
rethrow;
}
} on StateError catch (e, s) {
Expect.isTrue(caught);
Expect.identical(error, e);
Expect.equals(stack.toString(), s.toString());
Expect.equals(stack.toString(), e.stackTrace.toString());
}

// Single wildcard catch clause variable.
try {
try {
Error.throwWithStackTrace(error, stack);
} on StateError catch (_) {
caught = true;
rethrow;
}
} on StateError catch (e, s) {
Expect.isTrue(caught);
Expect.identical(error, e);
Expect.equals(stack.toString(), s.toString());
Expect.equals(stack.toString(), e.stackTrace.toString());
}

try {
try {
Error.throwWithStackTrace(error, stack);
} on StateError catch (_, s) {
Expect.equals(stack.toString(), s.toString());
caught = true;
rethrow;
}
} on StateError catch (e, s) {
Expect.isTrue(caught);
Expect.identical(error, e);
Expect.equals(stack.toString(), s.toString());
Expect.equals(stack.toString(), e.stackTrace.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.

// Tests multiple catch clause wildcard variable declarations.

// SharedOptions=--enable-experiment=wildcard-variables

void main() {
try {
throw '!';
} on Exception catch (_, _) {
rethrow; // Should not hit this catch.
} catch (_, _) {
print('catch');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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.

// Tests for-loop wildcard variable declarations.

// SharedOptions=--enable-experiment=wildcard-variables

void main() async {
// Multiple for-loop wildcard declarations.
for (int _ = 0, _ = 2;;) {
break;
}

var list = [];
for (var _ in list) {}

var stream = Stream.empty();
await for (var _ in stream) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// 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.

// Tests multiple wildcard function parameters.

// SharedOptions=--enable-experiment=wildcard-variables

import 'package:expect/expect.dart';

class Constructor {
final _;

// TODO(kallentu): Update this once the behaviour of super._ is finalized.
// https://github.com/dart-lang/language/issues/3792
Constructor.multiple(_, this._, void _()) {}
Constructor.parameter(_, _, this._) {}
Constructor.functionType(this._, void _(), void _()) {}
}

bool topLevelFunction(int _, int _) => true;
bool topLevelFunction2(_, _) => true;
bool topLevelFunction_functionType(void _(), void _()) => true;

class InstanceMethod {
bool instanceMethod(int _, int _) => true;
bool instanceMethod2(_, _) => true;
bool instanceMethod_functionType(void _(), void _()) => true;

// User-defined operators
int operator +(int _) => 42;
void operator []=(int _, _) {}

// Inherited `Object` methods
dynamic noSuchMethod(Invocation _) => true;
bool operator ==(Object _) => true;
}

abstract class AbstractMethod {
bool abstractMethod(int _, int _);
bool abstractMethod2(_, _);
bool abstractMethod_functionType(void _(), void _());
}

class AbstractMethodSubclass extends AbstractMethod {
bool abstractMethod(int _, int _) => true;
bool abstractMethod2(_, _) => true;
bool abstractMethod_functionType(void _(), void _()) => true;
}

class Setter {
int _x;
Setter(this._x);

int get x => _x;
set x(int _) => _x = 2;
}

class StaticMethod {
static int staticMethod(int _, int _) => 2;
static int staticMethod2(_, _) => 2;
static int staticMethod_functionType(void _(), void _()) => 2;
static int staticMethod_functionTypeNested(
void _(_, _), void _(int _, int _)) =>
2;
static int staticMethod_functionTypeNew(
void Function(int _, int _) _, void Function(int _, int _) _) =>
2;
static int staticMethod_functionTypeGeneric(
void Function<_, _>(int _, int _) _, void _<_>(_, _)) =>
2;
}

void main() {
// Function expression
var list = [true];
list.where(
(_, [_]) => true,
);

// Abstract methods
var abstractMethod = AbstractMethodSubclass();
abstractMethod.abstractMethod(1, 2);
abstractMethod.abstractMethod2(1, 2);
abstractMethod.abstractMethod_functionType(() {}, () {});

// Static methods
StaticMethod.staticMethod(1, 2);
StaticMethod.staticMethod2(1, 2);
StaticMethod.staticMethod_functionType(() {}, () {});
StaticMethod.staticMethod_functionTypeNested((e, x) {}, (e, x) {});
StaticMethod.staticMethod_functionTypeNew((e, x) {}, (e, x) {});
StaticMethod.staticMethod_functionTypeNested((_, _) {}, (_, _) {});
StaticMethod.staticMethod_functionTypeNew((_, _) {}, (_, _) {});

// Top level functions
topLevelFunction(1, 2);
topLevelFunction2(1, 2);
topLevelFunction_functionType(() {}, () {});

// Instance methods
var instanceMethod = InstanceMethod();
instanceMethod.instanceMethod(1, 2);
instanceMethod.instanceMethod2(1, 2);
instanceMethod.instanceMethod_functionType(() {}, () {});
Expect.equals(42, instanceMethod + 2);
instanceMethod[1] = 2;
Expect.isTrue(instanceMethod == 2);
Expect.isTrue((instanceMethod as dynamic).noMethod());

// Constructor
Constructor.multiple(1, 2, () {});
Constructor.parameter(1, 2, 3);
Constructor.functionType(() {}, () {}, () {});

// Setter
var setter = Setter(1);
setter.x = 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.

// Tests multiple local wildcard variable declarations.

// SharedOptions=--enable-experiment=wildcard-variables

void main() {
var _ = 1;
int _ = 2;
final _ = 3;

var i = 2, _ = 2;
i = i + 1;

int _;
final _;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.

// Tests multiple wildcard generic type parameters.

// SharedOptions=--enable-experiment=wildcard-variables

// Class type parameters
class T<_, _> {}

typedef ForgetfulMap<_, _> = Map<Object?, Object?>;
typedef F<_, X, _> = X Function<_ extends X>(X _, X _);

// Function type parameters
void genericFunction<_, _>() {}
void genericFunction2<_ extends Iterable<int>, _ extends num>() {}
void genericFunction3<_ extends void Function<_>(_, _), _>() {}

void main() {
void genericCallback(bool Function<T, E>() func) {}
genericCallback(<_, _>() => true);
}

0 comments on commit 03ea9eb

Please sign in to comment.