Skip to content

Commit

Permalink
[wildcard-variables] Add async rethrow test and wildcard patterns.
Browse files Browse the repository at this point in the history
Fix follow up comments with an async rethrow test.
Tests that wildcard patterns still work while mixed with wildcard variables.

Bug: #55652
Change-Id: If6ab4de68ff27ad51215427a7183f1aed7229947
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/367501
Reviewed-by: Erik Ernst <eernst@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
  • Loading branch information
kallentu authored and Commit Queue committed May 24, 2024
1 parent 8e405fd commit 70b9b46
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 and
// awaits.

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

import 'package:expect/expect.dart';
import "package:async_helper/async_helper.dart";

void main() async {
asyncStart();
test().then((_) => asyncEnd());
}

Future<void> test() async {
var tryValue = Future<int>.value(1);
var catchValue = Future<int>.value(1);

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

// Multiple wildcard catch clause variables.
try {
try {
await tryValue;
Error.throwWithStackTrace(error, stack);
} on StateError catch (_, _) {
await catchValue;
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 {
await tryValue;
Error.throwWithStackTrace(error, stack);
} on StateError catch (_) {
await catchValue;
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 {
await tryValue;
Error.throwWithStackTrace(error, stack);
} on StateError catch (_, s) {
await catchValue;
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
Expand Up @@ -6,6 +6,8 @@

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

import 'package:async_helper/async_helper.dart';

void main() async {
// Multiple for-loop wildcard declarations.
for (int _ = 0, _ = 2;;) {
Expand All @@ -15,6 +17,11 @@ void main() async {
var list = [];
for (var _ in list) {}

asyncStart();
streamTest().then((_) => asyncEnd());
}

Future streamTest() async {
var stream = Stream.empty();
await for (var _ in stream) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ class InstanceMember {
int _ = 2;
const _ = 2;
int _() => 2;
_ = 3;
Expect.equals(3, _);
var (_, _) = (3, '4');
Expect.equals(2, _);

int _() => 2;
_ = 4;
Expect.equals(4, _);

int foo<_>([int _ = 5]) => _;
Expect.equals(3, foo());
Expect.equals(4, foo());
}
}

Expand All @@ -41,10 +42,13 @@ class StaticMember {
const _ = 2;
const _ = _ - 1;
int _() => 2;
_ = 3;
Expect.equals(3, _);
var (_, _) = (3, '4');
Expect.equals(2, _);

_ = 4;
Expect.equals(4, _);

int foo<_>([int _ = _ + 1]) => _;
Expect.equals(3, foo());
Expect.equals(4, foo());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ void main() {
int _ = 1;
const _ = 1;
int _() => 1;
var (_, _) = (3, '4');

Expect.equals(2, _(1));
}

Expand All @@ -23,6 +25,8 @@ class Clas<_> {
int _ = 1;
const _ = 1;
int _() => 1;
var (_, _) = (3, '4');

Expect.equals(2, _(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ class CConst {
final _ = _;
const _ = _;
int _() => 1;
var (_, _) = (3, '4');
Expect.equals(42, _);

int foo<_>([String _ = "$_"]) => _;
_ = foo();
foo();
Expect.equals(42, _);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ void main() {
Clas().member();

int _ = _;
int _ = 2;
_ = 3;
Expect.equals(3, _);
int _ = 3;
var (_, _) = (3, '4');
Expect.equals(2, _);

_ = 4;
Expect.equals(4, _);
}

class Clas<_> {
void member<_>() {
int _ = _;
int _ = 2;
int _ = 3;
var (_, _) = (3, '4');
Expect.equals(2, _);

_ = 4;
Expect.equals(4, _);

Expand Down

0 comments on commit 70b9b46

Please sign in to comment.