Skip to content

Commit 739852d

Browse files
munificentcommit-bot@chromium.org
authored andcommitted
Migrate language_2/call to NNBD.
Change-Id: I3356296e72dfa07b0ac994da0ce96f766b182e80 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/137722 Commit-Queue: Bob Nystrom <[email protected]> Auto-Submit: Bob Nystrom <[email protected]> Reviewed-by: Leaf Petersen <[email protected]>
1 parent 48c6180 commit 739852d

File tree

47 files changed

+1864
-0
lines changed

Some content is hidden

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

47 files changed

+1864
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
import "package:expect/expect.dart";
6+
7+
class A {
8+
call(a) => a is num;
9+
}
10+
11+
main() {
12+
Expect.isTrue(new A().call(42));
13+
Expect.isFalse(new A()('foo'));
14+
}

tests/language/call/call_test.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2012, 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=--optimization_counter_threshold=10 --no-background_compilation
5+
6+
import "package:expect/expect.dart";
7+
8+
main() {
9+
bar(a) {
10+
return a is String;
11+
}
12+
13+
for (var i = 0; i < 20; i++) {
14+
Expect.isFalse(bar(1));
15+
Expect.isTrue(bar.call('foo'));
16+
}
17+
18+
opt_arg([a = "a"]) => a is String;
19+
20+
for (var i = 0; i < 20; i++) {
21+
Expect.isFalse(opt_arg(1));
22+
Expect.isFalse(opt_arg.call(1));
23+
Expect.isTrue(opt_arg());
24+
Expect.isTrue(opt_arg.call());
25+
Expect.isTrue(opt_arg("b"));
26+
Expect.isTrue(opt_arg.call("b"));
27+
}
28+
29+
named_arg({x: 11, y: 22}) => "$x$y";
30+
31+
for (var i = 0; i < 20; i++) {
32+
Expect.equals("1122", named_arg());
33+
Expect.equals("1122", named_arg.call());
34+
Expect.equals("4455", named_arg(y: 55, x: 44));
35+
Expect.equals("4455", named_arg.call(y: 55, x: 44));
36+
Expect.equals("4455", named_arg(x: 44, y: 55));
37+
Expect.equals("4455", named_arg.call(x: 44, y: 55));
38+
}
39+
40+
Expect.throwsNoSuchMethodError(() => (bar as dynamic).call());
41+
Expect.throwsNoSuchMethodError(() => (opt_arg as dynamic).call(x: "p"));
42+
Expect.throwsNoSuchMethodError(() => (named_arg as dynamic).call("p", "q"));
43+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2014, 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=--optimization_counter_threshold=10 --no-background_compilation
5+
6+
import "package:expect/expect.dart";
7+
8+
main() {
9+
bar(a) {
10+
return a is String;
11+
}
12+
13+
var bar_tearOff = bar.call;
14+
15+
for (var i = 0; i < 20; i++) {
16+
Expect.isFalse(bar_tearOff(1));
17+
Expect.isTrue(bar_tearOff.call('foo'));
18+
Expect.isFalse(bar_tearOff.call(1));
19+
Expect.isTrue(bar_tearOff('foo'));
20+
}
21+
22+
opt_arg([a = "a"]) => a is String;
23+
var opt_arg_tearOff = opt_arg.call;
24+
25+
for (var i = 0; i < 20; i++) {
26+
Expect.isFalse(opt_arg_tearOff(1));
27+
Expect.isFalse(opt_arg_tearOff.call(1));
28+
Expect.isTrue(opt_arg_tearOff());
29+
Expect.isTrue(opt_arg_tearOff.call());
30+
Expect.isTrue(opt_arg_tearOff("b"));
31+
Expect.isTrue(opt_arg_tearOff.call("b"));
32+
}
33+
34+
named_arg({x: 11, y: 22}) => "$x$y";
35+
var named_arg_tearOff = named_arg.call;
36+
37+
for (var i = 0; i < 20; i++) {
38+
Expect.equals("1122", named_arg_tearOff());
39+
Expect.equals("1122", named_arg_tearOff.call());
40+
Expect.equals("4455", named_arg_tearOff(y: 55, x: 44));
41+
Expect.equals("4455", named_arg_tearOff.call(y: 55, x: 44));
42+
Expect.equals("4455", named_arg_tearOff(x: 44, y: 55));
43+
Expect.equals("4455", named_arg_tearOff.call(x: 44, y: 55));
44+
}
45+
46+
// In order to test runtime behavior of calling with invalid arguments,
47+
// we cast to dynamic to make the type system forget about the actual types.
48+
dynamic bar_tearOff_d = bar_tearOff;
49+
dynamic opt_arg_tearOff_d = opt_arg_tearOff;
50+
dynamic named_arg_tearOff_d = named_arg_tearOff;
51+
Expect.throws(() => bar_tearOff_d.call());
52+
Expect.throwsNoSuchMethodError(() => opt_arg_tearOff_d.call(x: "p"));
53+
Expect.throwsNoSuchMethodError(() => named_arg_tearOff_d.call("p", "q"));
54+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// TODO(multitest): This was automatically migrated from a multitest and may
2+
// contain strange or dead code.
3+
4+
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
5+
// for details. All rights reserved. Use of this source code is governed by a
6+
// BSD-style license that can be found in the LICENSE file.
7+
8+
// Check that calling a constructor of a class that cannot be resolved causes
9+
// compile error.
10+
11+
import "package:expect/expect.dart";
12+
import 'dart:math';
13+
14+
main() {
15+
16+
17+
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2012, 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+
// Check that calling a constructor of a class that cannot be resolved causes
6+
// compile error.
7+
8+
import "package:expect/expect.dart";
9+
import 'dart:math';
10+
11+
main() {
12+
new A();
13+
// ^
14+
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_CLASS
15+
// [cfe] Method not found: 'A'.
16+
new A.foo();
17+
// ^^^^^
18+
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_CLASS
19+
// [cfe] Method not found: 'A.foo'.
20+
new lib.A();
21+
// ^^^^^
22+
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_CLASS
23+
// [cfe] Method not found: 'lib.A'.
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2016, 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+
typedef Object Func(Object x);
8+
9+
class Bar {
10+
int x = 42;
11+
12+
Object call(Object x) {
13+
return 'Bar $x';
14+
}
15+
}
16+
17+
Object baz(Object x) => x;
18+
19+
var map = <String, Func>{'baz': baz, 'bar': new Bar()};
20+
21+
Object test(String str, Object arg) {
22+
return map[str]!.call(arg);
23+
}
24+
25+
void main() {
26+
Expect.equals(42, test('baz', 42));
27+
Expect.equals('Bar 42', test('bar', 42));
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2015, 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+
class A {
8+
call({a: 42}) {
9+
return 499 + a;
10+
}
11+
}
12+
13+
main() {
14+
Expect.equals(497, Function.apply(new A(), [], {#a: -2}));
15+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) 2015, 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+
Object bar(Object x) {
8+
return x;
9+
}
10+
11+
Function baz = bar;
12+
13+
dynamic dyn = bar;
14+
15+
class Foo {
16+
Object call(Object x) {
17+
return 'Foo$x';
18+
}
19+
}
20+
21+
typedef Object FooType(Object x);
22+
FooType foo = bar;
23+
24+
void main() {
25+
Expect.equals(42, bar.call(42));
26+
Expect.equals(42, baz.call(42));
27+
Expect.equals(42, foo.call(42));
28+
Expect.equals(42, dyn.call(42));
29+
Expect.equals(42, bar(42));
30+
Expect.equals(42, baz(42));
31+
Expect.equals(42, foo(42));
32+
Expect.equals(42, dyn(42));
33+
34+
baz = new Foo();
35+
foo = new Foo();
36+
dyn = new Foo();
37+
Expect.equals('Foo42', baz.call(42));
38+
Expect.equals('Foo42', foo.call(42));
39+
Expect.equals('Foo42', dyn.call(42));
40+
Expect.equals('Foo42', baz(42));
41+
Expect.equals('Foo42', foo(42));
42+
Expect.equals('Foo42', dyn(42));
43+
44+
var s = (FooType).toString();
45+
var minified = s != 'FooType'; // dart2js --minify has minified names.
46+
dynamic d = null;
47+
Expect.throws(() => d(),
48+
(e) => e is NoSuchMethodError && (minified || '$e'.contains('call')));
49+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
// Dart test program to test arithmetic operations.
5+
6+
import "package:expect/expect.dart";
7+
8+
class B {}
9+
10+
class C {
11+
B call(B b) => b;
12+
}
13+
14+
class D implements Function {
15+
B call(B b) => b;
16+
}
17+
18+
typedef B BToB(B x);
19+
20+
typedef Object NullToObject(Null x);
21+
22+
main() {
23+
// The presence of a `.call` method does not cause class `C` to become a
24+
// subtype of any function type.
25+
C c = new C();
26+
Expect.throwsCastError(() => c as BToB); //# 01: ok
27+
Expect.throwsCastError(() => c as NullToObject); //# 02: ok
28+
Expect.throwsCastError(() => c as Function); //# 03: ok
29+
30+
// The same goes for class `D`: `implements Function` is ignored in Dart 2.
31+
D d = new D();
32+
Expect.throwsCastError(() => d as BToB); //# 04: ok
33+
Expect.throwsCastError(() => d as NullToObject); //# 05: ok
34+
Expect.throwsCastError(() => d as Function); //# 06: ok
35+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
// Dart test program to test arithmetic operations.
5+
6+
import "package:expect/expect.dart";
7+
8+
int f(int i) => 2 * i;
9+
10+
typedef int IntToInt(int x);
11+
12+
main() {
13+
// It is possible to use `.call` on a function-typed value (even though it is
14+
// redundant). Similarly, it is possible to tear off `.call` on a
15+
// function-typed value (but it is a no-op).
16+
Expect.equals(f.call(1), 2); //# 01: ok
17+
Expect.identical(f.call, f); //# 02: ok
18+
IntToInt f2 = f;
19+
Expect.equals(f2.call(1), 2); //# 03: ok
20+
Expect.identical(f2.call, f); //# 04: ok
21+
Function f3 = f;
22+
Expect.equals(f3.call(1), 2); //# 05: ok
23+
Expect.identical(f3.call, f); //# 06: ok
24+
dynamic d = f;
25+
Expect.equals(d.call(1), 2); //# 07: ok
26+
Expect.identical(d.call, f); //# 08: ok
27+
}

0 commit comments

Comments
 (0)