Migrate language_2/lazy to NNBD.

Change-Id: Icd7ea40455f207eca1dc817ff86823cfabed3de0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/149042
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Leaf Petersen <leafp@google.com>
This commit is contained in:
Robert Nystrom 2020-05-29 19:23:24 +00:00 committed by commit-bot@chromium.org
parent 8680ff91e2
commit 89c7c0b6e0
11 changed files with 340 additions and 0 deletions

View file

@ -0,0 +1,10 @@
// Copyright (c) 2012, 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.
import "package:expect/expect.dart";
var data = {'a': 'a'};
main() {
Expect.equals('a', data['a']);
}

View file

@ -0,0 +1,12 @@
// Copyright (c) 2012, 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.
import "package:expect/expect.dart";
final x = (int t) => t + 1;
main() {
Expect.equals(499, x(498));
Expect.equals(42, x(41));
}

View file

@ -0,0 +1,78 @@
// Copyright (c) 2012, 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.
import "package:expect/expect.dart";
final x = foo();
var x2 = foo2();
var x3 = foo3();
var x4 = foo4();
var x5 = foo5();
final x6 = foo6();
// Don't allow unlimited recursion to overflow the stack.
var x7Depth = 0;
int x7 = (++x7Depth > 10) ? x7Depth : x7 + 1;
foo() {
throw "interrupt initialization";
}
foo2() {
x2 = 499;
throw "interrupt initialization";
}
// Don't allow unlimited recursion to overflow the stack.
var foo3Depth = 0;
foo3() => (++foo3Depth > 10) ? foo3Depth : x3 + 1;
foo4() {
x4 = 498;
x4 = x4 + 1;
return x4;
}
foo5() {
x5 = 498;
x5 = x5 + 1;
}
foo6() {
try {
return x5 + 1;
} catch (e) {
return 499;
}
}
fib(x) {
if (x is! int) return 0;
if (x < 2) return x;
return fib(x - 1) + fib(x - 2);
}
main() {
// If an initializer throws, then accessing it again re-evaluates the
// initializer.
Expect.throws(() => fib(x), (e) => e == "interrupt initialization");
Expect.throws(() => fib(x), (e) => e == "interrupt initialization");
Expect.throws(() => fib(x), (e) => e == "interrupt initialization");
Expect.throws(() => fib(x2), (e) => e == "interrupt initialization");
// The store happened before the throw, so is there now.
Expect.equals(499, x2);
// This value means that the initializer did fully call itself recursively.
Expect.equals(21, x3);
Expect.equals(499, x4);
Expect.equals(null, x5);
Expect.equals(499, x6);
// This value means that the initializer did fully call itself recursively.
Expect.equals(21, x7);
}

View file

@ -0,0 +1,23 @@
// Copyright (c) 2012, 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.
import "package:expect/expect.dart";
final x = foo(499);
final y = foo(41) + 1;
final t = bar(499);
final u = bar(41) + 1;
final v = bar("some string");
foo(x) => x; // The return type will always be int.
bar(x) => x; // The return type varies and can be int or String.
main() {
Expect.equals(499, x);
Expect.equals(42, y);
Expect.equals(499, t);
Expect.equals(42, u);
Expect.equals("some string", v);
}

View file

@ -0,0 +1,12 @@
// Copyright (c) 2012, 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.
import "package:expect/expect.dart";
final x = (int t) => (int u) => t + u;
main() {
Expect.equals(499, x(498)(1));
Expect.equals(42, x(39)(3));
}

View file

@ -0,0 +1,28 @@
// Copyright (c) 2012, 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.
/*
* A lot of comments to make sure that the lazy initialization code has a
* position that does not fit into lazy_static6_test.dart file.
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
******************************************************************************
*/
part of lazy_static6_test;
final x = (int t) => (int u) => t + u;
main() {
Expect.equals(499, x(498)(1));
Expect.equals(42, x(39)(3));
}

View file

@ -0,0 +1,7 @@
// Copyright (c) 2012, 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.
library lazy_static6_test;
import "package:expect/expect.dart";
part "static6_src.dart";

View file

@ -0,0 +1,19 @@
// Copyright (c) 2015, 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.
import "package:expect/expect.dart";
var sideEffect = 0;
int x = (() {
sideEffect++;
return 499;
})();
main() {
if (new DateTime.now().day >= -1) {
x = 42;
}
Expect.equals(42, x);
Expect.equals(0, sideEffect);
}

View file

@ -0,0 +1,76 @@
// Copyright (c) 2017, 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.
import "package:expect/expect.dart";
// Test re-entrant initializer - calls throw StackOverflowError.
var trace;
final foo = bar;
var recursionDepth = 0;
get bar {
if (recursionDepth > 3) throw "recursed";
recursionDepth++;
trace.add(recursionDepth);
try {
return foo ?? 1;
} catch (e) {
trace.add(e);
}
return 42;
}
void testTopLevel() {
trace = [];
recursionDepth = 0;
var result = foo;
Expect.equals(42, result);
Expect.equals('1,2,3,4,recursed', trace.join(','));
trace = [];
recursionDepth = 0;
result = foo;
Expect.equals(42, result);
Expect.equals('', trace.join(','));
}
class X {
static final foo = X.bar;
static get bar {
if (recursionDepth > 3) throw "recursed";
recursionDepth++;
trace.add(recursionDepth);
try {
return foo ?? 1;
} catch (e) {
trace.add(e);
}
return 49;
}
}
void testClassStatic() {
trace = [];
recursionDepth = 0;
var result = X.foo;
Expect.equals(49, result);
Expect.equals('1,2,3,4,recursed', trace.join(','));
trace = [];
recursionDepth = 0;
result = X.foo;
Expect.equals(49, result);
Expect.equals('', trace.join(','));
}
main() {
testTopLevel();
testClassStatic();
}

View file

@ -0,0 +1,50 @@
// Copyright (c) 2012, 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.
import "package:expect/expect.dart";
final x = foo();
final y = y2(y3);
final y2 = incrementCreator();
final y3 = fib(5);
foo() => 499;
incrementCreator() => (x) => x + 1;
fib(x) {
if (x < 2) return x;
return fib(x - 1) + fib(x - 2);
}
var count = 0;
sideEffect() => count++;
final t = sideEffect();
var t2 = sideEffect();
class A {
static final a = toto();
static final b = b2(b3);
static final b2 = decrementCreator();
static final b3 = fact(5);
static toto() => 666;
static decrementCreator() => (x) => x - 1;
static fact(x) {
if (x <= 1) return x;
return x * fact(x - 1);
}
}
main() {
Expect.equals(499, x);
Expect.equals(6, y);
Expect.equals(666, A.a);
Expect.equals(119, A.b);
Expect.equals(0, t);
t2 = 499;
Expect.equals(499, t2);
Expect.equals(1, count);
}

View file

@ -0,0 +1,25 @@
// Copyright (c) 2013, 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.
// Test that engines will not infer that [a] is always of type int.
import 'package:expect/expect.dart';
var a = foo();
foo() {
() => 42;
if (true) throw 'Sorry';
return 42;
}
main() {
try {
a;
} catch (e) {
// Ignore.
}
Expect.throws(() {
if (a is num) throw 'Test failed';
}, (error) => error == 'Sorry');
}