Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: tests/language/conditional_method_invocation_test.dart

Issue 1255293005: Fix analyzer interpretation of 'ClassName?.staticMember'. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 // Verify semantics of the ?. operator when it is used to invoke a method. 5 // Verify semantics of the ?. operator when it is used to invoke a method.
6 6
7 import "package:expect/expect.dart"; 7 import "package:expect/expect.dart";
8 import "conditional_access_helper.dart" as h; 8 import "conditional_access_helper.dart" as h;
9 9
10 bad() { 10 bad() {
11 Expect.fail('Should not be executed'); 11 Expect.fail('Should not be executed');
12 } 12 }
13 13
14 noMethod(e) => e is NoSuchMethodError; 14 noMethod(e) => e is NoSuchMethodError;
15 15
16 class B {} 16 class B {}
17 17
18 class C extends B { 18 class C extends B {
19 f(callback()) => callback(); 19 f(callback()) => callback();
20 int g(int callback()) => callback(); 20 int g(int callback()) => callback();
21 static void staticMethod() {} 21 static staticF(callback()) => callback();
22 static int staticG(int callback()) => callback();
22 } 23 }
23 24
24 C nullC() => null; 25 C nullC() => null;
25 26
26 main() { 27 main() {
27 // Make sure the "none" test fails if method invocation using "?." is not 28 // Make sure the "none" test fails if method invocation using "?." is not
28 // implemented. This makes status files easier to maintain. 29 // implemented. This makes status files easier to maintain.
29 nullC()?.f(null); 30 nullC()?.f(null);
30 31
31 // o?.m(...) is equivalent to ((x) => x == null ? null : x.m(...))(o). 32 // o?.m(...) is equivalent to ((x) => x == null ? null : x.m(...))(o).
32 Expect.equals(null, nullC()?.f(bad())); /// 01: ok 33 Expect.equals(null, nullC()?.f(bad())); /// 01: ok
33 Expect.equals(1, new C()?.f(() => 1)); /// 02: ok 34 Expect.equals(1, new C()?.f(() => 1)); /// 02: ok
34 35
36 // C?.m(...) is equivalent to C.m(...).
37 Expect.equals(1, C?.staticF(() => 1)); /// 14: ok
38 Expect.equals(1, h.C?.staticF(() => 1)); /// 15: ok
39
35 // The static type of o?.m(...) is the same as the static type of 40 // The static type of o?.m(...) is the same as the static type of
36 // o.m(...). 41 // o.m(...).
37 { int i = nullC()?.g(bad()); Expect.equals(null, i); } /// 03: ok 42 { int i = nullC()?.g(bad()); Expect.equals(null, i); } /// 03: ok
38 { int i = new C()?.g(() => 1); Expect.equals(1, i); } /// 04: ok 43 { int i = new C()?.g(() => 1); Expect.equals(1, i); } /// 04: ok
39 { String s = nullC()?.g(bad()); Expect.equals(null, s); } /// 05: static type warning 44 { String s = nullC()?.g(bad()); Expect.equals(null, s); } /// 05: static type warning
40 { String s = new C()?.g(() => null); Expect.equals(null, s); } /// 06: static type warning 45 { String s = new C()?.g(() => null); Expect.equals(null, s); } /// 06: static type warning
46 { int i = C?.staticG(() => 1); Expect.equals(1, i); } /// 16: ok
47 { int i = h.C?.staticG(() => 1); Expect.equals(1, i); } /// 17: ok
48 { String s = C?.staticG(() => null); Expect.equals(null, s); } /// 18: static type warning
49 { String s = h.C?.staticG(() => null); Expect.equals(null, s); } /// 19: stati c type warning
41 50
42 // Let T be the static type of o and let y be a fresh variable of type T. 51 // Let T be the static type of o and let y be a fresh variable of type T.
43 // Exactly the same static warnings that would be caused by y.m(...) are also 52 // Exactly the same static warnings that would be caused by y.m(...) are also
44 // generated in the case of o?.m(...). 53 // generated in the case of o?.m(...).
45 { B b = new C(); Expect.equals(1, b?.f(() => 1)); } /// 07: static type warnin g 54 { B b = new C(); Expect.equals(1, b?.f(() => 1)); } /// 07: static type warnin g
46 { int i = 1; Expect.equals(null, nullC()?.f(i)); } /// 08: static type warning 55 { int i = 1; Expect.equals(null, nullC()?.f(i)); } /// 08: static type warning
47 56
48 // Consequently, '?.' cannot be used to invoke static methods of classes. 57 // '?.' can't be used to access toplevel functions in libraries imported via
49 Expect.throws(() => C?.staticMethod(), noMethod); /// 09: static type warning
50 Expect.throws(() => h.C?.staticMethod(), noMethod); /// 10: static type warnin g
51
52 // Nor can it be used to access toplevel functions in libraries imported via
53 // prefix. 58 // prefix.
54 h?.topLevelFunction(); /// 11: compile-time error 59 h?.topLevelFunction(); /// 11: compile-time error
55 60
56 // However, '?.' can be used to access the toString method on the class Type. 61 // Nor can it be used to access the toString method on the class Type.
57 Expect.equals(C?.toString(), (C).toString()); /// 12: ok 62 Expect.throws(() => C?.toString(), noMethod); /// 12: static type warning
58 Expect.equals(h.C?.toString(), (h.C).toString()); /// 13: ok 63 Expect.throws(() => h.C?.toString(), noMethod); /// 13: static type warning
59 } 64 }
OLDNEW
« no previous file with comments | « tests/language/conditional_access_helper.dart ('k') | tests/language/conditional_property_access_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698