Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 4eb896b

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
analyzer: Move tests for 8 more error codes into separate files
Change-Id: I59ee527c0ff68ff7c77c7a52483ba23355dfe3ad Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/129423 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Commit-Queue: Samuel Rawlins <srawlins@google.com>
1 parent de6faad commit 4eb896b

11 files changed

+533
-390
lines changed

pkg/analyzer/test/generated/compile_time_error_code.dart

Lines changed: 0 additions & 382 deletions
Large diffs are not rendered by default.

pkg/analyzer/test/generated/compile_time_error_code_test.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ class CompileTimeErrorCodeTest extends CompileTimeErrorCodeTestBase {
3636
test_objectCannotExtendAnotherClass() {
3737
return super.test_objectCannotExtendAnotherClass();
3838
}
39-
40-
@override
41-
@failingTest
42-
test_superInitializerInObject() {
43-
return super.test_superInitializerInObject();
44-
}
4539
}
4640

4741
@reflectiveTest

pkg/analyzer/test/src/diagnostics/const_eval_throws_exception_test.dart

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ main() {
1717
});
1818
}
1919

20-
/// TODO(paulberry): move other tests from [CheckedModeCompileTimeErrorCodeTest]
21-
/// and [CompileTimeErrorCodeTestBase] to this class.
20+
/// TODO(paulberry): move other tests from [CompileTimeErrorCodeTestBase] to
21+
/// this class.
2222
@reflectiveTest
2323
class ConstEvalThrowsExceptionTest extends DriverResolutionTest {
2424
test_CastError_intToDouble_constructor_importAnalyzedAfter() async {
@@ -121,6 +121,43 @@ main() {
121121
expect(otherFileResult.errors, isEmpty);
122122
}
123123

124+
test_finalAlreadySet_initializer() async {
125+
// If a final variable has an initializer at the site of its declaration,
126+
// and at the site of the constructor, then invoking that constructor would
127+
// produce a runtime error; hence invoking that constructor via the "const"
128+
// keyword results in a compile-time error.
129+
await assertErrorsInCode('''
130+
class C {
131+
final x = 1;
132+
const C() : x = 2;
133+
}
134+
var x = const C();
135+
''', [
136+
error(StaticWarningCode.FIELD_INITIALIZED_IN_INITIALIZER_AND_DECLARATION,
137+
39, 1),
138+
error(CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION, 56, 9),
139+
]);
140+
}
141+
142+
test_finalAlreadySet_initializing_formal() async {
143+
// If a final variable has an initializer at the site of its declaration,
144+
// and it is initialized using an initializing formal at the site of the
145+
// constructor, then invoking that constructor would produce a runtime
146+
// error; hence invoking that constructor via the "const" keyword results
147+
// in a compile-time error.
148+
await assertErrorsInCode('''
149+
class C {
150+
final x = 1;
151+
const C(this.x);
152+
}
153+
var x = const C(2);
154+
''', [
155+
error(StaticWarningCode.FINAL_INITIALIZED_IN_DECLARATION_AND_CONSTRUCTOR,
156+
40, 1),
157+
error(CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION, 54, 10),
158+
]);
159+
}
160+
124161
test_fromEnvironment_assertInitializer() async {
125162
await assertNoErrorsInCode('''
126163
class A {
@@ -207,6 +244,48 @@ const c = [if (0 < 1) 3 else nil + 1];
207244
error(CompileTimeErrorCode.NON_CONSTANT_LIST_ELEMENT, 37, 25),
208245
]);
209246
}
247+
248+
test_invalid_constructorFieldInitializer_fromSeparateLibrary() async {
249+
newFile('/test/lib/lib.dart', content: r'''
250+
class A<T> {
251+
final int f;
252+
const A() : f = T.foo;
253+
}
254+
''');
255+
await assertErrorsInCode(r'''
256+
import 'lib.dart';
257+
const a = const A();
258+
''', [
259+
error(CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION, 29, 9),
260+
]);
261+
}
262+
263+
test_unaryBitNot_null() async {
264+
await assertErrorsInCode('''
265+
const dynamic D = null;
266+
const C = ~D;
267+
''', [
268+
error(CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION, 34, 2),
269+
]);
270+
}
271+
272+
test_unaryNegated_null() async {
273+
await assertErrorsInCode('''
274+
const dynamic D = null;
275+
const C = -D;
276+
''', [
277+
error(CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION, 34, 2),
278+
]);
279+
}
280+
281+
test_unaryNot_null() async {
282+
await assertErrorsInCode('''
283+
const dynamic D = null;
284+
const C = !D;
285+
''', [
286+
error(CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION, 34, 2),
287+
]);
288+
}
210289
}
211290

212291
@reflectiveTest
@@ -218,6 +297,38 @@ class ConstEvalThrowsExceptionWithConstantUpdateTest
218297
EnableString.constant_update_2018,
219298
];
220299

300+
test_binaryMinus_null() async {
301+
await assertErrorsInCode('''
302+
const dynamic D = null;
303+
const C = D - 5;
304+
''', [
305+
error(CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION, 34, 5),
306+
]);
307+
308+
await assertErrorsInCode('''
309+
const dynamic D = null;
310+
const C = 5 - D;
311+
''', [
312+
error(CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION, 34, 5),
313+
]);
314+
}
315+
316+
test_binaryPlus_null() async {
317+
await assertErrorsInCode('''
318+
const dynamic D = null;
319+
const C = D + 5;
320+
''', [
321+
error(CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION, 34, 5),
322+
]);
323+
324+
await assertErrorsInCode('''
325+
const dynamic D = null;
326+
const C = 5 + D;
327+
''', [
328+
error(CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION, 34, 5),
329+
]);
330+
}
331+
221332
test_eqEq_nonPrimitiveRightOperand() async {
222333
await assertNoErrorsInCode('''
223334
const c = const T.eq(1, const Object());
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) 2019, 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+
import 'package:analyzer/src/error/codes.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../dart/resolution/driver_resolution.dart';
9+
10+
main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(RecursiveCompileTimeConstantTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class RecursiveCompileTimeConstantTest extends DriverResolutionTest {
18+
test_private() async {
19+
await assertErrorsInCode('''
20+
f({var _p}) {}
21+
''', [
22+
error(CompileTimeErrorCode.PRIVATE_OPTIONAL_PARAMETER, 3, 6),
23+
]);
24+
}
25+
26+
test_fieldFormal() async {
27+
await assertErrorsInCode(r'''
28+
class A {
29+
var _p;
30+
A({this._p: 0});
31+
}
32+
''', [
33+
error(HintCode.UNUSED_FIELD, 16, 2),
34+
error(CompileTimeErrorCode.PRIVATE_OPTIONAL_PARAMETER, 25, 7),
35+
]);
36+
}
37+
38+
test_withDefaultValue() async {
39+
await assertErrorsInCode('''
40+
f({_p : 0}) {}
41+
''', [
42+
error(CompileTimeErrorCode.PRIVATE_OPTIONAL_PARAMETER, 3, 2),
43+
]);
44+
}
45+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) 2019, 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+
import 'package:analyzer/src/error/codes.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../dart/resolution/driver_resolution.dart';
9+
10+
main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(RecursiveCompileTimeConstantTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class RecursiveCompileTimeConstantTest extends DriverResolutionTest {
18+
test_cycle() async {
19+
await assertErrorsInCode(r'''
20+
const x = y + 1;
21+
const y = x + 1;
22+
''', [
23+
error(CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT, 6, 1),
24+
error(StrongModeCode.TOP_LEVEL_CYCLE, 10, 1),
25+
error(CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT, 23, 1),
26+
error(StrongModeCode.TOP_LEVEL_CYCLE, 27, 1),
27+
]);
28+
}
29+
30+
test_field() async {
31+
await assertErrorsInCode(r'''
32+
class A {
33+
const A();
34+
final m = const A();
35+
}
36+
''', [
37+
error(CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT, 31, 1),
38+
]);
39+
}
40+
41+
test_fromMapLiteral() async {
42+
newFile(
43+
'/test/lib/constants.dart',
44+
content: r'''
45+
const int x = y;
46+
const int y = x;
47+
''',
48+
);
49+
// No errors, because the cycle is not in this source.
50+
await assertNoErrorsInCode(r'''
51+
import 'constants.dart';
52+
final z = {x: 0, y: 1};
53+
''');
54+
}
55+
56+
test_initializer_after_toplevel_var() async {
57+
await assertErrorsInCode('''
58+
const y = const C();
59+
class C {
60+
const C() : x = y;
61+
final x;
62+
}
63+
''', [
64+
error(CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT, 6, 1),
65+
]);
66+
}
67+
68+
test_singleVariable() async {
69+
await assertErrorsInCode(r'''
70+
const x = x;
71+
''', [
72+
error(CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT, 6, 1),
73+
error(StrongModeCode.TOP_LEVEL_CYCLE, 10, 1),
74+
]);
75+
}
76+
77+
test_singleVariable_fromConstList() async {
78+
await assertErrorsInCode(r'''
79+
const elems = const [
80+
const [
81+
1, elems, 3,
82+
],
83+
];
84+
''', [
85+
error(CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT, 6, 5),
86+
error(StrongModeCode.TOP_LEVEL_CYCLE, 39, 5),
87+
]);
88+
}
89+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2019, 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+
import 'package:analyzer/src/error/codes.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../dart/resolution/driver_resolution.dart';
9+
10+
main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(RecursiveConstructorRedirectTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class RecursiveConstructorRedirectTest extends DriverResolutionTest {
18+
test_recursive() async {
19+
await assertErrorsInCode(r'''
20+
class A {
21+
A.a() : this.b();
22+
A.b() : this.a();
23+
}
24+
''', [
25+
error(CompileTimeErrorCode.RECURSIVE_CONSTRUCTOR_REDIRECT, 20, 8),
26+
error(CompileTimeErrorCode.RECURSIVE_CONSTRUCTOR_REDIRECT, 40, 8),
27+
]);
28+
}
29+
30+
test_directSelfReference() async {
31+
await assertErrorsInCode(r'''
32+
class A {
33+
A() : this();
34+
}
35+
''', [
36+
error(CompileTimeErrorCode.RECURSIVE_CONSTRUCTOR_REDIRECT, 18, 6),
37+
]);
38+
}
39+
}

0 commit comments

Comments
 (0)