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

Side by Side Diff: packages/analyzer/test/generated/compile_time_error_code_test.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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
(Empty)
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
5 library engine.compile_time_error_code_test;
6
7 import 'package:analyzer/src/generated/error.dart';
8 import 'package:analyzer/src/generated/parser.dart' show ParserErrorCode;
9 import 'package:analyzer/src/generated/source_io.dart';
10
11 import '../reflective_tests.dart';
12 import '../utils.dart';
13 import 'resolver_test.dart';
14
15 main() {
16 initializeTestEnvironment();
17 runReflectiveTests(CompileTimeErrorCodeTest);
18 }
19
20 @reflectiveTest
21 class CompileTimeErrorCodeTest extends ResolverTestCase {
22 void fail_awaitInWrongContext_sync() {
23 // This test requires better error recovery than we currently have. In
24 // particular, we need to be able to distinguish between an await expression
25 // in the wrong context, and the use of 'await' as an identifier.
26 Source source = addSource(r'''
27 f(x) {
28 return await x;
29 }''');
30 computeLibrarySourceErrors(source);
31 assertErrors(source, [CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT]);
32 verify([source]);
33 }
34
35 void fail_awaitInWrongContext_syncStar() {
36 // This test requires better error recovery than we currently have. In
37 // particular, we need to be able to distinguish between an await expression
38 // in the wrong context, and the use of 'await' as an identifier.
39 Source source = addSource(r'''
40 f(x) sync* {
41 yield await x;
42 }''');
43 computeLibrarySourceErrors(source);
44 assertErrors(source, [CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT]);
45 verify([source]);
46 }
47
48 void fail_compileTimeConstantRaisesException() {
49 Source source = addSource(r'''
50 ''');
51 computeLibrarySourceErrors(source);
52 assertErrors(
53 source, [CompileTimeErrorCode.COMPILE_TIME_CONSTANT_RAISES_EXCEPTION]);
54 verify([source]);
55 }
56
57 void fail_constEvalThrowsException() {
58 Source source = addSource(r'''
59 class C {
60 const C();
61 }
62 f() { return const C(); }''');
63 computeLibrarySourceErrors(source);
64 assertErrors(
65 source, [CompileTimeErrorCode.CONST_CONSTRUCTOR_THROWS_EXCEPTION]);
66 verify([source]);
67 }
68
69 void fail_invalidIdentifierInAsync_async() {
70 // TODO(brianwilkerson) Report this error.
71 Source source = addSource(r'''
72 class A {
73 m() async {
74 int async;
75 }
76 }''');
77 computeLibrarySourceErrors(source);
78 assertErrors(source, [CompileTimeErrorCode.INVALID_IDENTIFIER_IN_ASYNC]);
79 verify([source]);
80 }
81
82 void fail_invalidIdentifierInAsync_await() {
83 // TODO(brianwilkerson) Report this error.
84 Source source = addSource(r'''
85 class A {
86 m() async {
87 int await;
88 }
89 }''');
90 computeLibrarySourceErrors(source);
91 assertErrors(source, [CompileTimeErrorCode.INVALID_IDENTIFIER_IN_ASYNC]);
92 verify([source]);
93 }
94
95 void fail_invalidIdentifierInAsync_yield() {
96 // TODO(brianwilkerson) Report this error.
97 Source source = addSource(r'''
98 class A {
99 m() async {
100 int yield;
101 }
102 }''');
103 computeLibrarySourceErrors(source);
104 assertErrors(source, [CompileTimeErrorCode.INVALID_IDENTIFIER_IN_ASYNC]);
105 verify([source]);
106 }
107
108 void fail_mixinDeclaresConstructor() {
109 Source source = addSource(r'''
110 class A {
111 A() {}
112 }
113 class B extends Object mixin A {}''');
114 computeLibrarySourceErrors(source);
115 assertErrors(source, [CompileTimeErrorCode.MIXIN_DECLARES_CONSTRUCTOR]);
116 verify([source]);
117 }
118
119 void fail_mixinOfNonClass() {
120 // TODO(brianwilkerson) Compare with MIXIN_WITH_NON_CLASS_SUPERCLASS.
121 Source source = addSource(r'''
122 var A;
123 class B extends Object mixin A {}''');
124 computeLibrarySourceErrors(source);
125 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_NON_CLASS]);
126 verify([source]);
127 }
128
129 void fail_objectCannotExtendAnotherClass() {
130 Source source = addSource(r'''
131 ''');
132 computeLibrarySourceErrors(source);
133 assertErrors(
134 source, [CompileTimeErrorCode.OBJECT_CANNOT_EXTEND_ANOTHER_CLASS]);
135 verify([source]);
136 }
137
138 void fail_superInitializerInObject() {
139 Source source = addSource(r'''
140 ''');
141 computeLibrarySourceErrors(source);
142 assertErrors(source, [CompileTimeErrorCode.SUPER_INITIALIZER_IN_OBJECT]);
143 verify([source]);
144 }
145
146 void fail_yieldEachInNonGenerator_async() {
147 // TODO(brianwilkerson) We are currently parsing the yield statement as a
148 // binary expression.
149 Source source = addSource(r'''
150 f() async {
151 yield* 0;
152 }''');
153 computeLibrarySourceErrors(source);
154 assertErrors(source, [CompileTimeErrorCode.YIELD_EACH_IN_NON_GENERATOR]);
155 verify([source]);
156 }
157
158 void fail_yieldEachInNonGenerator_sync() {
159 // TODO(brianwilkerson) We are currently parsing the yield statement as a
160 // binary expression.
161 Source source = addSource(r'''
162 f() {
163 yield* 0;
164 }''');
165 computeLibrarySourceErrors(source);
166 assertErrors(source, [CompileTimeErrorCode.YIELD_IN_NON_GENERATOR]);
167 verify([source]);
168 }
169
170 void fail_yieldInNonGenerator_async() {
171 // TODO(brianwilkerson) We are currently trying to parse the yield statement
172 // as a binary expression.
173 Source source = addSource(r'''
174 f() async {
175 yield 0;
176 }''');
177 computeLibrarySourceErrors(source);
178 assertErrors(source, [CompileTimeErrorCode.YIELD_IN_NON_GENERATOR]);
179 verify([source]);
180 }
181
182 void fail_yieldInNonGenerator_sync() {
183 // TODO(brianwilkerson) We are currently trying to parse the yield statement
184 // as a binary expression.
185 Source source = addSource(r'''
186 f() {
187 yield 0;
188 }''');
189 computeLibrarySourceErrors(source);
190 assertErrors(source, [CompileTimeErrorCode.YIELD_EACH_IN_NON_GENERATOR]);
191 verify([source]);
192 }
193
194 void test_accessPrivateEnumField() {
195 Source source = addSource(r'''
196 enum E { ONE }
197 String name(E e) {
198 return e._name;
199 }''');
200 computeLibrarySourceErrors(source);
201 assertErrors(source, [CompileTimeErrorCode.ACCESS_PRIVATE_ENUM_FIELD]);
202 // Cannot verify because "_name" cannot be resolved.
203 }
204
205 void test_ambiguousExport() {
206 Source source = addSource(r'''
207 library L;
208 export 'lib1.dart';
209 export 'lib2.dart';''');
210 addNamedSource(
211 "/lib1.dart",
212 r'''
213 library lib1;
214 class N {}''');
215 addNamedSource(
216 "/lib2.dart",
217 r'''
218 library lib2;
219 class N {}''');
220 computeLibrarySourceErrors(source);
221 assertErrors(source, [CompileTimeErrorCode.AMBIGUOUS_EXPORT]);
222 verify([source]);
223 }
224
225 void test_async_used_as_identifier_in_annotation() {
226 Source source = addSource('''
227 const int async = 0;
228 f() async {
229 g(@async x) {}
230 g(0);
231 }
232 ''');
233 computeLibrarySourceErrors(source);
234 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
235 verify([source]);
236 }
237
238 void test_async_used_as_identifier_in_argument_label() {
239 Source source = addSource('''
240 @proxy
241 class C {}
242 f() async {
243 new C().g(async: 0);
244 }
245 ''');
246 computeLibrarySourceErrors(source);
247 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
248 // Note: we don't call verify([source]) because verify() doesn't understand
249 // about @proxy.
250 }
251
252 void test_async_used_as_identifier_in_async_method() {
253 Source source = addSource('''
254 f() async {
255 var async = 1;
256 }
257 ''');
258 computeLibrarySourceErrors(source);
259 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
260 verify([source]);
261 }
262
263 void test_async_used_as_identifier_in_async_star_method() {
264 Source source = addSource('''
265 f() async* {
266 var async = 1;
267 }
268 ''');
269 computeLibrarySourceErrors(source);
270 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
271 verify([source]);
272 }
273
274 void test_async_used_as_identifier_in_break_statement() {
275 Source source = addSource('''
276 f() async {
277 while (true) {
278 break async;
279 }
280 }
281 ''');
282 computeLibrarySourceErrors(source);
283 assertErrors(source, [
284 ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER,
285 CompileTimeErrorCode.LABEL_UNDEFINED
286 ]);
287 // Note: we don't call verify([source]) because the reference to the
288 // "async" label is unresolved.
289 }
290
291 void test_async_used_as_identifier_in_cascaded_invocation() {
292 Source source = addSource('''
293 class C {
294 int async() => 1;
295 }
296 f() async {
297 return new C()..async();
298 }
299 ''');
300 computeLibrarySourceErrors(source);
301 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
302 verify([source]);
303 }
304
305 void test_async_used_as_identifier_in_cascaded_setter_invocation() {
306 Source source = addSource('''
307 class C {
308 void set async(int i) {}
309 }
310 f() async {
311 return new C()..async = 1;
312 }
313 ''');
314 computeLibrarySourceErrors(source);
315 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
316 verify([source]);
317 }
318
319 void test_async_used_as_identifier_in_catch_exception_argument() {
320 Source source = addSource('''
321 g() {}
322 f() async {
323 try {
324 g();
325 } catch (async) { }
326 }
327 ''');
328 computeLibrarySourceErrors(source);
329 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
330 verify([source]);
331 }
332
333 void test_async_used_as_identifier_in_catch_stacktrace_argument() {
334 Source source = addSource('''
335 g() {}
336 f() async {
337 try {
338 g();
339 } catch (e, async) { }
340 }
341 ''');
342 computeLibrarySourceErrors(source);
343 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
344 verify([source]);
345 }
346
347 void test_async_used_as_identifier_in_continue_statement() {
348 Source source = addSource('''
349 f() async {
350 while (true) {
351 continue async;
352 }
353 }
354 ''');
355 computeLibrarySourceErrors(source);
356 assertErrors(source, [
357 ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER,
358 CompileTimeErrorCode.LABEL_UNDEFINED
359 ]);
360 // Note: we don't call verify([source]) because the reference to the
361 // "async" label is unresolved.
362 }
363
364 void test_async_used_as_identifier_in_for_statement() {
365 Source source = addSource('''
366 var async;
367 f() async {
368 for (async in []) {}
369 }
370 ''');
371 computeLibrarySourceErrors(source);
372 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
373 verify([source]);
374 }
375
376 void test_async_used_as_identifier_in_formal_parameter_name() {
377 Source source = addSource('''
378 f() async {
379 g(int async) {}
380 g(0);
381 }
382 ''');
383 computeLibrarySourceErrors(source);
384 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
385 verify([source]);
386 }
387
388 void test_async_used_as_identifier_in_getter_name() {
389 Source source = addSource('''
390 class C {
391 int get async => 1;
392 }
393 f() async {
394 return new C().async;
395 }
396 ''');
397 computeLibrarySourceErrors(source);
398 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
399 verify([source]);
400 }
401
402 void test_async_used_as_identifier_in_invocation() {
403 Source source = addSource('''
404 class C {
405 int async() => 1;
406 }
407 f() async {
408 return new C().async();
409 }
410 ''');
411 computeLibrarySourceErrors(source);
412 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
413 verify([source]);
414 }
415
416 void test_async_used_as_identifier_in_local_function_name() {
417 Source source = addSource('''
418 f() async {
419 int async() => null;
420 }
421 ''');
422 computeLibrarySourceErrors(source);
423 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
424 verify([source]);
425 }
426
427 void test_async_used_as_identifier_in_prefix() {
428 Source source = addSource('''
429 import 'dart:async' as async;
430 f() async {
431 return new async.Future.value(0);
432 }
433 ''');
434 computeLibrarySourceErrors(source);
435 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
436 verify([source]);
437 }
438
439 void test_async_used_as_identifier_in_setter_name() {
440 Source source = addSource('''
441 class C {
442 void set async(int i) {}
443 }
444 f() async {
445 new C().async = 1;
446 }
447 ''');
448 computeLibrarySourceErrors(source);
449 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
450 verify([source]);
451 }
452
453 void test_async_used_as_identifier_in_statement_label() {
454 Source source = addSource('''
455 f() async {
456 async: g();
457 }
458 g() {}
459 ''');
460 computeLibrarySourceErrors(source);
461 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
462 verify([source]);
463 }
464
465 void test_async_used_as_identifier_in_string_interpolation() {
466 Source source = addSource(r'''
467 int async = 1;
468 f() async {
469 return "$async";
470 }
471 ''');
472 computeLibrarySourceErrors(source);
473 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
474 verify([source]);
475 }
476
477 void test_async_used_as_identifier_in_suffix() {
478 addNamedSource(
479 "/lib1.dart",
480 r'''
481 library lib1;
482 int async;
483 ''');
484 Source source = addSource('''
485 import 'lib1.dart' as l;
486 f() async {
487 return l.async;
488 }
489 ''');
490 computeLibrarySourceErrors(source);
491 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
492 verify([source]);
493 }
494
495 void test_async_used_as_identifier_in_switch_label() {
496 Source source = addSource('''
497 f() async {
498 switch (0) {
499 async: case 0: break;
500 }
501 }
502 ''');
503 computeLibrarySourceErrors(source);
504 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
505 verify([source]);
506 }
507
508 void test_async_used_as_identifier_in_sync_star_method() {
509 Source source = addSource('''
510 f() sync* {
511 var async = 1;
512 }
513 ''');
514 computeLibrarySourceErrors(source);
515 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
516 verify([source]);
517 }
518
519 void test_asyncForInWrongContext() {
520 Source source = addSource(r'''
521 f(list) {
522 await for (var e in list) {
523 }
524 }''');
525 computeLibrarySourceErrors(source);
526 assertErrors(source, [CompileTimeErrorCode.ASYNC_FOR_IN_WRONG_CONTEXT]);
527 verify([source]);
528 }
529
530 void test_await_used_as_identifier_in_async_method() {
531 Source source = addSource('''
532 f() async {
533 var await = 1;
534 }
535 ''');
536 computeLibrarySourceErrors(source);
537 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
538 verify([source]);
539 }
540
541 void test_await_used_as_identifier_in_async_star_method() {
542 Source source = addSource('''
543 f() async* {
544 var await = 1;
545 }
546 ''');
547 computeLibrarySourceErrors(source);
548 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
549 verify([source]);
550 }
551
552 void test_await_used_as_identifier_in_sync_star_method() {
553 Source source = addSource('''
554 f() sync* {
555 var await = 1;
556 }
557 ''');
558 computeLibrarySourceErrors(source);
559 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
560 verify([source]);
561 }
562
563 void test_bug_23176() {
564 Source source = addSource('''
565 class A {
566 const A([x]);
567 }
568 class B {
569 dynamic @A(const A()) x;
570 }
571 ''');
572 computeLibrarySourceErrors(source);
573 assertErrors(source, [
574 ParserErrorCode.EXPECTED_CLASS_MEMBER,
575 ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE
576 ]);
577 verify([source]);
578 }
579
580 void test_builtInIdentifierAsMixinName_classTypeAlias() {
581 Source source = addSource(r'''
582 class A {}
583 class B {}
584 class as = A with B;''');
585 computeLibrarySourceErrors(source);
586 assertErrors(
587 source, [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME]);
588 verify([source]);
589 }
590
591 void test_builtInIdentifierAsType_formalParameter_field() {
592 Source source = addSource(r'''
593 class A {
594 var x;
595 A(static this.x);
596 }''');
597 computeLibrarySourceErrors(source);
598 assertErrors(source, [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE]);
599 verify([source]);
600 }
601
602 void test_builtInIdentifierAsType_formalParameter_simple() {
603 Source source = addSource(r'''
604 f(static x) {
605 }''');
606 computeLibrarySourceErrors(source);
607 assertErrors(source, [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE]);
608 verify([source]);
609 }
610
611 void test_builtInIdentifierAsType_variableDeclaration() {
612 Source source = addSource(r'''
613 f() {
614 typedef x;
615 }''');
616 computeLibrarySourceErrors(source);
617 assertErrors(source, [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE]);
618 verify([source]);
619 }
620
621 void test_builtInIdentifierAsTypedefName_functionTypeAlias() {
622 Source source = addSource("typedef bool as();");
623 computeLibrarySourceErrors(source);
624 assertErrors(
625 source, [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME]);
626 verify([source]);
627 }
628
629 void test_builtInIdentifierAsTypeName() {
630 Source source = addSource("class as {}");
631 computeLibrarySourceErrors(source);
632 assertErrors(
633 source, [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME]);
634 verify([source]);
635 }
636
637 void test_builtInIdentifierAsTypeParameterName() {
638 Source source = addSource("class A<as> {}");
639 computeLibrarySourceErrors(source);
640 assertErrors(source,
641 [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_PARAMETER_NAME]);
642 verify([source]);
643 }
644
645 void test_caseExpressionTypeImplementsEquals() {
646 Source source = addSource(r'''
647 class IntWrapper {
648 final int value;
649 const IntWrapper(this.value);
650 bool operator ==(IntWrapper x) {
651 return value == x.value;
652 }
653 get hashCode => value;
654 }
655
656 f(var a) {
657 switch(a) {
658 case(const IntWrapper(1)) : return 1;
659 default: return 0;
660 }
661 }''');
662 computeLibrarySourceErrors(source);
663 assertErrors(
664 source, [CompileTimeErrorCode.CASE_EXPRESSION_TYPE_IMPLEMENTS_EQUALS]);
665 verify([source]);
666 }
667
668 void test_conflictingConstructorNameAndMember_field() {
669 Source source = addSource(r'''
670 class A {
671 int x;
672 A.x() {}
673 }''');
674 computeLibrarySourceErrors(source);
675 assertErrors(
676 source, [CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_NAME_AND_FIELD]);
677 verify([source]);
678 }
679
680 void test_conflictingConstructorNameAndMember_method() {
681 Source source = addSource(r'''
682 class A {
683 const A.x();
684 void x() {}
685 }''');
686 computeLibrarySourceErrors(source);
687 assertErrors(
688 source, [CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_NAME_AND_METHOD]);
689 verify([source]);
690 }
691
692 void test_conflictingGetterAndMethod_field_method() {
693 Source source = addSource(r'''
694 class A {
695 final int m = 0;
696 }
697 class B extends A {
698 m() {}
699 }''');
700 computeLibrarySourceErrors(source);
701 assertErrors(source, [CompileTimeErrorCode.CONFLICTING_GETTER_AND_METHOD]);
702 verify([source]);
703 }
704
705 void test_conflictingGetterAndMethod_getter_method() {
706 Source source = addSource(r'''
707 class A {
708 get m => 0;
709 }
710 class B extends A {
711 m() {}
712 }''');
713 computeLibrarySourceErrors(source);
714 assertErrors(source, [CompileTimeErrorCode.CONFLICTING_GETTER_AND_METHOD]);
715 verify([source]);
716 }
717
718 void test_conflictingGetterAndMethod_method_field() {
719 Source source = addSource(r'''
720 class A {
721 m() {}
722 }
723 class B extends A {
724 int m;
725 }''');
726 computeLibrarySourceErrors(source);
727 assertErrors(source, [CompileTimeErrorCode.CONFLICTING_METHOD_AND_GETTER]);
728 verify([source]);
729 }
730
731 void test_conflictingGetterAndMethod_method_getter() {
732 Source source = addSource(r'''
733 class A {
734 m() {}
735 }
736 class B extends A {
737 get m => 0;
738 }''');
739 computeLibrarySourceErrors(source);
740 assertErrors(source, [CompileTimeErrorCode.CONFLICTING_METHOD_AND_GETTER]);
741 verify([source]);
742 }
743
744 void test_conflictingTypeVariableAndClass() {
745 Source source = addSource(r'''
746 class T<T> {
747 }''');
748 computeLibrarySourceErrors(source);
749 assertErrors(
750 source, [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_CLASS]);
751 verify([source]);
752 }
753
754 void test_conflictingTypeVariableAndMember_field() {
755 Source source = addSource(r'''
756 class A<T> {
757 var T;
758 }''');
759 computeLibrarySourceErrors(source);
760 assertErrors(
761 source, [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_MEMBER]);
762 verify([source]);
763 }
764
765 void test_conflictingTypeVariableAndMember_getter() {
766 Source source = addSource(r'''
767 class A<T> {
768 get T => null;
769 }''');
770 computeLibrarySourceErrors(source);
771 assertErrors(
772 source, [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_MEMBER]);
773 verify([source]);
774 }
775
776 void test_conflictingTypeVariableAndMember_method() {
777 Source source = addSource(r'''
778 class A<T> {
779 T() {}
780 }''');
781 computeLibrarySourceErrors(source);
782 assertErrors(
783 source, [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_MEMBER]);
784 verify([source]);
785 }
786
787 void test_conflictingTypeVariableAndMember_method_static() {
788 Source source = addSource(r'''
789 class A<T> {
790 static T() {}
791 }''');
792 computeLibrarySourceErrors(source);
793 assertErrors(
794 source, [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_MEMBER]);
795 verify([source]);
796 }
797
798 void test_conflictingTypeVariableAndMember_setter() {
799 Source source = addSource(r'''
800 class A<T> {
801 set T(x) {}
802 }''');
803 computeLibrarySourceErrors(source);
804 assertErrors(
805 source, [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_MEMBER]);
806 verify([source]);
807 }
808
809 void test_consistentCaseExpressionTypes_dynamic() {
810 // Even though A.S and S have a static type of "dynamic", we should see
811 // that they match 'abc', because they are constant strings.
812 Source source = addSource(r'''
813 class A {
814 static const S = 'A.S';
815 }
816
817 const S = 'S';
818
819 foo(var p) {
820 switch (p) {
821 case S:
822 break;
823 case A.S:
824 break;
825 case 'abc':
826 break;
827 }
828 }''');
829 computeLibrarySourceErrors(source);
830 assertNoErrors(source);
831 verify([source]);
832 }
833
834 void test_constConstructorWithFieldInitializedByNonConst() {
835 Source source = addSource(r'''
836 class A {
837 final int i = f();
838 const A();
839 }
840 int f() {
841 return 3;
842 }''');
843 computeLibrarySourceErrors(source);
844 // TODO(paulberry): the error CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE is
845 // redundant and ought to be suppressed.
846 assertErrors(source, [
847 CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_FIELD_INITIALIZED_BY_NON_CONST ,
848 CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
849 ]);
850 verify([source]);
851 }
852
853 void test_constConstructorWithFieldInitializedByNonConst_static() {
854 Source source = addSource(r'''
855 class A {
856 static final int i = f();
857 const A();
858 }
859 int f() {
860 return 3;
861 }''');
862 computeLibrarySourceErrors(source);
863 assertNoErrors(source);
864 verify([source]);
865 }
866
867 void test_constConstructorWithMixin() {
868 Source source = addSource(r'''
869 class M {
870 }
871 class A extends Object with M {
872 const A();
873 }''');
874 computeLibrarySourceErrors(source);
875 assertErrors(source, [CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_MIXIN]);
876 verify([source]);
877 }
878
879 void test_constConstructorWithNonConstSuper_explicit() {
880 Source source = addSource(r'''
881 class A {
882 A();
883 }
884 class B extends A {
885 const B(): super();
886 }''');
887 computeLibrarySourceErrors(source);
888 assertErrors(
889 source, [CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_CONST_SUPER]);
890 verify([source]);
891 }
892
893 void test_constConstructorWithNonConstSuper_implicit() {
894 Source source = addSource(r'''
895 class A {
896 A();
897 }
898 class B extends A {
899 const B();
900 }''');
901 computeLibrarySourceErrors(source);
902 assertErrors(
903 source, [CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_CONST_SUPER]);
904 verify([source]);
905 }
906
907 void test_constConstructorWithNonFinalField_mixin() {
908 Source source = addSource(r'''
909 class A {
910 var a;
911 }
912 class B extends Object with A {
913 const B();
914 }''');
915 computeLibrarySourceErrors(source);
916 assertErrors(source, [
917 CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_MIXIN,
918 CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_FINAL_FIELD
919 ]);
920 verify([source]);
921 }
922
923 void test_constConstructorWithNonFinalField_super() {
924 Source source = addSource(r'''
925 class A {
926 var a;
927 }
928 class B extends A {
929 const B();
930 }''');
931 computeLibrarySourceErrors(source);
932 assertErrors(source, [
933 CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_FINAL_FIELD,
934 CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_CONST_SUPER
935 ]);
936 verify([source]);
937 }
938
939 void test_constConstructorWithNonFinalField_this() {
940 Source source = addSource(r'''
941 class A {
942 int x;
943 const A();
944 }''');
945 computeLibrarySourceErrors(source);
946 assertErrors(
947 source, [CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_FINAL_FIELD]);
948 verify([source]);
949 }
950
951 void test_constDeferredClass() {
952 resolveWithErrors(<String>[
953 r'''
954 library lib1;
955 class A {
956 const A();
957 }''',
958 r'''
959 library root;
960 import 'lib1.dart' deferred as a;
961 main() {
962 const a.A();
963 }'''
964 ], <ErrorCode>[
965 CompileTimeErrorCode.CONST_DEFERRED_CLASS
966 ]);
967 }
968
969 void test_constDeferredClass_namedConstructor() {
970 resolveWithErrors(<String>[
971 r'''
972 library lib1;
973 class A {
974 const A.b();
975 }''',
976 r'''
977 library root;
978 import 'lib1.dart' deferred as a;
979 main() {
980 const a.A.b();
981 }'''
982 ], <ErrorCode>[
983 CompileTimeErrorCode.CONST_DEFERRED_CLASS
984 ]);
985 }
986
987 void test_constEval_newInstance_constConstructor() {
988 Source source = addSource(r'''
989 class A {
990 const A();
991 }
992 const a = new A();''');
993 computeLibrarySourceErrors(source);
994 assertErrors(source,
995 [CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE]);
996 verify([source]);
997 }
998
999 void test_constEval_newInstance_externalFactoryConstConstructor() {
1000 // We can't evaluate "const A()" because its constructor is external. But
1001 // the code is correct--we shouldn't report an error.
1002 Source source = addSource(r'''
1003 class A {
1004 external factory const A();
1005 }
1006 const x = const A();''');
1007 computeLibrarySourceErrors(source);
1008 assertNoErrors(source);
1009 verify([source]);
1010 }
1011
1012 void test_constEval_propertyExtraction_targetNotConst() {
1013 Source source = addSource(r'''
1014 class A {
1015 const A();
1016 m() {}
1017 }
1018 final a = const A();
1019 const C = a.m;''');
1020 computeLibrarySourceErrors(source);
1021 assertErrors(source,
1022 [CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE]);
1023 verify([source]);
1024 }
1025
1026 void test_constEvalThrowsException_binaryMinus_null() {
1027 _check_constEvalThrowsException_binary_null("null - 5", false);
1028 _check_constEvalThrowsException_binary_null("5 - null", true);
1029 }
1030
1031 void test_constEvalThrowsException_binaryPlus_null() {
1032 _check_constEvalThrowsException_binary_null("null + 5", false);
1033 _check_constEvalThrowsException_binary_null("5 + null", true);
1034 }
1035
1036 void test_constEvalThrowsException_divisionByZero() {
1037 Source source = addSource("const C = 1 ~/ 0;");
1038 computeLibrarySourceErrors(source);
1039 assertErrors(source, [CompileTimeErrorCode.CONST_EVAL_THROWS_IDBZE]);
1040 verify([source]);
1041 }
1042
1043 void test_constEvalThrowsException_finalAlreadySet_initializer() {
1044 // If a final variable has an initializer at the site of its declaration,
1045 // and at the site of the constructor, then invoking that constructor would
1046 // produce a runtime error; hence invoking that constructor via the "const"
1047 // keyword results in a compile-time error.
1048 Source source = addSource('''
1049 class C {
1050 final x = 1;
1051 const C() : x = 2;
1052 }
1053 var x = const C();
1054 ''');
1055 computeLibrarySourceErrors(source);
1056 assertErrors(source, [
1057 CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION,
1058 StaticWarningCode.FIELD_INITIALIZED_IN_INITIALIZER_AND_DECLARATION
1059 ]);
1060 verify([source]);
1061 }
1062
1063 void test_constEvalThrowsException_finalAlreadySet_initializing_formal() {
1064 // If a final variable has an initializer at the site of its declaration,
1065 // and it is initialized using an initializing formal at the site of the
1066 // constructor, then invoking that constructor would produce a runtime
1067 // error; hence invoking that constructor via the "const" keyword results
1068 // in a compile-time error.
1069 Source source = addSource('''
1070 class C {
1071 final x = 1;
1072 const C(this.x);
1073 }
1074 var x = const C(2);
1075 ''');
1076 computeLibrarySourceErrors(source);
1077 assertErrors(source, [
1078 CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION,
1079 StaticWarningCode.FINAL_INITIALIZED_IN_DECLARATION_AND_CONSTRUCTOR
1080 ]);
1081 verify([source]);
1082 }
1083
1084 void test_constEvalThrowsException_unaryBitNot_null() {
1085 Source source = addSource("const C = ~null;");
1086 computeLibrarySourceErrors(source);
1087 assertErrors(source, [CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION]);
1088 // no verify(), '~null' is not resolved
1089 }
1090
1091 void test_constEvalThrowsException_unaryNegated_null() {
1092 Source source = addSource("const C = -null;");
1093 computeLibrarySourceErrors(source);
1094 assertErrors(source, [CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION]);
1095 // no verify(), '-null' is not resolved
1096 }
1097
1098 void test_constEvalThrowsException_unaryNot_null() {
1099 Source source = addSource("const C = !null;");
1100 computeLibrarySourceErrors(source);
1101 assertErrors(source, [CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION]);
1102 verify([source]);
1103 }
1104
1105 void test_constEvalTypeBool_binary() {
1106 _check_constEvalTypeBool_withParameter_binary("p && ''");
1107 _check_constEvalTypeBool_withParameter_binary("p || ''");
1108 }
1109
1110 void test_constEvalTypeBool_binary_leftTrue() {
1111 Source source = addSource("const C = (true || 0);");
1112 computeLibrarySourceErrors(source);
1113 assertErrors(source, [
1114 CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL,
1115 StaticTypeWarningCode.NON_BOOL_OPERAND,
1116 HintCode.DEAD_CODE
1117 ]);
1118 verify([source]);
1119 }
1120
1121 void test_constEvalTypeBoolNumString_equal() {
1122 Source source = addSource(r'''
1123 class A {
1124 const A();
1125 }
1126 class B {
1127 final a;
1128 const B(num p) : a = p == const A();
1129 }''');
1130 computeLibrarySourceErrors(source);
1131 assertErrors(
1132 source, [CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL_NUM_STRING]);
1133 verify([source]);
1134 }
1135
1136 void test_constEvalTypeBoolNumString_notEqual() {
1137 Source source = addSource(r'''
1138 class A {
1139 const A();
1140 }
1141 class B {
1142 final a;
1143 const B(String p) : a = p != const A();
1144 }''');
1145 computeLibrarySourceErrors(source);
1146 assertErrors(
1147 source, [CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL_NUM_STRING]);
1148 verify([source]);
1149 }
1150
1151 void test_constEvalTypeInt_binary() {
1152 _check_constEvalTypeInt_withParameter_binary("p ^ ''");
1153 _check_constEvalTypeInt_withParameter_binary("p & ''");
1154 _check_constEvalTypeInt_withParameter_binary("p | ''");
1155 _check_constEvalTypeInt_withParameter_binary("p >> ''");
1156 _check_constEvalTypeInt_withParameter_binary("p << ''");
1157 }
1158
1159 void test_constEvalTypeNum_binary() {
1160 _check_constEvalTypeNum_withParameter_binary("p + ''");
1161 _check_constEvalTypeNum_withParameter_binary("p - ''");
1162 _check_constEvalTypeNum_withParameter_binary("p * ''");
1163 _check_constEvalTypeNum_withParameter_binary("p / ''");
1164 _check_constEvalTypeNum_withParameter_binary("p ~/ ''");
1165 _check_constEvalTypeNum_withParameter_binary("p > ''");
1166 _check_constEvalTypeNum_withParameter_binary("p < ''");
1167 _check_constEvalTypeNum_withParameter_binary("p >= ''");
1168 _check_constEvalTypeNum_withParameter_binary("p <= ''");
1169 _check_constEvalTypeNum_withParameter_binary("p % ''");
1170 }
1171
1172 void test_constFormalParameter_fieldFormalParameter() {
1173 Source source = addSource(r'''
1174 class A {
1175 var x;
1176 A(const this.x) {}
1177 }''');
1178 computeLibrarySourceErrors(source);
1179 assertErrors(source, [CompileTimeErrorCode.CONST_FORMAL_PARAMETER]);
1180 verify([source]);
1181 }
1182
1183 void test_constFormalParameter_simpleFormalParameter() {
1184 Source source = addSource("f(const x) {}");
1185 computeLibrarySourceErrors(source);
1186 assertErrors(source, [CompileTimeErrorCode.CONST_FORMAL_PARAMETER]);
1187 verify([source]);
1188 }
1189
1190 void test_constInitializedWithNonConstValue() {
1191 Source source = addSource(r'''
1192 f(p) {
1193 const C = p;
1194 }''');
1195 computeLibrarySourceErrors(source);
1196 assertErrors(source,
1197 [CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE]);
1198 verify([source]);
1199 }
1200
1201 void test_constInitializedWithNonConstValue_missingConstInListLiteral() {
1202 Source source = addSource("const List L = [0];");
1203 computeLibrarySourceErrors(source);
1204 assertErrors(source,
1205 [CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE]);
1206 verify([source]);
1207 }
1208
1209 void test_constInitializedWithNonConstValue_missingConstInMapLiteral() {
1210 Source source = addSource("const Map M = {'a' : 0};");
1211 computeLibrarySourceErrors(source);
1212 assertErrors(source,
1213 [CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE]);
1214 verify([source]);
1215 }
1216
1217 void test_constInitializedWithNonConstValueFromDeferredClass() {
1218 resolveWithErrors(<String>[
1219 r'''
1220 library lib1;
1221 const V = 1;''',
1222 r'''
1223 library root;
1224 import 'lib1.dart' deferred as a;
1225 const B = a.V;'''
1226 ], <ErrorCode>[
1227 CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE_FROM_DEFERR ED_LIBRARY
1228 ]);
1229 }
1230
1231 void test_constInitializedWithNonConstValueFromDeferredClass_nested() {
1232 resolveWithErrors(<String>[
1233 r'''
1234 library lib1;
1235 const V = 1;''',
1236 r'''
1237 library root;
1238 import 'lib1.dart' deferred as a;
1239 const B = a.V + 1;'''
1240 ], <ErrorCode>[
1241 CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE_FROM_DEFERR ED_LIBRARY
1242 ]);
1243 }
1244
1245 void test_constInstanceField() {
1246 Source source = addSource(r'''
1247 class C {
1248 const int f = 0;
1249 }''');
1250 computeLibrarySourceErrors(source);
1251 assertErrors(source, [CompileTimeErrorCode.CONST_INSTANCE_FIELD]);
1252 verify([source]);
1253 }
1254
1255 void test_constMapKeyTypeImplementsEquals_direct() {
1256 Source source = addSource(r'''
1257 class A {
1258 const A();
1259 operator ==(other) => false;
1260 }
1261 main() {
1262 const {const A() : 0};
1263 }''');
1264 computeLibrarySourceErrors(source);
1265 assertErrors(source,
1266 [CompileTimeErrorCode.CONST_MAP_KEY_EXPRESSION_TYPE_IMPLEMENTS_EQUALS]);
1267 verify([source]);
1268 }
1269
1270 void test_constMapKeyTypeImplementsEquals_dynamic() {
1271 // Note: static type of B.a is "dynamic", but actual type of the const
1272 // object is A. We need to make sure we examine the actual type when
1273 // deciding whether there is a problem with operator==.
1274 Source source = addSource(r'''
1275 class A {
1276 const A();
1277 operator ==(other) => false;
1278 }
1279 class B {
1280 static const a = const A();
1281 }
1282 main() {
1283 const {B.a : 0};
1284 }''');
1285 computeLibrarySourceErrors(source);
1286 assertErrors(source,
1287 [CompileTimeErrorCode.CONST_MAP_KEY_EXPRESSION_TYPE_IMPLEMENTS_EQUALS]);
1288 verify([source]);
1289 }
1290
1291 void test_constMapKeyTypeImplementsEquals_factory() {
1292 Source source = addSource(r'''
1293 class A { const factory A() = B; }
1294
1295 class B implements A {
1296 const B();
1297
1298 operator ==(o) => true;
1299 }
1300
1301 main() {
1302 var m = const { const A(): 42 };
1303 }''');
1304 computeLibrarySourceErrors(source);
1305 assertErrors(source,
1306 [CompileTimeErrorCode.CONST_MAP_KEY_EXPRESSION_TYPE_IMPLEMENTS_EQUALS]);
1307 verify([source]);
1308 }
1309
1310 void test_constMapKeyTypeImplementsEquals_super() {
1311 Source source = addSource(r'''
1312 class A {
1313 const A();
1314 operator ==(other) => false;
1315 }
1316 class B extends A {
1317 const B();
1318 }
1319 main() {
1320 const {const B() : 0};
1321 }''');
1322 computeLibrarySourceErrors(source);
1323 assertErrors(source,
1324 [CompileTimeErrorCode.CONST_MAP_KEY_EXPRESSION_TYPE_IMPLEMENTS_EQUALS]);
1325 verify([source]);
1326 }
1327
1328 void test_constWithInvalidTypeParameters() {
1329 Source source = addSource(r'''
1330 class A {
1331 const A();
1332 }
1333 f() { return const A<A>(); }''');
1334 computeLibrarySourceErrors(source);
1335 assertErrors(
1336 source, [CompileTimeErrorCode.CONST_WITH_INVALID_TYPE_PARAMETERS]);
1337 verify([source]);
1338 }
1339
1340 void test_constWithInvalidTypeParameters_tooFew() {
1341 Source source = addSource(r'''
1342 class A {}
1343 class C<K, V> {
1344 const C();
1345 }
1346 f(p) {
1347 return const C<A>();
1348 }''');
1349 computeLibrarySourceErrors(source);
1350 assertErrors(
1351 source, [CompileTimeErrorCode.CONST_WITH_INVALID_TYPE_PARAMETERS]);
1352 verify([source]);
1353 }
1354
1355 void test_constWithInvalidTypeParameters_tooMany() {
1356 Source source = addSource(r'''
1357 class A {}
1358 class C<E> {
1359 const C();
1360 }
1361 f(p) {
1362 return const C<A, A>();
1363 }''');
1364 computeLibrarySourceErrors(source);
1365 assertErrors(
1366 source, [CompileTimeErrorCode.CONST_WITH_INVALID_TYPE_PARAMETERS]);
1367 verify([source]);
1368 }
1369
1370 void test_constWithNonConst() {
1371 Source source = addSource(r'''
1372 class T {
1373 T(a, b, {c, d}) {}
1374 }
1375 f() { return const T(0, 1, c: 2, d: 3); }''');
1376 computeLibrarySourceErrors(source);
1377 assertErrors(source, [CompileTimeErrorCode.CONST_WITH_NON_CONST]);
1378 verify([source]);
1379 }
1380
1381 void test_constWithNonConstantArgument_annotation() {
1382 Source source = addSource(r'''
1383 class A {
1384 const A(int p);
1385 }
1386 var v = 42;
1387 @A(v)
1388 main() {
1389 }''');
1390 computeLibrarySourceErrors(source);
1391 assertErrors(
1392 source, [CompileTimeErrorCode.CONST_WITH_NON_CONSTANT_ARGUMENT]);
1393 verify([source]);
1394 }
1395
1396 void test_constWithNonConstantArgument_instanceCreation() {
1397 Source source = addSource(r'''
1398 class A {
1399 const A(a);
1400 }
1401 f(p) { return const A(p); }''');
1402 computeLibrarySourceErrors(source);
1403 // TODO(paulberry): the error INVALID_CONSTAT is redundant and ought to be
1404 // suppressed.
1405 assertErrors(source, [
1406 CompileTimeErrorCode.CONST_WITH_NON_CONSTANT_ARGUMENT,
1407 CompileTimeErrorCode.INVALID_CONSTANT
1408 ]);
1409 verify([source]);
1410 }
1411
1412 void test_constWithNonType() {
1413 Source source = addSource(r'''
1414 int A;
1415 f() {
1416 return const A();
1417 }''');
1418 computeLibrarySourceErrors(source);
1419 assertErrors(source, [CompileTimeErrorCode.CONST_WITH_NON_TYPE]);
1420 verify([source]);
1421 }
1422
1423 void test_constWithNonType_fromLibrary() {
1424 Source source1 = addNamedSource("/lib.dart", "");
1425 Source source2 = addNamedSource(
1426 "/lib2.dart",
1427 r'''
1428 import 'lib.dart' as lib;
1429 void f() {
1430 const lib.A();
1431 }''');
1432 computeLibrarySourceErrors(source1);
1433 computeLibrarySourceErrors(source2);
1434 assertErrors(source2, [CompileTimeErrorCode.CONST_WITH_NON_TYPE]);
1435 verify([source1]);
1436 }
1437
1438 void test_constWithTypeParameters_direct() {
1439 Source source = addSource(r'''
1440 class A<T> {
1441 static const V = const A<T>();
1442 const A();
1443 }''');
1444 computeLibrarySourceErrors(source);
1445 assertErrors(source, [
1446 CompileTimeErrorCode.CONST_WITH_TYPE_PARAMETERS,
1447 StaticWarningCode.TYPE_PARAMETER_REFERENCED_BY_STATIC
1448 ]);
1449 verify([source]);
1450 }
1451
1452 void test_constWithTypeParameters_indirect() {
1453 Source source = addSource(r'''
1454 class A<T> {
1455 static const V = const A<List<T>>();
1456 const A();
1457 }''');
1458 computeLibrarySourceErrors(source);
1459 assertErrors(source, [
1460 CompileTimeErrorCode.CONST_WITH_TYPE_PARAMETERS,
1461 StaticWarningCode.TYPE_PARAMETER_REFERENCED_BY_STATIC
1462 ]);
1463 verify([source]);
1464 }
1465
1466 void test_constWithUndefinedConstructor() {
1467 Source source = addSource(r'''
1468 class A {
1469 const A();
1470 }
1471 f() {
1472 return const A.noSuchConstructor();
1473 }''');
1474 computeLibrarySourceErrors(source);
1475 assertErrors(
1476 source, [CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR]);
1477 // no verify(), 'noSuchConstructor' is not resolved
1478 }
1479
1480 void test_constWithUndefinedConstructorDefault() {
1481 Source source = addSource(r'''
1482 class A {
1483 const A.name();
1484 }
1485 f() {
1486 return const A();
1487 }''');
1488 computeLibrarySourceErrors(source);
1489 assertErrors(source,
1490 [CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT]);
1491 verify([source]);
1492 }
1493
1494 void test_defaultValueInFunctionTypeAlias() {
1495 Source source = addSource("typedef F([x = 0]);");
1496 computeLibrarySourceErrors(source);
1497 assertErrors(
1498 source, [CompileTimeErrorCode.DEFAULT_VALUE_IN_FUNCTION_TYPE_ALIAS]);
1499 verify([source]);
1500 }
1501
1502 void test_defaultValueInFunctionTypedParameter_named() {
1503 Source source = addSource("f(g({p: null})) {}");
1504 computeLibrarySourceErrors(source);
1505 assertErrors(source,
1506 [CompileTimeErrorCode.DEFAULT_VALUE_IN_FUNCTION_TYPED_PARAMETER]);
1507 verify([source]);
1508 }
1509
1510 void test_defaultValueInFunctionTypedParameter_optional() {
1511 Source source = addSource("f(g([p = null])) {}");
1512 computeLibrarySourceErrors(source);
1513 assertErrors(source,
1514 [CompileTimeErrorCode.DEFAULT_VALUE_IN_FUNCTION_TYPED_PARAMETER]);
1515 verify([source]);
1516 }
1517
1518 void test_defaultValueInRedirectingFactoryConstructor() {
1519 Source source = addSource(r'''
1520 class A {
1521 factory A([int x = 0]) = B;
1522 }
1523
1524 class B implements A {
1525 B([int x = 1]) {}
1526 }''');
1527 computeLibrarySourceErrors(source);
1528 assertErrors(source, [
1529 CompileTimeErrorCode.DEFAULT_VALUE_IN_REDIRECTING_FACTORY_CONSTRUCTOR
1530 ]);
1531 verify([source]);
1532 }
1533
1534 void test_duplicateConstructorName_named() {
1535 Source source = addSource(r'''
1536 class A {
1537 A.a() {}
1538 A.a() {}
1539 }''');
1540 computeLibrarySourceErrors(source);
1541 assertErrors(source, [
1542 CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_NAME,
1543 CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_NAME
1544 ]);
1545 verify([source]);
1546 }
1547
1548 void test_duplicateConstructorName_unnamed() {
1549 Source source = addSource(r'''
1550 class A {
1551 A() {}
1552 A() {}
1553 }''');
1554 computeLibrarySourceErrors(source);
1555 assertErrors(source, [
1556 CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_DEFAULT,
1557 CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_DEFAULT
1558 ]);
1559 verify([source]);
1560 }
1561
1562 void test_duplicateDefinition_acrossLibraries() {
1563 Source librarySource = addNamedSource(
1564 "/lib.dart",
1565 r'''
1566 library lib;
1567
1568 part 'a.dart';
1569 part 'b.dart';''');
1570 Source sourceA = addNamedSource(
1571 "/a.dart",
1572 r'''
1573 part of lib;
1574
1575 class A {}''');
1576 Source sourceB = addNamedSource(
1577 "/b.dart",
1578 r'''
1579 part of lib;
1580
1581 class A {}''');
1582 computeLibrarySourceErrors(librarySource);
1583 assertErrors(sourceB, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
1584 verify([librarySource, sourceA, sourceB]);
1585 }
1586
1587 void test_duplicateDefinition_catch() {
1588 Source source = addSource(r'''
1589 main() {
1590 try {} catch (e, e) {}
1591 }''');
1592 computeLibrarySourceErrors(source);
1593 assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
1594 verify([source]);
1595 }
1596
1597 void test_duplicateDefinition_classMembers_fields() {
1598 Source source = addSource(r'''
1599 class A {
1600 int a;
1601 int a;
1602 }''');
1603 computeLibrarySourceErrors(source);
1604 assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
1605 verify([source]);
1606 }
1607
1608 void test_duplicateDefinition_classMembers_fields_oneStatic() {
1609 Source source = addSource(r'''
1610 class A {
1611 int x;
1612 static int x;
1613 }''');
1614 computeLibrarySourceErrors(source);
1615 assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
1616 verify([source]);
1617 }
1618
1619 void test_duplicateDefinition_classMembers_methods() {
1620 Source source = addSource(r'''
1621 class A {
1622 m() {}
1623 m() {}
1624 }''');
1625 computeLibrarySourceErrors(source);
1626 assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
1627 verify([source]);
1628 }
1629
1630 void test_duplicateDefinition_locals_inCase() {
1631 Source source = addSource(r'''
1632 main() {
1633 switch(1) {
1634 case 1:
1635 var a;
1636 var a;
1637 }
1638 }''');
1639 computeLibrarySourceErrors(source);
1640 assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
1641 verify([source]);
1642 }
1643
1644 void test_duplicateDefinition_locals_inFunctionBlock() {
1645 Source source = addSource(r'''
1646 main() {
1647 int m = 0;
1648 m(a) {}
1649 }''');
1650 computeLibrarySourceErrors(source);
1651 assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
1652 verify([source]);
1653 }
1654
1655 void test_duplicateDefinition_locals_inIf() {
1656 Source source = addSource(r'''
1657 main(int p) {
1658 if (p != 0) {
1659 var a;
1660 var a;
1661 }
1662 }''');
1663 computeLibrarySourceErrors(source);
1664 assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
1665 verify([source]);
1666 }
1667
1668 void test_duplicateDefinition_locals_inMethodBlock() {
1669 Source source = addSource(r'''
1670 class A {
1671 m() {
1672 int a;
1673 int a;
1674 }
1675 }''');
1676 computeLibrarySourceErrors(source);
1677 assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
1678 verify([source]);
1679 }
1680
1681 void test_duplicateDefinition_parameters_inFunctionTypeAlias() {
1682 Source source = addSource(r'''
1683 typedef F(int a, double a);
1684 ''');
1685 computeLibrarySourceErrors(source);
1686 assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
1687 verify([source]);
1688 }
1689
1690 void test_duplicateDefinition_parameters_inLocalFunction() {
1691 Source source = addSource(r'''
1692 main() {
1693 f(int a, double a) {
1694 };
1695 }''');
1696 computeLibrarySourceErrors(source);
1697 assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
1698 verify([source]);
1699 }
1700
1701 void test_duplicateDefinition_parameters_inMethod() {
1702 Source source = addSource(r'''
1703 class A {
1704 m(int a, double a) {
1705 }
1706 }''');
1707 computeLibrarySourceErrors(source);
1708 assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
1709 verify([source]);
1710 }
1711
1712 void test_duplicateDefinition_parameters_inTopLevelFunction() {
1713 Source source = addSource(r'''
1714 f(int a, double a) {
1715 }''');
1716 computeLibrarySourceErrors(source);
1717 assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
1718 verify([source]);
1719 }
1720
1721 void test_duplicateDefinition_typeParameters() {
1722 Source source = addSource(r'''
1723 class A<T, T> {
1724 }''');
1725 computeLibrarySourceErrors(source);
1726 assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
1727 verify([source]);
1728 }
1729
1730 void test_duplicateDefinitionInheritance_instanceGetter_staticGetter() {
1731 Source source = addSource(r'''
1732 class A {
1733 int get x => 0;
1734 }
1735 class B extends A {
1736 static int get x => 0;
1737 }''');
1738 computeLibrarySourceErrors(source);
1739 assertErrors(
1740 source, [CompileTimeErrorCode.DUPLICATE_DEFINITION_INHERITANCE]);
1741 verify([source]);
1742 }
1743
1744 void test_duplicateDefinitionInheritance_instanceGetterAbstract_staticGetter() {
1745 Source source = addSource(r'''
1746 abstract class A {
1747 int get x;
1748 }
1749 class B extends A {
1750 static int get x => 0;
1751 }''');
1752 computeLibrarySourceErrors(source);
1753 assertErrors(
1754 source, [CompileTimeErrorCode.DUPLICATE_DEFINITION_INHERITANCE]);
1755 verify([source]);
1756 }
1757
1758 void test_duplicateDefinitionInheritance_instanceMethod_staticMethod() {
1759 Source source = addSource(r'''
1760 class A {
1761 x() {}
1762 }
1763 class B extends A {
1764 static x() {}
1765 }''');
1766 computeLibrarySourceErrors(source);
1767 assertErrors(
1768 source, [CompileTimeErrorCode.DUPLICATE_DEFINITION_INHERITANCE]);
1769 verify([source]);
1770 }
1771
1772 void test_duplicateDefinitionInheritance_instanceMethodAbstract_staticMethod() {
1773 Source source = addSource(r'''
1774 abstract class A {
1775 x();
1776 }
1777 abstract class B extends A {
1778 static x() {}
1779 }''');
1780 computeLibrarySourceErrors(source);
1781 assertErrors(
1782 source, [CompileTimeErrorCode.DUPLICATE_DEFINITION_INHERITANCE]);
1783 verify([source]);
1784 }
1785
1786 void test_duplicateDefinitionInheritance_instanceSetter_staticSetter() {
1787 Source source = addSource(r'''
1788 class A {
1789 set x(value) {}
1790 }
1791 class B extends A {
1792 static set x(value) {}
1793 }''');
1794 computeLibrarySourceErrors(source);
1795 assertErrors(
1796 source, [CompileTimeErrorCode.DUPLICATE_DEFINITION_INHERITANCE]);
1797 verify([source]);
1798 }
1799
1800 void test_duplicateDefinitionInheritance_instanceSetterAbstract_staticSetter() {
1801 Source source = addSource(r'''
1802 abstract class A {
1803 set x(value);
1804 }
1805 class B extends A {
1806 static set x(value) {}
1807 }''');
1808 computeLibrarySourceErrors(source);
1809 assertErrors(
1810 source, [CompileTimeErrorCode.DUPLICATE_DEFINITION_INHERITANCE]);
1811 verify([source]);
1812 }
1813
1814 void test_duplicateNamedArgument() {
1815 Source source = addSource(r'''
1816 f({a, b}) {}
1817 main() {
1818 f(a: 1, a: 2);
1819 }''');
1820 computeLibrarySourceErrors(source);
1821 assertErrors(source, [CompileTimeErrorCode.DUPLICATE_NAMED_ARGUMENT]);
1822 verify([source]);
1823 }
1824
1825 void test_exportInternalLibrary() {
1826 Source source = addSource("export 'dart:_interceptors';");
1827 computeLibrarySourceErrors(source);
1828 assertErrors(source, [CompileTimeErrorCode.EXPORT_INTERNAL_LIBRARY]);
1829 verify([source]);
1830 }
1831
1832 void test_exportOfNonLibrary() {
1833 Source source = addSource(r'''
1834 library L;
1835 export 'lib1.dart';''');
1836 addNamedSource("/lib1.dart", "part of lib;");
1837 computeLibrarySourceErrors(source);
1838 assertErrors(source, [CompileTimeErrorCode.EXPORT_OF_NON_LIBRARY]);
1839 verify([source]);
1840 }
1841
1842 void test_extendsDeferredClass() {
1843 resolveWithErrors(<String>[
1844 r'''
1845 library lib1;
1846 class A {}''',
1847 r'''
1848 library root;
1849 import 'lib1.dart' deferred as a;
1850 class B extends a.A {}'''
1851 ], <ErrorCode>[
1852 CompileTimeErrorCode.EXTENDS_DEFERRED_CLASS
1853 ]);
1854 }
1855
1856 void test_extendsDeferredClass_classTypeAlias() {
1857 resolveWithErrors(<String>[
1858 r'''
1859 library lib1;
1860 class A {}''',
1861 r'''
1862 library root;
1863 import 'lib1.dart' deferred as a;
1864 class M {}
1865 class C = a.A with M;'''
1866 ], <ErrorCode>[
1867 CompileTimeErrorCode.EXTENDS_DEFERRED_CLASS
1868 ]);
1869 }
1870
1871 void test_extendsDisallowedClass_class_bool() {
1872 Source source = addSource("class A extends bool {}");
1873 computeLibrarySourceErrors(source);
1874 assertErrors(source, [
1875 CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS,
1876 CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT
1877 ]);
1878 verify([source]);
1879 }
1880
1881 void test_extendsDisallowedClass_class_double() {
1882 Source source = addSource("class A extends double {}");
1883 computeLibrarySourceErrors(source);
1884 assertErrors(source, [
1885 CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS,
1886 CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT
1887 ]);
1888 verify([source]);
1889 }
1890
1891 void test_extendsDisallowedClass_class_int() {
1892 Source source = addSource("class A extends int {}");
1893 computeLibrarySourceErrors(source);
1894 assertErrors(source, [
1895 CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS,
1896 CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT
1897 ]);
1898 verify([source]);
1899 }
1900
1901 void test_extendsDisallowedClass_class_Null() {
1902 Source source = addSource("class A extends Null {}");
1903 computeLibrarySourceErrors(source);
1904 assertErrors(source, [
1905 CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS,
1906 CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT
1907 ]);
1908 verify([source]);
1909 }
1910
1911 void test_extendsDisallowedClass_class_num() {
1912 Source source = addSource("class A extends num {}");
1913 computeLibrarySourceErrors(source);
1914 assertErrors(source, [
1915 CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS,
1916 CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT
1917 ]);
1918 verify([source]);
1919 }
1920
1921 void test_extendsDisallowedClass_class_String() {
1922 Source source = addSource("class A extends String {}");
1923 computeLibrarySourceErrors(source);
1924 assertErrors(source, [
1925 CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS,
1926 CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT
1927 ]);
1928 verify([source]);
1929 }
1930
1931 void test_extendsDisallowedClass_classTypeAlias_bool() {
1932 Source source = addSource(r'''
1933 class M {}
1934 class C = bool with M;''');
1935 computeLibrarySourceErrors(source);
1936 assertErrors(source, [CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS]);
1937 verify([source]);
1938 }
1939
1940 void test_extendsDisallowedClass_classTypeAlias_double() {
1941 Source source = addSource(r'''
1942 class M {}
1943 class C = double with M;''');
1944 computeLibrarySourceErrors(source);
1945 assertErrors(source, [CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS]);
1946 verify([source]);
1947 }
1948
1949 void test_extendsDisallowedClass_classTypeAlias_int() {
1950 Source source = addSource(r'''
1951 class M {}
1952 class C = int with M;''');
1953 computeLibrarySourceErrors(source);
1954 assertErrors(source, [CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS]);
1955 verify([source]);
1956 }
1957
1958 void test_extendsDisallowedClass_classTypeAlias_Null() {
1959 Source source = addSource(r'''
1960 class M {}
1961 class C = Null with M;''');
1962 computeLibrarySourceErrors(source);
1963 assertErrors(source, [CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS]);
1964 verify([source]);
1965 }
1966
1967 void test_extendsDisallowedClass_classTypeAlias_num() {
1968 Source source = addSource(r'''
1969 class M {}
1970 class C = num with M;''');
1971 computeLibrarySourceErrors(source);
1972 assertErrors(source, [CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS]);
1973 verify([source]);
1974 }
1975
1976 void test_extendsDisallowedClass_classTypeAlias_String() {
1977 Source source = addSource(r'''
1978 class M {}
1979 class C = String with M;''');
1980 computeLibrarySourceErrors(source);
1981 assertErrors(source, [CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS]);
1982 verify([source]);
1983 }
1984
1985 void test_extendsEnum() {
1986 Source source = addSource(r'''
1987 enum E { ONE }
1988 class A extends E {}''');
1989 computeLibrarySourceErrors(source);
1990 assertErrors(source, [CompileTimeErrorCode.EXTENDS_ENUM]);
1991 verify([source]);
1992 }
1993
1994 void test_extendsNonClass_class() {
1995 Source source = addSource(r'''
1996 int A;
1997 class B extends A {}''');
1998 computeLibrarySourceErrors(source);
1999 assertErrors(source, [CompileTimeErrorCode.EXTENDS_NON_CLASS]);
2000 verify([source]);
2001 }
2002
2003 void test_extendsNonClass_dynamic() {
2004 Source source = addSource("class B extends dynamic {}");
2005 computeLibrarySourceErrors(source);
2006 assertErrors(source, [CompileTimeErrorCode.EXTENDS_NON_CLASS]);
2007 verify([source]);
2008 }
2009
2010 void test_extraPositionalArguments_const() {
2011 Source source = addSource(r'''
2012 class A {
2013 const A();
2014 }
2015 main() {
2016 const A(0);
2017 }''');
2018 computeLibrarySourceErrors(source);
2019 assertErrors(source, [CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS]);
2020 verify([source]);
2021 }
2022
2023 void test_extraPositionalArguments_const_super() {
2024 Source source = addSource(r'''
2025 class A {
2026 const A();
2027 }
2028 class B extends A {
2029 const B() : super(0);
2030 }''');
2031 computeLibrarySourceErrors(source);
2032 assertErrors(source, [CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS]);
2033 verify([source]);
2034 }
2035
2036 void test_fieldInitializedByMultipleInitializers() {
2037 Source source = addSource(r'''
2038 class A {
2039 int x;
2040 A() : x = 0, x = 1 {}
2041 }''');
2042 computeLibrarySourceErrors(source);
2043 assertErrors(source,
2044 [CompileTimeErrorCode.FIELD_INITIALIZED_BY_MULTIPLE_INITIALIZERS]);
2045 verify([source]);
2046 }
2047
2048 void test_fieldInitializedByMultipleInitializers_multipleInits() {
2049 Source source = addSource(r'''
2050 class A {
2051 int x;
2052 A() : x = 0, x = 1, x = 2 {}
2053 }''');
2054 computeLibrarySourceErrors(source);
2055 assertErrors(source, [
2056 CompileTimeErrorCode.FIELD_INITIALIZED_BY_MULTIPLE_INITIALIZERS,
2057 CompileTimeErrorCode.FIELD_INITIALIZED_BY_MULTIPLE_INITIALIZERS
2058 ]);
2059 verify([source]);
2060 }
2061
2062 void test_fieldInitializedByMultipleInitializers_multipleNames() {
2063 Source source = addSource(r'''
2064 class A {
2065 int x;
2066 int y;
2067 A() : x = 0, x = 1, y = 0, y = 1 {}
2068 }''');
2069 computeLibrarySourceErrors(source);
2070 assertErrors(source, [
2071 CompileTimeErrorCode.FIELD_INITIALIZED_BY_MULTIPLE_INITIALIZERS,
2072 CompileTimeErrorCode.FIELD_INITIALIZED_BY_MULTIPLE_INITIALIZERS
2073 ]);
2074 verify([source]);
2075 }
2076
2077 void test_fieldInitializedInParameterAndInitializer() {
2078 Source source = addSource(r'''
2079 class A {
2080 int x;
2081 A(this.x) : x = 1 {}
2082 }''');
2083 computeLibrarySourceErrors(source);
2084 assertErrors(source,
2085 [CompileTimeErrorCode.FIELD_INITIALIZED_IN_PARAMETER_AND_INITIALIZER]);
2086 verify([source]);
2087 }
2088
2089 void test_fieldInitializerFactoryConstructor() {
2090 Source source = addSource(r'''
2091 class A {
2092 int x;
2093 factory A(this.x) {}
2094 }''');
2095 computeLibrarySourceErrors(source);
2096 assertErrors(
2097 source, [CompileTimeErrorCode.FIELD_INITIALIZER_FACTORY_CONSTRUCTOR]);
2098 verify([source]);
2099 }
2100
2101 void test_fieldInitializerOutsideConstructor() {
2102 // TODO(brianwilkerson) Fix the duplicate error messages.
2103 Source source = addSource(r'''
2104 class A {
2105 int x;
2106 m(this.x) {}
2107 }''');
2108 computeLibrarySourceErrors(source);
2109 assertErrors(source, [
2110 ParserErrorCode.FIELD_INITIALIZER_OUTSIDE_CONSTRUCTOR,
2111 CompileTimeErrorCode.FIELD_INITIALIZER_OUTSIDE_CONSTRUCTOR
2112 ]);
2113 verify([source]);
2114 }
2115
2116 void test_fieldInitializerOutsideConstructor_defaultParameter() {
2117 Source source = addSource(r'''
2118 class A {
2119 int x;
2120 m([this.x]) {}
2121 }''');
2122 computeLibrarySourceErrors(source);
2123 assertErrors(
2124 source, [CompileTimeErrorCode.FIELD_INITIALIZER_OUTSIDE_CONSTRUCTOR]);
2125 verify([source]);
2126 }
2127
2128 void test_fieldInitializerRedirectingConstructor_afterRedirection() {
2129 Source source = addSource(r'''
2130 class A {
2131 int x;
2132 A.named() {}
2133 A() : this.named(), x = 42;
2134 }''');
2135 computeLibrarySourceErrors(source);
2136 assertErrors(source,
2137 [CompileTimeErrorCode.FIELD_INITIALIZER_REDIRECTING_CONSTRUCTOR]);
2138 verify([source]);
2139 }
2140
2141 void test_fieldInitializerRedirectingConstructor_beforeRedirection() {
2142 Source source = addSource(r'''
2143 class A {
2144 int x;
2145 A.named() {}
2146 A() : x = 42, this.named();
2147 }''');
2148 computeLibrarySourceErrors(source);
2149 assertErrors(source,
2150 [CompileTimeErrorCode.FIELD_INITIALIZER_REDIRECTING_CONSTRUCTOR]);
2151 verify([source]);
2152 }
2153
2154 void test_fieldInitializingFormalRedirectingConstructor() {
2155 Source source = addSource(r'''
2156 class A {
2157 int x;
2158 A.named() {}
2159 A(this.x) : this.named();
2160 }''');
2161 computeLibrarySourceErrors(source);
2162 assertErrors(source,
2163 [CompileTimeErrorCode.FIELD_INITIALIZER_REDIRECTING_CONSTRUCTOR]);
2164 verify([source]);
2165 }
2166
2167 void test_finalInitializedMultipleTimes_initializers() {
2168 Source source = addSource(r'''
2169 class A {
2170 final x;
2171 A() : x = 0, x = 0 {}
2172 }''');
2173 computeLibrarySourceErrors(source);
2174 assertErrors(source,
2175 [CompileTimeErrorCode.FIELD_INITIALIZED_BY_MULTIPLE_INITIALIZERS]);
2176 verify([source]);
2177 }
2178
2179 /**
2180 * This test doesn't test the FINAL_INITIALIZED_MULTIPLE_TIMES code, but tests the
2181 * FIELD_INITIALIZED_IN_PARAMETER_AND_INITIALIZER code instead. It is provided here to show
2182 * coverage over all of the permutations of initializers in constructor declar ations.
2183 *
2184 * Note: FIELD_INITIALIZED_IN_PARAMETER_AND_INITIALIZER covers a subset of
2185 * FINAL_INITIALIZED_MULTIPLE_TIMES, since it more specific, we use it instead of the broader code
2186 */
2187 void test_finalInitializedMultipleTimes_initializingFormal_initializer() {
2188 Source source = addSource(r'''
2189 class A {
2190 final x;
2191 A(this.x) : x = 0 {}
2192 }''');
2193 computeLibrarySourceErrors(source);
2194 assertErrors(source,
2195 [CompileTimeErrorCode.FIELD_INITIALIZED_IN_PARAMETER_AND_INITIALIZER]);
2196 verify([source]);
2197 }
2198
2199 void test_finalInitializedMultipleTimes_initializingFormals() {
2200 Source source = addSource(r'''
2201 class A {
2202 final x;
2203 A(this.x, this.x) {}
2204 }''');
2205 computeLibrarySourceErrors(source);
2206 assertErrors(
2207 source, [CompileTimeErrorCode.FINAL_INITIALIZED_MULTIPLE_TIMES]);
2208 verify([source]);
2209 }
2210
2211 void test_finalNotInitialized_instanceField_const_static() {
2212 Source source = addSource(r'''
2213 class A {
2214 static const F;
2215 }''');
2216 computeLibrarySourceErrors(source);
2217 assertErrors(source, [CompileTimeErrorCode.CONST_NOT_INITIALIZED]);
2218 verify([source]);
2219 }
2220
2221 void test_finalNotInitialized_library_const() {
2222 Source source = addSource("const F;");
2223 computeLibrarySourceErrors(source);
2224 assertErrors(source, [CompileTimeErrorCode.CONST_NOT_INITIALIZED]);
2225 verify([source]);
2226 }
2227
2228 void test_finalNotInitialized_local_const() {
2229 Source source = addSource(r'''
2230 f() {
2231 const int x;
2232 }''');
2233 computeLibrarySourceErrors(source);
2234 assertErrors(source, [CompileTimeErrorCode.CONST_NOT_INITIALIZED]);
2235 verify([source]);
2236 }
2237
2238 void test_fromEnvironment_bool_badArgs() {
2239 Source source = addSource(r'''
2240 var b1 = const bool.fromEnvironment(1);
2241 var b2 = const bool.fromEnvironment('x', defaultValue: 1);''');
2242 computeLibrarySourceErrors(source);
2243 assertErrors(source, [
2244 CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION,
2245 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE,
2246 CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION,
2247 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
2248 ]);
2249 verify([source]);
2250 }
2251
2252 void test_fromEnvironment_bool_badDefault_whenDefined() {
2253 // The type of the defaultValue needs to be correct even when the default
2254 // value isn't used (because the variable is defined in the environment).
2255 analysisContext2.declaredVariables.define("x", "true");
2256 Source source =
2257 addSource("var b = const bool.fromEnvironment('x', defaultValue: 1);");
2258 computeLibrarySourceErrors(source);
2259 assertErrors(source, [
2260 CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION,
2261 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
2262 ]);
2263 verify([source]);
2264 }
2265
2266 void test_getterAndMethodWithSameName() {
2267 Source source = addSource(r'''
2268 class A {
2269 x(y) {}
2270 get x => 0;
2271 }''');
2272 computeLibrarySourceErrors(source);
2273 assertErrors(
2274 source, [CompileTimeErrorCode.GETTER_AND_METHOD_WITH_SAME_NAME]);
2275 verify([source]);
2276 }
2277
2278 void test_implementsDeferredClass() {
2279 resolveWithErrors(<String>[
2280 r'''
2281 library lib1;
2282 class A {}''',
2283 r'''
2284 library root;
2285 import 'lib1.dart' deferred as a;
2286 class B implements a.A {}'''
2287 ], <ErrorCode>[
2288 CompileTimeErrorCode.IMPLEMENTS_DEFERRED_CLASS
2289 ]);
2290 }
2291
2292 void test_implementsDeferredClass_classTypeAlias() {
2293 resolveWithErrors(<String>[
2294 r'''
2295 library lib1;
2296 class A {}''',
2297 r'''
2298 library root;
2299 import 'lib1.dart' deferred as a;
2300 class B {}
2301 class M {}
2302 class C = B with M implements a.A;'''
2303 ], <ErrorCode>[
2304 CompileTimeErrorCode.IMPLEMENTS_DEFERRED_CLASS
2305 ]);
2306 }
2307
2308 void test_implementsDisallowedClass_class_bool() {
2309 Source source = addSource("class A implements bool {}");
2310 computeLibrarySourceErrors(source);
2311 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
2312 verify([source]);
2313 }
2314
2315 void test_implementsDisallowedClass_class_double() {
2316 Source source = addSource("class A implements double {}");
2317 computeLibrarySourceErrors(source);
2318 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
2319 verify([source]);
2320 }
2321
2322 void test_implementsDisallowedClass_class_int() {
2323 Source source = addSource("class A implements int {}");
2324 computeLibrarySourceErrors(source);
2325 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
2326 verify([source]);
2327 }
2328
2329 void test_implementsDisallowedClass_class_Null() {
2330 Source source = addSource("class A implements Null {}");
2331 computeLibrarySourceErrors(source);
2332 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
2333 verify([source]);
2334 }
2335
2336 void test_implementsDisallowedClass_class_num() {
2337 Source source = addSource("class A implements num {}");
2338 computeLibrarySourceErrors(source);
2339 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
2340 verify([source]);
2341 }
2342
2343 void test_implementsDisallowedClass_class_String() {
2344 Source source = addSource("class A implements String {}");
2345 computeLibrarySourceErrors(source);
2346 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
2347 verify([source]);
2348 }
2349
2350 void test_implementsDisallowedClass_class_String_num() {
2351 Source source = addSource("class A implements String, num {}");
2352 computeLibrarySourceErrors(source);
2353 assertErrors(source, [
2354 CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS,
2355 CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS
2356 ]);
2357 verify([source]);
2358 }
2359
2360 void test_implementsDisallowedClass_classTypeAlias_bool() {
2361 Source source = addSource(r'''
2362 class A {}
2363 class M {}
2364 class C = A with M implements bool;''');
2365 computeLibrarySourceErrors(source);
2366 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
2367 verify([source]);
2368 }
2369
2370 void test_implementsDisallowedClass_classTypeAlias_double() {
2371 Source source = addSource(r'''
2372 class A {}
2373 class M {}
2374 class C = A with M implements double;''');
2375 computeLibrarySourceErrors(source);
2376 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
2377 verify([source]);
2378 }
2379
2380 void test_implementsDisallowedClass_classTypeAlias_int() {
2381 Source source = addSource(r'''
2382 class A {}
2383 class M {}
2384 class C = A with M implements int;''');
2385 computeLibrarySourceErrors(source);
2386 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
2387 verify([source]);
2388 }
2389
2390 void test_implementsDisallowedClass_classTypeAlias_Null() {
2391 Source source = addSource(r'''
2392 class A {}
2393 class M {}
2394 class C = A with M implements Null;''');
2395 computeLibrarySourceErrors(source);
2396 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
2397 verify([source]);
2398 }
2399
2400 void test_implementsDisallowedClass_classTypeAlias_num() {
2401 Source source = addSource(r'''
2402 class A {}
2403 class M {}
2404 class C = A with M implements num;''');
2405 computeLibrarySourceErrors(source);
2406 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
2407 verify([source]);
2408 }
2409
2410 void test_implementsDisallowedClass_classTypeAlias_String() {
2411 Source source = addSource(r'''
2412 class A {}
2413 class M {}
2414 class C = A with M implements String;''');
2415 computeLibrarySourceErrors(source);
2416 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
2417 verify([source]);
2418 }
2419
2420 void test_implementsDisallowedClass_classTypeAlias_String_num() {
2421 Source source = addSource(r'''
2422 class A {}
2423 class M {}
2424 class C = A with M implements String, num;''');
2425 computeLibrarySourceErrors(source);
2426 assertErrors(source, [
2427 CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS,
2428 CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS
2429 ]);
2430 verify([source]);
2431 }
2432
2433 void test_implementsDynamic() {
2434 Source source = addSource("class A implements dynamic {}");
2435 computeLibrarySourceErrors(source);
2436 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DYNAMIC]);
2437 verify([source]);
2438 }
2439
2440 void test_implementsEnum() {
2441 Source source = addSource(r'''
2442 enum E { ONE }
2443 class A implements E {}''');
2444 computeLibrarySourceErrors(source);
2445 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_ENUM]);
2446 verify([source]);
2447 }
2448
2449 void test_implementsNonClass_class() {
2450 Source source = addSource(r'''
2451 int A;
2452 class B implements A {}''');
2453 computeLibrarySourceErrors(source);
2454 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_NON_CLASS]);
2455 verify([source]);
2456 }
2457
2458 void test_implementsNonClass_typeAlias() {
2459 Source source = addSource(r'''
2460 class A {}
2461 class M {}
2462 int B;
2463 class C = A with M implements B;''');
2464 computeLibrarySourceErrors(source);
2465 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_NON_CLASS]);
2466 verify([source]);
2467 }
2468
2469 void test_implementsRepeated() {
2470 Source source = addSource(r'''
2471 class A {}
2472 class B implements A, A {}''');
2473 computeLibrarySourceErrors(source);
2474 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_REPEATED]);
2475 verify([source]);
2476 }
2477
2478 void test_implementsRepeated_3times() {
2479 Source source = addSource(r'''
2480 class A {} class C{}
2481 class B implements A, A, A, A {}''');
2482 computeLibrarySourceErrors(source);
2483 assertErrors(source, [
2484 CompileTimeErrorCode.IMPLEMENTS_REPEATED,
2485 CompileTimeErrorCode.IMPLEMENTS_REPEATED,
2486 CompileTimeErrorCode.IMPLEMENTS_REPEATED
2487 ]);
2488 verify([source]);
2489 }
2490
2491 void test_implementsSuperClass() {
2492 Source source = addSource(r'''
2493 class A {}
2494 class B extends A implements A {}''');
2495 computeLibrarySourceErrors(source);
2496 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_SUPER_CLASS]);
2497 verify([source]);
2498 }
2499
2500 void test_implementsSuperClass_Object() {
2501 Source source = addSource("class A implements Object {}");
2502 computeLibrarySourceErrors(source);
2503 assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_SUPER_CLASS]);
2504 verify([source]);
2505 }
2506
2507 void test_implicitThisReferenceInInitializer_field() {
2508 Source source = addSource(r'''
2509 class A {
2510 var v;
2511 A() : v = f;
2512 var f;
2513 }''');
2514 computeLibrarySourceErrors(source);
2515 assertErrors(
2516 source, [CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER]);
2517 verify([source]);
2518 }
2519
2520 void test_implicitThisReferenceInInitializer_field2() {
2521 Source source = addSource(r'''
2522 class A {
2523 final x = 0;
2524 final y = x;
2525 }''');
2526 computeLibrarySourceErrors(source);
2527 assertErrors(
2528 source, [CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER]);
2529 verify([source]);
2530 }
2531
2532 void test_implicitThisReferenceInInitializer_invocation() {
2533 Source source = addSource(r'''
2534 class A {
2535 var v;
2536 A() : v = f();
2537 f() {}
2538 }''');
2539 computeLibrarySourceErrors(source);
2540 assertErrors(
2541 source, [CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER]);
2542 verify([source]);
2543 }
2544
2545 void test_implicitThisReferenceInInitializer_invocationInStatic() {
2546 Source source = addSource(r'''
2547 class A {
2548 static var F = m();
2549 m() {}
2550 }''');
2551 computeLibrarySourceErrors(source);
2552 assertErrors(
2553 source, [CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER]);
2554 verify([source]);
2555 }
2556
2557 void test_implicitThisReferenceInInitializer_redirectingConstructorInvocation( ) {
2558 Source source = addSource(r'''
2559 class A {
2560 A(p) {}
2561 A.named() : this(f);
2562 var f;
2563 }''');
2564 computeLibrarySourceErrors(source);
2565 assertErrors(
2566 source, [CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER]);
2567 verify([source]);
2568 }
2569
2570 void test_implicitThisReferenceInInitializer_superConstructorInvocation() {
2571 Source source = addSource(r'''
2572 class A {
2573 A(p) {}
2574 }
2575 class B extends A {
2576 B() : super(f);
2577 var f;
2578 }''');
2579 computeLibrarySourceErrors(source);
2580 assertErrors(
2581 source, [CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER]);
2582 verify([source]);
2583 }
2584
2585 void test_importInternalLibrary() {
2586 Source source = addSource("import 'dart:_interceptors';");
2587 computeLibrarySourceErrors(source);
2588 // Note, in these error cases we may generate an UNUSED_IMPORT hint, while
2589 // we could prevent the hint from being generated by testing the import
2590 // directive for the error, this is such a minor corner case that we don't
2591 // think we should add the additional computation time to figure out such
2592 // cases.
2593 assertErrors(source,
2594 [CompileTimeErrorCode.IMPORT_INTERNAL_LIBRARY, HintCode.UNUSED_IMPORT]);
2595 verify([source]);
2596 }
2597
2598 void test_importInternalLibrary_js_helper() {
2599 Source source = addSource("import 'dart:_js_helper';");
2600 computeLibrarySourceErrors(source);
2601 // Note, in these error cases we may generate an UNUSED_IMPORT hint, while
2602 // we could prevent the hint from being generated by testing the import
2603 // directive for the error, this is such a minor corner case that we don't
2604 // think we should add the additional computation time to figure out such
2605 // cases.
2606 assertErrors(source,
2607 [CompileTimeErrorCode.IMPORT_INTERNAL_LIBRARY, HintCode.UNUSED_IMPORT]);
2608 verify([source]);
2609 }
2610
2611 void test_importOfNonLibrary() {
2612 Source source = addSource(r'''
2613 library lib;
2614 import 'part.dart';
2615 A a;''');
2616 addNamedSource(
2617 "/part.dart",
2618 r'''
2619 part of lib;
2620 class A{}''');
2621 computeLibrarySourceErrors(source);
2622 assertErrors(source, [CompileTimeErrorCode.IMPORT_OF_NON_LIBRARY]);
2623 verify([source]);
2624 }
2625
2626 void test_inconsistentCaseExpressionTypes() {
2627 Source source = addSource(r'''
2628 f(var p) {
2629 switch (p) {
2630 case 1:
2631 break;
2632 case 'a':
2633 break;
2634 }
2635 }''');
2636 computeLibrarySourceErrors(source);
2637 assertErrors(
2638 source, [CompileTimeErrorCode.INCONSISTENT_CASE_EXPRESSION_TYPES]);
2639 verify([source]);
2640 }
2641
2642 void test_inconsistentCaseExpressionTypes_dynamic() {
2643 // Even though A.S and S have a static type of "dynamic", we should see
2644 // that they fail to match 3, because they are constant strings.
2645 Source source = addSource(r'''
2646 class A {
2647 static const S = 'A.S';
2648 }
2649
2650 const S = 'S';
2651
2652 foo(var p) {
2653 switch (p) {
2654 case 3:
2655 break;
2656 case S:
2657 break;
2658 case A.S:
2659 break;
2660 }
2661 }''');
2662 computeLibrarySourceErrors(source);
2663 assertErrors(source, [
2664 CompileTimeErrorCode.INCONSISTENT_CASE_EXPRESSION_TYPES,
2665 CompileTimeErrorCode.INCONSISTENT_CASE_EXPRESSION_TYPES
2666 ]);
2667 verify([source]);
2668 }
2669
2670 void test_inconsistentCaseExpressionTypes_repeated() {
2671 Source source = addSource(r'''
2672 f(var p) {
2673 switch (p) {
2674 case 1:
2675 break;
2676 case 'a':
2677 break;
2678 case 'b':
2679 break;
2680 }
2681 }''');
2682 computeLibrarySourceErrors(source);
2683 assertErrors(source, [
2684 CompileTimeErrorCode.INCONSISTENT_CASE_EXPRESSION_TYPES,
2685 CompileTimeErrorCode.INCONSISTENT_CASE_EXPRESSION_TYPES
2686 ]);
2687 verify([source]);
2688 }
2689
2690 void test_initializerForNonExistent_const() {
2691 // Check that the absence of a matching field doesn't cause a
2692 // crash during constant evaluation.
2693 Source source = addSource(r'''
2694 class A {
2695 const A() : x = 'foo';
2696 }
2697 A a = const A();''');
2698 computeLibrarySourceErrors(source);
2699 assertErrors(
2700 source, [CompileTimeErrorCode.INITIALIZER_FOR_NON_EXISTENT_FIELD]);
2701 }
2702
2703 void test_initializerForNonExistent_initializer() {
2704 Source source = addSource(r'''
2705 class A {
2706 A() : x = 0 {}
2707 }''');
2708 computeLibrarySourceErrors(source);
2709 assertErrors(
2710 source, [CompileTimeErrorCode.INITIALIZER_FOR_NON_EXISTENT_FIELD]);
2711 }
2712
2713 void test_initializerForStaticField() {
2714 Source source = addSource(r'''
2715 class A {
2716 static int x;
2717 A() : x = 0 {}
2718 }''');
2719 computeLibrarySourceErrors(source);
2720 assertErrors(source, [CompileTimeErrorCode.INITIALIZER_FOR_STATIC_FIELD]);
2721 verify([source]);
2722 }
2723
2724 void test_initializingFormalForNonExistentField() {
2725 Source source = addSource(r'''
2726 class A {
2727 A(this.x) {}
2728 }''');
2729 computeLibrarySourceErrors(source);
2730 assertErrors(source,
2731 [CompileTimeErrorCode.INITIALIZING_FORMAL_FOR_NON_EXISTENT_FIELD]);
2732 verify([source]);
2733 }
2734
2735 void test_initializingFormalForNonExistentField_notInEnclosingClass() {
2736 Source source = addSource(r'''
2737 class A {
2738 int x;
2739 }
2740 class B extends A {
2741 B(this.x) {}
2742 }''');
2743 computeLibrarySourceErrors(source);
2744 assertErrors(source,
2745 [CompileTimeErrorCode.INITIALIZING_FORMAL_FOR_NON_EXISTENT_FIELD]);
2746 verify([source]);
2747 }
2748
2749 void test_initializingFormalForNonExistentField_optional() {
2750 Source source = addSource(r'''
2751 class A {
2752 A([this.x]) {}
2753 }''');
2754 computeLibrarySourceErrors(source);
2755 assertErrors(source,
2756 [CompileTimeErrorCode.INITIALIZING_FORMAL_FOR_NON_EXISTENT_FIELD]);
2757 verify([source]);
2758 }
2759
2760 void test_initializingFormalForNonExistentField_synthetic() {
2761 Source source = addSource(r'''
2762 class A {
2763 int get x => 1;
2764 A(this.x) {}
2765 }''');
2766 computeLibrarySourceErrors(source);
2767 assertErrors(source,
2768 [CompileTimeErrorCode.INITIALIZING_FORMAL_FOR_NON_EXISTENT_FIELD]);
2769 verify([source]);
2770 }
2771
2772 void test_initializingFormalForStaticField() {
2773 Source source = addSource(r'''
2774 class A {
2775 static int x;
2776 A([this.x]) {}
2777 }''');
2778 computeLibrarySourceErrors(source);
2779 assertErrors(
2780 source, [CompileTimeErrorCode.INITIALIZING_FORMAL_FOR_STATIC_FIELD]);
2781 verify([source]);
2782 }
2783
2784 void test_instanceMemberAccessFromFactory_named() {
2785 Source source = addSource(r'''
2786 class A {
2787 m() {}
2788 A();
2789 factory A.make() {
2790 m();
2791 return new A();
2792 }
2793 }''');
2794 computeLibrarySourceErrors(source);
2795 assertErrors(
2796 source, [CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY]);
2797 verify([source]);
2798 }
2799
2800 void test_instanceMemberAccessFromFactory_unnamed() {
2801 Source source = addSource(r'''
2802 class A {
2803 m() {}
2804 A._();
2805 factory A() {
2806 m();
2807 return new A._();
2808 }
2809 }''');
2810 computeLibrarySourceErrors(source);
2811 assertErrors(
2812 source, [CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY]);
2813 verify([source]);
2814 }
2815
2816 void test_instanceMemberAccessFromStatic_field() {
2817 Source source = addSource(r'''
2818 class A {
2819 int f;
2820 static foo() {
2821 f;
2822 }
2823 }''');
2824 computeLibrarySourceErrors(source);
2825 assertErrors(
2826 source, [CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_STATIC]);
2827 verify([source]);
2828 }
2829
2830 void test_instanceMemberAccessFromStatic_getter() {
2831 Source source = addSource(r'''
2832 class A {
2833 get g => null;
2834 static foo() {
2835 g;
2836 }
2837 }''');
2838 computeLibrarySourceErrors(source);
2839 assertErrors(
2840 source, [CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_STATIC]);
2841 verify([source]);
2842 }
2843
2844 void test_instanceMemberAccessFromStatic_method() {
2845 Source source = addSource(r'''
2846 class A {
2847 m() {}
2848 static foo() {
2849 m();
2850 }
2851 }''');
2852 computeLibrarySourceErrors(source);
2853 assertErrors(
2854 source, [CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_STATIC]);
2855 verify([source]);
2856 }
2857
2858 void test_instantiateEnum_const() {
2859 Source source = addSource(r'''
2860 enum E { ONE }
2861 E e(String name) {
2862 return const E();
2863 }''');
2864 computeLibrarySourceErrors(source);
2865 assertErrors(source, [CompileTimeErrorCode.INSTANTIATE_ENUM]);
2866 verify([source]);
2867 }
2868
2869 void test_instantiateEnum_new() {
2870 Source source = addSource(r'''
2871 enum E { ONE }
2872 E e(String name) {
2873 return new E();
2874 }''');
2875 computeLibrarySourceErrors(source);
2876 assertErrors(source, [CompileTimeErrorCode.INSTANTIATE_ENUM]);
2877 verify([source]);
2878 }
2879
2880 void test_invalidAnnotation_getter() {
2881 Source source = addSource(r'''
2882 get V => 0;
2883 @V
2884 main() {
2885 }''');
2886 computeLibrarySourceErrors(source);
2887 assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
2888 verify([source]);
2889 }
2890
2891 void test_invalidAnnotation_importWithPrefix_getter() {
2892 addNamedSource(
2893 "/lib.dart",
2894 r'''
2895 library lib;
2896 get V => 0;''');
2897 Source source = addSource(r'''
2898 import 'lib.dart' as p;
2899 @p.V
2900 main() {
2901 }''');
2902 computeLibrarySourceErrors(source);
2903 assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
2904 verify([source]);
2905 }
2906
2907 void test_invalidAnnotation_importWithPrefix_notConstantVariable() {
2908 addNamedSource(
2909 "/lib.dart",
2910 r'''
2911 library lib;
2912 final V = 0;''');
2913 Source source = addSource(r'''
2914 import 'lib.dart' as p;
2915 @p.V
2916 main() {
2917 }''');
2918 computeLibrarySourceErrors(source);
2919 assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
2920 verify([source]);
2921 }
2922
2923 void test_invalidAnnotation_importWithPrefix_notVariableOrConstructorInvocatio n() {
2924 addNamedSource(
2925 "/lib.dart",
2926 r'''
2927 library lib;
2928 typedef V();''');
2929 Source source = addSource(r'''
2930 import 'lib.dart' as p;
2931 @p.V
2932 main() {
2933 }''');
2934 computeLibrarySourceErrors(source);
2935 assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
2936 verify([source]);
2937 }
2938
2939 void test_invalidAnnotation_notConstantVariable() {
2940 Source source = addSource(r'''
2941 final V = 0;
2942 @V
2943 main() {
2944 }''');
2945 computeLibrarySourceErrors(source);
2946 assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
2947 verify([source]);
2948 }
2949
2950 void test_invalidAnnotation_notVariableOrConstructorInvocation() {
2951 Source source = addSource(r'''
2952 typedef V();
2953 @V
2954 main() {
2955 }''');
2956 computeLibrarySourceErrors(source);
2957 assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
2958 verify([source]);
2959 }
2960
2961 void test_invalidAnnotation_staticMethodReference() {
2962 Source source = addSource(r'''
2963 class A {
2964 static f() {}
2965 }
2966 @A.f
2967 main() {
2968 }''');
2969 computeLibrarySourceErrors(source);
2970 assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
2971 verify([source]);
2972 }
2973
2974 void test_invalidAnnotation_unresolved_identifier() {
2975 Source source = addSource(r'''
2976 @unresolved
2977 main() {
2978 }''');
2979 computeLibrarySourceErrors(source);
2980 assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
2981 }
2982
2983 void test_invalidAnnotation_unresolved_invocation() {
2984 Source source = addSource(r'''
2985 @Unresolved()
2986 main() {
2987 }''');
2988 computeLibrarySourceErrors(source);
2989 assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
2990 }
2991
2992 void test_invalidAnnotation_unresolved_prefixedIdentifier() {
2993 Source source = addSource(r'''
2994 import 'dart:math' as p;
2995 @p.unresolved
2996 main() {
2997 }''');
2998 computeLibrarySourceErrors(source);
2999 assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
3000 }
3001
3002 void test_invalidAnnotation_useLibraryScope() {
3003 Source source = addSource(r'''
3004 @foo
3005 class A {
3006 static const foo = null;
3007 }''');
3008 computeLibrarySourceErrors(source);
3009 assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
3010 }
3011
3012 void test_invalidAnnotationFromDeferredLibrary() {
3013 // See test_invalidAnnotation_notConstantVariable
3014 resolveWithErrors(<String>[
3015 r'''
3016 library lib1;
3017 class V { const V(); }
3018 const v = const V();''',
3019 r'''
3020 library root;
3021 import 'lib1.dart' deferred as a;
3022 @a.v main () {}'''
3023 ], <ErrorCode>[
3024 CompileTimeErrorCode.INVALID_ANNOTATION_FROM_DEFERRED_LIBRARY
3025 ]);
3026 }
3027
3028 void test_invalidAnnotationFromDeferredLibrary_constructor() {
3029 // See test_invalidAnnotation_notConstantVariable
3030 resolveWithErrors(<String>[
3031 r'''
3032 library lib1;
3033 class C { const C(); }''',
3034 r'''
3035 library root;
3036 import 'lib1.dart' deferred as a;
3037 @a.C() main () {}'''
3038 ], <ErrorCode>[
3039 CompileTimeErrorCode.INVALID_ANNOTATION_FROM_DEFERRED_LIBRARY
3040 ]);
3041 }
3042
3043 void test_invalidAnnotationFromDeferredLibrary_namedConstructor() {
3044 // See test_invalidAnnotation_notConstantVariable
3045 resolveWithErrors(<String>[
3046 r'''
3047 library lib1;
3048 class C { const C.name(); }''',
3049 r'''
3050 library root;
3051 import 'lib1.dart' deferred as a;
3052 @a.C.name() main () {}'''
3053 ], <ErrorCode>[
3054 CompileTimeErrorCode.INVALID_ANNOTATION_FROM_DEFERRED_LIBRARY
3055 ]);
3056 }
3057
3058 void test_invalidConstructorName_notEnclosingClassName_defined() {
3059 Source source = addSource(r'''
3060 class A {
3061 B() : super();
3062 }
3063 class B {}''');
3064 computeLibrarySourceErrors(source);
3065 assertErrors(source, [CompileTimeErrorCode.INVALID_CONSTRUCTOR_NAME]);
3066 // no verify() call, "B" is not resolved
3067 }
3068
3069 void test_invalidConstructorName_notEnclosingClassName_undefined() {
3070 Source source = addSource(r'''
3071 class A {
3072 B() : super();
3073 }''');
3074 computeLibrarySourceErrors(source);
3075 assertErrors(source, [CompileTimeErrorCode.INVALID_CONSTRUCTOR_NAME]);
3076 // no verify() call, "B" is not resolved
3077 }
3078
3079 void test_invalidFactoryNameNotAClass_notClassName() {
3080 Source source = addSource(r'''
3081 int B;
3082 class A {
3083 factory B() {}
3084 }''');
3085 computeLibrarySourceErrors(source);
3086 assertErrors(
3087 source, [CompileTimeErrorCode.INVALID_FACTORY_NAME_NOT_A_CLASS]);
3088 verify([source]);
3089 }
3090
3091 void test_invalidFactoryNameNotAClass_notEnclosingClassName() {
3092 Source source = addSource(r'''
3093 class A {
3094 factory B() {}
3095 }''');
3096 computeLibrarySourceErrors(source);
3097 assertErrors(
3098 source, [CompileTimeErrorCode.INVALID_FACTORY_NAME_NOT_A_CLASS]);
3099 // no verify() call, "B" is not resolved
3100 }
3101
3102 void test_invalidModifierOnConstructor_async() {
3103 Source source = addSource(r'''
3104 class A {
3105 A() async {}
3106 }''');
3107 computeLibrarySourceErrors(source);
3108 assertErrors(
3109 source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_CONSTRUCTOR]);
3110 verify([source]);
3111 }
3112
3113 void test_invalidModifierOnConstructor_asyncStar() {
3114 Source source = addSource(r'''
3115 class A {
3116 A() async* {}
3117 }''');
3118 computeLibrarySourceErrors(source);
3119 assertErrors(
3120 source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_CONSTRUCTOR]);
3121 verify([source]);
3122 }
3123
3124 void test_invalidModifierOnConstructor_syncStar() {
3125 Source source = addSource(r'''
3126 class A {
3127 A() sync* {}
3128 }''');
3129 computeLibrarySourceErrors(source);
3130 assertErrors(
3131 source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_CONSTRUCTOR]);
3132 verify([source]);
3133 }
3134
3135 void test_invalidModifierOnSetter_member_async() {
3136 Source source = addSource(r'''
3137 class A {
3138 set x(v) async {}
3139 }''');
3140 computeLibrarySourceErrors(source);
3141 assertErrors(source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER]);
3142 verify([source]);
3143 }
3144
3145 void test_invalidModifierOnSetter_member_asyncStar() {
3146 Source source = addSource(r'''
3147 class A {
3148 set x(v) async* {}
3149 }''');
3150 computeLibrarySourceErrors(source);
3151 assertErrors(source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER]);
3152 verify([source]);
3153 }
3154
3155 void test_invalidModifierOnSetter_member_syncStar() {
3156 Source source = addSource(r'''
3157 class A {
3158 set x(v) sync* {}
3159 }''');
3160 computeLibrarySourceErrors(source);
3161 assertErrors(source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER]);
3162 verify([source]);
3163 }
3164
3165 void test_invalidModifierOnSetter_topLevel_async() {
3166 Source source = addSource("set x(v) async {}");
3167 computeLibrarySourceErrors(source);
3168 assertErrors(source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER]);
3169 verify([source]);
3170 }
3171
3172 void test_invalidModifierOnSetter_topLevel_asyncStar() {
3173 Source source = addSource("set x(v) async* {}");
3174 computeLibrarySourceErrors(source);
3175 assertErrors(source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER]);
3176 verify([source]);
3177 }
3178
3179 void test_invalidModifierOnSetter_topLevel_syncStar() {
3180 Source source = addSource("set x(v) sync* {}");
3181 computeLibrarySourceErrors(source);
3182 assertErrors(source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER]);
3183 verify([source]);
3184 }
3185
3186 void test_invalidReferenceToThis_factoryConstructor() {
3187 Source source = addSource(r'''
3188 class A {
3189 factory A() { return this; }
3190 }''');
3191 computeLibrarySourceErrors(source);
3192 assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
3193 verify([source]);
3194 }
3195
3196 void test_invalidReferenceToThis_instanceVariableInitializer_inConstructor() {
3197 Source source = addSource(r'''
3198 class A {
3199 var f;
3200 A() : f = this;
3201 }''');
3202 computeLibrarySourceErrors(source);
3203 assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
3204 verify([source]);
3205 }
3206
3207 void test_invalidReferenceToThis_instanceVariableInitializer_inDeclaration() {
3208 Source source = addSource(r'''
3209 class A {
3210 var f = this;
3211 }''');
3212 computeLibrarySourceErrors(source);
3213 assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
3214 verify([source]);
3215 }
3216
3217 void test_invalidReferenceToThis_staticMethod() {
3218 Source source = addSource(r'''
3219 class A {
3220 static m() { return this; }
3221 }''');
3222 computeLibrarySourceErrors(source);
3223 assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
3224 verify([source]);
3225 }
3226
3227 void test_invalidReferenceToThis_staticVariableInitializer() {
3228 Source source = addSource(r'''
3229 class A {
3230 static A f = this;
3231 }''');
3232 computeLibrarySourceErrors(source);
3233 assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
3234 verify([source]);
3235 }
3236
3237 void test_invalidReferenceToThis_superInitializer() {
3238 Source source = addSource(r'''
3239 class A {
3240 A(var x) {}
3241 }
3242 class B extends A {
3243 B() : super(this);
3244 }''');
3245 computeLibrarySourceErrors(source);
3246 assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
3247 verify([source]);
3248 }
3249
3250 void test_invalidReferenceToThis_topLevelFunction() {
3251 Source source = addSource("f() { return this; }");
3252 computeLibrarySourceErrors(source);
3253 assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
3254 verify([source]);
3255 }
3256
3257 void test_invalidReferenceToThis_variableInitializer() {
3258 Source source = addSource("int x = this;");
3259 computeLibrarySourceErrors(source);
3260 assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
3261 verify([source]);
3262 }
3263
3264 void test_invalidTypeArgumentInConstList() {
3265 Source source = addSource(r'''
3266 class A<E> {
3267 m() {
3268 return const <E>[];
3269 }
3270 }''');
3271 computeLibrarySourceErrors(source);
3272 assertErrors(
3273 source, [CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_LIST]);
3274 verify([source]);
3275 }
3276
3277 void test_invalidTypeArgumentInConstMap() {
3278 Source source = addSource(r'''
3279 class A<E> {
3280 m() {
3281 return const <String, E>{};
3282 }
3283 }''');
3284 computeLibrarySourceErrors(source);
3285 assertErrors(
3286 source, [CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_MAP]);
3287 verify([source]);
3288 }
3289
3290 void test_invalidUri_export() {
3291 Source source = addSource("export 'ht:';");
3292 computeLibrarySourceErrors(source);
3293 assertErrors(source, [CompileTimeErrorCode.INVALID_URI]);
3294 }
3295
3296 void test_invalidUri_import() {
3297 Source source = addSource("import 'ht:';");
3298 computeLibrarySourceErrors(source);
3299 assertErrors(source, [CompileTimeErrorCode.INVALID_URI]);
3300 }
3301
3302 void test_invalidUri_part() {
3303 Source source = addSource(r'''
3304 library lib;
3305 part 'ht:';''');
3306 computeLibrarySourceErrors(source);
3307 assertErrors(source, [CompileTimeErrorCode.INVALID_URI]);
3308 }
3309
3310 void test_isInConstInstanceCreation_restored() {
3311 // If ErrorVerifier._isInConstInstanceCreation is not properly restored on
3312 // exit from visitInstanceCreationExpression, the error at (1) will be
3313 // treated as a warning rather than an error.
3314 Source source = addSource(r'''
3315 class Foo<T extends num> {
3316 const Foo(x, y);
3317 }
3318 const x = const Foo<int>(const Foo<int>(0, 1),
3319 const <Foo<String>>[]); // (1)
3320 ''');
3321 computeLibrarySourceErrors(source);
3322 assertErrors(
3323 source, [CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS]);
3324 verify([source]);
3325 }
3326
3327 void test_isInInstanceVariableInitializer_restored() {
3328 // If ErrorVerifier._isInInstanceVariableInitializer is not properly
3329 // restored on exit from visitVariableDeclaration, the error at (1)
3330 // won't be detected.
3331 Source source = addSource(r'''
3332 class Foo {
3333 var bar;
3334 Map foo = {
3335 'bar': () {
3336 var _bar;
3337 },
3338 'bop': _foo // (1)
3339 };
3340 _foo() {
3341 }
3342 }''');
3343 computeLibrarySourceErrors(source);
3344 assertErrors(
3345 source, [CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER]);
3346 verify([source]);
3347 }
3348
3349 void test_labelInOuterScope() {
3350 Source source = addSource(r'''
3351 class A {
3352 void m(int i) {
3353 l: while (i > 0) {
3354 void f() {
3355 break l;
3356 };
3357 }
3358 }
3359 }''');
3360 computeLibrarySourceErrors(source);
3361 assertErrors(source, [CompileTimeErrorCode.LABEL_IN_OUTER_SCOPE]);
3362 // We cannot verify resolution with unresolvable labels
3363 }
3364
3365 void test_labelUndefined_break() {
3366 Source source = addSource(r'''
3367 f() {
3368 x: while (true) {
3369 break y;
3370 }
3371 }''');
3372 computeLibrarySourceErrors(source);
3373 assertErrors(source, [CompileTimeErrorCode.LABEL_UNDEFINED]);
3374 // We cannot verify resolution with undefined labels
3375 }
3376
3377 void test_labelUndefined_continue() {
3378 Source source = addSource(r'''
3379 f() {
3380 x: while (true) {
3381 continue y;
3382 }
3383 }''');
3384 computeLibrarySourceErrors(source);
3385 assertErrors(source, [CompileTimeErrorCode.LABEL_UNDEFINED]);
3386 // We cannot verify resolution with undefined labels
3387 }
3388
3389 void test_length_of_erroneous_constant() {
3390 // Attempting to compute the length of constant that couldn't be evaluated
3391 // (due to an error) should not crash the analyzer (see dartbug.com/23383)
3392 Source source = addSource("const int i = (1 ? 'alpha' : 'beta').length;");
3393 computeLibrarySourceErrors(source);
3394 assertErrors(source, [
3395 CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE,
3396 CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL,
3397 StaticTypeWarningCode.NON_BOOL_CONDITION
3398 ]);
3399 verify([source]);
3400 }
3401
3402 void test_memberWithClassName_field() {
3403 Source source = addSource(r'''
3404 class A {
3405 int A = 0;
3406 }''');
3407 computeLibrarySourceErrors(source);
3408 assertErrors(source, [CompileTimeErrorCode.MEMBER_WITH_CLASS_NAME]);
3409 verify([source]);
3410 }
3411
3412 void test_memberWithClassName_field2() {
3413 Source source = addSource(r'''
3414 class A {
3415 int z, A, b = 0;
3416 }''');
3417 computeLibrarySourceErrors(source);
3418 assertErrors(source, [CompileTimeErrorCode.MEMBER_WITH_CLASS_NAME]);
3419 verify([source]);
3420 }
3421
3422 void test_memberWithClassName_getter() {
3423 Source source = addSource(r'''
3424 class A {
3425 get A => 0;
3426 }''');
3427 computeLibrarySourceErrors(source);
3428 assertErrors(source, [CompileTimeErrorCode.MEMBER_WITH_CLASS_NAME]);
3429 verify([source]);
3430 }
3431
3432 void test_memberWithClassName_method() {
3433 // no test because indistinguishable from constructor
3434 }
3435
3436 void test_methodAndGetterWithSameName() {
3437 Source source = addSource(r'''
3438 class A {
3439 get x => 0;
3440 x(y) {}
3441 }''');
3442 computeLibrarySourceErrors(source);
3443 assertErrors(
3444 source, [CompileTimeErrorCode.METHOD_AND_GETTER_WITH_SAME_NAME]);
3445 verify([source]);
3446 }
3447
3448 void test_missingEnumConstantInSwitch() {
3449 Source source = addSource(r'''
3450 enum E { ONE, TWO, THREE, FOUR }
3451 bool odd(E e) {
3452 switch (e) {
3453 case E.ONE:
3454 case E.THREE: return true;
3455 }
3456 return false;
3457 }''');
3458 computeLibrarySourceErrors(source);
3459 assertErrors(source, [
3460 CompileTimeErrorCode.MISSING_ENUM_CONSTANT_IN_SWITCH,
3461 CompileTimeErrorCode.MISSING_ENUM_CONSTANT_IN_SWITCH
3462 ]);
3463 verify([source]);
3464 }
3465
3466 void test_mixinDeclaresConstructor_classDeclaration() {
3467 Source source = addSource(r'''
3468 class A {
3469 A() {}
3470 }
3471 class B extends Object with A {}''');
3472 computeLibrarySourceErrors(source);
3473 assertErrors(source, [CompileTimeErrorCode.MIXIN_DECLARES_CONSTRUCTOR]);
3474 verify([source]);
3475 }
3476
3477 void test_mixinDeclaresConstructor_typeAlias() {
3478 Source source = addSource(r'''
3479 class A {
3480 A() {}
3481 }
3482 class B = Object with A;''');
3483 computeLibrarySourceErrors(source);
3484 assertErrors(source, [CompileTimeErrorCode.MIXIN_DECLARES_CONSTRUCTOR]);
3485 verify([source]);
3486 }
3487
3488 void test_mixinDeferredClass() {
3489 resolveWithErrors(<String>[
3490 r'''
3491 library lib1;
3492 class A {}''',
3493 r'''
3494 library root;
3495 import 'lib1.dart' deferred as a;
3496 class B extends Object with a.A {}'''
3497 ], <ErrorCode>[
3498 CompileTimeErrorCode.MIXIN_DEFERRED_CLASS
3499 ]);
3500 }
3501
3502 void test_mixinDeferredClass_classTypeAlias() {
3503 resolveWithErrors(<String>[
3504 r'''
3505 library lib1;
3506 class A {}''',
3507 r'''
3508 library root;
3509 import 'lib1.dart' deferred as a;
3510 class B {}
3511 class C = B with a.A;'''
3512 ], <ErrorCode>[
3513 CompileTimeErrorCode.MIXIN_DEFERRED_CLASS
3514 ]);
3515 }
3516
3517 void test_mixinHasNoConstructors_mixinApp() {
3518 Source source = addSource(r'''
3519 class B {
3520 B({x});
3521 }
3522 class M {}
3523 class C = B with M;
3524 ''');
3525 computeLibrarySourceErrors(source);
3526 assertErrors(source, [CompileTimeErrorCode.MIXIN_HAS_NO_CONSTRUCTORS]);
3527 verify([source]);
3528 }
3529
3530 void test_mixinHasNoConstructors_mixinClass() {
3531 Source source = addSource(r'''
3532 class B {
3533 B({x});
3534 }
3535 class M {}
3536 class C extends B with M {}
3537 ''');
3538 // Note: the implicit call from C's default constructor to B() should not
3539 // generate a further error (despite the fact that it's not forwarded),
3540 // since CompileTimeErrorCode.MIXIN_HAS_NO_CONSTRUCTORS does a better job
3541 // of explaining the probem to the user.
3542 computeLibrarySourceErrors(source);
3543 assertErrors(source, [CompileTimeErrorCode.MIXIN_HAS_NO_CONSTRUCTORS]);
3544 verify([source]);
3545 }
3546
3547 void test_mixinHasNoConstructors_mixinClass_explicitSuperCall() {
3548 Source source = addSource(r'''
3549 class B {
3550 B({x});
3551 }
3552 class M {}
3553 class C extends B with M {
3554 C() : super();
3555 }
3556 ''');
3557 // Note: the explicit call from C() to B() should not generate a further
3558 // error (despite the fact that it's not forwarded), since
3559 // CompileTimeErrorCode.MIXIN_HAS_NO_CONSTRUCTORS does a better job of
3560 // explaining the error to the user.
3561 computeLibrarySourceErrors(source);
3562 assertErrors(source, [CompileTimeErrorCode.MIXIN_HAS_NO_CONSTRUCTORS]);
3563 verify([source]);
3564 }
3565
3566 void test_mixinHasNoConstructors_mixinClass_implicitSuperCall() {
3567 Source source = addSource(r'''
3568 class B {
3569 B({x});
3570 }
3571 class M {}
3572 class C extends B with M {
3573 C();
3574 }
3575 ''');
3576 // Note: the implicit call from C() to B() should not generate a further
3577 // error (despite the fact that it's not forwarded), since
3578 // CompileTimeErrorCode.MIXIN_HAS_NO_CONSTRUCTORS does a better job of
3579 // explaining the error to the user.
3580 computeLibrarySourceErrors(source);
3581 assertErrors(source, [CompileTimeErrorCode.MIXIN_HAS_NO_CONSTRUCTORS]);
3582 verify([source]);
3583 }
3584
3585 void test_mixinHasNoConstructors_mixinClass_namedSuperCall() {
3586 Source source = addSource(r'''
3587 class B {
3588 B.named({x});
3589 }
3590 class M {}
3591 class C extends B with M {
3592 C() : super.named();
3593 }
3594 ''');
3595 // Note: the explicit call from C() to B.named() should not generate a
3596 // further error (despite the fact that it's not forwarded), since
3597 // CompileTimeErrorCode.MIXIN_HAS_NO_CONSTRUCTORS does a better job of
3598 // explaining the error to the user.
3599 computeLibrarySourceErrors(source);
3600 assertErrors(source, [CompileTimeErrorCode.MIXIN_HAS_NO_CONSTRUCTORS]);
3601 verify([source]);
3602 }
3603
3604 void test_mixinInheritsFromNotObject_classDeclaration_extends() {
3605 Source source = addSource(r'''
3606 class A {}
3607 class B extends A {}
3608 class C extends Object with B {}''');
3609 computeLibrarySourceErrors(source);
3610 assertErrors(source, [CompileTimeErrorCode.MIXIN_INHERITS_FROM_NOT_OBJECT]);
3611 verify([source]);
3612 }
3613
3614 void test_mixinInheritsFromNotObject_classDeclaration_with() {
3615 Source source = addSource(r'''
3616 class A {}
3617 class B extends Object with A {}
3618 class C extends Object with B {}''');
3619 computeLibrarySourceErrors(source);
3620 assertErrors(source, [CompileTimeErrorCode.MIXIN_INHERITS_FROM_NOT_OBJECT]);
3621 verify([source]);
3622 }
3623
3624 void test_mixinInheritsFromNotObject_typeAlias_extends() {
3625 Source source = addSource(r'''
3626 class A {}
3627 class B extends A {}
3628 class C = Object with B;''');
3629 computeLibrarySourceErrors(source);
3630 assertErrors(source, [CompileTimeErrorCode.MIXIN_INHERITS_FROM_NOT_OBJECT]);
3631 verify([source]);
3632 }
3633
3634 void test_mixinInheritsFromNotObject_typeAlias_with() {
3635 Source source = addSource(r'''
3636 class A {}
3637 class B extends Object with A {}
3638 class C = Object with B;''');
3639 computeLibrarySourceErrors(source);
3640 assertErrors(source, [CompileTimeErrorCode.MIXIN_INHERITS_FROM_NOT_OBJECT]);
3641 verify([source]);
3642 }
3643
3644 void test_mixinOfDisallowedClass_class_bool() {
3645 Source source = addSource("class A extends Object with bool {}");
3646 computeLibrarySourceErrors(source);
3647 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS]);
3648 verify([source]);
3649 }
3650
3651 void test_mixinOfDisallowedClass_class_double() {
3652 Source source = addSource("class A extends Object with double {}");
3653 computeLibrarySourceErrors(source);
3654 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS]);
3655 verify([source]);
3656 }
3657
3658 void test_mixinOfDisallowedClass_class_int() {
3659 Source source = addSource("class A extends Object with int {}");
3660 computeLibrarySourceErrors(source);
3661 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS]);
3662 verify([source]);
3663 }
3664
3665 void test_mixinOfDisallowedClass_class_Null() {
3666 Source source = addSource("class A extends Object with Null {}");
3667 computeLibrarySourceErrors(source);
3668 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS]);
3669 verify([source]);
3670 }
3671
3672 void test_mixinOfDisallowedClass_class_num() {
3673 Source source = addSource("class A extends Object with num {}");
3674 computeLibrarySourceErrors(source);
3675 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS]);
3676 verify([source]);
3677 }
3678
3679 void test_mixinOfDisallowedClass_class_String() {
3680 Source source = addSource("class A extends Object with String {}");
3681 computeLibrarySourceErrors(source);
3682 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS]);
3683 verify([source]);
3684 }
3685
3686 void test_mixinOfDisallowedClass_classTypeAlias_bool() {
3687 Source source = addSource(r'''
3688 class A {}
3689 class C = A with bool;''');
3690 computeLibrarySourceErrors(source);
3691 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS]);
3692 verify([source]);
3693 }
3694
3695 void test_mixinOfDisallowedClass_classTypeAlias_double() {
3696 Source source = addSource(r'''
3697 class A {}
3698 class C = A with double;''');
3699 computeLibrarySourceErrors(source);
3700 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS]);
3701 verify([source]);
3702 }
3703
3704 void test_mixinOfDisallowedClass_classTypeAlias_int() {
3705 Source source = addSource(r'''
3706 class A {}
3707 class C = A with int;''');
3708 computeLibrarySourceErrors(source);
3709 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS]);
3710 verify([source]);
3711 }
3712
3713 void test_mixinOfDisallowedClass_classTypeAlias_Null() {
3714 Source source = addSource(r'''
3715 class A {}
3716 class C = A with Null;''');
3717 computeLibrarySourceErrors(source);
3718 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS]);
3719 verify([source]);
3720 }
3721
3722 void test_mixinOfDisallowedClass_classTypeAlias_num() {
3723 Source source = addSource(r'''
3724 class A {}
3725 class C = A with num;''');
3726 computeLibrarySourceErrors(source);
3727 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS]);
3728 verify([source]);
3729 }
3730
3731 void test_mixinOfDisallowedClass_classTypeAlias_String() {
3732 Source source = addSource(r'''
3733 class A {}
3734 class C = A with String;''');
3735 computeLibrarySourceErrors(source);
3736 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS]);
3737 verify([source]);
3738 }
3739
3740 void test_mixinOfDisallowedClass_classTypeAlias_String_num() {
3741 Source source = addSource(r'''
3742 class A {}
3743 class C = A with String, num;''');
3744 computeLibrarySourceErrors(source);
3745 assertErrors(source, [
3746 CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS,
3747 CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS
3748 ]);
3749 verify([source]);
3750 }
3751
3752 void test_mixinOfEnum() {
3753 Source source = addSource(r'''
3754 enum E { ONE }
3755 class A extends Object with E {}''');
3756 computeLibrarySourceErrors(source);
3757 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_ENUM]);
3758 verify([source]);
3759 }
3760
3761 void test_mixinOfNonClass_class() {
3762 Source source = addSource(r'''
3763 int A;
3764 class B extends Object with A {}''');
3765 computeLibrarySourceErrors(source);
3766 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_NON_CLASS]);
3767 verify([source]);
3768 }
3769
3770 void test_mixinOfNonClass_typeAlias() {
3771 Source source = addSource(r'''
3772 class A {}
3773 int B;
3774 class C = A with B;''');
3775 computeLibrarySourceErrors(source);
3776 assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_NON_CLASS]);
3777 verify([source]);
3778 }
3779
3780 void test_mixinReferencesSuper() {
3781 Source source = addSource(r'''
3782 class A {
3783 toString() => super.toString();
3784 }
3785 class B extends Object with A {}''');
3786 computeLibrarySourceErrors(source);
3787 assertErrors(source, [CompileTimeErrorCode.MIXIN_REFERENCES_SUPER]);
3788 verify([source]);
3789 }
3790
3791 void test_mixinWithNonClassSuperclass_class() {
3792 Source source = addSource(r'''
3793 int A;
3794 class B {}
3795 class C extends A with B {}''');
3796 computeLibrarySourceErrors(source);
3797 assertErrors(
3798 source, [CompileTimeErrorCode.MIXIN_WITH_NON_CLASS_SUPERCLASS]);
3799 verify([source]);
3800 }
3801
3802 void test_mixinWithNonClassSuperclass_typeAlias() {
3803 Source source = addSource(r'''
3804 int A;
3805 class B {}
3806 class C = A with B;''');
3807 computeLibrarySourceErrors(source);
3808 assertErrors(
3809 source, [CompileTimeErrorCode.MIXIN_WITH_NON_CLASS_SUPERCLASS]);
3810 verify([source]);
3811 }
3812
3813 void test_multipleRedirectingConstructorInvocations() {
3814 Source source = addSource(r'''
3815 class A {
3816 A() : this.a(), this.b();
3817 A.a() {}
3818 A.b() {}
3819 }''');
3820 computeLibrarySourceErrors(source);
3821 assertErrors(source,
3822 [CompileTimeErrorCode.MULTIPLE_REDIRECTING_CONSTRUCTOR_INVOCATIONS]);
3823 verify([source]);
3824 }
3825
3826 void test_multipleSuperInitializers() {
3827 Source source = addSource(r'''
3828 class A {}
3829 class B extends A {
3830 B() : super(), super() {}
3831 }''');
3832 computeLibrarySourceErrors(source);
3833 assertErrors(source, [CompileTimeErrorCode.MULTIPLE_SUPER_INITIALIZERS]);
3834 verify([source]);
3835 }
3836
3837 void test_nativeClauseInNonSDKCode() {
3838 // TODO(jwren) Move this test somewhere else: This test verifies a parser
3839 // error code is generated through the ErrorVerifier, it is not a
3840 // CompileTimeErrorCode.
3841 Source source = addSource("class A native 'string' {}");
3842 computeLibrarySourceErrors(source);
3843 assertErrors(source, [ParserErrorCode.NATIVE_CLAUSE_IN_NON_SDK_CODE]);
3844 verify([source]);
3845 }
3846
3847 void test_nativeFunctionBodyInNonSDKCode_function() {
3848 // TODO(jwren) Move this test somewhere else: This test verifies a parser
3849 // error code is generated through the ErrorVerifier, it is not a
3850 // CompileTimeErrorCode.
3851 Source source = addSource("int m(a) native 'string';");
3852 computeLibrarySourceErrors(source);
3853 assertErrors(
3854 source, [ParserErrorCode.NATIVE_FUNCTION_BODY_IN_NON_SDK_CODE]);
3855 verify([source]);
3856 }
3857
3858 void test_nativeFunctionBodyInNonSDKCode_method() {
3859 // TODO(jwren) Move this test somewhere else: This test verifies a parser
3860 // error code is generated through the ErrorVerifier, it is not a
3861 // CompileTimeErrorCode.
3862 Source source = addSource(r'''
3863 class A{
3864 static int m(a) native 'string';
3865 }''');
3866 computeLibrarySourceErrors(source);
3867 assertErrors(
3868 source, [ParserErrorCode.NATIVE_FUNCTION_BODY_IN_NON_SDK_CODE]);
3869 verify([source]);
3870 }
3871
3872 void test_noAnnotationConstructorArguments() {
3873 Source source = addSource(r'''
3874 class A {
3875 const A();
3876 }
3877 @A
3878 main() {
3879 }''');
3880 computeLibrarySourceErrors(source);
3881 assertErrors(
3882 source, [CompileTimeErrorCode.NO_ANNOTATION_CONSTRUCTOR_ARGUMENTS]);
3883 verify([source]);
3884 }
3885
3886 void test_noDefaultSuperConstructorExplicit() {
3887 Source source = addSource(r'''
3888 class A {
3889 A(p);
3890 }
3891 class B extends A {
3892 B() {}
3893 }''');
3894 computeLibrarySourceErrors(source);
3895 assertErrors(
3896 source, [CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_EXPLICIT]);
3897 verify([source]);
3898 }
3899
3900 void test_noDefaultSuperConstructorExplicit_MixinAppWithDirectSuperCall() {
3901 Source source = addSource(r'''
3902 class M {}
3903 class B {
3904 B({x});
3905 B.named(); // To avoid MIXIN_HAS_NO_CONSTRUCTORS
3906 }
3907 class Mixed = B with M;
3908 class C extends Mixed {
3909 C(x) : super();
3910 }
3911 ''');
3912 computeLibrarySourceErrors(source);
3913 assertErrors(source,
3914 [CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER_DEFAULT]);
3915 verify([source]);
3916 }
3917
3918 void test_noDefaultSuperConstructorExplicit_mixinAppWithNamedParam() {
3919 Source source = addSource(r'''
3920 class M {}
3921 class B {
3922 B({x});
3923 B.named(); // To avoid MIXIN_HAS_NO_CONSTRUCTORS
3924 }
3925 class Mixed = B with M;
3926 class C extends Mixed {
3927 C();
3928 }
3929 ''');
3930 computeLibrarySourceErrors(source);
3931 assertErrors(source,
3932 [CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER_DEFAULT]);
3933 verify([source]);
3934 }
3935
3936 void test_noDefaultSuperConstructorExplicit_MixinAppWithNamedSuperCall() {
3937 Source source = addSource(r'''
3938 class M {}
3939 class B {
3940 B.named({x});
3941 B.named2(); // To avoid MIXIN_HAS_NO_CONSTRUCTORS
3942 }
3943 class Mixed = B with M;
3944 class C extends Mixed {
3945 C(x) : super.named();
3946 }
3947 ''');
3948 computeLibrarySourceErrors(source);
3949 assertErrors(
3950 source, [CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER]);
3951 // Don't verify since call to super.named() can't be resolved.
3952 }
3953
3954 void test_noDefaultSuperConstructorExplicit_mixinAppWithOptionalParam() {
3955 Source source = addSource(r'''
3956 class M {}
3957 class B {
3958 B([x]);
3959 B.named(); // To avoid MIXIN_HAS_NO_CONSTRUCTORS
3960 }
3961 class Mixed = B with M;
3962 class C extends Mixed {
3963 C();
3964 }
3965 ''');
3966 computeLibrarySourceErrors(source);
3967 assertErrors(source,
3968 [CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER_DEFAULT]);
3969 verify([source]);
3970 }
3971
3972 void test_noDefaultSuperConstructorExplicit_MixinWithDirectSuperCall() {
3973 Source source = addSource(r'''
3974 class M {}
3975 class B {
3976 B({x});
3977 B.other();
3978 }
3979 class C extends B with M {
3980 C(x) : super();
3981 }
3982 ''');
3983 computeLibrarySourceErrors(source);
3984 assertErrors(source,
3985 [CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER_DEFAULT]);
3986 verify([source]);
3987 }
3988
3989 void test_noDefaultSuperConstructorExplicit_mixinWithNamedParam() {
3990 Source source = addSource(r'''
3991 class M {}
3992 class B {
3993 B({x});
3994 B.named();
3995 }
3996 class C extends B with M {
3997 C();
3998 }
3999 ''');
4000 computeLibrarySourceErrors(source);
4001 assertErrors(
4002 source, [CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_EXPLICIT]);
4003 verify([source]);
4004 }
4005
4006 void test_noDefaultSuperConstructorExplicit_MixinWithNamedSuperCall() {
4007 Source source = addSource(r'''
4008 class M {}
4009 class B {
4010 B.named({x});
4011 B.other();
4012 }
4013 class C extends B with M {
4014 C(x) : super.named();
4015 }
4016 ''');
4017 computeLibrarySourceErrors(source);
4018 assertErrors(
4019 source, [CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER]);
4020 // Don't verify since call to super.named() can't be resolved.
4021 }
4022
4023 void test_noDefaultSuperConstructorExplicit_mixinWithOptionalParam() {
4024 Source source = addSource(r'''
4025 class M {}
4026 class B {
4027 B([x]);
4028 B.other();
4029 }
4030 class C extends B with M {
4031 C();
4032 }
4033 ''');
4034 computeLibrarySourceErrors(source);
4035 assertErrors(
4036 source, [CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_EXPLICIT]);
4037 verify([source]);
4038 }
4039
4040 void test_noDefaultSuperConstructorImplicit_mixinAppWithNamedParam() {
4041 Source source = addSource(r'''
4042 class M {}
4043 class B {
4044 B({x});
4045 B.named(); // To avoid MIXIN_HAS_NO_CONSTRUCTORS
4046 }
4047 class Mixed = B with M;
4048 class C extends Mixed {}
4049 ''');
4050 computeLibrarySourceErrors(source);
4051 assertErrors(
4052 source, [CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT]);
4053 verify([source]);
4054 }
4055
4056 void test_noDefaultSuperConstructorImplicit_mixinAppWithOptionalParam() {
4057 Source source = addSource(r'''
4058 class M {}
4059 class B {
4060 B([x]);
4061 B.named(); // To avoid MIXIN_HAS_NO_CONSTRUCTORS
4062 }
4063 class Mixed = B with M;
4064 class C extends Mixed {}
4065 ''');
4066 computeLibrarySourceErrors(source);
4067 assertErrors(
4068 source, [CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT]);
4069 verify([source]);
4070 }
4071
4072 void test_noDefaultSuperConstructorImplicit_mixinWithNamedParam() {
4073 Source source = addSource(r'''
4074 class M {}
4075 class B {
4076 B({x});
4077 B.other();
4078 }
4079 class C extends B with M {}
4080 ''');
4081 computeLibrarySourceErrors(source);
4082 assertErrors(
4083 source, [CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT]);
4084 verify([source]);
4085 }
4086
4087 void test_noDefaultSuperConstructorImplicit_mixinWithOptionalParam() {
4088 Source source = addSource(r'''
4089 class M {}
4090 class B {
4091 B([x]);
4092 B.other();
4093 }
4094 class C extends B with M {}
4095 ''');
4096 computeLibrarySourceErrors(source);
4097 assertErrors(
4098 source, [CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT]);
4099 verify([source]);
4100 }
4101
4102 void test_noDefaultSuperConstructorImplicit_superHasParameters() {
4103 Source source = addSource(r'''
4104 class A {
4105 A(p);
4106 }
4107 class B extends A {
4108 }''');
4109 computeLibrarySourceErrors(source);
4110 assertErrors(
4111 source, [CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT]);
4112 verify([source]);
4113 }
4114
4115 void test_noDefaultSuperConstructorImplicit_superOnlyNamed() {
4116 Source source = addSource(r'''
4117 class A { A.named() {} }
4118 class B extends A {}''');
4119 computeLibrarySourceErrors(source);
4120 assertErrors(
4121 source, [CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT]);
4122 verify([source]);
4123 }
4124
4125 void test_nonConstantAnnotationConstructor_named() {
4126 Source source = addSource(r'''
4127 class A {
4128 A.fromInt() {}
4129 }
4130 @A.fromInt()
4131 main() {
4132 }''');
4133 computeLibrarySourceErrors(source);
4134 assertErrors(
4135 source, [CompileTimeErrorCode.NON_CONSTANT_ANNOTATION_CONSTRUCTOR]);
4136 verify([source]);
4137 }
4138
4139 void test_nonConstantAnnotationConstructor_unnamed() {
4140 Source source = addSource(r'''
4141 class A {
4142 A() {}
4143 }
4144 @A()
4145 main() {
4146 }''');
4147 computeLibrarySourceErrors(source);
4148 assertErrors(
4149 source, [CompileTimeErrorCode.NON_CONSTANT_ANNOTATION_CONSTRUCTOR]);
4150 verify([source]);
4151 }
4152
4153 void test_nonConstantDefaultValue_function_named() {
4154 Source source = addSource(r'''
4155 int y;
4156 f({x : y}) {}''');
4157 computeLibrarySourceErrors(source);
4158 assertErrors(source, [CompileTimeErrorCode.NON_CONSTANT_DEFAULT_VALUE]);
4159 verify([source]);
4160 }
4161
4162 void test_nonConstantDefaultValue_function_positional() {
4163 Source source = addSource(r'''
4164 int y;
4165 f([x = y]) {}''');
4166 computeLibrarySourceErrors(source);
4167 assertErrors(source, [CompileTimeErrorCode.NON_CONSTANT_DEFAULT_VALUE]);
4168 verify([source]);
4169 }
4170
4171 void test_nonConstantDefaultValue_inConstructor_named() {
4172 Source source = addSource(r'''
4173 class A {
4174 int y;
4175 A({x : y}) {}
4176 }''');
4177 computeLibrarySourceErrors(source);
4178 assertErrors(source, [CompileTimeErrorCode.NON_CONSTANT_DEFAULT_VALUE]);
4179 verify([source]);
4180 }
4181
4182 void test_nonConstantDefaultValue_inConstructor_positional() {
4183 Source source = addSource(r'''
4184 class A {
4185 int y;
4186 A([x = y]) {}
4187 }''');
4188 computeLibrarySourceErrors(source);
4189 assertErrors(source, [CompileTimeErrorCode.NON_CONSTANT_DEFAULT_VALUE]);
4190 verify([source]);
4191 }
4192
4193 void test_nonConstantDefaultValue_method_named() {
4194 Source source = addSource(r'''
4195 class A {
4196 int y;
4197 m({x : y}) {}
4198 }''');
4199 computeLibrarySourceErrors(source);
4200 assertErrors(source, [CompileTimeErrorCode.NON_CONSTANT_DEFAULT_VALUE]);
4201 verify([source]);
4202 }
4203
4204 void test_nonConstantDefaultValue_method_positional() {
4205 Source source = addSource(r'''
4206 class A {
4207 int y;
4208 m([x = y]) {}
4209 }''');
4210 computeLibrarySourceErrors(source);
4211 assertErrors(source, [CompileTimeErrorCode.NON_CONSTANT_DEFAULT_VALUE]);
4212 verify([source]);
4213 }
4214
4215 void test_nonConstantDefaultValueFromDeferredLibrary() {
4216 resolveWithErrors(<String>[
4217 r'''
4218 library lib1;
4219 const V = 1;''',
4220 r'''
4221 library root;
4222 import 'lib1.dart' deferred as a;
4223 f({x : a.V}) {}'''
4224 ], <ErrorCode>[
4225 CompileTimeErrorCode.NON_CONSTANT_DEFAULT_VALUE_FROM_DEFERRED_LIBRARY
4226 ]);
4227 }
4228
4229 void test_nonConstantDefaultValueFromDeferredLibrary_nested() {
4230 resolveWithErrors(<String>[
4231 r'''
4232 library lib1;
4233 const V = 1;''',
4234 r'''
4235 library root;
4236 import 'lib1.dart' deferred as a;
4237 f({x : a.V + 1}) {}'''
4238 ], <ErrorCode>[
4239 CompileTimeErrorCode.NON_CONSTANT_DEFAULT_VALUE_FROM_DEFERRED_LIBRARY
4240 ]);
4241 }
4242
4243 void test_nonConstCaseExpression() {
4244 Source source = addSource(r'''
4245 f(int p, int q) {
4246 switch (p) {
4247 case 3 + q:
4248 break;
4249 }
4250 }''');
4251 computeLibrarySourceErrors(source);
4252 assertErrors(source, [CompileTimeErrorCode.NON_CONSTANT_CASE_EXPRESSION]);
4253 verify([source]);
4254 }
4255
4256 void test_nonConstCaseExpressionFromDeferredLibrary() {
4257 resolveWithErrors(<String>[
4258 r'''
4259 library lib1;
4260 const int c = 1;''',
4261 r'''
4262 library root;
4263 import 'lib1.dart' deferred as a;
4264 main (int p) {
4265 switch (p) {
4266 case a.c:
4267 break;
4268 }
4269 }'''
4270 ], <ErrorCode>[
4271 CompileTimeErrorCode.NON_CONSTANT_CASE_EXPRESSION_FROM_DEFERRED_LIBRARY
4272 ]);
4273 }
4274
4275 void test_nonConstCaseExpressionFromDeferredLibrary_nested() {
4276 resolveWithErrors(<String>[
4277 r'''
4278 library lib1;
4279 const int c = 1;''',
4280 r'''
4281 library root;
4282 import 'lib1.dart' deferred as a;
4283 main (int p) {
4284 switch (p) {
4285 case a.c + 1:
4286 break;
4287 }
4288 }'''
4289 ], <ErrorCode>[
4290 CompileTimeErrorCode.NON_CONSTANT_CASE_EXPRESSION_FROM_DEFERRED_LIBRARY
4291 ]);
4292 }
4293
4294 void test_nonConstListElement() {
4295 Source source = addSource(r'''
4296 f(a) {
4297 return const [a];
4298 }''');
4299 computeLibrarySourceErrors(source);
4300 assertErrors(source, [CompileTimeErrorCode.NON_CONSTANT_LIST_ELEMENT]);
4301 verify([source]);
4302 }
4303
4304 void test_nonConstListElementFromDeferredLibrary() {
4305 resolveWithErrors(<String>[
4306 r'''
4307 library lib1;
4308 const int c = 1;''',
4309 r'''
4310 library root;
4311 import 'lib1.dart' deferred as a;
4312 f() {
4313 return const [a.c];
4314 }'''
4315 ], <ErrorCode>[
4316 CompileTimeErrorCode.NON_CONSTANT_LIST_ELEMENT_FROM_DEFERRED_LIBRARY
4317 ]);
4318 }
4319
4320 void test_nonConstListElementFromDeferredLibrary_nested() {
4321 resolveWithErrors(<String>[
4322 r'''
4323 library lib1;
4324 const int c = 1;''',
4325 r'''
4326 library root;
4327 import 'lib1.dart' deferred as a;
4328 f() {
4329 return const [a.c + 1];
4330 }'''
4331 ], <ErrorCode>[
4332 CompileTimeErrorCode.NON_CONSTANT_LIST_ELEMENT_FROM_DEFERRED_LIBRARY
4333 ]);
4334 }
4335
4336 void test_nonConstMapAsExpressionStatement_begin() {
4337 Source source = addSource(r'''
4338 f() {
4339 {'a' : 0, 'b' : 1}.length;
4340 }''');
4341 computeLibrarySourceErrors(source);
4342 assertErrors(
4343 source, [CompileTimeErrorCode.NON_CONST_MAP_AS_EXPRESSION_STATEMENT]);
4344 verify([source]);
4345 }
4346
4347 void test_nonConstMapAsExpressionStatement_only() {
4348 Source source = addSource(r'''
4349 f() {
4350 {'a' : 0, 'b' : 1};
4351 }''');
4352 computeLibrarySourceErrors(source);
4353 assertErrors(
4354 source, [CompileTimeErrorCode.NON_CONST_MAP_AS_EXPRESSION_STATEMENT]);
4355 verify([source]);
4356 }
4357
4358 void test_nonConstMapKey() {
4359 Source source = addSource(r'''
4360 f(a) {
4361 return const {a : 0};
4362 }''');
4363 computeLibrarySourceErrors(source);
4364 assertErrors(source, [CompileTimeErrorCode.NON_CONSTANT_MAP_KEY]);
4365 verify([source]);
4366 }
4367
4368 void test_nonConstMapKeyFromDeferredLibrary() {
4369 resolveWithErrors(<String>[
4370 r'''
4371 library lib1;
4372 const int c = 1;''',
4373 r'''
4374 library root;
4375 import 'lib1.dart' deferred as a;
4376 f() {
4377 return const {a.c : 0};
4378 }'''
4379 ], <ErrorCode>[
4380 CompileTimeErrorCode.NON_CONSTANT_MAP_KEY_FROM_DEFERRED_LIBRARY
4381 ]);
4382 }
4383
4384 void test_nonConstMapKeyFromDeferredLibrary_nested() {
4385 resolveWithErrors(<String>[
4386 r'''
4387 library lib1;
4388 const int c = 1;''',
4389 r'''
4390 library root;
4391 import 'lib1.dart' deferred as a;
4392 f() {
4393 return const {a.c + 1 : 0};
4394 }'''
4395 ], <ErrorCode>[
4396 CompileTimeErrorCode.NON_CONSTANT_MAP_KEY_FROM_DEFERRED_LIBRARY
4397 ]);
4398 }
4399
4400 void test_nonConstMapValue() {
4401 Source source = addSource(r'''
4402 f(a) {
4403 return const {'a' : a};
4404 }''');
4405 computeLibrarySourceErrors(source);
4406 assertErrors(source, [CompileTimeErrorCode.NON_CONSTANT_MAP_VALUE]);
4407 verify([source]);
4408 }
4409
4410 void test_nonConstMapValueFromDeferredLibrary() {
4411 resolveWithErrors(<String>[
4412 r'''
4413 library lib1;
4414 const int c = 1;''',
4415 r'''
4416 library root;
4417 import 'lib1.dart' deferred as a;
4418 f() {
4419 return const {'a' : a.c};
4420 }'''
4421 ], <ErrorCode>[
4422 CompileTimeErrorCode.NON_CONSTANT_MAP_VALUE_FROM_DEFERRED_LIBRARY
4423 ]);
4424 }
4425
4426 void test_nonConstMapValueFromDeferredLibrary_nested() {
4427 resolveWithErrors(<String>[
4428 r'''
4429 library lib1;
4430 const int c = 1;''',
4431 r'''
4432 library root;
4433 import 'lib1.dart' deferred as a;
4434 f() {
4435 return const {'a' : a.c + 1};
4436 }'''
4437 ], <ErrorCode>[
4438 CompileTimeErrorCode.NON_CONSTANT_MAP_VALUE_FROM_DEFERRED_LIBRARY
4439 ]);
4440 }
4441
4442 void test_nonConstValueInInitializer_binary_notBool_left() {
4443 Source source = addSource(r'''
4444 class A {
4445 final bool a;
4446 const A(String p) : a = p && true;
4447 }''');
4448 computeLibrarySourceErrors(source);
4449 assertErrors(source, [
4450 CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL,
4451 StaticTypeWarningCode.NON_BOOL_OPERAND
4452 ]);
4453 verify([source]);
4454 }
4455
4456 void test_nonConstValueInInitializer_binary_notBool_right() {
4457 Source source = addSource(r'''
4458 class A {
4459 final bool a;
4460 const A(String p) : a = true && p;
4461 }''');
4462 computeLibrarySourceErrors(source);
4463 assertErrors(source, [
4464 CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL,
4465 StaticTypeWarningCode.NON_BOOL_OPERAND
4466 ]);
4467 verify([source]);
4468 }
4469
4470 void test_nonConstValueInInitializer_binary_notInt() {
4471 Source source = addSource(r'''
4472 class A {
4473 final int a;
4474 const A(String p) : a = 5 & p;
4475 }''');
4476 computeLibrarySourceErrors(source);
4477 assertErrors(source, [
4478 CompileTimeErrorCode.CONST_EVAL_TYPE_INT,
4479 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
4480 ]);
4481 verify([source]);
4482 }
4483
4484 void test_nonConstValueInInitializer_binary_notNum() {
4485 Source source = addSource(r'''
4486 class A {
4487 final int a;
4488 const A(String p) : a = 5 + p;
4489 }''');
4490 computeLibrarySourceErrors(source);
4491 assertErrors(source, [
4492 CompileTimeErrorCode.CONST_EVAL_TYPE_NUM,
4493 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
4494 ]);
4495 verify([source]);
4496 }
4497
4498 void test_nonConstValueInInitializer_field() {
4499 Source source = addSource(r'''
4500 class A {
4501 static int C;
4502 final int a;
4503 const A() : a = C;
4504 }''');
4505 computeLibrarySourceErrors(source);
4506 assertErrors(
4507 source, [CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER]);
4508 verify([source]);
4509 }
4510
4511 void test_nonConstValueInInitializer_instanceCreation() {
4512 Source source = addSource(r'''
4513 class A {
4514 A();
4515 }
4516 class B {
4517 const B() : a = new A();
4518 final a;
4519 }
4520 var b = const B();''');
4521 computeLibrarySourceErrors(source);
4522 // TODO(paulberry): the error INVALID_CONSTAT is redundant and ought to be
4523 // suppressed.
4524 assertErrors(source, [
4525 CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER,
4526 CompileTimeErrorCode.INVALID_CONSTANT
4527 ]);
4528 verify([source]);
4529 }
4530
4531 void test_nonConstValueInInitializer_redirecting() {
4532 Source source = addSource(r'''
4533 class A {
4534 static var C;
4535 const A.named(p);
4536 const A() : this.named(C);
4537 }''');
4538 computeLibrarySourceErrors(source);
4539 assertErrors(
4540 source, [CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER]);
4541 verify([source]);
4542 }
4543
4544 void test_nonConstValueInInitializer_super() {
4545 Source source = addSource(r'''
4546 class A {
4547 const A(p);
4548 }
4549 class B extends A {
4550 static var C;
4551 const B() : super(C);
4552 }''');
4553 computeLibrarySourceErrors(source);
4554 assertErrors(
4555 source, [CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER]);
4556 verify([source]);
4557 }
4558
4559 void test_nonConstValueInInitializerFromDeferredLibrary_field() {
4560 resolveWithErrors(<String>[
4561 r'''
4562 library lib1;
4563 const int c = 1;''',
4564 r'''
4565 library root;
4566 import 'lib1.dart' deferred as a;
4567 class A {
4568 final int x;
4569 const A() : x = a.c;
4570 }'''
4571 ], <ErrorCode>[
4572 CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER_FROM_DEFERRED_LIBRA RY
4573 ]);
4574 }
4575
4576 void test_nonConstValueInInitializerFromDeferredLibrary_field_nested() {
4577 resolveWithErrors(<String>[
4578 r'''
4579 library lib1;
4580 const int c = 1;''',
4581 r'''
4582 library root;
4583 import 'lib1.dart' deferred as a;
4584 class A {
4585 final int x;
4586 const A() : x = a.c + 1;
4587 }'''
4588 ], <ErrorCode>[
4589 CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER_FROM_DEFERRED_LIBRA RY
4590 ]);
4591 }
4592
4593 void test_nonConstValueInInitializerFromDeferredLibrary_redirecting() {
4594 resolveWithErrors(<String>[
4595 r'''
4596 library lib1;
4597 const int c = 1;''',
4598 r'''
4599 library root;
4600 import 'lib1.dart' deferred as a;
4601 class A {
4602 const A.named(p);
4603 const A() : this.named(a.c);
4604 }'''
4605 ], <ErrorCode>[
4606 CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER_FROM_DEFERRED_LIBRA RY
4607 ]);
4608 }
4609
4610 void test_nonConstValueInInitializerFromDeferredLibrary_super() {
4611 resolveWithErrors(<String>[
4612 r'''
4613 library lib1;
4614 const int c = 1;''',
4615 r'''
4616 library root;
4617 import 'lib1.dart' deferred as a;
4618 class A {
4619 const A(p);
4620 }
4621 class B extends A {
4622 const B() : super(a.c);
4623 }'''
4624 ], <ErrorCode>[
4625 CompileTimeErrorCode.NON_CONSTANT_VALUE_IN_INITIALIZER_FROM_DEFERRED_LIBRA RY
4626 ]);
4627 }
4628
4629 void test_nonGenerativeConstructor_explicit() {
4630 Source source = addSource(r'''
4631 class A {
4632 factory A.named() {}
4633 }
4634 class B extends A {
4635 B() : super.named();
4636 }''');
4637 computeLibrarySourceErrors(source);
4638 assertErrors(source, [CompileTimeErrorCode.NON_GENERATIVE_CONSTRUCTOR]);
4639 verify([source]);
4640 }
4641
4642 void test_nonGenerativeConstructor_implicit() {
4643 Source source = addSource(r'''
4644 class A {
4645 factory A() {}
4646 }
4647 class B extends A {
4648 B();
4649 }''');
4650 computeLibrarySourceErrors(source);
4651 assertErrors(source, [CompileTimeErrorCode.NON_GENERATIVE_CONSTRUCTOR]);
4652 verify([source]);
4653 }
4654
4655 void test_nonGenerativeConstructor_implicit2() {
4656 Source source = addSource(r'''
4657 class A {
4658 factory A() {}
4659 }
4660 class B extends A {
4661 }''');
4662 computeLibrarySourceErrors(source);
4663 assertErrors(source, [CompileTimeErrorCode.NON_GENERATIVE_CONSTRUCTOR]);
4664 verify([source]);
4665 }
4666
4667 void test_notEnoughRequiredArguments_const() {
4668 Source source = addSource(r'''
4669 class A {
4670 const A(int p);
4671 }
4672 main() {
4673 const A();
4674 }''');
4675 computeLibrarySourceErrors(source);
4676 assertErrors(source, [CompileTimeErrorCode.NOT_ENOUGH_REQUIRED_ARGUMENTS]);
4677 verify([source]);
4678 }
4679
4680 void test_notEnoughRequiredArguments_const_super() {
4681 Source source = addSource(r'''
4682 class A {
4683 const A(int p);
4684 }
4685 class B extends A {
4686 const B() : super();
4687 }''');
4688 computeLibrarySourceErrors(source);
4689 assertErrors(source, [CompileTimeErrorCode.NOT_ENOUGH_REQUIRED_ARGUMENTS]);
4690 verify([source]);
4691 }
4692
4693 void test_optionalParameterInOperator_named() {
4694 Source source = addSource(r'''
4695 class A {
4696 operator +({p}) {}
4697 }''');
4698 computeLibrarySourceErrors(source);
4699 assertErrors(source, [CompileTimeErrorCode.OPTIONAL_PARAMETER_IN_OPERATOR]);
4700 verify([source]);
4701 }
4702
4703 void test_optionalParameterInOperator_positional() {
4704 Source source = addSource(r'''
4705 class A {
4706 operator +([p]) {}
4707 }''');
4708 computeLibrarySourceErrors(source);
4709 assertErrors(source, [CompileTimeErrorCode.OPTIONAL_PARAMETER_IN_OPERATOR]);
4710 verify([source]);
4711 }
4712
4713 void test_partOfNonPart() {
4714 Source source = addSource(r'''
4715 library l1;
4716 part 'l2.dart';''');
4717 addNamedSource("/l2.dart", "library l2;");
4718 computeLibrarySourceErrors(source);
4719 assertErrors(source, [CompileTimeErrorCode.PART_OF_NON_PART]);
4720 verify([source]);
4721 }
4722
4723 void test_partOfNonPart_self() {
4724 Source source = addSource(r'''
4725 library lib;
4726 part 'test.dart';''');
4727 computeLibrarySourceErrors(source);
4728 assertErrors(source, [CompileTimeErrorCode.PART_OF_NON_PART]);
4729 verify([source]);
4730 }
4731
4732 void test_prefix_assignment_compound_in_method() {
4733 addNamedSource('/lib.dart', 'library lib;');
4734 Source source = addSource('''
4735 import 'lib.dart' as p;
4736 class C {
4737 f() {
4738 p += 1;
4739 }
4740 }
4741 ''');
4742 computeLibrarySourceErrors(source);
4743 assertErrors(
4744 source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
4745 verify([source]);
4746 }
4747
4748 void test_prefix_assignment_compound_not_in_method() {
4749 addNamedSource('/lib.dart', 'library lib;');
4750 Source source = addSource('''
4751 import 'lib.dart' as p;
4752 f() {
4753 p += 1;
4754 }
4755 ''');
4756 computeLibrarySourceErrors(source);
4757 assertErrors(
4758 source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
4759 verify([source]);
4760 }
4761
4762 void test_prefix_assignment_in_method() {
4763 addNamedSource('/lib.dart', 'library lib;');
4764 Source source = addSource('''
4765 import 'lib.dart' as p;
4766 class C {
4767 f() {
4768 p = 1;
4769 }
4770 }
4771 ''');
4772 computeLibrarySourceErrors(source);
4773 assertErrors(
4774 source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
4775 verify([source]);
4776 }
4777
4778 void test_prefix_assignment_not_in_method() {
4779 addNamedSource('/lib.dart', 'library lib;');
4780 Source source = addSource('''
4781 import 'lib.dart' as p;
4782 f() {
4783 p = 1;
4784 }
4785 ''');
4786 computeLibrarySourceErrors(source);
4787 assertErrors(
4788 source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
4789 verify([source]);
4790 }
4791
4792 void test_prefix_conditionalPropertyAccess_call() {
4793 addNamedSource(
4794 '/lib.dart',
4795 '''
4796 library lib;
4797 g() {}
4798 ''');
4799 Source source = addSource('''
4800 import 'lib.dart' as p;
4801 f() {
4802 p?.g();
4803 }
4804 ''');
4805 computeLibrarySourceErrors(source);
4806 assertErrors(
4807 source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
4808 verify([source]);
4809 }
4810
4811 void test_prefix_conditionalPropertyAccess_call_loadLibrary() {
4812 addNamedSource(
4813 '/lib.dart',
4814 '''
4815 library lib;
4816 ''');
4817 Source source = addSource('''
4818 import 'lib.dart' deferred as p;
4819 f() {
4820 p?.loadLibrary();
4821 }
4822 ''');
4823 computeLibrarySourceErrors(source);
4824 assertErrors(
4825 source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
4826 verify([source]);
4827 }
4828
4829 void test_prefix_conditionalPropertyAccess_get() {
4830 addNamedSource(
4831 '/lib.dart',
4832 '''
4833 library lib;
4834 var x;
4835 ''');
4836 Source source = addSource('''
4837 import 'lib.dart' as p;
4838 f() {
4839 return p?.x;
4840 }
4841 ''');
4842 computeLibrarySourceErrors(source);
4843 assertErrors(
4844 source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
4845 verify([source]);
4846 }
4847
4848 void test_prefix_conditionalPropertyAccess_get_loadLibrary() {
4849 addNamedSource(
4850 '/lib.dart',
4851 '''
4852 library lib;
4853 ''');
4854 Source source = addSource('''
4855 import 'lib.dart' deferred as p;
4856 f() {
4857 return p?.loadLibrary;
4858 }
4859 ''');
4860 computeLibrarySourceErrors(source);
4861 assertErrors(
4862 source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
4863 verify([source]);
4864 }
4865
4866 void test_prefix_conditionalPropertyAccess_set() {
4867 addNamedSource(
4868 '/lib.dart',
4869 '''
4870 library lib;
4871 var x;
4872 ''');
4873 Source source = addSource('''
4874 import 'lib.dart' as p;
4875 f() {
4876 p?.x = null;
4877 }
4878 ''');
4879 computeLibrarySourceErrors(source);
4880 assertErrors(
4881 source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
4882 verify([source]);
4883 }
4884
4885 void test_prefix_conditionalPropertyAccess_set_loadLibrary() {
4886 addNamedSource(
4887 '/lib.dart',
4888 '''
4889 library lib;
4890 ''');
4891 Source source = addSource('''
4892 import 'lib.dart' deferred as p;
4893 f() {
4894 p?.loadLibrary = null;
4895 }
4896 ''');
4897 computeLibrarySourceErrors(source);
4898 assertErrors(
4899 source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
4900 verify([source]);
4901 }
4902
4903 void test_prefix_unqualified_invocation_in_method() {
4904 addNamedSource('/lib.dart', 'librarylib;');
4905 Source source = addSource('''
4906 import 'lib.dart' as p;
4907 class C {
4908 f() {
4909 p();
4910 }
4911 }
4912 ''');
4913 computeLibrarySourceErrors(source);
4914 assertErrors(
4915 source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
4916 verify([source]);
4917 }
4918
4919 void test_prefix_unqualified_invocation_not_in_method() {
4920 addNamedSource('/lib.dart', 'librarylib;');
4921 Source source = addSource('''
4922 import 'lib.dart' as p;
4923 f() {
4924 p();
4925 }
4926 ''');
4927 computeLibrarySourceErrors(source);
4928 assertErrors(
4929 source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
4930 verify([source]);
4931 }
4932
4933 void test_prefixCollidesWithTopLevelMembers_functionTypeAlias() {
4934 addNamedSource(
4935 "/lib.dart",
4936 r'''
4937 library lib;
4938 class A{}''');
4939 Source source = addSource(r'''
4940 import 'lib.dart' as p;
4941 typedef p();
4942 p.A a;''');
4943 computeLibrarySourceErrors(source);
4944 assertErrors(
4945 source, [CompileTimeErrorCode.PREFIX_COLLIDES_WITH_TOP_LEVEL_MEMBER]);
4946 verify([source]);
4947 }
4948
4949 void test_prefixCollidesWithTopLevelMembers_topLevelFunction() {
4950 addNamedSource(
4951 "/lib.dart",
4952 r'''
4953 library lib;
4954 class A{}''');
4955 Source source = addSource(r'''
4956 import 'lib.dart' as p;
4957 p() {}
4958 p.A a;''');
4959 computeLibrarySourceErrors(source);
4960 assertErrors(
4961 source, [CompileTimeErrorCode.PREFIX_COLLIDES_WITH_TOP_LEVEL_MEMBER]);
4962 verify([source]);
4963 }
4964
4965 void test_prefixCollidesWithTopLevelMembers_topLevelVariable() {
4966 addNamedSource(
4967 "/lib.dart",
4968 r'''
4969 library lib;
4970 class A{}''');
4971 Source source = addSource(r'''
4972 import 'lib.dart' as p;
4973 var p = null;
4974 p.A a;''');
4975 computeLibrarySourceErrors(source);
4976 assertErrors(
4977 source, [CompileTimeErrorCode.PREFIX_COLLIDES_WITH_TOP_LEVEL_MEMBER]);
4978 verify([source]);
4979 }
4980
4981 void test_prefixCollidesWithTopLevelMembers_type() {
4982 addNamedSource(
4983 "/lib.dart",
4984 r'''
4985 library lib;
4986 class A{}''');
4987 Source source = addSource(r'''
4988 import 'lib.dart' as p;
4989 class p {}
4990 p.A a;''');
4991 computeLibrarySourceErrors(source);
4992 assertErrors(
4993 source, [CompileTimeErrorCode.PREFIX_COLLIDES_WITH_TOP_LEVEL_MEMBER]);
4994 verify([source]);
4995 }
4996
4997 void test_prefixNotFollowedByDot() {
4998 addNamedSource('/lib.dart', 'library lib;');
4999 Source source = addSource('''
5000 import 'lib.dart' as p;
5001 f() {
5002 return p;
5003 }
5004 ''');
5005 computeLibrarySourceErrors(source);
5006 assertErrors(
5007 source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
5008 verify([source]);
5009 }
5010
5011 void test_prefixNotFollowedByDot_compoundAssignment() {
5012 addNamedSource('/lib.dart', 'library lib;');
5013 Source source = addSource('''
5014 import 'lib.dart' as p;
5015 f() {
5016 p += 1;
5017 }
5018 ''');
5019 computeLibrarySourceErrors(source);
5020 assertErrors(
5021 source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
5022 verify([source]);
5023 }
5024
5025 void test_prefixNotFollowedByDot_conditionalMethodInvocation() {
5026 addNamedSource(
5027 '/lib.dart',
5028 '''
5029 library lib;
5030 g() {}
5031 ''');
5032 Source source = addSource('''
5033 import 'lib.dart' as p;
5034 f() {
5035 p?.g();
5036 }
5037 ''');
5038 computeLibrarySourceErrors(source);
5039 assertErrors(
5040 source, [CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT]);
5041 verify([source]);
5042 }
5043
5044 void test_privateOptionalParameter() {
5045 Source source = addSource("f({var _p}) {}");
5046 computeLibrarySourceErrors(source);
5047 assertErrors(source, [CompileTimeErrorCode.PRIVATE_OPTIONAL_PARAMETER]);
5048 verify([source]);
5049 }
5050
5051 void test_privateOptionalParameter_fieldFormal() {
5052 Source source = addSource(r'''
5053 class A {
5054 var _p;
5055 A({this._p: 0});
5056 }''');
5057 computeLibrarySourceErrors(source);
5058 assertErrors(source, [CompileTimeErrorCode.PRIVATE_OPTIONAL_PARAMETER]);
5059 verify([source]);
5060 }
5061
5062 void test_privateOptionalParameter_withDefaultValue() {
5063 Source source = addSource("f({_p : 0}) {}");
5064 computeLibrarySourceErrors(source);
5065 assertErrors(source, [CompileTimeErrorCode.PRIVATE_OPTIONAL_PARAMETER]);
5066 verify([source]);
5067 }
5068
5069 void test_recursiveCompileTimeConstant() {
5070 Source source = addSource(r'''
5071 class A {
5072 const A();
5073 final m = const A();
5074 }''');
5075 computeLibrarySourceErrors(source);
5076 assertErrors(
5077 source, [CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT]);
5078 verify([source]);
5079 }
5080
5081 void test_recursiveCompileTimeConstant_cycle() {
5082 Source source = addSource(r'''
5083 const x = y + 1;
5084 const y = x + 1;''');
5085 computeLibrarySourceErrors(source);
5086 assertErrors(source, [
5087 CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT,
5088 CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT
5089 ]);
5090 verify([source]);
5091 }
5092
5093 void test_recursiveCompileTimeConstant_initializer_after_toplevel_var() {
5094 Source source = addSource('''
5095 const y = const C();
5096 class C {
5097 const C() : x = y;
5098 final x;
5099 }
5100 ''');
5101 computeLibrarySourceErrors(source);
5102 assertErrors(
5103 source, [CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT]);
5104 verify([source]);
5105 }
5106
5107 void test_recursiveCompileTimeConstant_singleVariable() {
5108 Source source = addSource(r'''
5109 const x = x;
5110 ''');
5111 computeLibrarySourceErrors(source);
5112 assertErrors(
5113 source, [CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT]);
5114 verify([source]);
5115 }
5116
5117 void test_recursiveConstructorRedirect() {
5118 Source source = addSource(r'''
5119 class A {
5120 A.a() : this.b();
5121 A.b() : this.a();
5122 }''');
5123 computeLibrarySourceErrors(source);
5124 assertErrors(source, [
5125 CompileTimeErrorCode.RECURSIVE_CONSTRUCTOR_REDIRECT,
5126 CompileTimeErrorCode.RECURSIVE_CONSTRUCTOR_REDIRECT
5127 ]);
5128 verify([source]);
5129 }
5130
5131 void test_recursiveConstructorRedirect_directSelfReference() {
5132 Source source = addSource(r'''
5133 class A {
5134 A() : this();
5135 }''');
5136 computeLibrarySourceErrors(source);
5137 assertErrors(source, [CompileTimeErrorCode.RECURSIVE_CONSTRUCTOR_REDIRECT]);
5138 verify([source]);
5139 }
5140
5141 void test_recursiveFactoryRedirect() {
5142 Source source = addSource(r'''
5143 class A implements B {
5144 factory A() = C;
5145 }
5146 class B implements C {
5147 factory B() = A;
5148 }
5149 class C implements A {
5150 factory C() = B;
5151 }''');
5152 computeLibrarySourceErrors(source);
5153 assertErrors(source, [
5154 CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT,
5155 CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT,
5156 CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT,
5157 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
5158 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
5159 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE
5160 ]);
5161 verify([source]);
5162 }
5163
5164 void test_recursiveFactoryRedirect_directSelfReference() {
5165 Source source = addSource(r'''
5166 class A {
5167 factory A() = A;
5168 }''');
5169 computeLibrarySourceErrors(source);
5170 assertErrors(source, [CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT]);
5171 verify([source]);
5172 }
5173
5174 void test_recursiveFactoryRedirect_diverging() {
5175 // Analysis should terminate even though the redirections don't reach a
5176 // fixed point. (C<int> redirects to C<C<int>>, then to C<C<C<int>>>, and
5177 // so on).
5178 Source source = addSource('''
5179 class C<T> {
5180 const factory C() = C<C<T>>;
5181 }
5182 main() {
5183 const C<int>();
5184 }
5185 ''');
5186 computeLibrarySourceErrors(source);
5187 assertErrors(source, [CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT]);
5188 verify([source]);
5189 }
5190
5191 void test_recursiveFactoryRedirect_generic() {
5192 Source source = addSource(r'''
5193 class A<T> implements B<T> {
5194 factory A() = C;
5195 }
5196 class B<T> implements C<T> {
5197 factory B() = A;
5198 }
5199 class C<T> implements A<T> {
5200 factory C() = B;
5201 }''');
5202 computeLibrarySourceErrors(source);
5203 assertErrors(source, [
5204 CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT,
5205 CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT,
5206 CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT,
5207 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
5208 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
5209 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE
5210 ]);
5211 verify([source]);
5212 }
5213
5214 void test_recursiveFactoryRedirect_named() {
5215 Source source = addSource(r'''
5216 class A implements B {
5217 factory A.nameA() = C.nameC;
5218 }
5219 class B implements C {
5220 factory B.nameB() = A.nameA;
5221 }
5222 class C implements A {
5223 factory C.nameC() = B.nameB;
5224 }''');
5225 computeLibrarySourceErrors(source);
5226 assertErrors(source, [
5227 CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT,
5228 CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT,
5229 CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT,
5230 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
5231 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
5232 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE
5233 ]);
5234 verify([source]);
5235 }
5236
5237 /**
5238 * "A" references "C" which has cycle with "B". But we should not report probl em for "A" - it is
5239 * not the part of a cycle.
5240 */
5241 void test_recursiveFactoryRedirect_outsideCycle() {
5242 Source source = addSource(r'''
5243 class A {
5244 factory A() = C;
5245 }
5246 class B implements C {
5247 factory B() = C;
5248 }
5249 class C implements A, B {
5250 factory C() = B;
5251 }''');
5252 computeLibrarySourceErrors(source);
5253 assertErrors(source, [
5254 CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT,
5255 CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT,
5256 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
5257 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE
5258 ]);
5259 verify([source]);
5260 }
5261
5262 void test_recursiveInterfaceInheritance_extends() {
5263 Source source = addSource(r'''
5264 class A extends B {}
5265 class B extends A {}''');
5266 computeLibrarySourceErrors(source);
5267 assertErrors(source, [
5268 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
5269 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE
5270 ]);
5271 verify([source]);
5272 }
5273
5274 void test_recursiveInterfaceInheritance_extends_implements() {
5275 Source source = addSource(r'''
5276 class A extends B {}
5277 class B implements A {}''');
5278 computeLibrarySourceErrors(source);
5279 assertErrors(source, [
5280 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
5281 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE
5282 ]);
5283 verify([source]);
5284 }
5285
5286 void test_recursiveInterfaceInheritance_implements() {
5287 Source source = addSource(r'''
5288 class A implements B {}
5289 class B implements A {}''');
5290 computeLibrarySourceErrors(source);
5291 assertErrors(source, [
5292 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
5293 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE
5294 ]);
5295 verify([source]);
5296 }
5297
5298 void test_recursiveInterfaceInheritance_mixin() {
5299 Source source = addSource(r'''
5300 class M1 = Object with M2;
5301 class M2 = Object with M1;''');
5302 computeLibrarySourceErrors(source);
5303 assertErrors(source, [
5304 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
5305 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE
5306 ]);
5307 verify([source]);
5308 }
5309
5310 void test_recursiveInterfaceInheritance_mixin_superclass() {
5311 // Make sure we don't get CompileTimeErrorCode.MIXIN_HAS_NO_CONSTRUCTORS in
5312 // addition--that would just be confusing.
5313 Source source = addSource('''
5314 class C = D with M;
5315 class D = C with M;
5316 class M {}
5317 ''');
5318 computeLibrarySourceErrors(source);
5319 assertErrors(source, [
5320 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
5321 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE
5322 ]);
5323 verify([source]);
5324 }
5325
5326 void test_recursiveInterfaceInheritance_tail() {
5327 Source source = addSource(r'''
5328 abstract class A implements A {}
5329 class B implements A {}''');
5330 computeLibrarySourceErrors(source);
5331 assertErrors(source, [
5332 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE_BASE_CASE_IMPLEMENTS
5333 ]);
5334 verify([source]);
5335 }
5336
5337 void test_recursiveInterfaceInheritance_tail2() {
5338 Source source = addSource(r'''
5339 abstract class A implements B {}
5340 abstract class B implements A {}
5341 class C implements A {}''');
5342 computeLibrarySourceErrors(source);
5343 assertErrors(source, [
5344 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
5345 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE
5346 ]);
5347 verify([source]);
5348 }
5349
5350 void test_recursiveInterfaceInheritance_tail3() {
5351 Source source = addSource(r'''
5352 abstract class A implements B {}
5353 abstract class B implements C {}
5354 abstract class C implements A {}
5355 class D implements A {}''');
5356 computeLibrarySourceErrors(source);
5357 assertErrors(source, [
5358 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
5359 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE,
5360 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE
5361 ]);
5362 verify([source]);
5363 }
5364
5365 void test_recursiveInterfaceInheritanceBaseCaseExtends() {
5366 Source source = addSource("class A extends A {}");
5367 computeLibrarySourceErrors(source);
5368 assertErrors(source, [
5369 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE_BASE_CASE_EXTENDS
5370 ]);
5371 verify([source]);
5372 }
5373
5374 void test_recursiveInterfaceInheritanceBaseCaseImplements() {
5375 Source source = addSource("class A implements A {}");
5376 computeLibrarySourceErrors(source);
5377 assertErrors(source, [
5378 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE_BASE_CASE_IMPLEMENTS
5379 ]);
5380 verify([source]);
5381 }
5382
5383 void test_recursiveInterfaceInheritanceBaseCaseImplements_typeAlias() {
5384 Source source = addSource(r'''
5385 class A {}
5386 class M {}
5387 class B = A with M implements B;''');
5388 computeLibrarySourceErrors(source);
5389 assertErrors(source, [
5390 CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE_BASE_CASE_IMPLEMENTS
5391 ]);
5392 verify([source]);
5393 }
5394
5395 void test_recursiveInterfaceInheritanceBaseCaseWith() {
5396 Source source = addSource("class M = Object with M;");
5397 computeLibrarySourceErrors(source);
5398 assertErrors(source,
5399 [CompileTimeErrorCode.RECURSIVE_INTERFACE_INHERITANCE_BASE_CASE_WITH]);
5400 verify([source]);
5401 }
5402
5403 void test_redirectGenerativeToMissingConstructor() {
5404 Source source = addSource(r'''
5405 class A {
5406 A() : this.noSuchConstructor();
5407 }''');
5408 computeLibrarySourceErrors(source);
5409 assertErrors(source,
5410 [CompileTimeErrorCode.REDIRECT_GENERATIVE_TO_MISSING_CONSTRUCTOR]);
5411 }
5412
5413 void test_redirectGenerativeToNonGenerativeConstructor() {
5414 Source source = addSource(r'''
5415 class A {
5416 A() : this.x();
5417 factory A.x() => null;
5418 }''');
5419 computeLibrarySourceErrors(source);
5420 assertErrors(source, [
5421 CompileTimeErrorCode.REDIRECT_GENERATIVE_TO_NON_GENERATIVE_CONSTRUCTOR
5422 ]);
5423 verify([source]);
5424 }
5425
5426 void test_redirectToMissingConstructor_named() {
5427 Source source = addSource(r'''
5428 class A implements B{
5429 A() {}
5430 }
5431 class B {
5432 const factory B() = A.name;
5433 }''');
5434 computeLibrarySourceErrors(source);
5435 assertErrors(
5436 source, [CompileTimeErrorCode.REDIRECT_TO_MISSING_CONSTRUCTOR]);
5437 }
5438
5439 void test_redirectToMissingConstructor_unnamed() {
5440 Source source = addSource(r'''
5441 class A implements B{
5442 A.name() {}
5443 }
5444 class B {
5445 const factory B() = A;
5446 }''');
5447 computeLibrarySourceErrors(source);
5448 assertErrors(
5449 source, [CompileTimeErrorCode.REDIRECT_TO_MISSING_CONSTRUCTOR]);
5450 }
5451
5452 void test_redirectToNonClass_notAType() {
5453 Source source = addSource(r'''
5454 int A;
5455 class B {
5456 const factory B() = A;
5457 }''');
5458 computeLibrarySourceErrors(source);
5459 assertErrors(source, [CompileTimeErrorCode.REDIRECT_TO_NON_CLASS]);
5460 verify([source]);
5461 }
5462
5463 void test_redirectToNonClass_undefinedIdentifier() {
5464 Source source = addSource(r'''
5465 class B {
5466 const factory B() = A;
5467 }''');
5468 computeLibrarySourceErrors(source);
5469 assertErrors(source, [CompileTimeErrorCode.REDIRECT_TO_NON_CLASS]);
5470 verify([source]);
5471 }
5472
5473 void test_redirectToNonConstConstructor() {
5474 Source source = addSource(r'''
5475 class A {
5476 A.a() {}
5477 const factory A.b() = A.a;
5478 }''');
5479 computeLibrarySourceErrors(source);
5480 assertErrors(
5481 source, [CompileTimeErrorCode.REDIRECT_TO_NON_CONST_CONSTRUCTOR]);
5482 verify([source]);
5483 }
5484
5485 void test_referencedBeforeDeclaration_hideInBlock_function() {
5486 Source source = addSource(r'''
5487 var v = 1;
5488 main() {
5489 print(v);
5490 v() {}
5491 }
5492 print(x) {}''');
5493 computeLibrarySourceErrors(source);
5494 assertErrors(source, [CompileTimeErrorCode.REFERENCED_BEFORE_DECLARATION]);
5495 }
5496
5497 void test_referencedBeforeDeclaration_hideInBlock_local() {
5498 Source source = addSource(r'''
5499 var v = 1;
5500 main() {
5501 print(v);
5502 var v = 2;
5503 }
5504 print(x) {}''');
5505 computeLibrarySourceErrors(source);
5506 assertErrors(source, [CompileTimeErrorCode.REFERENCED_BEFORE_DECLARATION]);
5507 }
5508
5509 void test_referencedBeforeDeclaration_hideInBlock_subBlock() {
5510 Source source = addSource(r'''
5511 var v = 1;
5512 main() {
5513 {
5514 print(v);
5515 }
5516 var v = 2;
5517 }
5518 print(x) {}''');
5519 computeLibrarySourceErrors(source);
5520 assertErrors(source, [CompileTimeErrorCode.REFERENCED_BEFORE_DECLARATION]);
5521 }
5522
5523 void test_referencedBeforeDeclaration_inInitializer_closure() {
5524 Source source = addSource(r'''
5525 main() {
5526 var v = () => v;
5527 }''');
5528 computeLibrarySourceErrors(source);
5529 assertErrors(source, [CompileTimeErrorCode.REFERENCED_BEFORE_DECLARATION]);
5530 }
5531
5532 void test_referencedBeforeDeclaration_inInitializer_directly() {
5533 Source source = addSource(r'''
5534 main() {
5535 var v = v;
5536 }''');
5537 computeLibrarySourceErrors(source);
5538 assertErrors(source, [CompileTimeErrorCode.REFERENCED_BEFORE_DECLARATION]);
5539 }
5540
5541 void test_rethrowOutsideCatch() {
5542 Source source = addSource(r'''
5543 f() {
5544 rethrow;
5545 }''');
5546 computeLibrarySourceErrors(source);
5547 assertErrors(source, [CompileTimeErrorCode.RETHROW_OUTSIDE_CATCH]);
5548 verify([source]);
5549 }
5550
5551 void test_returnInGenerativeConstructor() {
5552 Source source = addSource(r'''
5553 class A {
5554 A() { return 0; }
5555 }''');
5556 computeLibrarySourceErrors(source);
5557 assertErrors(
5558 source, [CompileTimeErrorCode.RETURN_IN_GENERATIVE_CONSTRUCTOR]);
5559 verify([source]);
5560 }
5561
5562 void test_returnInGenerativeConstructor_expressionFunctionBody() {
5563 Source source = addSource(r'''
5564 class A {
5565 A() => null;
5566 }''');
5567 computeLibrarySourceErrors(source);
5568 assertErrors(
5569 source, [CompileTimeErrorCode.RETURN_IN_GENERATIVE_CONSTRUCTOR]);
5570 verify([source]);
5571 }
5572
5573 void test_returnInGenerator_asyncStar() {
5574 Source source = addSource(r'''
5575 f() async* {
5576 return 0;
5577 }''');
5578 computeLibrarySourceErrors(source);
5579 assertErrors(source, [CompileTimeErrorCode.RETURN_IN_GENERATOR]);
5580 verify([source]);
5581 }
5582
5583 void test_returnInGenerator_syncStar() {
5584 Source source = addSource(r'''
5585 f() sync* {
5586 return 0;
5587 }''');
5588 computeLibrarySourceErrors(source);
5589 assertErrors(source, [CompileTimeErrorCode.RETURN_IN_GENERATOR]);
5590 verify([source]);
5591 }
5592
5593 void test_sharedDeferredPrefix() {
5594 resolveWithErrors(<String>[
5595 r'''
5596 library lib1;
5597 f1() {}''',
5598 r'''
5599 library lib2;
5600 f2() {}''',
5601 r'''
5602 library root;
5603 import 'lib1.dart' deferred as lib;
5604 import 'lib2.dart' as lib;
5605 main() { lib.f1(); lib.f2(); }'''
5606 ], <ErrorCode>[
5607 CompileTimeErrorCode.SHARED_DEFERRED_PREFIX
5608 ]);
5609 }
5610
5611 void test_superInInvalidContext_binaryExpression() {
5612 Source source = addSource("var v = super + 0;");
5613 computeLibrarySourceErrors(source);
5614 assertErrors(source, [CompileTimeErrorCode.SUPER_IN_INVALID_CONTEXT]);
5615 // no verify(), 'super.v' is not resolved
5616 }
5617
5618 void test_superInInvalidContext_constructorFieldInitializer() {
5619 Source source = addSource(r'''
5620 class A {
5621 m() {}
5622 }
5623 class B extends A {
5624 var f;
5625 B() : f = super.m();
5626 }''');
5627 computeLibrarySourceErrors(source);
5628 assertErrors(source, [CompileTimeErrorCode.SUPER_IN_INVALID_CONTEXT]);
5629 // no verify(), 'super.m' is not resolved
5630 }
5631
5632 void test_superInInvalidContext_factoryConstructor() {
5633 Source source = addSource(r'''
5634 class A {
5635 m() {}
5636 }
5637 class B extends A {
5638 factory B() {
5639 super.m();
5640 }
5641 }''');
5642 computeLibrarySourceErrors(source);
5643 assertErrors(source, [CompileTimeErrorCode.SUPER_IN_INVALID_CONTEXT]);
5644 // no verify(), 'super.m' is not resolved
5645 }
5646
5647 void test_superInInvalidContext_instanceVariableInitializer() {
5648 Source source = addSource(r'''
5649 class A {
5650 var a;
5651 }
5652 class B extends A {
5653 var b = super.a;
5654 }''');
5655 computeLibrarySourceErrors(source);
5656 assertErrors(source, [CompileTimeErrorCode.SUPER_IN_INVALID_CONTEXT]);
5657 // no verify(), 'super.a' is not resolved
5658 }
5659
5660 void test_superInInvalidContext_staticMethod() {
5661 Source source = addSource(r'''
5662 class A {
5663 static m() {}
5664 }
5665 class B extends A {
5666 static n() { return super.m(); }
5667 }''');
5668 computeLibrarySourceErrors(source);
5669 assertErrors(source, [CompileTimeErrorCode.SUPER_IN_INVALID_CONTEXT]);
5670 // no verify(), 'super.m' is not resolved
5671 }
5672
5673 void test_superInInvalidContext_staticVariableInitializer() {
5674 Source source = addSource(r'''
5675 class A {
5676 static int a = 0;
5677 }
5678 class B extends A {
5679 static int b = super.a;
5680 }''');
5681 computeLibrarySourceErrors(source);
5682 assertErrors(source, [CompileTimeErrorCode.SUPER_IN_INVALID_CONTEXT]);
5683 // no verify(), 'super.a' is not resolved
5684 }
5685
5686 void test_superInInvalidContext_topLevelFunction() {
5687 Source source = addSource(r'''
5688 f() {
5689 super.f();
5690 }''');
5691 computeLibrarySourceErrors(source);
5692 assertErrors(source, [CompileTimeErrorCode.SUPER_IN_INVALID_CONTEXT]);
5693 // no verify(), 'super.f' is not resolved
5694 }
5695
5696 void test_superInInvalidContext_topLevelVariableInitializer() {
5697 Source source = addSource("var v = super.y;");
5698 computeLibrarySourceErrors(source);
5699 assertErrors(source, [CompileTimeErrorCode.SUPER_IN_INVALID_CONTEXT]);
5700 // no verify(), 'super.y' is not resolved
5701 }
5702
5703 void test_superInRedirectingConstructor_redirectionSuper() {
5704 Source source = addSource(r'''
5705 class A {}
5706 class B {
5707 B() : this.name(), super();
5708 B.name() {}
5709 }''');
5710 computeLibrarySourceErrors(source);
5711 assertErrors(
5712 source, [CompileTimeErrorCode.SUPER_IN_REDIRECTING_CONSTRUCTOR]);
5713 verify([source]);
5714 }
5715
5716 void test_superInRedirectingConstructor_superRedirection() {
5717 Source source = addSource(r'''
5718 class A {}
5719 class B {
5720 B() : super(), this.name();
5721 B.name() {}
5722 }''');
5723 computeLibrarySourceErrors(source);
5724 assertErrors(
5725 source, [CompileTimeErrorCode.SUPER_IN_REDIRECTING_CONSTRUCTOR]);
5726 verify([source]);
5727 }
5728
5729 void test_symbol_constructor_badArgs() {
5730 Source source = addSource(r'''
5731 var s1 = const Symbol('3');
5732 var s2 = const Symbol(3);
5733 var s3 = const Symbol();
5734 var s4 = const Symbol('x', 'y');
5735 var s5 = const Symbol('x', foo: 'x');''');
5736 computeLibrarySourceErrors(source);
5737 assertErrors(source, [
5738 CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION,
5739 CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION,
5740 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE,
5741 CompileTimeErrorCode.NOT_ENOUGH_REQUIRED_ARGUMENTS,
5742 CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS,
5743 CompileTimeErrorCode.UNDEFINED_NAMED_PARAMETER
5744 ]);
5745 verify([source]);
5746 }
5747
5748 void test_typeAliasCannotReferenceItself_11987() {
5749 Source source = addSource(r'''
5750 typedef void F(List<G> l);
5751 typedef void G(List<F> l);
5752 main() {
5753 F foo(G g) => g;
5754 }''');
5755 computeLibrarySourceErrors(source);
5756 assertErrors(source, [
5757 CompileTimeErrorCode.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF,
5758 CompileTimeErrorCode.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF
5759 ]);
5760 verify([source]);
5761 }
5762
5763 void test_typeAliasCannotReferenceItself_19459() {
5764 // A complex example involving multiple classes. This is legal, since
5765 // typedef F references itself only via a class.
5766 Source source = addSource(r'''
5767 class A<B, C> {}
5768 abstract class D {
5769 f(E e);
5770 }
5771 abstract class E extends A<dynamic, F> {}
5772 typedef D F();
5773 ''');
5774 computeLibrarySourceErrors(source);
5775 assertNoErrors(source);
5776 verify([source]);
5777 }
5778
5779 void test_typeAliasCannotReferenceItself_parameterType_named() {
5780 Source source = addSource("typedef A({A a});");
5781 computeLibrarySourceErrors(source);
5782 assertErrors(
5783 source, [CompileTimeErrorCode.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF]);
5784 verify([source]);
5785 }
5786
5787 void test_typeAliasCannotReferenceItself_parameterType_positional() {
5788 Source source = addSource("typedef A([A a]);");
5789 computeLibrarySourceErrors(source);
5790 assertErrors(
5791 source, [CompileTimeErrorCode.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF]);
5792 verify([source]);
5793 }
5794
5795 void test_typeAliasCannotReferenceItself_parameterType_required() {
5796 Source source = addSource("typedef A(A a);");
5797 computeLibrarySourceErrors(source);
5798 assertErrors(
5799 source, [CompileTimeErrorCode.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF]);
5800 verify([source]);
5801 }
5802
5803 void test_typeAliasCannotReferenceItself_parameterType_typeArgument() {
5804 Source source = addSource("typedef A(List<A> a);");
5805 computeLibrarySourceErrors(source);
5806 assertErrors(
5807 source, [CompileTimeErrorCode.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF]);
5808 verify([source]);
5809 }
5810
5811 void test_typeAliasCannotReferenceItself_returnClass_withTypeAlias() {
5812 // A typedef is allowed to indirectly reference itself via a class.
5813 Source source = addSource(r'''
5814 typedef C A();
5815 typedef A B();
5816 class C {
5817 B a;
5818 }''');
5819 computeLibrarySourceErrors(source);
5820 assertNoErrors(source);
5821 verify([source]);
5822 }
5823
5824 void test_typeAliasCannotReferenceItself_returnType() {
5825 Source source = addSource("typedef A A();");
5826 computeLibrarySourceErrors(source);
5827 assertErrors(
5828 source, [CompileTimeErrorCode.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF]);
5829 verify([source]);
5830 }
5831
5832 void test_typeAliasCannotReferenceItself_returnType_indirect() {
5833 Source source = addSource(r'''
5834 typedef B A();
5835 typedef A B();''');
5836 computeLibrarySourceErrors(source);
5837 assertErrors(source, [
5838 CompileTimeErrorCode.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF,
5839 CompileTimeErrorCode.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF
5840 ]);
5841 verify([source]);
5842 }
5843
5844 void test_typeAliasCannotReferenceItself_typeVariableBounds() {
5845 Source source = addSource("typedef A<T extends A>();");
5846 computeLibrarySourceErrors(source);
5847 assertErrors(
5848 source, [CompileTimeErrorCode.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF]);
5849 verify([source]);
5850 }
5851
5852 void test_typeArgumentNotMatchingBounds_const() {
5853 Source source = addSource(r'''
5854 class A {}
5855 class B {}
5856 class G<E extends A> {
5857 const G();
5858 }
5859 f() { return const G<B>(); }''');
5860 computeLibrarySourceErrors(source);
5861 assertErrors(
5862 source, [CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS]);
5863 verify([source]);
5864 }
5865
5866 void test_undefinedClass_const() {
5867 Source source = addSource(r'''
5868 f() {
5869 return const A();
5870 }''');
5871 computeLibrarySourceErrors(source);
5872 assertErrors(source, [CompileTimeErrorCode.UNDEFINED_CLASS]);
5873 verify([source]);
5874 }
5875
5876 void test_undefinedConstructorInInitializer_explicit_named() {
5877 Source source = addSource(r'''
5878 class A {}
5879 class B extends A {
5880 B() : super.named();
5881 }''');
5882 computeLibrarySourceErrors(source);
5883 assertErrors(
5884 source, [CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER]);
5885 // no verify(), "super.named()" is not resolved
5886 }
5887
5888 void test_undefinedConstructorInInitializer_explicit_unnamed() {
5889 Source source = addSource(r'''
5890 class A {
5891 A.named() {}
5892 }
5893 class B extends A {
5894 B() : super();
5895 }''');
5896 computeLibrarySourceErrors(source);
5897 assertErrors(source,
5898 [CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER_DEFAULT]);
5899 verify([source]);
5900 }
5901
5902 void test_undefinedConstructorInInitializer_implicit() {
5903 Source source = addSource(r'''
5904 class A {
5905 A.named() {}
5906 }
5907 class B extends A {
5908 B();
5909 }''');
5910 computeLibrarySourceErrors(source);
5911 assertErrors(source,
5912 [CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER_DEFAULT]);
5913 verify([source]);
5914 }
5915
5916 void test_undefinedNamedParameter() {
5917 Source source = addSource(r'''
5918 class A {
5919 const A();
5920 }
5921 main() {
5922 const A(p: 0);
5923 }''');
5924 computeLibrarySourceErrors(source);
5925 assertErrors(source, [CompileTimeErrorCode.UNDEFINED_NAMED_PARAMETER]);
5926 // no verify(), 'p' is not resolved
5927 }
5928
5929 void test_uriDoesNotExist_export() {
5930 Source source = addSource("export 'unknown.dart';");
5931 computeLibrarySourceErrors(source);
5932 assertErrors(source, [CompileTimeErrorCode.URI_DOES_NOT_EXIST]);
5933 }
5934
5935 void test_uriDoesNotExist_import() {
5936 Source source = addSource("import 'unknown.dart';");
5937 computeLibrarySourceErrors(source);
5938 assertErrors(source, [CompileTimeErrorCode.URI_DOES_NOT_EXIST]);
5939 }
5940
5941 void test_uriDoesNotExist_part() {
5942 Source source = addSource(r'''
5943 library lib;
5944 part 'unknown.dart';''');
5945 computeLibrarySourceErrors(source);
5946 assertErrors(source, [CompileTimeErrorCode.URI_DOES_NOT_EXIST]);
5947 }
5948
5949 void test_uriWithInterpolation_constant() {
5950 Source source = addSource("import 'stuff_\$platform.dart';");
5951 computeLibrarySourceErrors(source);
5952 assertErrors(source, [
5953 CompileTimeErrorCode.URI_WITH_INTERPOLATION,
5954 StaticWarningCode.UNDEFINED_IDENTIFIER
5955 ]);
5956 // We cannot verify resolution with an unresolvable
5957 // URI: 'stuff_$platform.dart'
5958 }
5959
5960 void test_uriWithInterpolation_nonConstant() {
5961 Source source = addSource(r'''
5962 library lib;
5963 part '${'a'}.dart';''');
5964 computeLibrarySourceErrors(source);
5965 assertErrors(source, [CompileTimeErrorCode.URI_WITH_INTERPOLATION]);
5966 // We cannot verify resolution with an unresolvable URI: '${'a'}.dart'
5967 }
5968
5969 void test_wrongNumberOfParametersForOperator1() {
5970 _check_wrongNumberOfParametersForOperator1("<");
5971 _check_wrongNumberOfParametersForOperator1(">");
5972 _check_wrongNumberOfParametersForOperator1("<=");
5973 _check_wrongNumberOfParametersForOperator1(">=");
5974 _check_wrongNumberOfParametersForOperator1("+");
5975 _check_wrongNumberOfParametersForOperator1("/");
5976 _check_wrongNumberOfParametersForOperator1("~/");
5977 _check_wrongNumberOfParametersForOperator1("*");
5978 _check_wrongNumberOfParametersForOperator1("%");
5979 _check_wrongNumberOfParametersForOperator1("|");
5980 _check_wrongNumberOfParametersForOperator1("^");
5981 _check_wrongNumberOfParametersForOperator1("&");
5982 _check_wrongNumberOfParametersForOperator1("<<");
5983 _check_wrongNumberOfParametersForOperator1(">>");
5984 _check_wrongNumberOfParametersForOperator1("[]");
5985 }
5986
5987 void test_wrongNumberOfParametersForOperator_minus() {
5988 Source source = addSource(r'''
5989 class A {
5990 operator -(a, b) {}
5991 }''');
5992 computeLibrarySourceErrors(source);
5993 assertErrors(source,
5994 [CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_OPERATOR_MINUS]);
5995 verify([source]);
5996 reset();
5997 }
5998
5999 void test_wrongNumberOfParametersForOperator_tilde() {
6000 _check_wrongNumberOfParametersForOperator("~", "a");
6001 _check_wrongNumberOfParametersForOperator("~", "a, b");
6002 }
6003
6004 void test_wrongNumberOfParametersForSetter_function_named() {
6005 Source source = addSource("set x({p}) {}");
6006 computeLibrarySourceErrors(source);
6007 assertErrors(
6008 source, [CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER]);
6009 verify([source]);
6010 }
6011
6012 void test_wrongNumberOfParametersForSetter_function_optional() {
6013 Source source = addSource("set x([p]) {}");
6014 computeLibrarySourceErrors(source);
6015 assertErrors(
6016 source, [CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER]);
6017 verify([source]);
6018 }
6019
6020 void test_wrongNumberOfParametersForSetter_function_tooFew() {
6021 Source source = addSource("set x() {}");
6022 computeLibrarySourceErrors(source);
6023 assertErrors(
6024 source, [CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER]);
6025 verify([source]);
6026 }
6027
6028 void test_wrongNumberOfParametersForSetter_function_tooMany() {
6029 Source source = addSource("set x(a, b) {}");
6030 computeLibrarySourceErrors(source);
6031 assertErrors(
6032 source, [CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER]);
6033 verify([source]);
6034 }
6035
6036 void test_wrongNumberOfParametersForSetter_method_named() {
6037 Source source = addSource(r'''
6038 class A {
6039 set x({p}) {}
6040 }''');
6041 computeLibrarySourceErrors(source);
6042 assertErrors(
6043 source, [CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER]);
6044 verify([source]);
6045 }
6046
6047 void test_wrongNumberOfParametersForSetter_method_optional() {
6048 Source source = addSource(r'''
6049 class A {
6050 set x([p]) {}
6051 }''');
6052 computeLibrarySourceErrors(source);
6053 assertErrors(
6054 source, [CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER]);
6055 verify([source]);
6056 }
6057
6058 void test_wrongNumberOfParametersForSetter_method_tooFew() {
6059 Source source = addSource(r'''
6060 class A {
6061 set x() {}
6062 }''');
6063 computeLibrarySourceErrors(source);
6064 assertErrors(
6065 source, [CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER]);
6066 verify([source]);
6067 }
6068
6069 void test_wrongNumberOfParametersForSetter_method_tooMany() {
6070 Source source = addSource(r'''
6071 class A {
6072 set x(a, b) {}
6073 }''');
6074 computeLibrarySourceErrors(source);
6075 assertErrors(
6076 source, [CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_SETTER]);
6077 verify([source]);
6078 }
6079
6080 void test_yield_used_as_identifier_in_async_method() {
6081 Source source = addSource('''
6082 f() async {
6083 var yield = 1;
6084 }
6085 ''');
6086 computeLibrarySourceErrors(source);
6087 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
6088 verify([source]);
6089 }
6090
6091 void test_yield_used_as_identifier_in_async_star_method() {
6092 Source source = addSource('''
6093 f() async* {
6094 var yield = 1;
6095 }
6096 ''');
6097 computeLibrarySourceErrors(source);
6098 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
6099 verify([source]);
6100 }
6101
6102 void test_yield_used_as_identifier_in_sync_star_method() {
6103 Source source = addSource('''
6104 f() sync* {
6105 var yield = 1;
6106 }
6107 ''');
6108 computeLibrarySourceErrors(source);
6109 assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
6110 verify([source]);
6111 }
6112
6113 void _check_constEvalThrowsException_binary_null(String expr, bool resolved) {
6114 Source source = addSource("const C = $expr;");
6115 computeLibrarySourceErrors(source);
6116 if (resolved) {
6117 assertErrors(source, [CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION]);
6118 verify([source]);
6119 } else {
6120 assertErrors(source, [CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION]);
6121 // no verify(), 'null x' is not resolved
6122 }
6123 reset();
6124 }
6125
6126 void _check_constEvalTypeBool_withParameter_binary(String expr) {
6127 Source source = addSource('''
6128 class A {
6129 final a;
6130 const A(bool p) : a = $expr;
6131 }''');
6132 computeLibrarySourceErrors(source);
6133 assertErrors(source, [
6134 CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL,
6135 StaticTypeWarningCode.NON_BOOL_OPERAND
6136 ]);
6137 verify([source]);
6138 reset();
6139 }
6140
6141 void _check_constEvalTypeInt_withParameter_binary(String expr) {
6142 Source source = addSource('''
6143 class A {
6144 final a;
6145 const A(int p) : a = $expr;
6146 }''');
6147 computeLibrarySourceErrors(source);
6148 assertErrors(source, [
6149 CompileTimeErrorCode.CONST_EVAL_TYPE_INT,
6150 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
6151 ]);
6152 verify([source]);
6153 reset();
6154 }
6155
6156 void _check_constEvalTypeNum_withParameter_binary(String expr) {
6157 Source source = addSource('''
6158 class A {
6159 final a;
6160 const A(num p) : a = $expr;
6161 }''');
6162 computeLibrarySourceErrors(source);
6163 assertErrors(source, [
6164 CompileTimeErrorCode.CONST_EVAL_TYPE_NUM,
6165 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
6166 ]);
6167 verify([source]);
6168 reset();
6169 }
6170
6171 void _check_wrongNumberOfParametersForOperator(
6172 String name, String parameters) {
6173 Source source = addSource('''
6174 class A {
6175 operator $name($parameters) {}
6176 }''');
6177 computeLibrarySourceErrors(source);
6178 assertErrors(
6179 source, [CompileTimeErrorCode.WRONG_NUMBER_OF_PARAMETERS_FOR_OPERATOR]);
6180 verify([source]);
6181 reset();
6182 }
6183
6184 void _check_wrongNumberOfParametersForOperator1(String name) {
6185 _check_wrongNumberOfParametersForOperator(name, "");
6186 _check_wrongNumberOfParametersForOperator(name, "a, b");
6187 }
6188 }
OLDNEW
« no previous file with comments | « packages/analyzer/test/generated/ast_test.dart ('k') | packages/analyzer/test/generated/element_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698