OLD | NEW |
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 // Validate that assignment to a prefix is handled consistently with the | 5 // Validate that assignment to a prefix is handled consistently with the |
6 // following spec text from section 16.19 (Assignment): | 6 // following spec text from section 16.19 (Assignment): |
7 // Evaluation of an assignment a of the form v = e proceeds as follows: | 7 // Evaluation of an assignment a of the form v = e proceeds as follows: |
8 // Let d be the innermost declaration whose name is v or v=, if it exists. | 8 // Let d be the innermost declaration whose name is v or v=, if it exists. |
9 // If d is the declaration of a local variable, ... | 9 // It is a compile-time error if d denotes a prefix object. |
10 // If d is the declaration of a library variable, ... | |
11 // Otherwise, if d is the declaration of a static variable, ... | |
12 // Otherwise, if a ocurs inside a top level or static function (be it | |
13 // function, method, getter, or setter) or variable initializer, evaluation | |
14 // of a causes e to be evaluated, after which a NoSuchMethodError is thrown. | |
15 // Otherwise, the assignment is equivalent to the assignment this.v = e. | |
16 // | |
17 // Therefore, if p is an import prefix, evaluation of "p = ..." should be | |
18 // equivalent to "this.p = ..." inside a method, and should produce a | |
19 // NoSuchMethodError outside a method. | |
20 | 10 |
21 import "package:expect/expect.dart"; | |
22 import "empty_library.dart" as p; | 11 import "empty_library.dart" as p; |
23 | 12 |
24 class Base { | 13 class Base { |
25 var p; | 14 var p; |
26 } | 15 } |
27 | 16 |
28 class Derived extends Base { | 17 class Derived extends Base { |
29 void f() { | 18 void f() { |
30 p = 1; Expect.equals(1, this.p); /// 01: ok | 19 p = 1; /// 01: compile-time error |
31 } | 20 } |
32 } | 21 } |
33 | 22 |
34 bool gCalled = false; | |
35 | |
36 g() { | |
37 gCalled = true; | |
38 return 1; | |
39 } | |
40 | |
41 noMethod(e) => e is NoSuchMethodError; | |
42 | |
43 main() { | 23 main() { |
44 new Derived().f(); | 24 new Derived().f(); |
45 Expect.throws(() { p = g(); }, noMethod); Expect.isTrue(gCalled); /// 02: stat
ic type warning | 25 p = 1; /// 02: compile-time error |
46 } | 26 } |
OLD | NEW |