Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 5ed8847

Browse files
munificentcommit-bot@chromium.org
authored andcommitted
Migrate language_2/function to NNBD.
Change-Id: I0726fa252736a83a2d80345d71c05aacf8ed0649 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/142941 Commit-Queue: Bob Nystrom <[email protected]> Auto-Submit: Bob Nystrom <[email protected]> Reviewed-by: Leaf Petersen <[email protected]>
1 parent c99da68 commit 5ed8847

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2988
-1
lines changed

sdk_nnbd/lib/_internal/js_runtime/lib/core_patch.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ class StringBuffer {
639639
return string;
640640
}
641641

642-
static String _writeOne(String string, Object obj) {
642+
static String _writeOne(String string, Object? obj) {
643643
return Primitives.stringConcatUnchecked(string, '$obj');
644644
}
645645
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2013, 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+
library dynamic_type_helper;
6+
7+
import 'package:expect/expect.dart';
8+
9+
/// Checks that a dynamic type error is thrown when [f] is executed
10+
/// and [expectTypeError] is `true`.
11+
void testDynamicTypeError(bool expectTypeError, f(), [String? message]) {
12+
if (expectTypeError) {
13+
checkDynamicTypeError(f, message);
14+
} else {
15+
checkNoDynamicTypeError(f, message);
16+
}
17+
}
18+
19+
/// Checks that a dynamic type error is thrown when f is executed.
20+
void checkDynamicTypeError(f(), [String? message]) {
21+
message = message != null ? ': $message' : '';
22+
try {
23+
f();
24+
Expect.fail('Missing type error$message.');
25+
} on TypeError catch (e) {}
26+
}
27+
28+
/// Checks that no dynamic type error is thrown when [f] is executed.
29+
void checkNoDynamicTypeError(f(), [String? message]) {
30+
message = message != null ? ': $message' : '';
31+
try {
32+
f();
33+
} on TypeError catch (e) {
34+
Expect.fail('Unexpected type error$message.');
35+
}
36+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2018, 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+
import "package:expect/expect.dart";
6+
7+
makeFn() {
8+
return <T extends num>({T? a1, T? a2, T? a3, T? a4, T? a5}) {
9+
return <T?>[a1, a2, a3, a4, a5];
10+
};
11+
}
12+
13+
staticFn<T extends num>({T? a1, T? a2, T? a3, T? a4, T? a5, T? xx}) {
14+
return <T?>[a1, a2, a3, a4, a5, xx];
15+
}
16+
17+
class CCC {
18+
memberFn<T extends num>({T? a1, T? a2, T? a3, T? a4, T? a5, T? yy}) {
19+
return <T?>[a1, a2, a3, a4, a5, yy];
20+
}
21+
}
22+
23+
check(a, b) {
24+
print('a: $a\nb: $b');
25+
Expect.equals(a.toString(), b.toString());
26+
}
27+
28+
main() {
29+
check('[null, 33, null, 11, 22, null]',
30+
Function.apply(new CCC().memberFn, [], {#a4: 11, #a5: 22, #a2: 33}));
31+
32+
Expect.throwsTypeError(
33+
() => Function.apply(new CCC().memberFn, [], {#a3: 'hi'}));
34+
35+
check('[11, 22, 33, null, null]',
36+
Function.apply(makeFn(), [], {#a1: 11, #a2: 22, #a3: 33}));
37+
38+
check('[null, 33, null, 11, 22]',
39+
Function.apply(makeFn(), [], {#a4: 11, #a5: 22, #a2: 33}));
40+
41+
Expect.throwsTypeError(() => Function.apply(makeFn(), [], {#a3: 'hi'}));
42+
43+
check('[null, 33, null, 11, 22, null]',
44+
Function.apply(staticFn, [], {#a4: 11, #a5: 22, #a2: 33}));
45+
46+
Expect.throwsTypeError(() => Function.apply(staticFn, [], {#a3: 'hi'}));
47+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2018, 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+
import "package:expect/expect.dart";
6+
7+
List<T?> staticFn<T>(
8+
[T? a1, T? a2, T? a3, T? a4, T? a5, T? a6, T? a7, T? a8, T? a9, T? a10]) {
9+
return <T?>[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10];
10+
}
11+
12+
class C<CT> {
13+
List<T?> memberFn<T>(
14+
[T? a1, T? a2, T? a3, T? a4, T? a5, T? a6, T? a7, T? a8, T? a9, T? a10]) {
15+
return <T?>[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10];
16+
}
17+
18+
// Intercepted, e.g. on JSArray.
19+
List<T?> map<T>(
20+
[T? a1, T? a2, T? a3, T? a4, T? a5, T? a6, T? a7, T? a8, T? a9, T? a10]) {
21+
return <T?>[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10];
22+
}
23+
}
24+
25+
check(a, b) {
26+
print('a: $a\nb: $b');
27+
Expect.equals(a.toString(), b.toString());
28+
}
29+
30+
main() {
31+
check('[1, 2, 3, null, null, null, null, null, null, null]',
32+
Function.apply(staticFn, [1, 2, 3]));
33+
34+
check('[1, 2, 3, 4, null, null, null, null, null, null]',
35+
Function.apply(staticFn, [1, 2, 3, 4]));
36+
37+
check('[1, 2, 3, 4, 5, 6, 7, null, null, null]',
38+
Function.apply(staticFn, [1, 2, 3, 4, 5, 6, 7]));
39+
40+
var o = new C<num>();
41+
dynamic memberFn1 = o.map;
42+
43+
check('[1, 2, 3, null, null, null, null, null, null, null]',
44+
Function.apply(memberFn1, [1, 2, 3]));
45+
46+
check('[1, 2, 3, 4, null, null, null, null, null, null]',
47+
Function.apply(memberFn1, [1, 2, 3, 4]));
48+
49+
check('[1, 2, 3, 4, 5, 6, 7, null, null, null]',
50+
Function.apply(memberFn1, [1, 2, 3, 4, 5, 6, 7]));
51+
52+
dynamic memberFn2 = o.memberFn;
53+
54+
check('[1, 2, 3, null, null, null, null, null, null, null]',
55+
Function.apply(memberFn2, [1, 2, 3]));
56+
57+
check('[1, 2, 3, 4, null, null, null, null, null, null]',
58+
Function.apply(memberFn2, [1, 2, 3, 4]));
59+
60+
check('[1, 2, 3, 4, 5, 6, 7, null, null, null]',
61+
Function.apply(memberFn2, [1, 2, 3, 4, 5, 6, 7]));
62+
63+
// TODO(sra): Apply of instantiations
64+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2011, 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+
// Dart test for function passing.
5+
6+
import "package:expect/expect.dart";
7+
8+
class FunctionArgumentTest {
9+
static testMe(Function f) {
10+
return f();
11+
}
12+
13+
static void testMain() {
14+
Expect.equals(42, testMe(() {
15+
return 42;
16+
}));
17+
}
18+
}
19+
20+
main() {
21+
FunctionArgumentTest.testMain();
22+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) 2018, 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+
// dart2jsOptions=-Ddart.isdart2js=true
5+
6+
import "package:expect/expect.dart";
7+
8+
@pragma('dart2js:noInline')
9+
List staticFn<T>([T? a1, T? a2, T? a3, T? a4, T? a5]) => [T, a1, a2, a3, a4, a5];
10+
11+
class C {
12+
@pragma('dart2js:noInline')
13+
List memberFn<T>([T? a1, T? a2, T? a3, T? a4, T? a5]) => [T, a1, a2, a3, a4, a5];
14+
15+
@pragma('dart2js:noInline')
16+
// 'map' is implemented by native iterables. On dart2js, 'map' has interceptor
17+
// calling convention.
18+
List map<T>([T? a1, T? a2, T? a3, T? a4, T? a5]) => [T, a1, a2, a3, a4, a5];
19+
}
20+
21+
check(expected, actual) {
22+
print('a: $expected');
23+
print('b: $actual');
24+
if (((actual[0] == Object && expected[0] == dynamic) ||
25+
(actual[0] == dynamic && expected[0] == Object)) &&
26+
!const bool.fromEnvironment('dart.isdart2js')) {
27+
// TODO(32483): dartdevk sometimes defaults type to 'Object' when 'dynamic'
28+
// is required. Remove this hack when fixed.
29+
// TODO(31581): dart2js needs instantiate-to-bound to generic 'dynamic'
30+
// instead of 'Object'.
31+
actual = actual.toList()..[0] = expected[0];
32+
print('b*: $actual');
33+
}
34+
Expect.equals(expected.toString(), actual.toString());
35+
}
36+
37+
main() {
38+
check([dynamic, 1, 2, 3, null, null], staticFn(1 as dynamic, 2, 3));
39+
40+
check([Object, 'Z', 2, 4, null, null], staticFn('Z', 2, 4));
41+
42+
check([int, 3, 2, 1, null, null], staticFn(3, 2, 1));
43+
44+
dynamic f1 = staticFn;
45+
46+
check([dynamic, 4, 2, 3, null, null], f1(4 as dynamic, 2, 3));
47+
48+
check([dynamic, 'Q', 2, 3, null, null], f1('Q', 2, 3));
49+
50+
check([dynamic, 6, 2, 3, null, null], f1(6, 2, 3));
51+
52+
check([int, 7, 2, null, null, null], f1<int>(7, 2));
53+
54+
var c = new C();
55+
56+
check([dynamic, 8, 2, 3, null, null], c.memberFn(8 as dynamic, 2, 3));
57+
58+
check([Object, 'A', 2, 3, null, null], c.memberFn('A', 2, 3));
59+
60+
check([int, 9, 2, 3, null, null], c.memberFn<int>(9, 2, 3));
61+
62+
check([dynamic, 10, 2, 3, null, null], c.map(10 as dynamic, 2, 3));
63+
64+
check([Object, 'B', 2, 3, null, null], c.map('B', 2, 3));
65+
66+
check([int, 11, 2, 3, null, null], c.map(11, 2, 3));
67+
68+
dynamic o = new C();
69+
70+
check([dynamic, 12, 2, 3, null, null], o.memberFn(12 as dynamic, 2, 3));
71+
72+
check([dynamic, 'C', 2, 3, null, null], o.memberFn('C', 2, 3));
73+
74+
check([int, 13, 2, null, null, null], o.memberFn<int>(13, 2));
75+
76+
check([dynamic, 14, 2, 3, null, null], o.map(14 as dynamic, 2, 3));
77+
78+
check([dynamic, 'D', 2, 3, null, null], o.map('D', 2, 3));
79+
80+
check([int, 15, null, null, null, null], o.map<int>(15));
81+
82+
check([int, 16, 2, 3, 4, null], o.map<int>(16, 2, 3, 4));
83+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2011, 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+
// VMOptions=--fatal-type-errors --enable_type_checks
5+
//
6+
// Test of calling Function, which is field of some class.
7+
8+
import "package:expect/expect.dart";
9+
10+
class Wrapper {
11+
late Function f;
12+
}
13+
14+
main() {
15+
Wrapper w = new Wrapper();
16+
w.f = () {
17+
return 42;
18+
};
19+
Expect.equals(42, w.f());
20+
}

0 commit comments

Comments
 (0)