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

Side by Side Diff: tests/language/conditional_property_access_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 does not appear on the LHS of an 5 // Verify semantics of the ?. operator when it does not appear on the LHS of an
6 // assignment. 6 // assignment.
7 7
8 import "package:expect/expect.dart"; 8 import "package:expect/expect.dart";
9 import "conditional_access_helper.dart" as h; 9 import "conditional_access_helper.dart" as h;
10 10
11 noMethod(e) => e is NoSuchMethodError; 11 noMethod(e) => e is NoSuchMethodError;
12 12
13 class B {} 13 class B {}
14 14
15 class C extends B { 15 class C extends B {
16 int v; 16 int v;
17 C(this.v); 17 C(this.v);
18 static var staticField; 18 static int staticInt;
19 } 19 }
20 20
21 C nullC() => null; 21 C nullC() => null;
22 22
23 main() { 23 main() {
24 // Make sure the "none" test fails if property access using "?." is not 24 // Make sure the "none" test fails if property access using "?." is not
25 // implemented. This makes status files easier to maintain. 25 // implemented. This makes status files easier to maintain.
26 nullC()?.v; 26 nullC()?.v;
27 27
28 // e1?.id is equivalent to ((x) => x == null ? null : x.id)(e1). 28 // e1?.id is equivalent to ((x) => x == null ? null : x.id)(e1).
29 Expect.equals(null, nullC()?.v); /// 01: ok 29 Expect.equals(null, nullC()?.v); /// 01: ok
30 Expect.equals(1, new C(1)?.v); /// 02: ok 30 Expect.equals(1, new C(1)?.v); /// 02: ok
31 31
32 // C?.id is equivalent to C.id.
33 { C.staticInt = 1; Expect.equals(1, C?.staticInt); } /// 12: ok
34 { h.C.staticInt = 1; Expect.equals(1, h.C?.staticInt); } /// 13: ok
35
32 // The static type of e1?.d is the static type of e1.id. 36 // The static type of e1?.d is the static type of e1.id.
33 { int i = new C(1)?.v; Expect.equals(1, i); } /// 03: ok 37 { int i = new C(1)?.v; Expect.equals(1, i); } /// 03: ok
34 { String s = new C(null)?.v; Expect.equals(null, s); } /// 04: static type war ning 38 { String s = new C(null)?.v; Expect.equals(null, s); } /// 04: static type war ning
39 { C.staticInt = 1; int i = C?.staticInt; Expect.equals(1, i); } /// 14: ok
40 { h.C.staticInt = 1; int i = h.C?.staticInt; Expect.equals(1, i); } /// 15: ok
41 { C.staticInt = null; String s = C?.staticInt; Expect.equals(null, s); } /// 1 6: static type warning
42 { h.C.staticInt = null; String s = h.C?.staticInt; Expect.equals(null, s); } / // 17: static type warning
35 43
36 // Let T be the static type of e1 and let y be a fresh variable of type T. 44 // Let T be the static type of e1 and let y be a fresh variable of type T.
37 // Exactly the same static warnings that would be caused by y.id are also 45 // Exactly the same static warnings that would be caused by y.id are also
38 // generated in the case of e1?.id. 46 // generated in the case of e1?.id.
39 Expect.equals(null, nullC()?.bad); /// 05: static type warning 47 Expect.equals(null, nullC()?.bad); /// 05: static type warning
40 { B b = new C(1); Expect.equals(1, b?.v); } /// 06: static type warning 48 { B b = new C(1); Expect.equals(1, b?.v); } /// 06: static type warning
41 49
42 // Consequently, '?.' cannot be used to access static properties of classes. 50 // '?.' cannot be used to access toplevel properties in libraries imported via
43 Expect.throws(() => C?.staticField, noMethod); /// 07: static type warning
44 Expect.throws(() => h.C?.staticField, noMethod); /// 08: static type warning
45
46 // Nor can it be used to access toplevel properties in libraries imported via
47 // prefix. 51 // prefix.
48 var x = h?.topLevelVar; /// 09: compile-time error 52 var x = h?.topLevelVar; /// 09: compile-time error
49 53
50 // However, '?.' can be used to access the hashCode getter on the class Type. 54 // Nor can it be used to access the hashCode getter on the class Type.
51 Expect.equals(C?.hashCode, (C).hashCode); /// 10: ok 55 Expect.throws(() => C?.hashCode, noMethod); /// 10: static type warning
52 Expect.equals(h.C?.hashCode, (h.C).hashCode); /// 11: ok 56 Expect.throws(() => h.C?.hashCode, noMethod); /// 11: static type warning
53 } 57 }
OLDNEW
« no previous file with comments | « tests/language/conditional_method_invocation_test.dart ('k') | tests/language/conditional_property_assignment_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698