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 an attempt to invoke a prefix is handled consistently with the | 5 // Validate the following spec text from section 16.14.3 (Unqualified |
6 // following spec text from section 16.14.3 (Unqualified invocation): | 6 // invocation): |
7 // An unqualifiedfunction invocation i has the form | 7 // An unqualifiedfunction invocation i has the form |
8 // id(a1, ..., an, xn+1 : an+1, ..., xn+k : an+k), | 8 // id(a1, ..., an, xn+1 : an+1, ..., xn+k : an+k), |
9 // where id is an identifier. | 9 // where id is an identifier. |
10 // If there exists a lexically visible declaration named id, let fid be the | 10 // If there exists a lexically visible declaration named id, let fid be the |
11 // innermost such declaration. Then | 11 // innermost such declaration. Then: |
12 // - If fid isa local function, a library function, a library or static | 12 // - If fid is a prefix object, a compile-time error occurs. |
13 // getter or a variable then ... | |
14 // - Otherwise, if fid is a static method of the enclosing class C, ... | |
15 // - Otherwise, fid is considered equivalent to the ordinary method | |
16 // invocation this.id(a1, ..., an, xn+1 : an+1, ..., xn+k : an+k). | |
17 // | |
18 // Therefore, if p is an import prefix, evaluation of "p()" should be | |
19 // equivalent to "this.p()". That is, it should call the method "p" | |
20 // dynamically if inside a method, and should produce a NoSucMethodError (and a | |
21 // static warning) outside a method. | |
22 | 13 |
23 import "package:expect/expect.dart"; | |
24 import "empty_library.dart" as p; | 14 import "empty_library.dart" as p; |
25 | 15 |
26 class Base { | 16 class Base { |
27 var pCalled = false; | 17 void p() {} |
28 | |
29 void p() { | |
30 pCalled = true; | |
31 } | |
32 } | 18 } |
33 | 19 |
34 class Derived extends Base { | 20 class Derived extends Base { |
35 void f() { | 21 void f() { |
36 p(); Expect.isTrue(pCalled); /// 01: ok | 22 p(); /// 01: compile-time error |
37 } | 23 } |
38 } | 24 } |
39 | 25 |
40 noMethod(e) => e is NoSuchMethodError; | |
41 | |
42 main() { | 26 main() { |
43 new Derived().f(); | 27 new Derived().f(); |
44 Expect.throws(() { p(); }, noMethod); /// 02: static type warning | 28 p(); /// 02: compile-time error |
45 } | 29 } |
OLD | NEW |